[AppleMusicBridge] Further data request to receive artist information. (#4271)

This commit is contained in:
Jonas Taedcke 2024-10-18 08:29:07 +02:00 committed by GitHub
parent 51cdb66f9c
commit f9a51b6768
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -18,9 +18,42 @@ class AppleMusicBridge extends BridgeAbstract
'required' => true, 'required' => true,
], ],
]]; ]];
const CACHE_TIMEOUT = 21600; // 6 hours const CACHE_TIMEOUT = 60 * 60 * 6; // 6 hours
private $title;
public function collectData() public function collectData()
{
$items = $this->getJson();
$artist = $this->getArtist($items);
$this->title = $artist->artistName;
foreach ($items as $item) {
if ($item->wrapperType === 'collection') {
$copyright = $item->copyright ?? '';
$artworkUrl500 = str_replace('/100x100', '/500x500', $item->artworkUrl100);
$artworkUrl2000 = str_replace('/100x100', '/2000x2000', $item->artworkUrl100);
$escapedCollectionName = htmlspecialchars($item->collectionName);
$this->items[] = [
'title' => $item->collectionName,
'uri' => $item->collectionViewUrl,
'timestamp' => $item->releaseDate,
'enclosures' => $artworkUrl500,
'author' => $item->artistName,
'content' => "<figure>
<img srcset=\"$item->artworkUrl60 60w, $item->artworkUrl100 100w, $artworkUrl500 500w, $artworkUrl2000 2000w\" sizes=\"100%\" src=\"$artworkUrl2000\" alt=\"Cover of $escapedCollectionName\" style=\"display: block; margin: 0 auto;\" />
<figcaption>
from <a href=\"$artist->artistLinkUrl\">$item->artistName</a><br />$copyright
</figcaption>
</figure>",
];
}
}
}
private function getJson()
{ {
# Limit the amount of releases to 50 # Limit the amount of releases to 50
if ($this->getInput('limit') > 50) { if ($this->getInput('limit') > 50) {
@ -29,31 +62,53 @@ class AppleMusicBridge extends BridgeAbstract
$limit = $this->getInput('limit'); $limit = $this->getInput('limit');
} }
$url = 'https://itunes.apple.com/lookup?id=' $url = 'https://itunes.apple.com/lookup?id=' . $this->getInput('artist') . '&entity=album&limit=' . $limit . '&sort=recent';
. $this->getInput('artist')
. '&entity=album&limit='
. $limit .
'&sort=recent';
$html = getSimpleHTMLDOM($url); $html = getSimpleHTMLDOM($url);
$json = json_decode($html); $json = json_decode($html);
$result = $json->results;
foreach ($json->results as $obj) { if (!is_array($result) || count($result) == 0) {
if ($obj->wrapperType === 'collection') { returnServerError('There is no artist with id "' . $this->getInput('artist') . '".');
$copyright = $obj->copyright ?? '';
$this->items[] = [
'title' => $obj->artistName . ' - ' . $obj->collectionName,
'uri' => $obj->collectionViewUrl,
'timestamp' => $obj->releaseDate,
'enclosures' => $obj->artworkUrl100,
'content' => '<a href=' . $obj->collectionViewUrl
. '><img src="' . $obj->artworkUrl100 . '" /></a><br><br>'
. $obj->artistName . ' - ' . $obj->collectionName
. '<br>'
. $copyright,
];
}
} }
return $result;
}
private function getArtist($json)
{
$nameArray = array_filter($json, function ($obj) {
return $obj->wrapperType == 'artist';
});
if (count($nameArray) === 1) {
return $nameArray[0];
}
return parent::getName();
}
public function getName()
{
if (isset($this->title)) {
return $this->title;
}
return parent::getName();
}
public function getIcon()
{
if (empty($this->getInput('artist'))) {
return parent::getIcon();
}
// it isn't necessary to set the correct artist name into the url
$url = 'https://music.apple.com/us/artist/jon-bellion/' . $this->getInput('artist');
$html = getSimpleHTMLDOMCached($url);
$image = $html->find('meta[property="og:image"]', 0)->content;
$imageUpdatedSize = preg_replace('/\/\d*x\d*cw/i', '/144x144-999', $image);
return $imageUpdatedSize;
} }
} }