From 11026af80e07caaca0c52a01c1efde0600d340b6 Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Thu, 24 Aug 2023 19:03:11 +0200 Subject: [PATCH] Add missing informations --- .../EpisodeAction/EpisodeActionExtraData.php | 27 +++++++++++++++ .../EpisodeAction/EpisodeActionReader.php | 34 +++++++++++++++---- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/lib/Core/EpisodeAction/EpisodeActionExtraData.php b/lib/Core/EpisodeAction/EpisodeActionExtraData.php index aef1543..08ae327 100644 --- a/lib/Core/EpisodeAction/EpisodeActionExtraData.php +++ b/lib/Core/EpisodeAction/EpisodeActionExtraData.php @@ -19,6 +19,9 @@ use OCA\GPodderSync\Core\EpisodeAction\EpisodeAction; * episodeImage: ?string, * episodeDescription: ?string, * fetchedAtUnix: int, + * pubDate: ?\DateTime, + * filesize: ?int, + * duration: ?int, * episodeAction: ?EpisodeActionType * } */ @@ -32,6 +35,9 @@ class EpisodeActionExtraData implements \JsonSerializable private ?string $episodeImage, private ?string $episodeDescription, private int $fetchedAtUnix, + private ?\DateTime $pubDate, + private ?int $filesize, + private ?int $duration, private ?EpisodeAction $episodeAction ) { $this->episodeUrl = $episodeUrl; @@ -41,6 +47,9 @@ class EpisodeActionExtraData implements \JsonSerializable $this->episodeImage = $episodeImage; $this->episodeDescription = $episodeDescription; $this->fetchedAtUnix = $fetchedAtUnix; + $this->pubDate = $pubDate; + $this->filesize = $filesize; + $this->duration = $duration; $this->episodeAction = $episodeAction; } @@ -49,6 +58,21 @@ class EpisodeActionExtraData implements \JsonSerializable return $this->episodeUrl ?? '/no episodeUrl/'; } + public function getPubDate(): ?\DateTime + { + return $this->pubDate; + } + + public function getFilesize(): ?int + { + return $this->filesize; + } + + public function getDuration(): ?int + { + return $this->duration; + } + public function getEpisodeAction(): ?EpisodeAction { return $this->episodeAction; @@ -73,6 +97,9 @@ class EpisodeActionExtraData implements \JsonSerializable 'episodeImage' => $this->episodeImage, 'episodeDescription' => $this->episodeDescription, 'fetchedAtUnix' => $this->fetchedAtUnix, + 'pubDate' => $this->pubDate, + 'filesize' => $this->filesize, + 'duration' => $this->duration, 'episodeAction' => $this->episodeAction ? $this->episodeAction->toArray() : null, ]; } diff --git a/lib/Core/EpisodeAction/EpisodeActionReader.php b/lib/Core/EpisodeAction/EpisodeActionReader.php index 08c31ab..5dece7c 100644 --- a/lib/Core/EpisodeAction/EpisodeActionReader.php +++ b/lib/Core/EpisodeAction/EpisodeActionReader.php @@ -30,12 +30,18 @@ class EpisodeActionReader extends GPodderSyncEpisodeActionReader $episodeLink = null; $episodeImage = null; $episodeDescription = null; + $episodeFilesize = null; + $episodeDuration = null; + $episodePubDate = null; // Find episode by url and add data for it /** @var \SimpleXMLElement $item */ foreach ($channel->item as $item) { $episodeUrl = (string) $item->enclosure['url']; + // Get episode filesize + $episodeFilesize = (int) $item->enclosure['length']; + // Get episode action $episodeAction = $this->episodeActionRepository->findByEpisodeUrl($episodeUrl, $this->userService->getUserUID()); @@ -46,12 +52,21 @@ class EpisodeActionReader extends GPodderSyncEpisodeActionReader $episodeLink = $this->stringOrNull($item->link); // Get episode image - $episodeImageChildren = $item->children('http://www.itunes.com/dtds/podcast-1.0.dtd'); - if ($episodeImageChildren) { - $episodeImageAttributes = (array) $episodeImageChildren->image->attributes(); + $episodeChildren = $item->children('http://www.itunes.com/dtds/podcast-1.0.dtd'); + if ($episodeChildren) { + $episodeImageAttributes = (array) $episodeChildren->image->attributes(); $episodeImage = $this->stringOrNull(array_key_exists('href', $episodeImageAttributes) ? (string) $episodeImageAttributes['href'] : ''); $iTunesChildren = $item->children('itunes', true); + // 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; + } + if ($iTunesChildren && !$episodeImage) { $episodeImage = $this->stringOrNull((string) $iTunesChildren->image['href']); } @@ -65,9 +80,9 @@ class EpisodeActionReader extends GPodderSyncEpisodeActionReader } if (!$episodeImage) { - $channelImageChildren = $channel->children('http://www.itunes.com/dtds/podcast-1.0.dtd'); - if ($channelImageChildren) { - $episodeImageAttributes = (array) $channelImageChildren->image->attributes(); + $channelChildren = $channel->children('http://www.itunes.com/dtds/podcast-1.0.dtd'); + if ($channelChildren) { + $episodeImageAttributes = (array) $channelChildren->image->attributes(); $episodeImage = $this->stringOrNull(array_key_exists('href', $episodeImageAttributes) ? (string) $episodeImageAttributes['href'] : ''); } } @@ -92,6 +107,10 @@ class EpisodeActionReader extends GPodderSyncEpisodeActionReader // Open links in new browser window/tab $episodeDescription = str_replace('stringOrNull($item->pubDate); + $episodePubDate = $rawPubDate ? new \DateTime($rawPubDate) : null; + $episodes[] = new EpisodeActionExtraData( $episodeUrl, $this->stringOrNull($channel->title), @@ -100,6 +119,9 @@ class EpisodeActionReader extends GPodderSyncEpisodeActionReader $episodeImage, $episodeDescription, $fetchedAtUnix ?? (new \DateTime())->getTimestamp(), + $episodePubDate, + $episodeFilesize, + $episodeDuration, $episodeAction ); }