2023-08-24 12:48:10 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\RePod\Core\EpisodeAction;
|
|
|
|
|
2023-08-29 08:48:54 +02:00
|
|
|
class EpisodeActionReader
|
2023-08-24 12:48:10 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* https://github.com/pbek/nextcloud-nextpod/blob/main/lib/Core/EpisodeAction/EpisodeActionExtraData.php#L119.
|
|
|
|
*
|
|
|
|
* @throws \Exception if the XML data could not be parsed
|
|
|
|
*/
|
2023-08-24 17:43:10 +02:00
|
|
|
public function parseRssXml(string $xmlString, ?int $fetchedAtUnix = null): array
|
2023-08-24 12:48:10 +02:00
|
|
|
{
|
|
|
|
$episodes = [];
|
|
|
|
$xml = new \SimpleXMLElement($xmlString);
|
|
|
|
$channel = $xml->channel;
|
|
|
|
$episodeName = null;
|
|
|
|
$episodeLink = null;
|
|
|
|
$episodeImage = null;
|
|
|
|
$episodeDescription = null;
|
2023-08-28 17:44:17 +02:00
|
|
|
$episodeGuid = null;
|
2023-08-24 19:03:11 +02:00
|
|
|
$episodeFilesize = null;
|
|
|
|
$episodeDuration = null;
|
|
|
|
$episodePubDate = null;
|
2023-08-24 12:48:10 +02:00
|
|
|
|
|
|
|
// Find episode by url and add data for it
|
|
|
|
/** @var \SimpleXMLElement $item */
|
|
|
|
foreach ($channel->item as $item) {
|
2023-08-24 17:43:10 +02:00
|
|
|
$episodeUrl = (string) $item->enclosure['url'];
|
2023-08-24 12:48:10 +02:00
|
|
|
|
2023-08-28 17:44:17 +02:00
|
|
|
// Get episode guid
|
|
|
|
$episodeGuid = (string) $item->guid;
|
|
|
|
|
2023-08-24 19:03:11 +02:00
|
|
|
// Get episode filesize
|
|
|
|
$episodeFilesize = (int) $item->enclosure['length'];
|
|
|
|
|
2023-08-24 12:48:10 +02:00
|
|
|
// Get episode name
|
|
|
|
$episodeName = $this->stringOrNull($item->title);
|
|
|
|
|
|
|
|
// Get episode link
|
|
|
|
$episodeLink = $this->stringOrNull($item->link);
|
|
|
|
|
|
|
|
// Get episode image
|
2023-08-24 19:03:11 +02:00
|
|
|
$episodeChildren = $item->children('http://www.itunes.com/dtds/podcast-1.0.dtd');
|
|
|
|
if ($episodeChildren) {
|
|
|
|
$episodeImageAttributes = (array) $episodeChildren->image->attributes();
|
2023-08-24 12:48:10 +02:00
|
|
|
$episodeImage = $this->stringOrNull(array_key_exists('href', $episodeImageAttributes) ? (string) $episodeImageAttributes['href'] : '');
|
|
|
|
$iTunesChildren = $item->children('itunes', true);
|
|
|
|
|
2023-08-24 19:03:11 +02:00
|
|
|
// Get episode duration
|
|
|
|
if ($iTunesChildren) {
|
|
|
|
$rawDuration = $this->stringOrNull((string) $iTunesChildren->duration);
|
|
|
|
$splitDuration = array_reverse(explode(':', $rawDuration ?? ''));
|
|
|
|
$episodeDuration = (int) $splitDuration[0];
|
|
|
|
$episodeDuration += !empty($splitDuration[1]) ? (int) $splitDuration[1] * 60 : 0;
|
|
|
|
$episodeDuration += !empty($splitDuration[2]) ? (int) $splitDuration[2] * 60 : 0;
|
|
|
|
}
|
|
|
|
|
2023-08-24 12:48:10 +02:00
|
|
|
if ($iTunesChildren && !$episodeImage) {
|
|
|
|
$episodeImage = $this->stringOrNull((string) $iTunesChildren->image['href']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$episodeImage) {
|
|
|
|
$episodeImage = $this->stringOrNull($channel->image->url);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($iTunesChildren && !$episodeImage) {
|
|
|
|
$episodeImage = $this->stringOrNull((string) $iTunesChildren->image['href']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$episodeImage) {
|
2023-08-24 19:03:11 +02:00
|
|
|
$channelChildren = $channel->children('http://www.itunes.com/dtds/podcast-1.0.dtd');
|
|
|
|
if ($channelChildren) {
|
|
|
|
$episodeImageAttributes = (array) $channelChildren->image->attributes();
|
2023-08-24 12:48:10 +02:00
|
|
|
$episodeImage = $this->stringOrNull(array_key_exists('href', $episodeImageAttributes) ? (string) $episodeImageAttributes['href'] : '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$episodeImage) {
|
|
|
|
preg_match('/<itunes:image\s+href="([^"]+)"/', $xmlString, $matches);
|
|
|
|
$episodeImage = $this->stringOrNull($matches[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get episode description
|
|
|
|
$episodeContentChildren = $item->children('content', true);
|
|
|
|
|
|
|
|
if ($episodeContentChildren) {
|
|
|
|
$episodeDescription = $this->stringOrNull($episodeContentChildren->encoded);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$episodeDescription) {
|
|
|
|
$episodeDescription = $this->stringOrNull($item->description);
|
|
|
|
}
|
|
|
|
|
2023-08-24 20:53:54 +02:00
|
|
|
// Remove tags
|
|
|
|
$episodeDescription = strip_tags($episodeDescription ?? '');
|
2023-08-24 12:48:10 +02:00
|
|
|
|
2023-08-24 19:03:11 +02:00
|
|
|
// Get episode pubDate
|
|
|
|
$rawPubDate = $this->stringOrNull($item->pubDate);
|
|
|
|
$episodePubDate = $rawPubDate ? new \DateTime($rawPubDate) : null;
|
|
|
|
|
2023-08-24 12:48:10 +02:00
|
|
|
$episodes[] = new EpisodeActionExtraData(
|
|
|
|
$episodeUrl,
|
|
|
|
$this->stringOrNull($channel->title),
|
|
|
|
$episodeName,
|
|
|
|
$episodeLink,
|
|
|
|
$episodeImage,
|
|
|
|
$episodeDescription,
|
|
|
|
$fetchedAtUnix ?? (new \DateTime())->getTimestamp(),
|
2023-08-28 17:44:17 +02:00
|
|
|
$episodeGuid,
|
2023-08-24 19:03:11 +02:00
|
|
|
$episodePubDate,
|
|
|
|
$episodeFilesize,
|
2023-08-29 08:48:54 +02:00
|
|
|
$episodeDuration
|
2023-08-24 12:48:10 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $episodes;
|
|
|
|
}
|
|
|
|
|
2023-08-24 18:22:40 +02:00
|
|
|
/**
|
|
|
|
* @param null|\SimpleXMLElement|string $value
|
|
|
|
*/
|
|
|
|
private function stringOrNull($value): ?string
|
2023-08-24 12:48:10 +02:00
|
|
|
{
|
|
|
|
if ($value) {
|
|
|
|
return (string) $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|