refacto: rework parsing image, duration and description
All checks were successful
repod / xml (push) Successful in 25s
repod / php (push) Successful in 59s
repod / nodejs (push) Successful in 2m31s
repod / release (push) Has been skipped

This commit is contained in:
Michel Roux 2024-01-11 23:00:14 +01:00
parent 0c2154a2ab
commit f89440005e

View File

@ -39,7 +39,9 @@ class EpisodeActionReader
$type = (string) $item->enclosure['type'];
$size = (int) $item->enclosure['length'];
$guid = (string) $item->guid;
$rawDuration = $this->stringOrNull($item->duration);
$iTunesItemChildren = $item->children('itunes', true);
$iTunesChannelChildren = $channel->children('itunes', true);
// Get episode action
$action = $this->episodeActionRepository->findByGuid($guid, $this->userService->getUserUID());
@ -57,40 +59,37 @@ class EpisodeActionReader
$link = $this->stringOrNull($item->link);
// Get episode image
$image = $this->stringOrNull($channel->image->url);
$image = $this->stringOrNull($item->image->url);
$itemChildren = $item->children('http://www.itunes.com/dtds/podcast-1.0.dtd');
if ($itemChildren) {
if (!$image && $itemChildren) {
$imageAttributes = (array) $itemChildren->image->attributes();
$image = $this->stringOrNull(array_key_exists('href', $imageAttributes) ? (string) $imageAttributes['href'] : '');
$iTunesItemChildren = $item->children('itunes', true);
$iTunesChannelChildren = $channel->children('itunes', true);
}
// Get episode duration
if ($iTunesItemChildren) {
$rawDuration = $this->stringOrNull($rawDuration ?? $iTunesItemChildren->duration);
}
if (!$image && $iTunesItemChildren) {
$image = $this->stringOrNull($iTunesItemChildren->image['href']);
}
if ($iTunesItemChildren && !$image) {
$image = $this->stringOrNull($iTunesItemChildren->image['href']);
}
if (!$image) {
$image = $this->stringOrNull($channel->image->url);
}
if ($iTunesChannelChildren && !$image) {
$image = $this->stringOrNull($iTunesChannelChildren->image['href']);
}
if (!$image && $iTunesChannelChildren) {
$image = $this->stringOrNull($iTunesChannelChildren->image['href']);
}
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'] : '');
}
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'] : '');
}
}
if (!$image) {
preg_match('/<itunes:image\s+href="([^"]+)"/', $xmlString, $matches);
$image = $this->stringOrNull($matches[1]);
}
if (!$image) {
preg_match('/<itunes:image\s+href="([^"]+)"/', $xmlString, $matches);
$image = $this->stringOrNull($matches[1]);
}
// Get episode description
@ -101,19 +100,29 @@ class EpisodeActionReader
$description = $this->stringOrNull($item->description);
}
// Remove tags
$description = strip_tags($description ?? '');
if (!$description && $iTunesItemChildren) {
$description = $this->stringOrNull($iTunesItemChildren->summary);
}
// Get episode pubDate
$rawPubDate = $this->stringOrNull($item->pubDate);
$pubDate = $rawPubDate ? new \DateTime($rawPubDate) : null;
// 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;
// Get episode pubDate
$rawPubDate = $this->stringOrNull($item->pubDate);
$pubDate = $rawPubDate ? new \DateTime($rawPubDate) : null;
$episodes[] = new EpisodeActionExtraData(
$podcast,
$url,