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
{
/**
* @param $title url encoded string
* @param $artist url encoded string
*/
public static function sendMetadataToTunein($title, $artist)
{
$credQryStr = self::getCredentialsQueryString();

View file

@ -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 {

View file

@ -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 = <<<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['current'] = 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.
if ($rows[$i]['ends'] > $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)

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_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)