Merge branch 'saas-dev' into saas-installer-albert

This commit is contained in:
Albert Santoni 2015-05-22 19:09:30 -04:00
commit 6fad0a3ee6
4 changed files with 142 additions and 153 deletions

View file

@ -2,6 +2,10 @@
class Application_Common_TuneIn class Application_Common_TuneIn
{ {
/**
* @param $title url encoded string
* @param $artist url encoded string
*/
public static function sendMetadataToTunein($title, $artist) public static function sendMetadataToTunein($title, $artist)
{ {
$credQryStr = self::getCredentialsQueryString(); $credQryStr = self::getCredentialsQueryString();

View file

@ -88,42 +88,6 @@ class Application_Form_TuneInPreferences extends Zend_Form_SubForm
$valid = false; $valid = false;
} else if ($xmlObj->head->status == "200") { } else if ($xmlObj->head->status == "200") {
$valid = true; $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 { } else {

View file

@ -98,10 +98,8 @@ SQL;
$utcNow = new DateTime("now", new DateTimeZone("UTC")); $utcNow = new DateTime("now", new DateTimeZone("UTC"));
$shows = Application_Model_Show::getPrevCurrentNext($utcNow, $utcTimeEnd, $showsToRetrieve); $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; $currentShowID = count($shows['currentShow'])>0?$shows['currentShow']['instance_id']:null;
$nextShowID = count($shows['nextShow'])>0?$shows['nextShow'][0]['instance_id']:null; $results = Application_Model_Schedule::getPreviousCurrentNextMedia($utcNow, $currentShowID);
$results = self::GetPrevCurrentNext($previousShowID, $currentShowID, $nextShowID, $utcNow);
$range = array( $range = array(
"station" => array ( "station" => array (
@ -136,10 +134,8 @@ SQL;
$utcNow = new DateTime("now", new DateTimeZone("UTC")); $utcNow = new DateTime("now", new DateTimeZone("UTC"));
$shows = Application_Model_Show::getPrevCurrentNextOld($utcNow); $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; $currentShowID = count($shows['currentShow'])>0?$shows['currentShow'][0]['instance_id']:null;
$nextShowID = count($shows['nextShow'])>0?$shows['nextShow'][0]['instance_id']:null; $results = Application_Model_Schedule::getPreviousCurrentNextMedia($utcNow, $currentShowID);
$results = self::GetPrevCurrentNext($previousShowID, $currentShowID, $nextShowID, $utcNow);
$range = array( $range = array(
"env" => APPLICATION_ENV, "env" => APPLICATION_ENV,
@ -157,129 +153,158 @@ SQL;
} }
/** /**
* Queries the database for the set of schedules one hour before * Attempts to find a media item (track or webstream) that is currently playing.
* and after the given time. If a show starts and ends within that * If a current media item is currently playing, this function then attempts to
* time that is considered the current show. Then the scheduled item * find an item that played previously and is scheduled to play next.
* before it is the previous show, and the scheduled item after it *
* is the next show. This way the dashboard getCurrentPlaylist is * @param $utcNow DateTime current time in UTC
* very fast. But if any one of the three show types are not found * @param $currentShowInstanceId cc_show_instance id of the show instance currently playing
* through this mechanism a call is made to the old way of querying * @return array with data about the previous, current, and next media items playing.
* the database to find the track info. * Returns an empty arrays if there is no media item currently playing
**/ */
public static function GetPrevCurrentNext($p_previousShowID, $p_currentShowID, $p_nextShowID, $utcNow) public static function getPreviousCurrentNextMedia($utcNow, $currentShowInstanceId)
{ {
$timeZone = new DateTimeZone("UTC"); //This function works entirely in UTC. $timeZone = new DateTimeZone("UTC"); //This function works entirely in UTC.
assert(get_class($utcNow) === "DateTime"); assert(get_class($utcNow) === "DateTime");
assert($utcNow->getTimeZone() == $timeZone); 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 = <<<SQL
FROM cc_schedule AS st
JOIN cc_webstream ws ON st.stream_id = ws.id
LEFT JOIN cc_show_instances AS si ON st.instance_id = si.id
LEFT JOIN cc_subjs AS sub ON sub.id = ws.creator_id
LEFT JOIN
(SELECT *
FROM cc_webstream_metadata
ORDER BY start_time DESC LIMIT 1) AS wm ON st.id = wm.instance_id
SQL;
$predicateArr = array();
$paramMap = array();
if (isset($p_previousShowID)) {
$predicateArr[] = 'st.instance_id = :previousShowId';
$paramMap[':previousShowId'] = $p_previousShowID;
}
if (isset($p_currentShowID)) {
$predicateArr[] = 'st.instance_id = :currentShowId';
$paramMap[':currentShowId'] = $p_currentShowID;
}
if (isset($p_nextShowID)) {
$predicateArr[] = 'st.instance_id = :nextShowId';
$paramMap[':nextShowId'] = $p_nextShowID;
}
$sql .= " (".implode(" OR ", $predicateArr).") ";
$sql .= ' AND st.playout_status > 0 ORDER BY st.starts';
$filesSql = str_replace("%%columns%%", $fileColumns, $sql);
$filesSql = str_replace("%%tables%%", $fileJoin, $filesSql);
$streamSql = str_replace("%%columns%%", $streamColumns, $sql);
$streamSql = str_replace("%%tables%%", $streamJoin, $streamSql);
$sql = "SELECT * FROM (($filesSql) UNION ($streamSql)) AS unioned ORDER BY starts";
$rows = Application_Common_Database::prepareAndExecute($sql, $paramMap);
$numberOfRows = count($rows);
$results['previous'] = null; $results['previous'] = null;
$results['current'] = null; $results['current'] = null;
$results['next'] = null; $results['next'] = null;
for ($i = 0; $i < $numberOfRows; ++$i) { $sql = "select s.id, s.starts, s.ends, s.file_id, s.stream_id, s.media_item_played,
s.instance_id, si.ends as show_ends from cc_schedule s left join cc_show_instances si
on s.instance_id = si.id where s.playout_status > 0 and s.starts <= :p1
and s.ends >= :p2 and s.instance_id = :p3 order by starts desc limit 1";
// if the show is overbooked, then update the track end time to the end of the show time. $params = array(
if ($rows[$i]['ends'] > $rows[$i]["show_ends"]) { ":p1" => $utcNow->format("Y-m-d H:i:s"),
$rows[$i]['ends'] = $rows[$i]["show_ends"]; ":p2" => $utcNow->format("Y-m-d H:i:s"),
":p3" => $currentShowInstanceId
);
$rows = Application_Common_Database::prepareAndExecute($sql, $params);
if (count($rows) < 1) {
return $results;
} }
$curShowStartTime = new DateTime($rows[$i]['starts'], $timeZone); if ($rows[0]["show_ends"] < $utcNow->format("Y-m-d H:i:s")) {
$curShowEndTime = new DateTime($rows[$i]['ends'], $timeZone); return $results;
}
if (($curShowStartTime <= $utcNow) && ($curShowEndTime >= $utcNow)) { $currentMedia = $rows[0];
if ($i - 1 >= 0) {
$results['previous'] = array("name"=>$rows[$i-1]["artist_name"]." - ".$rows[$i-1]["track_title"], if ($currentMedia["ends"] > $currentMedia["show_ends"]) {
"starts"=>$rows[$i-1]["starts"], $currentMedia["ends"] = $currentMedia["show_ends"];
"ends"=>$rows[$i-1]["ends"],
"type"=>'track');
} }
$results['current'] = array("name"=>$rows[$i]["artist_name"]." - ".$rows[$i]["track_title"],
"starts"=>$rows[$i]["starts"], $currentMediaScheduleId = $currentMedia["id"];
"ends"=> (($rows[$i]["ends"] > $rows[$i]["show_ends"]) ? $rows[$i]["show_ends"]: $rows[$i]["ends"]), $currentMediaFileId = $currentMedia["file_id"];
"media_item_played"=>$rows[$i]["media_item_played"], $currentMediaStreamId = $currentMedia["stream_id"];
"record"=>0, if (isset($currentMediaFileId)) {
"type"=>'track'); $currentMediaType = "track";
if (isset($rows[$i+1])) { $currentFile = CcFilesQuery::create()
$results['next'] = array("name"=>$rows[$i+1]["artist_name"]." - ".$rows[$i+1]["track_title"], ->filterByDbId($currentMediaFileId)
"starts"=>$rows[$i+1]["starts"], ->findOne();
"ends"=>$rows[$i+1]["ends"], $currentMediaName = $currentFile->getDbArtistName() . " - " . $currentFile->getDbTrackTitle();
"type"=>'track'); } 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();
} }
break; } else {
$currentMediaType = null;
} }
if ($curShowEndTime < $utcNow ) { $results["current"] = array(
$previousIndex = $i; "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;
} }
if ($curShowStartTime > $utcNow) { $results["previous"] = array(
$results['next'] = array("name"=>$rows[$i]["artist_name"]." - ".$rows[$i]["track_title"], "starts" => $previousMedia->getDbStarts(),
"starts"=>$rows[$i]["starts"], "ends" => $previousMedia->getDbEnds(),
"ends"=>$rows[$i]["ends"], "type" => $previousMediaType,
"type"=>'track'); "name" => $previousMediaName
break; );
} }
$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;
} }
//If we didn't find a a current show because the time didn't fit we may still have $results["next"] = array(
//found a previous show so use it. "starts" => $nextMedia->getDbStarts(),
if ($results['previous'] === null && isset($previousIndex)) { "ends" => $nextMedia->getDbEnds(),
$results['previous'] = array("name"=>$rows[$previousIndex]["artist_name"]." - ".$rows[$previousIndex]["track_title"], "type" => $nextMediaType,
"starts"=>$rows[$previousIndex]["starts"], "name" => $nextMediaName
"ends"=>$rows[$previousIndex]["ends"]);; );
} }
return $results; return $results;
} }
public static function GetLastScheduleItem($p_timeNow) public static function GetLastScheduleItem($p_timeNow)

View file

@ -249,7 +249,6 @@ s = if dj_live_stream_port != 0 and dj_live_stream_mp != "" then
on_connect=live_dj_connect, on_connect=live_dj_connect,
on_disconnect=live_dj_disconnect)) on_disconnect=live_dj_disconnect))
dj_live = on_metadata(notify_queue, dj_live)
ignore(output.dummy(dj_live, fallible=true)) ignore(output.dummy(dj_live, fallible=true))
switch(id="show_schedule_noise_switch", 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_connect=master_dj_connect,
on_disconnect=master_dj_disconnect)) on_disconnect=master_dj_disconnect))
master_dj = on_metadata(notify_queue, master_dj)
ignore(output.dummy(master_dj, fallible=true)) ignore(output.dummy(master_dj, fallible=true))
switch(id="master_show_schedule_noise_switch", switch(id="master_show_schedule_noise_switch",
@ -284,8 +282,6 @@ else
s s
end end
# Send metadata notifications when using master source
s = on_metadata(notify_queue, s)
# Attach a skip command to the source s: # Attach a skip command to the source s:
#add_skip_command(s) #add_skip_command(s)