repod/lib/Core/EpisodeAction/EpisodeActionReader.php

157 lines
4.6 KiB
PHP
Raw Normal View History

2023-08-24 10:48:10 +00:00
<?php
declare(strict_types=1);
namespace OCA\RePod\Core\EpisodeAction;
2023-08-29 10:04:14 +00:00
use OCA\GPodderSync\Core\EpisodeAction\EpisodeAction;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionRepository;
use OCA\RePod\Service\UserService;
2023-08-29 06:48:54 +00:00
class EpisodeActionReader
2023-08-24 10:48:10 +00:00
{
2023-08-29 10:04:14 +00:00
public function __construct(
private EpisodeActionRepository $episodeActionRepository,
private UserService $userService
2023-12-23 16:25:20 +00:00
) {}
2023-08-29 10:04:14 +00:00
2023-12-23 16:25:20 +00:00
public function findByEpisodeUrl(string $episodeUrl): ?EpisodeAction {
2023-08-29 10:04:14 +00:00
return $this->episodeActionRepository->findByEpisodeUrl($episodeUrl, $this->userService->getUserUID());
}
2023-08-24 10:48:10 +00:00
/**
* Base: https://github.com/pbek/nextcloud-nextpod/blob/main/lib/Core/EpisodeAction/EpisodeActionExtraData.php#L119.
* Specs : https://github.com/Podcast-Standards-Project/PSP-1-Podcast-RSS-Specification/blob/main/README.md.
2023-08-24 10:48:10 +00:00
*
2023-12-24 16:05:35 +00:00
* @return EpisodeActionExtraData[]
2024-01-07 10:07:48 +00:00
* @throws \Exception if the XML data could not be parsed
2023-08-24 10:48:10 +00:00
*/
2023-12-23 16:25:20 +00:00
public function parseRssXml(string $xmlString, ?int $fetchedAtUnix = null): array {
2023-08-24 10:48:10 +00:00
$episodes = [];
$xml = new \SimpleXMLElement($xmlString);
$channel = $xml->channel;
$podcast = (string) $channel->title;
2023-08-24 10:48:10 +00:00
// Find episode by url and add data for it
/** @var \SimpleXMLElement $item */
foreach ($channel->item as $item) {
$url = (string) $item->enclosure['url'];
$type = (string) $item->enclosure['type'];
$size = (int) $item->enclosure['length'];
$guid = (string) $item->guid;
$iTunesItemChildren = $item->children('itunes', true);
$iTunesChannelChildren = $channel->children('itunes', true);
2023-08-24 17:03:11 +00:00
2023-08-29 10:04:14 +00:00
// Get episode action
$action = $this->episodeActionRepository->findByGuid($guid, $this->userService->getUserUID());
2023-12-23 20:51:48 +00:00
if ($action) {
$url = $action->getEpisode();
2023-12-23 20:51:48 +00:00
} else {
$action = $this->episodeActionRepository->findByEpisodeUrl($url, $this->userService->getUserUID());
2023-12-23 20:51:48 +00:00
}
2023-08-29 10:04:14 +00:00
2023-08-24 10:48:10 +00:00
// Get episode name
$name = (string) $item->title;
2023-08-24 10:48:10 +00:00
// Get episode link
$link = $this->stringOrNull($item->link);
2023-08-24 10:48:10 +00:00
// Get episode image
$image = $this->stringOrNull($item->image->url);
2023-08-29 10:04:14 +00:00
$itemChildren = $item->children('http://www.itunes.com/dtds/podcast-1.0.dtd');
if (!$image && $itemChildren) {
$imageAttributes = (array) $itemChildren->image->attributes();
$image = $this->stringOrNull(array_key_exists('href', $imageAttributes) ? (string) $imageAttributes['href'] : '');
}
2023-08-24 10:48:10 +00:00
if (!$image && $iTunesItemChildren) {
$image = $this->stringOrNull($iTunesItemChildren->image['href']);
}
2023-08-24 17:03:11 +00:00
if (!$image) {
$image = $this->stringOrNull($channel->image->url);
}
2023-08-24 10:48:10 +00:00
if (!$image && $iTunesChannelChildren) {
$image = $this->stringOrNull($iTunesChannelChildren->image['href']);
}
2023-08-24 10:48:10 +00:00
if (!$image) {
$channelChildren = $channel->children('http://www.itunes.com/dtds/podcast-1.0.dtd');
if ($channelChildren) {
$imageAttributes = (array) $channelChildren->image->attributes();
$image = $this->stringOrNull(array_key_exists('href', $imageAttributes) ? (string) $imageAttributes['href'] : '');
2023-08-24 10:48:10 +00:00
}
}
2023-08-24 10:48:10 +00:00
if (!$image) {
preg_match('/<itunes:image\s+href="([^"]+)"/', $xmlString, $matches);
$image = $this->stringOrNull($matches[1]);
2023-08-24 10:48:10 +00:00
}
// Get episode description
$itemContent = $item->children('content', true);
if ($itemContent) {
$description = $this->stringOrNull($itemContent->encoded);
} else {
$description = $this->stringOrNull($item->description);
2023-08-24 10:48:10 +00:00
}
if (!$description && $iTunesItemChildren) {
$description = $this->stringOrNull($iTunesItemChildren->summary);
}
2023-08-24 10:48:10 +00:00
// Remove tags
$description = strip_tags(str_replace(['<br>', '<br/>', '<br />'], "\n", $description ?? ''));
// Get episode duration
if ($iTunesItemChildren) {
$rawDuration = $this->stringOrNull($iTunesItemChildren->duration);
} else {
$rawDuration = $this->stringOrNull($item->duration);
}
$splitDuration = array_reverse(explode(':', $rawDuration ?? ''));
$duration = (int) $splitDuration[0];
$duration += !empty($splitDuration[1]) ? (int) $splitDuration[1] * 60 : 0;
$duration += !empty($splitDuration[2]) ? (int) $splitDuration[2] * 60 : 0;
2023-08-24 17:03:11 +00:00
// Get episode pubDate
$rawPubDate = $this->stringOrNull($item->pubDate);
$pubDate = $rawPubDate ? new \DateTime($rawPubDate) : null;
2023-08-24 10:48:10 +00:00
$episodes[] = new EpisodeActionExtraData(
$podcast,
$url,
$name,
$link,
$image,
$description,
2023-08-24 10:48:10 +00:00
$fetchedAtUnix ?? (new \DateTime())->getTimestamp(),
$guid,
$type,
$size,
$pubDate,
$duration,
$action
2023-08-24 10:48:10 +00:00
);
}
return $episodes;
}
2023-08-24 16:22:40 +00:00
/**
* @param null|\SimpleXMLElement|string $value
*/
2023-12-23 16:25:20 +00:00
private function stringOrNull($value): ?string {
2023-08-24 10:48:10 +00:00
if ($value) {
return (string) $value;
}
return null;
}
}