2014-07-24 13:51:42 +02:00
|
|
|
<?php
|
2017-02-11 16:16:56 +01:00
|
|
|
class SoundCloudBridge extends BridgeAbstract {
|
2015-09-09 00:13:56 +02:00
|
|
|
|
2019-12-01 12:42:53 +01:00
|
|
|
const MAINTAINER = 'kranack, Roliga';
|
2017-02-11 16:16:56 +01:00
|
|
|
const NAME = 'Soundcloud Bridge';
|
|
|
|
const URI = 'https://soundcloud.com/';
|
2016-09-25 17:04:28 +02:00
|
|
|
const CACHE_TIMEOUT = 600; // 10min
|
2017-02-11 16:16:56 +01:00
|
|
|
const DESCRIPTION = 'Returns 10 newest music from user profile';
|
2015-11-05 16:50:18 +01:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
const PARAMETERS = array( array(
|
|
|
|
'u' => array(
|
|
|
|
'name' => 'username',
|
|
|
|
'required' => true
|
2020-07-05 19:49:46 +02:00
|
|
|
),
|
|
|
|
't' => array(
|
|
|
|
'name' => 'type',
|
|
|
|
'type' => 'list',
|
|
|
|
'defaultValue' => 'tracks',
|
|
|
|
'values' => array(
|
|
|
|
'Tracks' => 'tracks',
|
|
|
|
'Playlists' => 'playlists'
|
|
|
|
)
|
2017-02-11 16:16:56 +01:00
|
|
|
)
|
|
|
|
));
|
2015-11-05 16:50:18 +01:00
|
|
|
|
2020-09-25 08:43:12 +02:00
|
|
|
private $feedTitle = null;
|
2019-06-01 15:04:43 +02:00
|
|
|
private $feedIcon = null;
|
2019-12-01 12:42:53 +01:00
|
|
|
private $clientIDCache = null;
|
2019-06-01 15:04:43 +02:00
|
|
|
|
2016-08-25 01:24:53 +02:00
|
|
|
public function collectData(){
|
2019-12-01 12:42:53 +01:00
|
|
|
$res = $this->apiGet('resolve', array(
|
2020-07-05 19:49:46 +02:00
|
|
|
'url' => 'https://soundcloud.com/' . $this->getInput('u')
|
2017-02-11 16:16:56 +01:00
|
|
|
)) or returnServerError('No results for this query');
|
|
|
|
|
2020-09-25 08:43:12 +02:00
|
|
|
$this->feedTitle = $res->username;
|
2019-06-01 15:04:43 +02:00
|
|
|
$this->feedIcon = $res->avatar_url;
|
|
|
|
|
2020-09-25 08:43:12 +02:00
|
|
|
$tracks = $this->apiGet(
|
|
|
|
'users/' . urlencode($res->id) . '/' . $this->getInput('t'),
|
|
|
|
array('limit' => 31)
|
|
|
|
) or returnServerError('No results for this user/playlist');
|
2017-02-11 16:16:56 +01:00
|
|
|
|
2020-09-25 08:43:12 +02:00
|
|
|
foreach ($tracks->collection as $index => $track) {
|
2017-02-11 16:16:56 +01:00
|
|
|
$item = array();
|
2020-09-25 08:43:12 +02:00
|
|
|
$item['author'] = $track->user->username;
|
|
|
|
$item['title'] = $track->user->username . ' - ' . $track->title;
|
|
|
|
$item['timestamp'] = strtotime($track->created_at);
|
|
|
|
$item['content'] = nl2br($track->description);
|
|
|
|
$item['enclosures'][] = $track->artwork_url;
|
2017-02-11 16:16:56 +01:00
|
|
|
|
|
|
|
$item['id'] = self::URI
|
|
|
|
. urlencode($this->getInput('u'))
|
|
|
|
. '/'
|
2020-09-25 08:43:12 +02:00
|
|
|
. urlencode($track->permalink);
|
2017-02-11 16:16:56 +01:00
|
|
|
$item['uri'] = self::URI
|
|
|
|
. urlencode($this->getInput('u'))
|
|
|
|
. '/'
|
2020-09-25 08:43:12 +02:00
|
|
|
. urlencode($track->permalink);
|
2017-02-11 16:16:56 +01:00
|
|
|
$this->items[] = $item;
|
2015-09-09 00:13:56 +02:00
|
|
|
|
2020-09-25 08:43:12 +02:00
|
|
|
if (count($this->items) >= 10) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-02-11 16:16:56 +01:00
|
|
|
}
|
2018-12-26 21:58:07 +01:00
|
|
|
|
2019-06-01 15:04:43 +02:00
|
|
|
public function getIcon(){
|
|
|
|
if ($this->feedIcon) {
|
|
|
|
return $this->feedIcon;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getIcon();
|
|
|
|
}
|
|
|
|
|
2020-02-07 16:16:55 +01:00
|
|
|
public function getURI(){
|
|
|
|
return 'https://soundcloud.com/' . $this->getInput('u');
|
|
|
|
}
|
|
|
|
|
2014-07-24 13:51:42 +02:00
|
|
|
public function getName(){
|
2020-09-25 08:43:12 +02:00
|
|
|
if($this->feedTitle) {
|
|
|
|
return $this->feedTitle . ' - ' . self::NAME;
|
2017-02-14 22:20:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getName();
|
2014-07-24 13:51:42 +02:00
|
|
|
}
|
2019-12-01 12:42:53 +01: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();
|
|
|
|
|
|
|
|
// Without url=http, this returns a 404
|
|
|
|
$playerHTML = getContents('https://w.soundcloud.com/player/?url=http')
|
2020-07-05 19:49:46 +02:00
|
|
|
or returnServerError('Unable to get player page.');
|
2019-12-01 12:42:53 +01:00
|
|
|
$regex = '/widget-.+?\.js/';
|
|
|
|
if(preg_match($regex, $playerHTML, $matches) == false)
|
|
|
|
returnServerError('Unable to find widget JS URL.');
|
|
|
|
$widgetURL = 'https://widget.sndcdn.com/' . $matches[0];
|
|
|
|
|
|
|
|
$widgetJS = getContents($widgetURL)
|
2020-07-05 19:49:46 +02:00
|
|
|
or returnServerError('Unable to get widget JS page.');
|
2019-12-01 12:42:53 +01:00
|
|
|
$regex = '/client_id.*?"(.+?)"/';
|
|
|
|
if(preg_match($regex, $widgetJS, $matches) == false)
|
|
|
|
returnServerError('Unable to find client ID.');
|
|
|
|
$clientID = $matches[1];
|
|
|
|
|
|
|
|
$this->clientIDCache->saveData($clientID);
|
|
|
|
return $clientID;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildAPIURL($endpoint, $parameters){
|
2020-07-05 19:49:46 +02:00
|
|
|
return 'https://api-v2.soundcloud.com/'
|
2019-12-01 12:42:53 +01:00
|
|
|
. $endpoint
|
|
|
|
. '?'
|
|
|
|
. http_build_query($parameters);
|
|
|
|
}
|
|
|
|
|
2020-09-25 08:43:12 +02:00
|
|
|
private function apiGet($endpoint, $parameters = array()) {
|
2019-12-01 12:42:53 +01:00
|
|
|
$parameters['client_id'] = $this->getClientID();
|
|
|
|
|
|
|
|
try {
|
|
|
|
return json_decode(getContents($this->buildAPIURL($endpoint, $parameters)));
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// Retry once with refreshed client ID
|
|
|
|
$parameters['client_id'] = $this->refreshClientID();
|
|
|
|
return json_decode(getContents($this->buildAPIURL($endpoint, $parameters)));
|
|
|
|
}
|
|
|
|
}
|
2014-07-24 13:51:42 +02:00
|
|
|
}
|