$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) { $episodes = static::_findUningestedEpisodes($podcast, $service); $podcast->setDbAutoIngestTimestamp(gmdate('r'))->save(); $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(); // Sort the episodes by publication date to get the most recent // usort($episodeList, array(static::class, "_sortByEpisodePubDate")); 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 if (strtotime($episodeData["pub_date"]) < strtotime($podcast->getDbAutoIngestTimestamp())) continue; $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)) { $placeholder = $service->addPlaceholder($podcast->getDbPodcastId(), $episodeData); array_push($episodes, $placeholder); } } return $episodes; } /** * 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 } }