2015-09-16 20:22:13 +02:00
|
|
|
<?php
|
|
|
|
|
2015-10-13 16:14:23 +02:00
|
|
|
class Application_Service_PodcastEpisodeService extends Application_Service_ThirdPartyCeleryService
|
2015-09-16 20:22:13 +02:00
|
|
|
{
|
2015-09-23 02:22:06 +02:00
|
|
|
/**
|
|
|
|
* Arbitrary constant identifiers for the internal tasks array
|
|
|
|
*/
|
|
|
|
|
|
|
|
const DOWNLOAD = 'download';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string service name to store in ThirdPartyTrackReferences database
|
|
|
|
*/
|
|
|
|
protected static $_SERVICE_NAME = PODCAST_SERVICE_NAME; // Service name constant from constants.php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string exchange name for Podcast tasks
|
|
|
|
*/
|
|
|
|
protected static $_CELERY_EXCHANGE_NAME = 'podcast';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array map of constant identifiers to Celery task names
|
|
|
|
*/
|
|
|
|
protected static $_CELERY_TASKS = [
|
|
|
|
self::DOWNLOAD => 'podcast-download' // TODO: rename this to ingest?
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2015-10-15 20:44:17 +02:00
|
|
|
* Given an array of episodes, store them in the database as placeholder objects until
|
|
|
|
* they can be processed by Celery
|
2015-09-23 02:22:06 +02:00
|
|
|
*
|
2015-09-24 21:57:38 +02:00
|
|
|
* @param int $podcastId Podcast object identifier
|
|
|
|
* @param array $episodes array of podcast episodes
|
|
|
|
*
|
|
|
|
* @return array the stored PodcastEpisodes objects
|
|
|
|
*/
|
|
|
|
public function addPodcastEpisodePlaceholders($podcastId, $episodes) {
|
|
|
|
$storedEpisodes = array();
|
|
|
|
foreach ($episodes as $episode) {
|
2015-10-15 20:44:17 +02:00
|
|
|
$e = $this->addPodcastEpisodePlaceholder($podcastId, $episode);
|
2015-09-24 21:57:38 +02:00
|
|
|
array_push($storedEpisodes, $e);
|
|
|
|
}
|
|
|
|
return $storedEpisodes;
|
|
|
|
}
|
|
|
|
|
2015-10-15 20:44:17 +02:00
|
|
|
/**
|
|
|
|
* Given an episode, store it in the database as a placeholder object until
|
|
|
|
* it can be processed by Celery
|
|
|
|
*
|
|
|
|
* @param int $podcastId Podcast object identifier
|
|
|
|
* @param array $episode array of podcast episode data
|
|
|
|
*
|
|
|
|
* @return PodcastEpisodes the stored PodcastEpisodes object
|
|
|
|
*/
|
|
|
|
public function addPodcastEpisodePlaceholder($podcastId, $episode) {
|
|
|
|
// We need to check whether the array is parsed directly from the SimplePie
|
|
|
|
// feed object, or whether it's passed in as json
|
|
|
|
if ($episode["enclosure"] instanceof SimplePie_Enclosure) {
|
|
|
|
$url = $episode["enclosure"]->get_link();
|
|
|
|
} else {
|
|
|
|
$url = $episode["enclosure"]["link"];
|
|
|
|
}
|
|
|
|
$e = new PodcastEpisodes();
|
|
|
|
$e->setDbPodcastId($podcastId);
|
|
|
|
$e->setDbDownloadUrl($url);
|
|
|
|
$e->setDbEpisodeGuid($episode["guid"]);
|
|
|
|
$e->setDbPublicationDate($episode["pub_date"]);
|
|
|
|
$e->save();
|
|
|
|
return $e;
|
|
|
|
}
|
|
|
|
|
2015-09-24 21:57:38 +02:00
|
|
|
/**
|
|
|
|
* Given an array of episodes, extract the IDs and download URLs and send them to Celery
|
|
|
|
*
|
2015-09-24 18:58:02 +02:00
|
|
|
* @param array $episodes array of podcast episodes
|
|
|
|
*/
|
|
|
|
public function downloadEpisodes($episodes) {
|
|
|
|
$episodeUrls = array();
|
2015-09-24 21:57:38 +02:00
|
|
|
/** @var PodcastEpisodes $episode */
|
2015-09-24 18:58:02 +02:00
|
|
|
foreach($episodes as $episode) {
|
2015-09-24 21:57:38 +02:00
|
|
|
array_push($episodeUrls, array("id" => $episode->getDbId(),
|
|
|
|
"url" => $episode->getDbDownloadUrl()));
|
2015-09-24 18:58:02 +02:00
|
|
|
}
|
2015-10-15 20:44:17 +02:00
|
|
|
if (empty($episodeUrls)) return;
|
2015-09-24 18:58:02 +02:00
|
|
|
$this->_download($episodeUrls);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an array of download URLs, download RSS feed tracks
|
|
|
|
*
|
2015-09-24 21:57:38 +02:00
|
|
|
* @param array $episodes array of episodes containing download URLs and IDs to send to Celery
|
2015-09-23 02:22:06 +02:00
|
|
|
*/
|
2015-09-24 21:57:38 +02:00
|
|
|
private function _download($episodes) {
|
2015-09-23 02:22:06 +02:00
|
|
|
$CC_CONFIG = Config::getConfig();
|
|
|
|
$data = array(
|
2015-09-24 21:57:38 +02:00
|
|
|
'episodes' => $episodes,
|
2015-09-23 02:22:06 +02:00
|
|
|
'callback_url' => Application_Common_HTTPHelper::getStationUrl() . '/rest/media',
|
|
|
|
'api_key' => $apiKey = $CC_CONFIG["apiKey"][0],
|
|
|
|
);
|
2015-09-24 21:57:38 +02:00
|
|
|
$this->_executeTask(static::$_CELERY_TASKS[self::DOWNLOAD], $data);
|
2015-09-23 02:22:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a ThirdPartyTrackReferences object for a completed upload
|
|
|
|
*
|
2015-09-24 21:57:38 +02:00
|
|
|
* @param $task CeleryTasks the completed CeleryTasks object
|
|
|
|
* @param $episodeId int PodcastEpisodes identifier
|
|
|
|
* @param $episodes array array containing Podcast episode information
|
|
|
|
* @param $status string Celery task status
|
2015-09-23 02:22:06 +02:00
|
|
|
*
|
|
|
|
* @return ThirdPartyTrackReferences the updated ThirdPartyTrackReferences object
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
* @throws PropelException
|
|
|
|
*/
|
2015-09-24 21:57:38 +02:00
|
|
|
public function updateTrackReference($task, $episodeId, $episodes, $status) {
|
|
|
|
$ref = parent::updateTrackReference($task, $episodeId, $episodes, $status);
|
2015-09-23 02:22:06 +02:00
|
|
|
|
|
|
|
if ($status == CELERY_SUCCESS_STATUS) {
|
2015-10-19 21:36:34 +02:00
|
|
|
foreach ($episodes as $episode) {
|
2015-09-24 21:57:38 +02:00
|
|
|
// Since we process episode downloads as a batch, individual downloads can fail
|
|
|
|
// even if the task itself succeeds
|
2015-10-19 21:36:34 +02:00
|
|
|
$dbEpisode = PodcastEpisodesQuery::create()
|
|
|
|
->findOneByDbId($episode->episodeid);
|
2015-09-24 21:57:38 +02:00
|
|
|
if ($episode->status) {
|
|
|
|
$dbEpisode->setDbFileId($episode->fileid)
|
|
|
|
->save();
|
2015-10-19 21:36:34 +02:00
|
|
|
} else {
|
|
|
|
Logging::warn("Celery task $task episode $episode->episodeid unsuccessful with status $episode->status");
|
|
|
|
$dbEpisode->delete();
|
2015-09-24 21:57:38 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-23 02:22:06 +02:00
|
|
|
}
|
2015-10-19 21:36:34 +02:00
|
|
|
// TODO: do we need a broader fail condition here?
|
2015-09-23 02:22:06 +02:00
|
|
|
|
|
|
|
return $ref;
|
|
|
|
}
|
2015-09-16 20:22:13 +02:00
|
|
|
}
|