[SoundcloudBridge] Automatically acquire client_id (#1375)
Also some slight refactoring, as well as adding Roliga as maintainer.
This commit is contained in:
parent
583dfb4958
commit
e518936be7
1 changed files with 71 additions and 16 deletions
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
class SoundCloudBridge extends BridgeAbstract {
|
class SoundCloudBridge extends BridgeAbstract {
|
||||||
|
|
||||||
const MAINTAINER = 'kranack';
|
const MAINTAINER = 'kranack, Roliga';
|
||||||
const NAME = 'Soundcloud Bridge';
|
const NAME = 'Soundcloud Bridge';
|
||||||
const URI = 'https://soundcloud.com/';
|
const URI = 'https://soundcloud.com/';
|
||||||
const CACHE_TIMEOUT = 600; // 10min
|
const CACHE_TIMEOUT = 600; // 10min
|
||||||
|
@ -14,27 +14,18 @@ class SoundCloudBridge extends BridgeAbstract {
|
||||||
)
|
)
|
||||||
));
|
));
|
||||||
|
|
||||||
const CLIENT_ID = 'W0KEWWILAjDiRH89X0jpwzuq6rbSK08R';
|
|
||||||
|
|
||||||
private $feedIcon = null;
|
private $feedIcon = null;
|
||||||
|
private $clientIDCache = null;
|
||||||
|
|
||||||
public function collectData(){
|
public function collectData(){
|
||||||
|
$res = $this->apiGet('resolve', array(
|
||||||
$res = json_decode(getContents(
|
'url' => 'http://www.soundcloud.com/' . $this->getInput('u')
|
||||||
'https://api.soundcloud.com/resolve?url=http://www.soundcloud.com/'
|
|
||||||
. urlencode($this->getInput('u'))
|
|
||||||
. '&client_id='
|
|
||||||
. self::CLIENT_ID
|
|
||||||
)) or returnServerError('No results for this query');
|
)) or returnServerError('No results for this query');
|
||||||
|
|
||||||
$this->feedIcon = $res->avatar_url;
|
$this->feedIcon = $res->avatar_url;
|
||||||
|
|
||||||
$tracks = json_decode(getContents(
|
$tracks = $this->apiGet('users/' . urlencode($res->id) . '/tracks')
|
||||||
'https://api.soundcloud.com/users/'
|
or returnServerError('No results for this user');
|
||||||
. urlencode($res->id)
|
|
||||||
. '/tracks?client_id='
|
|
||||||
. self::CLIENT_ID
|
|
||||||
)) or returnServerError('No results for this user');
|
|
||||||
|
|
||||||
$numTracks = min(count($tracks), 10);
|
$numTracks = min(count($tracks), 10);
|
||||||
for($i = 0; $i < $numTracks; $i++) {
|
for($i = 0; $i < $numTracks; $i++) {
|
||||||
|
@ -45,7 +36,7 @@ class SoundCloudBridge extends BridgeAbstract {
|
||||||
$item['content'] = $tracks[$i]->description;
|
$item['content'] = $tracks[$i]->description;
|
||||||
$item['enclosures'] = array($tracks[$i]->uri
|
$item['enclosures'] = array($tracks[$i]->uri
|
||||||
. '/stream?client_id='
|
. '/stream?client_id='
|
||||||
. self::CLIENT_ID);
|
. $this->getClientID());
|
||||||
|
|
||||||
$item['id'] = self::URI
|
$item['id'] = self::URI
|
||||||
. urlencode($this->getInput('u'))
|
. urlencode($this->getInput('u'))
|
||||||
|
@ -75,4 +66,68 @@ class SoundCloudBridge extends BridgeAbstract {
|
||||||
|
|
||||||
return parent::getName();
|
return parent::getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue