2014-07-24 15:51:42 +04:00
|
|
|
<?php
|
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';
|
2015-11-05 18:50:18 +03:00
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
const PARAMETERS = array(array(
|
2017-02-11 18:16:56 +03:00
|
|
|
'u' => array(
|
|
|
|
'name' => 'username',
|
|
|
|
'required' => true
|
2020-07-05 20:49:46 +03:00
|
|
|
),
|
|
|
|
't' => array(
|
2021-09-20 15:53:41 +03:00
|
|
|
'name' => 'Content',
|
2020-07-05 20:49:46 +03:00
|
|
|
'type' => 'list',
|
|
|
|
'defaultValue' => 'tracks',
|
|
|
|
'values' => array(
|
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'
|
2020-07-05 20:49:46 +03:00
|
|
|
)
|
2017-02-11 18:16:56 +03:00
|
|
|
)
|
|
|
|
));
|
2015-11-05 18:50:18 +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/';
|
|
|
|
|
2020-09-25 09:43:12 +03:00
|
|
|
private $feedTitle = null;
|
2019-06-01 16:04:43 +03:00
|
|
|
private $feedIcon = null;
|
2019-12-01 14:42:53 +03:00
|
|
|
private $clientIDCache = null;
|
2019-06-01 16:04:43 +03:00
|
|
|
|
2021-02-09 15:33:14 +03:00
|
|
|
private $clientIdRegex = '/client_id.*?"(.+?)"/';
|
|
|
|
private $widgetRegex = '/widget-.+?\.js/';
|
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
public function collectData() {
|
2022-01-02 12:36:09 +03:00
|
|
|
$res = $this->getUser($this->getInput('u'));
|
2017-02-11 18:16:56 +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;
|
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
$apiItems = $this->getUserItems($res->id, $this->getInput('t'))
|
|
|
|
or returnServerError('No results for ' . $this->getInput('t'));
|
|
|
|
|
|
|
|
$hasTrackObject = array('all', 'reposts', 'likes');
|
|
|
|
|
|
|
|
foreach ($apiItems->collection as $index => $apiItem) {
|
|
|
|
if (in_array($this->getInput('t'), $hasTrackObject) === true) {
|
|
|
|
$apiItem = $apiItem->track;
|
|
|
|
}
|
2017-02-11 18:16:56 +03:00
|
|
|
|
|
|
|
$item = array();
|
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);
|
|
|
|
|
|
|
|
$description = nl2br($apiItem->description);
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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;
|
2015-09-09 01:13:56 +03:00
|
|
|
|
2020-09-25 09:43:12 +03:00
|
|
|
if (count($this->items) >= 10) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
2018-12-26 23:58:07 +03:00
|
|
|
|
2019-06-01 16:04:43 +03:00
|
|
|
public function getIcon(){
|
|
|
|
if ($this->feedIcon) {
|
|
|
|
return $this->feedIcon;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getIcon();
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
public function getURI() {
|
|
|
|
if ($this->getInput('u')) {
|
|
|
|
return self::URI . $this->getInput('u') . '/' . $this->getInput('t');
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getURI();
|
2020-02-07 18:16:55 +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
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getName();
|
2014-07-24 15:51:42 +04:00
|
|
|
}
|
2019-12-01 14:42:53 +03:00
|
|
|
|
|
|
|
private function initClientIDCache(){
|
|
|
|
if($this->clientIDCache !== null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
$cacheFac = new CacheFactory();
|
|
|
|
$cacheFac->setWorkingDir(PATH_LIB_CACHES);
|
|
|
|
$this->clientIDCache = $cacheFac->create(Configuration::getConfig('cache', 'type'));
|
|
|
|
$this->clientIDCache->setScope(get_called_class());
|
|
|
|
$this->clientIDCache->setKey(array('client_id'));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getClientID(){
|
|
|
|
$this->initClientIDCache();
|
|
|
|
|
|
|
|
$clientID = $this->clientIDCache->loadData();
|
|
|
|
|
|
|
|
if($clientID == null) {
|
|
|
|
return $this->refreshClientID();
|
|
|
|
} else {
|
|
|
|
return $clientID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function refreshClientID(){
|
|
|
|
$this->initClientIDCache();
|
|
|
|
|
2022-01-02 12:36:09 +03:00
|
|
|
$playerHTML = getContents($this->playerUrl);
|
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.');
|
|
|
|
|
2021-02-09 15:33:14 +03:00
|
|
|
$clientID = '';
|
|
|
|
|
|
|
|
// 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;
|
2021-02-09 15:33:14 +03:00
|
|
|
|
2022-01-02 12:36:09 +03:00
|
|
|
$widgetJS = getContents($widgetURL);
|
2021-02-09 15:33:14 +03:00
|
|
|
|
|
|
|
if(preg_match($this->clientIdRegex, $widgetJS, $matches)) {
|
|
|
|
$clientID = $matches[1];
|
|
|
|
$this->clientIDCache->saveData($clientID);
|
2019-12-01 14:42:53 +03:00
|
|
|
|
2021-02-09 15:33:14 +03:00
|
|
|
return $clientID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($clientID)) {
|
|
|
|
returnServerError('Unable to find client ID.');
|
|
|
|
}
|
2019-12-01 14:42:53 +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);
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:53:41 +03:00
|
|
|
private function getUser($username) {
|
|
|
|
$parameters = array('url' => self::URI . $username);
|
|
|
|
|
|
|
|
return $this->getApi('resolve', $parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getUserItems($userId, $type) {
|
|
|
|
$parameters = array('limit' => 10);
|
|
|
|
$endpoint = 'users/' . $userId . '/' . $type;
|
|
|
|
|
|
|
|
if ($type === 'playlists') {
|
|
|
|
$endpoint = 'users/' . $userId . '/playlists_without_albums';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type === 'all') {
|
|
|
|
$endpoint = 'stream/users/' . $userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type === 'reposts') {
|
|
|
|
$endpoint = 'stream/users/' . $userId . '/' . $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getApi($endpoint, $parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
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);
|
|
|
|
|
|
|
|
return json_decode(getContents($url));
|
2019-12-01 14:42:53 +03:00
|
|
|
}
|
|
|
|
}
|
2021-09-20 15:53:41 +03:00
|
|
|
|
|
|
|
private function getTrackList($tracks) {
|
|
|
|
$trackids = '';
|
|
|
|
|
|
|
|
foreach ($tracks as $track) {
|
|
|
|
$trackids .= $track->id . ',';
|
|
|
|
}
|
|
|
|
|
|
|
|
$apiItems = $this->getApi(
|
|
|
|
'tracks', array('ids' => $trackids)
|
|
|
|
);
|
|
|
|
|
|
|
|
$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
|
|
|
}
|