2014-07-24 15:51:42 +04:00
|
|
|
<?php
|
2017-02-11 18:16:56 +03:00
|
|
|
class SoundCloudBridge extends BridgeAbstract {
|
2015-09-09 01:13:56 +03:00
|
|
|
|
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
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
const PARAMETERS = array( array(
|
|
|
|
'u' => array(
|
|
|
|
'name' => 'username',
|
|
|
|
'required' => true
|
|
|
|
)
|
|
|
|
));
|
2015-11-05 18:50:18 +03:00
|
|
|
|
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
|
|
|
|
2016-08-25 02:24:53 +03:00
|
|
|
public function collectData(){
|
2019-12-01 14:42:53 +03:00
|
|
|
$res = $this->apiGet('resolve', array(
|
|
|
|
'url' => 'http://www.soundcloud.com/' . $this->getInput('u')
|
2017-02-11 18:16:56 +03:00
|
|
|
)) or returnServerError('No results for this query');
|
|
|
|
|
2019-06-01 16:04:43 +03:00
|
|
|
$this->feedIcon = $res->avatar_url;
|
|
|
|
|
2019-12-01 14:42:53 +03:00
|
|
|
$tracks = $this->apiGet('users/' . urlencode($res->id) . '/tracks')
|
|
|
|
or returnServerError('No results for this user');
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2018-12-26 22:35:05 +03:00
|
|
|
$numTracks = min(count($tracks), 10);
|
|
|
|
for($i = 0; $i < $numTracks; $i++) {
|
2017-02-11 18:16:56 +03:00
|
|
|
$item = array();
|
2018-12-10 23:35:18 +03:00
|
|
|
$item['author'] = $tracks[$i]->user->username;
|
2017-02-11 18:16:56 +03:00
|
|
|
$item['title'] = $tracks[$i]->user->username . ' - ' . $tracks[$i]->title;
|
2018-12-10 23:35:18 +03:00
|
|
|
$item['timestamp'] = strtotime($tracks[$i]->created_at);
|
|
|
|
$item['content'] = $tracks[$i]->description;
|
2018-12-04 18:16:19 +03:00
|
|
|
$item['enclosures'] = array($tracks[$i]->uri
|
|
|
|
. '/stream?client_id='
|
2019-12-01 14:42:53 +03:00
|
|
|
. $this->getClientID());
|
2017-02-11 18:16:56 +03:00
|
|
|
|
|
|
|
$item['id'] = self::URI
|
|
|
|
. urlencode($this->getInput('u'))
|
|
|
|
. '/'
|
|
|
|
. urlencode($tracks[$i]->permalink);
|
|
|
|
$item['uri'] = self::URI
|
|
|
|
. urlencode($this->getInput('u'))
|
|
|
|
. '/'
|
|
|
|
. urlencode($tracks[$i]->permalink);
|
|
|
|
$this->items[] = $item;
|
2014-07-24 15:51:42 +04:00
|
|
|
}
|
2015-09-09 01:13:56 +03:00
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2014-07-24 15:51:42 +04:00
|
|
|
public function getName(){
|
2017-07-29 20:28:00 +03:00
|
|
|
if(!is_null($this->getInput('u'))) {
|
2017-02-15 00:20:55 +03:00
|
|
|
return self::NAME . ' - ' . $this->getInput('u');
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
// Without url=http, this returns a 404
|
|
|
|
$playerHTML = getContents('https://w.soundcloud.com/player/?url=http')
|
|
|
|
or returnServerError('Unable to get player page.');
|
|
|
|
$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)
|
|
|
|
or returnServerError('Unable to get widget JS page.');
|
|
|
|
$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){
|
|
|
|
return 'https://api.soundcloud.com/'
|
|
|
|
. $endpoint
|
|
|
|
. '?'
|
|
|
|
. http_build_query($parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function apiGet($endpoint, $parameters = array()){
|
|
|
|
$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 15:51:42 +04:00
|
|
|
}
|