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-29 23:26:20 +01:00
|
|
|
// Since episodes don't have to be uploaded with a time (H:i:s) component,
|
|
|
|
// store the timestamp of the most recent (first pushed to the array) episode
|
|
|
|
// that we're ingesting.
|
|
|
|
// Note that this folds to the failure case (Celery task timeout/download failure)
|
|
|
|
// but will at least continue to ingest new episodes.
|
2015-10-30 15:29:22 +01:00
|
|
|
if (!empty($episodes)) {
|
|
|
|
$podcast->setDbAutoIngestTimestamp(gmdate('r', strtotime($episodes[0]->getDbPublicationDate())))->save();
|
|
|
|
$service->downloadEpisodes($episodes);
|
|
|
|
}
|
2015-10-21 01:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2015-11-03 00:07:16 +01:00
|
|
|
$episodeList = $service->getPodcastEpisodes($podcast->getDbPodcastId());
|
2015-10-21 01:03:34 +02:00
|
|
|
$episodes = array();
|
2017-02-20 21:47:53 +01:00
|
|
|
usort($episodeList, array(__CLASS__, "_sortByEpisodePubDate"));
|
2015-10-21 01:03:34 +02:00
|
|
|
for ($i = 0; $i < sizeof($episodeList); $i++) {
|
|
|
|
$episodeData = $episodeList[$i];
|
2015-10-29 22:53:45 +01:00
|
|
|
$ts = $podcast->getDbAutoIngestTimestamp();
|
|
|
|
// If the timestamp for this podcast is empty (no previous episodes have been ingested) and there are no
|
|
|
|
// episodes in the list of episodes to ingest, don't skip this episode - we should try to ingest the
|
|
|
|
// most recent episode when the user first sets the podcast to automatic ingest.
|
2015-10-21 01:03:34 +02:00
|
|
|
// If the publication date of this episode is before the ingest timestamp, we don't need to ingest it
|
2015-11-06 00:15:58 +01:00
|
|
|
if ((empty($ts) && ($i > 0)) || strtotime($episodeData["pub_date"]) < strtotime($ts)) {
|
2015-10-29 22:53:45 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2019-01-13 15:50:21 +01: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;
|
2015-10-29 22:53:45 +01:00
|
|
|
return (strtotime($a["pub_date"]) < strtotime($b["pub_date"])) ? 1 : -1; // Descending order
|
2015-10-15 20:44:17 +02:00
|
|
|
}
|
|
|
|
|
2019-01-13 17:46:55 +01:00
|
|
|
}
|