From 4451c49c067a79ce180aa033e6f9949e5c7ab999 Mon Sep 17 00:00:00 2001 From: drigato Date: Tue, 19 May 2015 14:39:05 -0400 Subject: [PATCH] SAAS-772: Send metadata to Tunein This is pretty much working except with master source metadata --- airtime_mvc/application/common/TuneIn.php | 13 ++- .../application/forms/TuneInPreferences.php | 84 +++++++++++++++---- airtime_mvc/application/models/Schedule.php | 23 +++++ .../scripts/form/preferences_tunein.phtml | 3 +- 4 files changed, 99 insertions(+), 24 deletions(-) diff --git a/airtime_mvc/application/common/TuneIn.php b/airtime_mvc/application/common/TuneIn.php index 54dedc516..1dcfdf742 100644 --- a/airtime_mvc/application/common/TuneIn.php +++ b/airtime_mvc/application/common/TuneIn.php @@ -4,12 +4,12 @@ class Application_Common_TuneIn { public static function sendMetadataToTunein($title, $artist) { - $tuneInStationID = null; - $tuneInPartnerKey = null; - $tuneInPartnerID = null; + $tuneInStationID = Application_Model_Preference::getTuneinStationId(); + $tuneInPartnerID = Application_Model_Preference::getTuneinPartnerId(); + $tuneInPartnerKey = Application_Model_Preference::getTuneinPartnerKey(); $qry_str = "?partnerId=".$tuneInPartnerID."&partnerKey=".$tuneInPartnerKey."&id=".$tuneInStationID - ."&title=".$title."&artist=".$artist; + ."&title=".$title."&artist=".$artist."&commercial=false"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, TUNEIN_API_URL . $qry_str); @@ -17,13 +17,12 @@ class Application_Common_TuneIn curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 30); - $jsonData = curl_exec($ch); + curl_exec($ch); 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); - $arr = json_decode($jsonData, true); - Logging::info($arr); } + } diff --git a/airtime_mvc/application/forms/TuneInPreferences.php b/airtime_mvc/application/forms/TuneInPreferences.php index 5eb729aca..6ee52588a 100644 --- a/airtime_mvc/application/forms/TuneInPreferences.php +++ b/airtime_mvc/application/forms/TuneInPreferences.php @@ -42,39 +42,91 @@ class Application_Form_TuneInPreferences extends Zend_Form_SubForm public function isValid($data) { - // Make request to TuneIn API to test the settings are valid + $valid = true; + // Make request to TuneIn API to test the settings are valid. + // TuneIn does not have an API to make test requests to check if + // the credentials are correct. Therefore we will make a request + // with the commercial flag set to true, which removes the metadata + // from the station on TuneIn. After that, and if the test request + // succeeds, we will make another request with the real metadata. if ($data["enable_tunein"]) { - $qry_str = "?partnerId=".$data["tunein_partner_id"]."&partnerKey=".$data["tunein_partner_key"]."&id=".$data["tunein_station_id"] - ."&title=&artist="; + $qry_str = "?partnerId=".$data["tunein_partner_id"]."&partnerKey=".$data["tunein_partner_key"]."&id=".$data["tunein_station_id"]; + $commercialFlagQryStr = "&commercial=true"; $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, TUNEIN_API_URL . $qry_str); + curl_setopt($ch, CURLOPT_URL, TUNEIN_API_URL . $qry_str . $commercialFlagQryStr); curl_setopt($ch, CURLOPT_FAILONERROR, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 30); - $jsonData = curl_exec($ch); + $xmlData = curl_exec($ch); if (curl_error($ch)) { Logging::error("Failed to reach TuneIn: ". curl_errno($ch)." - ". curl_error($ch) . " - " . curl_getinfo($ch, CURLINFO_EFFECTIVE_URL)); if (curl_error($ch) == "The requested URL returned error: 403 Forbidden") { $this->getElement("enable_tunein")->setErrors(array(_("Invalid TuneIn Settings. Please ensure your TuneIn settings are correct and try again."))); - // Set values to what the user entered since the form is invalid so they - // don't have to enter in the values again and can see what they entered. - $this->getElement("enable_tunein")->setValue($data["enable_tunein"]); - $this->getElement("tunein_partner_key")->setValue($data["tunein_partner_key"]); - $this->getElement("tunein_partner_id")->setValue($data["tunein_partner_id"]); - $this->getElement("tunein_station_id")->setValue($data["tunein_station_id"]); - - return false; + $valid = false; } } curl_close($ch); - $arr = json_decode($jsonData, true); - Logging::info($arr); + if ($valid) { + $xmlObj = new SimpleXMLElement($xmlData); + if (!$xmlObj || $xmlObj->head->status != "200") { + $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 { - return true; + $valid = true; } + + if (!$valid) { + // Set values to what the user entered since the form is invalid so they + // don't have to enter in the values again and can see what they entered. + $this->getElement("enable_tunein")->setValue($data["enable_tunein"]); + $this->getElement("tunein_partner_key")->setValue($data["tunein_partner_key"]); + $this->getElement("tunein_partner_id")->setValue($data["tunein_partner_id"]); + $this->getElement("tunein_station_id")->setValue($data["tunein_station_id"]); + } + + return $valid; } } diff --git a/airtime_mvc/application/models/Schedule.php b/airtime_mvc/application/models/Schedule.php index 2993dc03d..66d00661b 100644 --- a/airtime_mvc/application/models/Schedule.php +++ b/airtime_mvc/application/models/Schedule.php @@ -56,6 +56,29 @@ SQL; return $real_streams; } + + /** + * Returns an array with 2 elements: artist and title name of the track that is currently playing. + * Elements will be set to null if metadata is not set for those fields. + * + * Returns null if no track is currently playing. + * + * Data is based on GetPlayOrderRange() in this class. + */ + public static function getCurrentPlayingTrack() + { + $currentScheduleInfo = self::GetPlayOrderRange(); + if (empty($currentScheduleInfo["tracks"]["current"])) { + return null; + } + + $currentTrackArray = explode(" - ", $currentScheduleInfo["tracks"]["current"]["name"]); + $currentTrackMetadata = array( + "artist" => empty($currentTrackArray[0]) ? null : urlencode($currentTrackArray[0]), + "title" => empty($currentTrackArray[1]) ? null : urlencode($currentTrackArray[1]) + ); + return $currentTrackMetadata; + } /** * Returns data related to the scheduled items. diff --git a/airtime_mvc/application/views/scripts/form/preferences_tunein.phtml b/airtime_mvc/application/views/scripts/form/preferences_tunein.phtml index f7ae2bc84..d5fd34dd5 100644 --- a/airtime_mvc/application/views/scripts/form/preferences_tunein.phtml +++ b/airtime_mvc/application/views/scripts/form/preferences_tunein.phtml @@ -10,8 +10,9 @@ element->getElement('tunein_station_id')->render() ?> + element->getElement('tunein_partner_id')->render() ?> + element->getElement('tunein_partner_key')->render() ?> - element->getElement('tunein_partner_id')->render() ?> \ No newline at end of file