2022-03-15 16:47:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2023-06-21 16:17:11 +00:00
|
|
|
namespace Modules\WebSub\Commands;
|
2022-03-15 16:47:35 +00:00
|
|
|
|
|
|
|
use App\Models\EpisodeModel;
|
|
|
|
use App\Models\PodcastModel;
|
2023-06-21 16:17:11 +00:00
|
|
|
use CodeIgniter\CLI\BaseCommand;
|
|
|
|
use CodeIgniter\HTTP\CURLRequest;
|
2022-03-15 16:47:35 +00:00
|
|
|
use Exception;
|
2024-05-29 10:24:13 +00:00
|
|
|
use Override;
|
2022-03-15 16:47:35 +00:00
|
|
|
|
2023-06-21 16:17:11 +00:00
|
|
|
class Publish extends BaseCommand
|
2022-03-15 16:47:35 +00:00
|
|
|
{
|
2023-06-21 16:17:11 +00:00
|
|
|
protected $group = 'Websub';
|
|
|
|
|
|
|
|
protected $name = 'websub:publish';
|
|
|
|
|
|
|
|
protected $description = 'Publishes feed updates to websub hubs.';
|
|
|
|
|
2024-05-29 10:24:13 +00:00
|
|
|
#[Override]
|
2023-06-21 16:17:11 +00:00
|
|
|
public function run(array $params): void
|
2022-03-15 16:47:35 +00:00
|
|
|
{
|
|
|
|
if (ENVIRONMENT !== 'production') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-17 11:18:02 +00:00
|
|
|
helper('misc');
|
|
|
|
|
2022-03-15 16:47:35 +00:00
|
|
|
// get all podcasts that haven't been published yet
|
|
|
|
// or having a published episode that hasn't been pushed yet
|
|
|
|
$podcastModel = new PodcastModel();
|
2023-04-17 11:18:02 +00:00
|
|
|
$podcastModel->builder()
|
2022-03-15 16:47:35 +00:00
|
|
|
->select('podcasts.*')
|
2023-04-17 11:18:02 +00:00
|
|
|
->distinct()
|
2022-03-15 16:47:35 +00:00
|
|
|
->join('episodes', 'podcasts.id = episodes.podcast_id', 'left outer')
|
|
|
|
->where('podcasts.is_published_on_hubs', false)
|
2022-07-05 16:39:20 +00:00
|
|
|
->where('`' . $podcastModel->db->getPrefix() . 'podcasts`.`published_at` <= UTC_TIMESTAMP()', null, false)
|
2022-03-15 16:47:35 +00:00
|
|
|
->orGroupStart()
|
|
|
|
->where('episodes.is_published_on_hubs', false)
|
2022-04-14 14:33:53 +00:00
|
|
|
->where('`' . $podcastModel->db->getPrefix() . 'episodes`.`published_at` <= UTC_TIMESTAMP()', null, false)
|
2023-04-17 11:18:02 +00:00
|
|
|
->groupEnd();
|
|
|
|
$podcasts = $podcastModel->findAll();
|
2022-03-15 16:47:35 +00:00
|
|
|
|
|
|
|
if ($podcasts === []) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-21 16:17:11 +00:00
|
|
|
/** @var CURLRequest $request */
|
|
|
|
$request = service('curlrequest');
|
2022-03-15 16:47:35 +00:00
|
|
|
|
|
|
|
$requestOptions = [
|
|
|
|
'headers' => [
|
2023-06-12 14:47:38 +00:00
|
|
|
'User-Agent' => 'Castopod/' . CP_VERSION . '; +' . base_url('', 'https'),
|
2022-03-15 16:47:35 +00:00
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2024-04-28 16:39:01 +00:00
|
|
|
$hubUrls = config('WebSub')
|
2022-03-15 16:47:35 +00:00
|
|
|
->hubs;
|
|
|
|
|
|
|
|
foreach ($podcasts as $podcast) {
|
|
|
|
$requestOptions['form_params'] = [
|
|
|
|
'hub.mode' => 'publish',
|
2023-06-12 14:47:38 +00:00
|
|
|
'hub.url' => $podcast->feed_url,
|
2022-03-15 16:47:35 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($hubUrls as $hub) {
|
|
|
|
try {
|
|
|
|
$request->post($hub, $requestOptions);
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
log_message(
|
2023-08-06 12:14:05 +00:00
|
|
|
'warning',
|
2022-03-15 16:47:35 +00:00
|
|
|
"COULD NOT PUBLISH @{$podcast->handle} ON {$hub}" . PHP_EOL . $exception->getMessage()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set podcast feed as having been pushed onto hubs
|
|
|
|
(new PodcastModel())->update($podcast->id, [
|
2024-04-28 16:39:01 +00:00
|
|
|
'is_published_on_hubs' => 1,
|
2022-03-15 16:47:35 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// set newly published episodes as pushed onto hubs
|
|
|
|
(new EpisodeModel())->set('is_published_on_hubs', true)
|
|
|
|
->where([
|
2023-06-12 14:47:38 +00:00
|
|
|
'podcast_id' => $podcast->id,
|
2022-03-15 16:47:35 +00:00
|
|
|
'is_published_on_hubs' => false,
|
|
|
|
])
|
2022-04-14 14:33:53 +00:00
|
|
|
->where('`published_at` <= UTC_TIMESTAMP()', null, false)
|
2022-03-15 16:47:35 +00:00
|
|
|
->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|