2022-03-24 23:58:53 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
class SpotifyBridge extends BridgeAbstract
|
|
|
|
{
|
|
|
|
const NAME = 'Spotify';
|
|
|
|
const URI = 'https://spotify.com/';
|
2022-10-27 21:02:01 +03:00
|
|
|
const DESCRIPTION = 'Fetches the latest albums from one or more artists or the latest tracks from one or more playlists';
|
2022-03-24 23:58:53 +03:00
|
|
|
const MAINTAINER = 'Paroleen';
|
|
|
|
const CACHE_TIMEOUT = 3600;
|
|
|
|
const PARAMETERS = [ [
|
|
|
|
'clientid' => [
|
|
|
|
'name' => 'Client ID',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true
|
|
|
|
],
|
|
|
|
'clientsecret' => [
|
|
|
|
'name' => 'Client secret',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true
|
|
|
|
],
|
2022-10-27 21:02:01 +03:00
|
|
|
'country' => [
|
|
|
|
'name' => 'Country',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => false,
|
|
|
|
'exampleValue' => 'US',
|
|
|
|
'defaultValue' => 'US'
|
|
|
|
],
|
|
|
|
'limit' => [
|
|
|
|
'name' => 'Limit',
|
|
|
|
'type' => 'number',
|
|
|
|
'required' => false,
|
|
|
|
'exampleValue' => 10,
|
|
|
|
'defaultValue' => 10
|
|
|
|
],
|
2022-03-24 23:58:53 +03:00
|
|
|
'spotifyuri' => [
|
|
|
|
'name' => 'Spotify URIs',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true,
|
2022-10-27 21:02:01 +03:00
|
|
|
'exampleValue' => 'spotify:artist:4lianjyuR1tqf6oUX8kjrZ [,spotify:playlist:37i9dQZF1DXcBWIGoYBM5M]',
|
2022-03-24 23:58:53 +03:00
|
|
|
],
|
|
|
|
'albumtype' => [
|
|
|
|
'name' => 'Album type',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => false,
|
|
|
|
'exampleValue' => 'album,single,appears_on,compilation',
|
|
|
|
'defaultValue' => 'album,single'
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
2022-10-27 21:02:01 +03:00
|
|
|
] ];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
const TOKENURI = 'https://accounts.spotify.com/api/token';
|
|
|
|
const APIURI = 'https://api.spotify.com/v1/';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
private $uri = '';
|
|
|
|
private $name = '';
|
|
|
|
private $token = '';
|
2022-10-27 21:02:01 +03:00
|
|
|
private $uris = [];
|
|
|
|
private $entries = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
public function getURI()
|
|
|
|
{
|
|
|
|
if (empty($this->uri)) {
|
2022-10-27 21:02:01 +03:00
|
|
|
$this->getFirstEntry();
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
return $this->uri;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
if (empty($this->name)) {
|
2022-10-27 21:02:01 +03:00
|
|
|
$this->getFirstEntry();
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
return $this->name;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
public function getIcon()
|
|
|
|
{
|
|
|
|
return 'https://www.scdn.co/i/_global/favicon.png';
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-10-27 21:02:01 +03:00
|
|
|
private function getUriType($uri)
|
2022-03-24 23:58:53 +03:00
|
|
|
{
|
2022-10-27 21:02:01 +03:00
|
|
|
return explode(':', $uri)[1];
|
2022-03-24 23:58:53 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-10-27 21:02:01 +03:00
|
|
|
private function getId($uri)
|
2022-03-24 23:58:53 +03:00
|
|
|
{
|
2022-10-27 21:02:01 +03:00
|
|
|
return explode(':', $uri)[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getDate($entry)
|
|
|
|
{
|
|
|
|
if ($entry['type'] === 'album') {
|
|
|
|
$date = $entry['release_date'];
|
|
|
|
} else {
|
|
|
|
$date = $entry['added_at'];
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-10-27 21:02:01 +03:00
|
|
|
if (strlen($date) == 4) {
|
|
|
|
$date .= '-01-01';
|
|
|
|
} elseif (strlen($date) == 7) {
|
|
|
|
$date .= '-01';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen($date) > 10) {
|
|
|
|
return DateTime::createFromFormat('Y-m-d\TH:i:s\Z', $date)->getTimestamp();
|
|
|
|
}
|
|
|
|
|
|
|
|
return DateTime::createFromFormat('Y-m-d', $date)->getTimestamp();
|
2022-03-24 23:58:53 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
private function getAlbumType()
|
|
|
|
{
|
|
|
|
return $this->getInput('albumtype');
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
private function getCountry()
|
|
|
|
{
|
|
|
|
return $this->getInput('country');
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
private function getToken()
|
|
|
|
{
|
2022-07-06 13:14:04 +03:00
|
|
|
$cacheFactory = new CacheFactory();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-07-06 13:14:04 +03:00
|
|
|
$cache = $cacheFactory->create();
|
2022-08-02 16:03:54 +03:00
|
|
|
$cache->setScope('SpotifyBridge');
|
2022-03-24 23:58:53 +03:00
|
|
|
$cache->setKey(['token']);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
if ($cache->getTime()) {
|
|
|
|
$time = (new DateTime())->getTimestamp() - $cache->getTime();
|
|
|
|
Debug::log('Token time: ' . $time);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
if ($cache->getTime() == false || $time >= 3600) {
|
|
|
|
Debug::log('Fetching token from Spotify');
|
|
|
|
$this->fetchToken();
|
|
|
|
$cache->saveData($this->token);
|
|
|
|
} else {
|
|
|
|
Debug::log('Loading token from cache');
|
|
|
|
$this->token = $cache->loadData();
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
Debug::log('Token: ' . $this->token);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-10-27 21:02:01 +03:00
|
|
|
private function getFirstEntry()
|
2022-03-24 23:58:53 +03:00
|
|
|
{
|
|
|
|
if (!is_null($this->getInput('spotifyuri')) && strpos($this->getInput('spotifyuri'), ',') === false) {
|
2022-10-27 21:02:01 +03:00
|
|
|
$type = $this->getUriType($this->uris[0]) . 's';
|
|
|
|
$item = $this->fetchContent(self::APIURI . $type . '/'
|
|
|
|
. $this->getId($this->uris[0]));
|
|
|
|
$this->uri = $item['external_urls']['spotify'];
|
|
|
|
$this->name = $item['name'] . ' - Spotify';
|
2022-03-24 23:58:53 +03:00
|
|
|
} else {
|
|
|
|
$this->uri = parent::getURI();
|
|
|
|
$this->name = parent::getName();
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-10-27 21:02:01 +03:00
|
|
|
private function getAllUris()
|
2022-03-24 23:58:53 +03:00
|
|
|
{
|
2022-10-27 21:02:01 +03:00
|
|
|
Debug::log('Parsing all uris');
|
|
|
|
$this->uris = explode(',', $this->getInput('spotifyuri'));
|
2022-03-24 23:58:53 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-10-27 21:02:01 +03:00
|
|
|
private function getAllEntries()
|
2022-03-24 23:58:53 +03:00
|
|
|
{
|
2022-10-27 21:02:01 +03:00
|
|
|
$this->entries = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-10-27 21:02:01 +03:00
|
|
|
$this->getAllUris();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-10-27 21:02:01 +03:00
|
|
|
Debug::log('Fetching all entries');
|
|
|
|
foreach ($this->uris as $uri) {
|
|
|
|
$type = $this->getUriType($uri) . 's';
|
|
|
|
$entry_type = $type === 'artists' ? 'albums' : 'tracks';
|
2022-03-24 23:58:53 +03:00
|
|
|
$fetch = true;
|
|
|
|
$offset = 0;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-10-27 21:02:01 +03:00
|
|
|
$api_url = self::APIURI . $type . '/'
|
|
|
|
. $this->getId($uri)
|
|
|
|
. '/' . $entry_type
|
|
|
|
. '?limit=50&country='
|
|
|
|
. $this->getCountry();
|
|
|
|
|
|
|
|
if ($type === 'artists') {
|
|
|
|
$api_url = $api_url . '&include_groups=' . $this->getAlbumType();
|
|
|
|
}
|
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
while ($fetch) {
|
2022-10-27 21:02:01 +03:00
|
|
|
$partial = $this->fetchContent($api_url
|
2022-03-24 23:58:53 +03:00
|
|
|
. '&offset='
|
|
|
|
. $offset);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-10-27 21:02:01 +03:00
|
|
|
if (!empty($partial['items'])) {
|
|
|
|
$this->entries = array_merge(
|
|
|
|
$this->entries,
|
|
|
|
$partial['items']
|
2022-03-24 23:58:53 +03:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$fetch = false;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
$offset += 50;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
private function fetchToken()
|
|
|
|
{
|
|
|
|
$curl = curl_init();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
curl_setopt($curl, CURLOPT_URL, self::TOKENURI);
|
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($curl, CURLOPT_POST, 1);
|
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
|
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Basic '
|
|
|
|
. base64_encode($this->getInput('clientid')
|
|
|
|
. ':'
|
|
|
|
. $this->getInput('clientsecret'))]);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
$json = curl_exec($curl);
|
|
|
|
$json = json_decode($json)->access_token;
|
|
|
|
curl_close($curl);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
$this->token = $json;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
private function fetchContent($url)
|
|
|
|
{
|
|
|
|
$this->getToken();
|
|
|
|
$curl = curl_init();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer '
|
|
|
|
. $this->token]);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
Debug::log('Fetching content from ' . $url);
|
|
|
|
$json = curl_exec($curl);
|
|
|
|
$json = json_decode($json, true);
|
|
|
|
curl_close($curl);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
return $json;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-10-27 21:02:01 +03:00
|
|
|
private function sortEntries()
|
2022-03-24 23:58:53 +03:00
|
|
|
{
|
2022-10-27 21:02:01 +03:00
|
|
|
Debug::log('Sorting entries');
|
|
|
|
usort($this->entries, function ($entry1, $entry2) {
|
|
|
|
if ($this->getDate($entry1) < $this->getDate($entry2)) {
|
2022-03-24 23:58:53 +03:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return -1;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2022-03-24 23:58:53 +03:00
|
|
|
});
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-10-27 21:02:01 +03:00
|
|
|
private function getAlbumData($album)
|
|
|
|
{
|
|
|
|
$item = [];
|
|
|
|
$item['title'] = $album['name'];
|
|
|
|
$item['uri'] = $album['external_urls']['spotify'];
|
|
|
|
|
|
|
|
$item['timestamp'] = $this->getDate($album);
|
|
|
|
$item['author'] = $album['artists'][0]['name'];
|
|
|
|
$item['categories'] = [$album['album_type']];
|
|
|
|
|
|
|
|
$item['content'] = '<img style="width: 256px" src="'
|
|
|
|
. $album['images'][0]['url']
|
|
|
|
. '">';
|
|
|
|
|
|
|
|
if ($album['total_tracks'] > 1) {
|
|
|
|
$item['content'] .= '<p>Total tracks: '
|
|
|
|
. $album['total_tracks']
|
|
|
|
. '</p>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTrackData($track)
|
|
|
|
{
|
|
|
|
$item = [];
|
|
|
|
|
|
|
|
$item['title'] = $track['track']['name'];
|
|
|
|
$item['uri'] = $track['track']['external_urls']['spotify'];
|
|
|
|
|
|
|
|
$item['timestamp'] = $this->getDate($track);
|
|
|
|
$item['author'] = $track['track']['artists'][0]['name'];
|
|
|
|
$item['categories'] = ['track'];
|
|
|
|
|
|
|
|
$item['content'] = '<img style="width: 256px" src="'
|
|
|
|
. $track['track']['album']['images'][0]['url']
|
|
|
|
. '">';
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
public function collectData()
|
|
|
|
{
|
|
|
|
$offset = 0;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-10-27 21:02:01 +03:00
|
|
|
$this->getAllEntries();
|
|
|
|
$this->sortEntries();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
Debug::log('Building RSS feed');
|
2022-10-27 21:02:01 +03:00
|
|
|
foreach ($this->entries as $entry) {
|
|
|
|
if ($entry['type'] === 'album') {
|
|
|
|
$item = $this->getAlbumData($entry);
|
|
|
|
} else {
|
|
|
|
$item = $this->getTrackData($entry);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 23:58:53 +03:00
|
|
|
$this->items[] = $item;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-10-27 21:02:01 +03:00
|
|
|
if ($this->getInput('limit') > 0 && count($this->items) >= $this->getInput('limit')) {
|
2022-03-24 23:58:53 +03:00
|
|
|
break;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2022-03-24 23:58:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|