diff --git a/airtime_mvc/application/common/TuneIn.php b/airtime_mvc/application/common/TuneIn.php index 6040fb184..21d0f8684 100644 --- a/airtime_mvc/application/common/TuneIn.php +++ b/airtime_mvc/application/common/TuneIn.php @@ -2,6 +2,10 @@ class Application_Common_TuneIn { + /** + * @param $title url encoded string + * @param $artist url encoded string + */ public static function sendMetadataToTunein($title, $artist) { $credQryStr = self::getCredentialsQueryString(); diff --git a/airtime_mvc/application/forms/TuneInPreferences.php b/airtime_mvc/application/forms/TuneInPreferences.php index 7eee0d56f..d4eebc32c 100644 --- a/airtime_mvc/application/forms/TuneInPreferences.php +++ b/airtime_mvc/application/forms/TuneInPreferences.php @@ -88,42 +88,6 @@ class Application_Form_TuneInPreferences extends Zend_Form_SubForm $valid = false; } else if ($xmlObj->head->status == "200") { $valid = true; - - // Make another request to TuneIn to update the metadata right away - // and to turn off the commercial flag. - - /*$metadata = Application_Model_Schedule::getCurrentPlayingTrack(); - - if (!is_null($metadata)) { - - Logging::info($metadata); - // Replace empty strings with "n/a" since the TuneIn API will complain - // and return an error that title and/or artist is not set. - $metadata["artist"] = empty($metadata["artist"]) ? "n/a" : $metadata["artist"]; - $metadata["title"] = empty($metadata["title"]) ? "n/a" : $metadata["title"]; - Logging::info($metadata); - - $metadataQryStr = "&artist=" . $metadata["artist"] . "&title=" . $metadata["title"]; - - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, TUNEIN_API_URL . $qry_str . "&commercial=false" . $metadataQryStr); - curl_setopt($ch, CURLOPT_FAILONERROR, 1); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_TIMEOUT, 30); - - $xmlData = curl_exec($ch); - Logging::info($xmlData); - if (curl_error($ch)) { - Logging::error("Failed to reach TuneIn: " . curl_errno($ch) . " - " . curl_error($ch) . " - " . curl_getinfo($ch, CURLINFO_EFFECTIVE_URL)); - } - - curl_close($ch); - $xmlObj = new SimpleXMLElement($xmlData); - if (!$xmlObj || $xmlObj->head->status != "200") { - Logging::error("Failed updating metadata on TuneIn"); - } - }*/ - } } } else { diff --git a/airtime_mvc/application/models/Schedule.php b/airtime_mvc/application/models/Schedule.php index 66d00661b..d2e114243 100644 --- a/airtime_mvc/application/models/Schedule.php +++ b/airtime_mvc/application/models/Schedule.php @@ -98,10 +98,8 @@ SQL; $utcNow = new DateTime("now", new DateTimeZone("UTC")); $shows = Application_Model_Show::getPrevCurrentNext($utcNow, $utcTimeEnd, $showsToRetrieve); - $previousShowID = count($shows['previousShow'])>0?$shows['previousShow'][0]['instance_id']:null; $currentShowID = count($shows['currentShow'])>0?$shows['currentShow']['instance_id']:null; - $nextShowID = count($shows['nextShow'])>0?$shows['nextShow'][0]['instance_id']:null; - $results = self::GetPrevCurrentNext($previousShowID, $currentShowID, $nextShowID, $utcNow); + $results = Application_Model_Schedule::getPreviousCurrentNextMedia($utcNow, $currentShowID); $range = array( "station" => array ( @@ -136,10 +134,8 @@ SQL; $utcNow = new DateTime("now", new DateTimeZone("UTC")); $shows = Application_Model_Show::getPrevCurrentNextOld($utcNow); - $previousShowID = count($shows['previousShow'])>0?$shows['previousShow'][0]['instance_id']:null; $currentShowID = count($shows['currentShow'])>0?$shows['currentShow'][0]['instance_id']:null; - $nextShowID = count($shows['nextShow'])>0?$shows['nextShow'][0]['instance_id']:null; - $results = self::GetPrevCurrentNext($previousShowID, $currentShowID, $nextShowID, $utcNow); + $results = Application_Model_Schedule::getPreviousCurrentNextMedia($utcNow, $currentShowID); $range = array( "env" => APPLICATION_ENV, @@ -157,129 +153,158 @@ SQL; } /** - * Queries the database for the set of schedules one hour before - * and after the given time. If a show starts and ends within that - * time that is considered the current show. Then the scheduled item - * before it is the previous show, and the scheduled item after it - * is the next show. This way the dashboard getCurrentPlaylist is - * very fast. But if any one of the three show types are not found - * through this mechanism a call is made to the old way of querying - * the database to find the track info. - **/ - public static function GetPrevCurrentNext($p_previousShowID, $p_currentShowID, $p_nextShowID, $utcNow) + * Attempts to find a media item (track or webstream) that is currently playing. + * If a current media item is currently playing, this function then attempts to + * find an item that played previously and is scheduled to play next. + * + * @param $utcNow DateTime current time in UTC + * @param $currentShowInstanceId cc_show_instance id of the show instance currently playing + * @return array with data about the previous, current, and next media items playing. + * Returns an empty arrays if there is no media item currently playing + */ + public static function getPreviousCurrentNextMedia($utcNow, $currentShowInstanceId) { $timeZone = new DateTimeZone("UTC"); //This function works entirely in UTC. assert(get_class($utcNow) === "DateTime"); assert($utcNow->getTimeZone() == $timeZone); - if ($p_previousShowID == null && $p_currentShowID == null && $p_nextShowID == null) { - return; - } - - $sql = "SELECT %%columns%% st.starts as starts, st.ends as ends, - st.media_item_played as media_item_played, si.ends as show_ends - %%tables%% WHERE "; - - $fileColumns = "ft.artist_name, ft.track_title, "; - $fileJoin = "FROM cc_schedule st JOIN cc_files ft ON st.file_id = ft.id - LEFT JOIN cc_show_instances si ON st.instance_id = si.id"; - - $streamColumns = "ws.name AS artist_name, wm.liquidsoap_data AS track_title, "; - $streamJoin = << $rows[$i]["show_ends"]) { - $rows[$i]['ends'] = $rows[$i]["show_ends"]; - } + $params = array( + ":p1" => $utcNow->format("Y-m-d H:i:s"), + ":p2" => $utcNow->format("Y-m-d H:i:s"), + ":p3" => $currentShowInstanceId + ); - $curShowStartTime = new DateTime($rows[$i]['starts'], $timeZone); - $curShowEndTime = new DateTime($rows[$i]['ends'], $timeZone); + $rows = Application_Common_Database::prepareAndExecute($sql, $params); - if (($curShowStartTime <= $utcNow) && ($curShowEndTime >= $utcNow)) { - if ($i - 1 >= 0) { - $results['previous'] = array("name"=>$rows[$i-1]["artist_name"]." - ".$rows[$i-1]["track_title"], - "starts"=>$rows[$i-1]["starts"], - "ends"=>$rows[$i-1]["ends"], - "type"=>'track'); - } - $results['current'] = array("name"=>$rows[$i]["artist_name"]." - ".$rows[$i]["track_title"], - "starts"=>$rows[$i]["starts"], - "ends"=> (($rows[$i]["ends"] > $rows[$i]["show_ends"]) ? $rows[$i]["show_ends"]: $rows[$i]["ends"]), - "media_item_played"=>$rows[$i]["media_item_played"], - "record"=>0, - "type"=>'track'); - if (isset($rows[$i+1])) { - $results['next'] = array("name"=>$rows[$i+1]["artist_name"]." - ".$rows[$i+1]["track_title"], - "starts"=>$rows[$i+1]["starts"], - "ends"=>$rows[$i+1]["ends"], - "type"=>'track'); - } - break; - } - if ($curShowEndTime < $utcNow ) { - $previousIndex = $i; - } - if ($curShowStartTime > $utcNow) { - $results['next'] = array("name"=>$rows[$i]["artist_name"]." - ".$rows[$i]["track_title"], - "starts"=>$rows[$i]["starts"], - "ends"=>$rows[$i]["ends"], - "type"=>'track'); - break; - } + if (count($rows) < 1) { + return $results; } - //If we didn't find a a current show because the time didn't fit we may still have - //found a previous show so use it. - if ($results['previous'] === null && isset($previousIndex)) { - $results['previous'] = array("name"=>$rows[$previousIndex]["artist_name"]." - ".$rows[$previousIndex]["track_title"], - "starts"=>$rows[$previousIndex]["starts"], - "ends"=>$rows[$previousIndex]["ends"]);; + + if ($rows[0]["show_ends"] < $utcNow->format("Y-m-d H:i:s")) { + return $results; + } + + $currentMedia = $rows[0]; + + if ($currentMedia["ends"] > $currentMedia["show_ends"]) { + $currentMedia["ends"] = $currentMedia["show_ends"]; + } + + $currentMediaScheduleId = $currentMedia["id"]; + $currentMediaFileId = $currentMedia["file_id"]; + $currentMediaStreamId = $currentMedia["stream_id"]; + if (isset($currentMediaFileId)) { + $currentMediaType = "track"; + $currentFile = CcFilesQuery::create() + ->filterByDbId($currentMediaFileId) + ->findOne(); + $currentMediaName = $currentFile->getDbArtistName() . " - " . $currentFile->getDbTrackTitle(); + } else if (isset($currentMediaStreamId)) { + $currentMediaType = "webstream"; + $currentWebstream = CcWebstreamQuery::create() + ->filterByDbId($currentMediaStreamId) + ->findOne(); + $currentWebstreamMetadata = CcWebstreamMetadataQuery::create() + ->filterByDbInstanceId($currentMedia["instance_id"]) + ->orderByDbStartTime(Criteria::DESC) + ->findOne(); + $currentMediaName = $currentWebstream->getDbName(); + if (isset($currentWebstreamMetadata)) { + $currentMediaName .= " - " . $currentWebstreamMetadata->getDbLiquidsoapData(); + } + } else { + $currentMediaType = null; + } + $results["current"] = array( + "starts" => $currentMedia["starts"], + "ends" => $currentMedia["ends"], + "type" => $currentMediaType, + "name" => $currentMediaName, + "media_item_played" => $currentMedia["media_item_played"], + "record" => "0" + ); + + $previousMedia = CcScheduleQuery::create() + ->filterByDbId($currentMediaScheduleId-1) + ->filterByDbPlayoutStatus(0, Criteria::GREATER_THAN) + ->orderByDbStarts() + ->findOne(); + if (isset($previousMedia)) { + $previousMediaFileId = $previousMedia->getDbFileId(); + $previousMediaStreamId = $previousMedia->getDbStreamId(); + if (isset($previousMediaFileId)) { + $previousMediaType = "track"; + $previousFile = CcFilesQuery::create() + ->filterByDbId($previousMediaFileId) + ->findOne(); + $previousMediaName = $previousFile->getDbArtistName() . " - " . $previousFile->getDbTrackTitle(); + } else if (isset($previousMediaStreamId)) { + $previousMediaName = null; + $previousMediaType = "webstream"; + $previousWebstream = CcWebstreamQuery::create() + ->filterByDbId($previousMediaStreamId) + ->findOne(); + /*$previousWebstreamMetadata = CcWebstreamMetadataQuery::create() + ->filterByDbInstanceId($previousMedia->getDbInstanceId()) + ->orderByDbStartTime(Criteria::DESC) + ->findOne();*/ + $previousMediaName = $previousWebstream->getDbName(); + } else { + $previousMediaType = null; + } + $results["previous"] = array( + "starts" => $previousMedia->getDbStarts(), + "ends" => $previousMedia->getDbEnds(), + "type" => $previousMediaType, + "name" => $previousMediaName + ); + } + + $nextMedia = CcScheduleQuery::create() + ->filterByDbId($currentMediaScheduleId+1) + ->orderByDbStarts() + ->findOne(); + if (isset($nextMedia)) { + $nextMediaFileId = $nextMedia->getDbFileId(); + $nextMediaStreamId = $nextMedia->getDbStreamId(); + if (isset($nextMediaFileId)) { + $nextMediaType = "track"; + $nextFile = CcFilesQuery::create() + ->filterByDbId($nextMediaFileId) + ->findOne(); + $nextMediaName = $nextFile->getDbArtistName() . " - " . $nextFile->getDbTrackTitle(); + } else if (isset($nextMediaStreamId)) { + $nextMediaType = "webstream"; + $nextWebstream = CcWebstreamQuery::create() + ->filterByDbId($nextMediaStreamId) + ->findOne(); + /*$nextWebstreamMetadata = CcWebstreamMetadataQuery::create() + ->filterByDbInstanceId($nextMedia->getDbInstanceId()) + ->orderByDbStartTime(Criteria::DESC) + ->findOne();*/ + $nextMediaName = $nextWebstream->getDbName(); + } else { + $nextMediaType = null; + } + $results["next"] = array( + "starts" => $nextMedia->getDbStarts(), + "ends" => $nextMedia->getDbEnds(), + "type" => $nextMediaType, + "name" => $nextMediaName + ); } return $results; + } public static function GetLastScheduleItem($p_timeNow) diff --git a/python_apps/pypo/liquidsoap/ls_script.liq b/python_apps/pypo/liquidsoap/ls_script.liq index 3174d9c52..8055aa48b 100644 --- a/python_apps/pypo/liquidsoap/ls_script.liq +++ b/python_apps/pypo/liquidsoap/ls_script.liq @@ -249,7 +249,6 @@ s = if dj_live_stream_port != 0 and dj_live_stream_mp != "" then on_connect=live_dj_connect, on_disconnect=live_dj_disconnect)) - dj_live = on_metadata(notify_queue, dj_live) ignore(output.dummy(dj_live, fallible=true)) switch(id="show_schedule_noise_switch", @@ -272,7 +271,6 @@ s = if master_live_stream_port != 0 and master_live_stream_mp != "" then on_connect=master_dj_connect, on_disconnect=master_dj_disconnect)) - master_dj = on_metadata(notify_queue, master_dj) ignore(output.dummy(master_dj, fallible=true)) switch(id="master_show_schedule_noise_switch", @@ -284,8 +282,6 @@ else s end -# Send metadata notifications when using master source -s = on_metadata(notify_queue, s) # Attach a skip command to the source s: #add_skip_command(s)