2014-07-24 15:51:42 +04:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
class SoundCloudBridge extends BridgeAbstract
|
|
|
|
{
|
2019-12-01 14:42:53 +03:00
|
|
|
const MAINTAINER = 'kranack, Roliga';
|
2017-02-11 18:16:56 +03:00
|
|
|
const NAME = 'Soundcloud Bridge';
|
|
|
|
const URI = 'https://soundcloud.com/';
|
2016-09-25 18:04:28 +03:00
|
|
|
const CACHE_TIMEOUT = 600; // 10min
|
2017-02-11 18:16:56 +03:00
|
|
|
const DESCRIPTION = 'Returns 10 newest music from user profile';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
const PARAMETERS = [[
|
2017-02-11 18:16:56 +03:00
|
|
|
'u' => [
|
|
|
|
'name' => 'username',
|
2022-03-24 13:59:34 +03:00
|
|
|
'exampleValue' => 'thekidlaroi',
|
2017-02-11 18:16:56 +03:00
|
|
|
'required' => true
|
2020-07-05 20:49:46 +03:00
|
|
|
],
|
|
|
|
't' => [
|
2021-09-20 15:53:41 +03:00
|
|
|
'name' => 'Content',
|
2020-07-05 20:49:46 +03:00
|
|
|
'type' => 'list',
|
|
|
|
'defaultValue' => 'tracks',
|
|
|
|
'values' => [
|
2021-09-20 15:53:41 +03:00
|
|
|
'All (except likes)' => 'all',
|
2020-07-05 20:49:46 +03:00
|
|
|
'Tracks' => 'tracks',
|
2021-09-20 15:53:41 +03:00
|
|
|
'Albums' => 'albums',
|
|
|
|
'Playlists' => 'playlists',
|
|
|
|
'Reposts' => 'reposts',
|
|
|
|
'Likes' => 'likes'
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
|
|
|
]
|
2017-02-11 18:16:56 +03:00
|
|
|
]];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
private $apiUrl = 'https://api-v2.soundcloud.com/';
|
|
|
|
// Without url=http, player URL returns a 404
|
|
|
|
private $playerUrl = 'https://w.soundcloud.com/player/?url=http';
|
|
|
|
private $widgetUrl = 'https://widget.sndcdn.com/';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-09-25 09:43:12 +03:00
|
|
|
private $feedTitle = null;
|
2019-06-01 16:04:43 +03:00
|
|
|
private $feedIcon = null;
|
2023-07-08 23:53:23 +03:00
|
|
|
private $cache = null;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-09 15:33:14 +03:00
|
|
|
private $clientIdRegex = '/client_id.*?"(.+?)"/';
|
|
|
|
private $widgetRegex = '/widget-.+?\.js/';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
public function collectData()
|
|
|
|
{
|
2023-07-08 23:53:23 +03:00
|
|
|
$this->cache = RssBridge::getCache();
|
|
|
|
$this->cache->setScope('SoundCloudBridge');
|
|
|
|
$this->cache->setKey(['client_id']);
|
|
|
|
|
2022-01-02 12:36:09 +03:00
|
|
|
$res = $this->getUser($this->getInput('u'));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-09-25 09:43:12 +03:00
|
|
|
$this->feedTitle = $res->username;
|
2019-06-01 16:04:43 +03:00
|
|
|
$this->feedIcon = $res->avatar_url;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
$apiItems = $this->getUserItems($res->id, $this->getInput('t'))
|
|
|
|
or returnServerError('No results for ' . $this->getInput('t'));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
$hasTrackObject = ['all', 'reposts', 'likes'];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
foreach ($apiItems->collection as $index => $apiItem) {
|
|
|
|
if (in_array($this->getInput('t'), $hasTrackObject) === true) {
|
2022-09-04 04:50:40 +03:00
|
|
|
$apiItem = $apiItem->playlist ?? $apiItem->track;
|
2021-09-20 15:53:41 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
$item = [];
|
2021-09-20 15:53:41 +03:00
|
|
|
$item['author'] = $apiItem->user->username;
|
|
|
|
$item['title'] = $apiItem->user->username . ' - ' . $apiItem->title;
|
|
|
|
$item['timestamp'] = strtotime($apiItem->created_at);
|
2023-07-09 11:08:30 +03:00
|
|
|
$description = nl2br($apiItem->description ?? '');
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
$item['content'] = <<<HTML
|
|
|
|
<p>{$description}</p>
|
|
|
|
HTML;
|
|
|
|
|
|
|
|
if (isset($apiItem->tracks) && $apiItem->track_count > 0) {
|
|
|
|
$list = $this->getTrackList($apiItem->tracks);
|
|
|
|
|
|
|
|
$item['content'] .= <<<HTML
|
|
|
|
<p><strong>Tracks ({$apiItem->track_count})</strong></p>
|
|
|
|
{$list}
|
|
|
|
HTML;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
$item['enclosures'][] = $apiItem->artwork_url;
|
|
|
|
$item['id'] = $apiItem->permalink_url;
|
|
|
|
$item['uri'] = $apiItem->permalink_url;
|
2017-02-11 18:16:56 +03:00
|
|
|
$this->items[] = $item;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-09-25 09:43:12 +03:00
|
|
|
if (count($this->items) >= 10) {
|
|
|
|
break;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2020-09-25 09:43:12 +03:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-06-01 16:04:43 +03:00
|
|
|
public function getIcon()
|
|
|
|
{
|
|
|
|
if ($this->feedIcon) {
|
|
|
|
return $this->feedIcon;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-06-01 16:04:43 +03:00
|
|
|
return parent::getIcon();
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
public function getURI()
|
|
|
|
{
|
|
|
|
if ($this->getInput('u')) {
|
|
|
|
return self::URI . $this->getInput('u') . '/' . $this->getInput('t');
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
return parent::getURI();
|
2020-02-07 18:16:55 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
public function getName()
|
|
|
|
{
|
2020-09-25 09:43:12 +03:00
|
|
|
if ($this->feedTitle) {
|
2021-09-20 15:53:41 +03:00
|
|
|
return $this->feedTitle . ' - ' . ucfirst($this->getInput('t')) . ' - ' . self::NAME;
|
2017-02-15 00:20:55 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2017-02-15 00:20:55 +03:00
|
|
|
return parent::getName();
|
2014-07-24 15:51:42 +04:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-12-01 14:42:53 +03:00
|
|
|
private function getClientID()
|
|
|
|
{
|
2023-07-08 23:53:23 +03:00
|
|
|
$this->cache->setScope('SoundCloudBridge');
|
|
|
|
$this->cache->setKey(['client_id']);
|
|
|
|
$clientID = $this->cache->loadData();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-12-01 14:42:53 +03:00
|
|
|
if ($clientID == null) {
|
|
|
|
return $this->refreshClientID();
|
|
|
|
} else {
|
|
|
|
return $clientID;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2019-12-01 14:42:53 +03:00
|
|
|
private function refreshClientID()
|
|
|
|
{
|
2022-01-02 12:36:09 +03:00
|
|
|
$playerHTML = getContents($this->playerUrl);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-09 15:33:14 +03:00
|
|
|
// Extract widget JS filenames from player page
|
|
|
|
if (preg_match_all($this->widgetRegex, $playerHTML, $matches) == false) {
|
2019-12-01 14:42:53 +03:00
|
|
|
returnServerError('Unable to find widget JS URL.');
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 15:33:14 +03:00
|
|
|
$clientID = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-09 15:33:14 +03:00
|
|
|
// Loop widget js files and extract client ID
|
|
|
|
foreach ($matches[0] as $widgetFile) {
|
2021-09-20 15:53:41 +03:00
|
|
|
$widgetURL = $this->widgetUrl . $widgetFile;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-01-02 12:36:09 +03:00
|
|
|
$widgetJS = getContents($widgetURL);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-09 15:33:14 +03:00
|
|
|
if (preg_match($this->clientIdRegex, $widgetJS, $matches)) {
|
|
|
|
$clientID = $matches[1];
|
2023-07-08 23:53:23 +03:00
|
|
|
$this->cache->setScope('SoundCloudBridge');
|
|
|
|
$this->cache->setKey(['client_id']);
|
|
|
|
$this->cache->saveData($clientID);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-09 15:33:14 +03:00
|
|
|
return $clientID;
|
|
|
|
}
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-02-09 15:33:14 +03:00
|
|
|
if (empty($clientID)) {
|
|
|
|
returnServerError('Unable to find client ID.');
|
|
|
|
}
|
2019-12-01 14:42:53 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
private function buildApiUrl($endpoint, $parameters)
|
|
|
|
{
|
|
|
|
return $this->apiUrl
|
2019-12-01 14:42:53 +03:00
|
|
|
. $endpoint
|
|
|
|
. '?'
|
|
|
|
. http_build_query($parameters);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
private function getUser($username)
|
|
|
|
{
|
|
|
|
$parameters = ['url' => self::URI . $username];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
return $this->getApi('resolve', $parameters);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
private function getUserItems($userId, $type)
|
|
|
|
{
|
|
|
|
$parameters = ['limit' => 10];
|
|
|
|
$endpoint = 'users/' . $userId . '/' . $type;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
if ($type === 'playlists') {
|
|
|
|
$endpoint = 'users/' . $userId . '/playlists_without_albums';
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
if ($type === 'all') {
|
|
|
|
$endpoint = 'stream/users/' . $userId;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
if ($type === 'reposts') {
|
|
|
|
$endpoint = 'stream/users/' . $userId . '/' . $type;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
return $this->getApi($endpoint, $parameters);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
private function getApi($endpoint, $parameters)
|
|
|
|
{
|
2019-12-01 14:42:53 +03:00
|
|
|
$parameters['client_id'] = $this->getClientID();
|
2021-09-20 15:53:41 +03:00
|
|
|
$url = $this->buildApiUrl($endpoint, $parameters);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-12-01 14:42:53 +03:00
|
|
|
try {
|
2021-09-20 15:53:41 +03:00
|
|
|
return json_decode(getContents($url));
|
2019-12-01 14:42:53 +03:00
|
|
|
} catch (Exception $e) {
|
|
|
|
// Retry once with refreshed client ID
|
|
|
|
$parameters['client_id'] = $this->refreshClientID();
|
2021-09-20 15:53:41 +03:00
|
|
|
$url = $this->buildApiUrl($endpoint, $parameters);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
return json_decode(getContents($url));
|
2019-12-01 14:42:53 +03:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
private function getTrackList($tracks)
|
|
|
|
{
|
|
|
|
$trackids = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
foreach ($tracks as $track) {
|
|
|
|
$trackids .= $track->id . ',';
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
$apiItems = $this->getApi(
|
|
|
|
'tracks',
|
|
|
|
['ids' => $trackids]
|
|
|
|
);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
$list = '';
|
|
|
|
foreach ($apiItems as $track) {
|
|
|
|
$list .= <<<HTML
|
|
|
|
<li>{$track->user->username} — <a href="{$track->permalink_url}">{$track->title}</a></li>
|
|
|
|
HTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
$html = <<<HTML
|
|
|
|
<ul>{$list}</ul>
|
|
|
|
HTML;
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
2014-07-24 15:51:42 +04:00
|
|
|
}
|