2015-10-15 20:44:17 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class PodcastManager {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int how often, in seconds, to check for and ingest new podcast episodes
|
|
|
|
*/
|
2015-10-22 18:22:56 +02:00
|
|
|
private static $_PODCAST_POLL_INTERVAL_SECONDS = 3600; // 1 hour
|
2015-10-15 20:44:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether $_PODCAST_POLL_INTERVAL_SECONDS have passed since the last call to
|
|
|
|
* downloadNewestEpisodes
|
|
|
|
*
|
|
|
|
* @return bool true if $_PODCAST_POLL_INTERVAL_SECONDS has passed since the last check
|
|
|
|
*/
|
|
|
|
public static function hasPodcastPollIntervalPassed() {
|
|
|
|
$lastPolled = Application_Model_Preference::getPodcastPollLock();
|
|
|
|
return empty($lastPolled) || (microtime(true) > $lastPolled + self::$_PODCAST_POLL_INTERVAL_SECONDS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all podcasts flagged for automatic ingest whose most recent episode has
|
|
|
|
* yet to be downloaded and download it with Celery
|
|
|
|
*
|
|
|
|
* @throws InvalidPodcastException
|
|
|
|
* @throws PodcastNotFoundException
|
|
|
|
*/
|
|
|
|
public static function downloadNewestEpisodes() {
|
|
|
|
$autoIngestPodcasts = static::_getAutoIngestPodcasts();
|
|
|
|
$service = new Application_Service_PodcastEpisodeService();
|
|
|
|
foreach ($autoIngestPodcasts as $podcast) {
|
2015-10-21 01:03:34 +02:00
|
|
|
$episodes = static::_findUningestedEpisodes($podcast, $service);
|
2015-10-21 17:18:33 +02:00
|
|
|
$podcast->setDbAutoIngestTimestamp(gmdate('r'))->save();
|
2015-10-21 01:03:34 +02:00
|
|
|
$service->downloadEpisodes($episodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
Application_Model_Preference::setPodcastPollLock(microtime(true));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an ImportedPodcast, find all uningested episodes since the last automatic ingest,
|
|
|
|
* and add them to a given episodes array
|
|
|
|
*
|
|
|
|
* @param ImportedPodcast $podcast the podcast to search
|
|
|
|
* @param Application_Service_PodcastEpisodeService $service podcast episode service object
|
|
|
|
*
|
|
|
|
* @return array array of episodes to append be downloaded
|
|
|
|
*/
|
|
|
|
protected static function _findUningestedEpisodes($podcast, $service) {
|
|
|
|
$podcastArray = Application_Service_PodcastService::getPodcastById($podcast->getDbPodcastId());
|
|
|
|
$episodeList = $podcastArray["episodes"];
|
|
|
|
$episodes = array();
|
2015-10-23 00:03:38 +02:00
|
|
|
// Sort the episodes by publication date to get the most recent
|
2015-10-28 15:58:22 +01:00
|
|
|
// usort($episodeList, array(static::class, "_sortByEpisodePubDate"));
|
2015-10-21 01:03:34 +02:00
|
|
|
for ($i = 0; $i < sizeof($episodeList); $i++) {
|
|
|
|
$episodeData = $episodeList[$i];
|
|
|
|
// If the publication date of this episode is before the ingest timestamp, we don't need to ingest it
|
2015-10-28 15:58:22 +01:00
|
|
|
if (strtotime($episodeData["pub_date"]) < strtotime($podcast->getDbAutoIngestTimestamp())) continue;
|
2015-10-15 20:44:17 +02:00
|
|
|
$episode = PodcastEpisodesQuery::create()->findOneByDbEpisodeGuid($episodeData["guid"]);
|
|
|
|
// Make sure there's no existing episode placeholder or import, and that the data is non-empty
|
|
|
|
if (empty($episode) && !empty($episodeData)) {
|
2015-10-21 01:03:34 +02:00
|
|
|
$placeholder = $service->addPlaceholder($podcast->getDbPodcastId(), $episodeData);
|
2015-10-15 20:44:17 +02:00
|
|
|
array_push($episodes, $placeholder);
|
|
|
|
}
|
|
|
|
}
|
2015-10-21 01:03:34 +02:00
|
|
|
return $episodes;
|
2015-10-15 20:44:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all podcasts flagged for automatic ingest
|
|
|
|
*
|
|
|
|
* @return PropelObjectCollection collection of ImportedPodcast objects
|
|
|
|
* flagged for automatic ingest
|
|
|
|
*/
|
|
|
|
protected static function _getAutoIngestPodcasts() {
|
|
|
|
return ImportedPodcastQuery::create()
|
|
|
|
->filterByDbAutoIngest(true)
|
|
|
|
->find();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom sort function for podcast episodes
|
|
|
|
*
|
|
|
|
* @param array $a first episode array to compare
|
|
|
|
* @param array $b second episode array to compare
|
|
|
|
* @return bool boolean for ordering
|
|
|
|
*/
|
|
|
|
protected static function _sortByEpisodePubDate($a, $b) {
|
|
|
|
if ($a["pub_date"] == $b["pub_date"]) return 0;
|
|
|
|
return ($a["pub_date"] < $b["pub_date"]) ? 1 : -1; // Descending order
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|