refacto: rework parsing image, duration and description
This commit is contained in:
parent
0c2154a2ab
commit
f89440005e
@ -39,7 +39,9 @@ class EpisodeActionReader
|
|||||||
$type = (string) $item->enclosure['type'];
|
$type = (string) $item->enclosure['type'];
|
||||||
$size = (int) $item->enclosure['length'];
|
$size = (int) $item->enclosure['length'];
|
||||||
$guid = (string) $item->guid;
|
$guid = (string) $item->guid;
|
||||||
$rawDuration = $this->stringOrNull($item->duration);
|
|
||||||
|
$iTunesItemChildren = $item->children('itunes', true);
|
||||||
|
$iTunesChannelChildren = $channel->children('itunes', true);
|
||||||
|
|
||||||
// Get episode action
|
// Get episode action
|
||||||
$action = $this->episodeActionRepository->findByGuid($guid, $this->userService->getUserUID());
|
$action = $this->episodeActionRepository->findByGuid($guid, $this->userService->getUserUID());
|
||||||
@ -57,40 +59,37 @@ class EpisodeActionReader
|
|||||||
$link = $this->stringOrNull($item->link);
|
$link = $this->stringOrNull($item->link);
|
||||||
|
|
||||||
// Get episode image
|
// 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');
|
$itemChildren = $item->children('http://www.itunes.com/dtds/podcast-1.0.dtd');
|
||||||
if ($itemChildren) {
|
if (!$image && $itemChildren) {
|
||||||
$imageAttributes = (array) $itemChildren->image->attributes();
|
$imageAttributes = (array) $itemChildren->image->attributes();
|
||||||
$image = $this->stringOrNull(array_key_exists('href', $imageAttributes) ? (string) $imageAttributes['href'] : '');
|
$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 (!$image && $iTunesItemChildren) {
|
||||||
if ($iTunesItemChildren) {
|
$image = $this->stringOrNull($iTunesItemChildren->image['href']);
|
||||||
$rawDuration = $this->stringOrNull($rawDuration ?? $iTunesItemChildren->duration);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ($iTunesItemChildren && !$image) {
|
if (!$image) {
|
||||||
$image = $this->stringOrNull($iTunesItemChildren->image['href']);
|
$image = $this->stringOrNull($channel->image->url);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($iTunesChannelChildren && !$image) {
|
if (!$image && $iTunesChannelChildren) {
|
||||||
$image = $this->stringOrNull($iTunesChannelChildren->image['href']);
|
$image = $this->stringOrNull($iTunesChannelChildren->image['href']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$image) {
|
if (!$image) {
|
||||||
$channelChildren = $channel->children('http://www.itunes.com/dtds/podcast-1.0.dtd');
|
$channelChildren = $channel->children('http://www.itunes.com/dtds/podcast-1.0.dtd');
|
||||||
if ($channelChildren) {
|
if ($channelChildren) {
|
||||||
$imageAttributes = (array) $channelChildren->image->attributes();
|
$imageAttributes = (array) $channelChildren->image->attributes();
|
||||||
$image = $this->stringOrNull(array_key_exists('href', $imageAttributes) ? (string) $imageAttributes['href'] : '');
|
$image = $this->stringOrNull(array_key_exists('href', $imageAttributes) ? (string) $imageAttributes['href'] : '');
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!$image) {
|
if (!$image) {
|
||||||
preg_match('/<itunes:image\s+href="([^"]+)"/', $xmlString, $matches);
|
preg_match('/<itunes:image\s+href="([^"]+)"/', $xmlString, $matches);
|
||||||
$image = $this->stringOrNull($matches[1]);
|
$image = $this->stringOrNull($matches[1]);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get episode description
|
// Get episode description
|
||||||
@ -101,19 +100,29 @@ class EpisodeActionReader
|
|||||||
$description = $this->stringOrNull($item->description);
|
$description = $this->stringOrNull($item->description);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove tags
|
if (!$description && $iTunesItemChildren) {
|
||||||
$description = strip_tags($description ?? '');
|
$description = $this->stringOrNull($iTunesItemChildren->summary);
|
||||||
|
}
|
||||||
|
|
||||||
// Get episode pubDate
|
// Remove tags
|
||||||
$rawPubDate = $this->stringOrNull($item->pubDate);
|
$description = strip_tags(str_replace(['<br>', '<br/>', '<br />'], "\n", $description ?? ''));
|
||||||
$pubDate = $rawPubDate ? new \DateTime($rawPubDate) : null;
|
|
||||||
|
|
||||||
// Get episode duration
|
// Get episode duration
|
||||||
|
if ($iTunesItemChildren) {
|
||||||
|
$rawDuration = $this->stringOrNull($iTunesItemChildren->duration);
|
||||||
|
} else {
|
||||||
|
$rawDuration = $this->stringOrNull($item->duration);
|
||||||
|
}
|
||||||
|
|
||||||
$splitDuration = array_reverse(explode(':', $rawDuration ?? ''));
|
$splitDuration = array_reverse(explode(':', $rawDuration ?? ''));
|
||||||
$duration = (int) $splitDuration[0];
|
$duration = (int) $splitDuration[0];
|
||||||
$duration += !empty($splitDuration[1]) ? (int) $splitDuration[1] * 60 : 0;
|
$duration += !empty($splitDuration[1]) ? (int) $splitDuration[1] * 60 : 0;
|
||||||
$duration += !empty($splitDuration[2]) ? (int) $splitDuration[2] * 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(
|
$episodes[] = new EpisodeActionExtraData(
|
||||||
$podcast,
|
$podcast,
|
||||||
$url,
|
$url,
|
||||||
|
Loading…
Reference in New Issue
Block a user