2015-10-15 20:44:17 +02:00
|
|
|
<?php
|
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
class PodcastManager
|
|
|
|
{
|
2015-10-15 20:44:17 +02:00
|
|
|
/**
|
|
|
|
* @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
|
2021-10-11 16:10:47 +02:00
|
|
|
* downloadNewestEpisodes.
|
2015-10-15 20:44:17 +02:00
|
|
|
*
|
|
|
|
* @return bool true if $_PODCAST_POLL_INTERVAL_SECONDS has passed since the last check
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public static function hasPodcastPollIntervalPassed()
|
|
|
|
{
|
2015-10-15 20:44:17 +02:00
|
|
|
$lastPolled = Application_Model_Preference::getPodcastPollLock();
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2015-10-15 20:44:17 +02:00
|
|
|
return empty($lastPolled) || (microtime(true) > $lastPolled + self::$_PODCAST_POLL_INTERVAL_SECONDS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all podcasts flagged for automatic ingest whose most recent episode has
|
2021-10-11 16:10:47 +02:00
|
|
|
* yet to be downloaded and download it with Celery.
|
2015-10-15 20:44:17 +02:00
|
|
|
*
|
|
|
|
* @throws InvalidPodcastException
|
|
|
|
* @throws PodcastNotFoundException
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public static function downloadNewestEpisodes()
|
|
|
|
{
|
2015-10-15 20:44:17 +02:00
|
|
|
$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,
|
2021-10-11 16:10:47 +02:00
|
|
|
* and add them to a given episodes array.
|
2015-10-21 01:03:34 +02:00
|
|
|
*
|
2021-10-11 16:10:47 +02:00
|
|
|
* @param ImportedPodcast $podcast the podcast to search
|
|
|
|
* @param Application_Service_PodcastEpisodeService $service podcast episode service object
|
2015-10-21 01:03:34 +02:00
|
|
|
*
|
|
|
|
* @return array array of episodes to append be downloaded
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
protected static function _findUningestedEpisodes($podcast, $service)
|
|
|
|
{
|
2015-11-03 00:07:16 +01:00
|
|
|
$episodeList = $service->getPodcastEpisodes($podcast->getDbPodcastId());
|
2021-10-11 16:10:47 +02:00
|
|
|
$episodes = [];
|
|
|
|
usort($episodeList, [__CLASS__, '_sortByEpisodePubDate']);
|
Feature: Support php7.4 (#1354)
* Run CI tests against php 7.4
* Sort composer dependencies
* Remove unused Aws S3 php library
* Pin simplepie dependency to ^1.5
* Pin getid3 dependency to ^1.9
* Pin composer semver to ^3.2
* Pin php-amqplib to ^2.12
* Drop sentry logging support
* Update composer dependencies
* Move propel regenerate to Makefile
* Regenerate propel files with v1.7.0
* Pin propel orm to ^1.7
* Regenerate propel files with v1.7.2
* fix: generator_version in airtime-conf-production.php
* Replace propel/propel1 with jooola/propel1
* Regenerate propel files with v1.7.3-dev
* Fix php7.4 compatibility
Using php-cs-fixer:
'@PhpCsFixer' => true,
'concat_space' => ['spacing' => 'one'],
'ordered_class_elements' => false,
'yoda_style' => false,
'@PHP74Migration' => true,
'assign_null_coalescing_to_coalesce_equal' => false,
'ternary_to_null_coalescing' => false,
'heredoc_indentation' => false,
'@PHP74Migration:risky' => true,
'declare_strict_types' => false,
'void_return' => false,
'use_arrow_functions' => false,
* Fix pre-commit
2021-10-17 17:19:53 +02:00
|
|
|
for ($i = 0; $i < count($episodeList); ++$i) {
|
2015-10-21 01:03:34 +02:00
|
|
|
$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
|
2021-10-11 16:10:47 +02:00
|
|
|
if ((empty($ts) && ($i > 0)) || strtotime($episodeData['pub_date']) < strtotime($ts)) {
|
2015-10-29 22:53:45 +01:00
|
|
|
continue;
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
$episode = PodcastEpisodesQuery::create()->findOneByDbEpisodeGuid($episodeData['guid']);
|
2015-10-15 20:44:17 +02:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2019-01-13 15:50:21 +01:00
|
|
|
return $episodes;
|
2015-10-15 20:44:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Find all podcasts flagged for automatic ingest.
|
2015-10-15 20:44:17 +02:00
|
|
|
*
|
|
|
|
* @return PropelObjectCollection collection of ImportedPodcast objects
|
|
|
|
* flagged for automatic ingest
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
protected static function _getAutoIngestPodcasts()
|
|
|
|
{
|
2015-10-15 20:44:17 +02:00
|
|
|
return ImportedPodcastQuery::create()
|
|
|
|
->filterByDbAutoIngest(true)
|
2022-01-23 19:15:55 +01:00
|
|
|
->find();
|
2015-10-15 20:44:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Custom sort function for podcast episodes.
|
2015-10-15 20:44:17 +02:00
|
|
|
*
|
|
|
|
* @param array $a first episode array to compare
|
|
|
|
* @param array $b second episode array to compare
|
2021-10-11 16:10:47 +02:00
|
|
|
*
|
2015-10-15 20:44:17 +02:00
|
|
|
* @return bool boolean for ordering
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
protected static function _sortByEpisodePubDate($a, $b)
|
|
|
|
{
|
|
|
|
if ($a['pub_date'] == $b['pub_date']) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-15 20:44:17 +02:00
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
return (strtotime($a['pub_date']) < strtotime($b['pub_date'])) ? 1 : -1; // Descending order
|
|
|
|
}
|
2019-01-13 17:46:55 +01:00
|
|
|
}
|