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,25 +59,23 @@ 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 ($iTunesItemChildren && !$image) {
if (!$image && $iTunesItemChildren) {
$image = $this->stringOrNull($iTunesItemChildren->image['href']);
}
if ($iTunesChannelChildren && !$image) {
if (!$image) {
$image = $this->stringOrNull($channel->image->url);
}
if (!$image && $iTunesChannelChildren) {
$image = $this->stringOrNull($iTunesChannelChildren->image['href']);
}
@ -91,7 +91,6 @@ class EpisodeActionReader
preg_match('/<itunes:image\s+href="([^"]+)"/', $xmlString, $matches);
$image = $this->stringOrNull($matches[1]);
}
}
// Get episode description
$itemContent = $item->children('content', true);
@ -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,