From f89440005ee300f1009bf841648cf65f36787523 Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Thu, 11 Jan 2024 23:00:14 +0100 Subject: [PATCH] refacto: rework parsing image, duration and description --- .../EpisodeAction/EpisodeActionReader.php | 69 +++++++++++-------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/lib/Core/EpisodeAction/EpisodeActionReader.php b/lib/Core/EpisodeAction/EpisodeActionReader.php index 81a6136..c0450cd 100644 --- a/lib/Core/EpisodeAction/EpisodeActionReader.php +++ b/lib/Core/EpisodeAction/EpisodeActionReader.php @@ -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('/stringOrNull($matches[1]); - } + if (!$image) { + preg_match('/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(['
', '
', '
'], "\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,