Improve Soundcloud bridge (#1500)

* [SoundcloudBridge] Add playlist support, migrate to `api-v2`
This commit is contained in:
somini 2020-07-05 18:49:46 +01:00 committed by GitHub
parent 3ad126cdf2
commit 976445b490
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -11,6 +11,15 @@ class SoundCloudBridge extends BridgeAbstract {
'u' => array( 'u' => array(
'name' => 'username', 'name' => 'username',
'required' => true 'required' => true
),
't' => array(
'name' => 'type',
'type' => 'list',
'defaultValue' => 'tracks',
'values' => array(
'Tracks' => 'tracks',
'Playlists' => 'playlists'
)
) )
)); ));
@ -19,13 +28,13 @@ class SoundCloudBridge extends BridgeAbstract {
public function collectData(){ public function collectData(){
$res = $this->apiGet('resolve', array( $res = $this->apiGet('resolve', array(
'url' => 'http://www.soundcloud.com/' . $this->getInput('u') 'url' => 'https://soundcloud.com/' . $this->getInput('u')
)) 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 = $this->apiGet('users/' . urlencode($res->id) . '/tracks') $tracks = $this->apiGet('users/' . urlencode($res->id) . '/' . $this->getInput('t'))->collection
or returnServerError('No results for this user'); or returnServerError('No results for this user/playlist');
$numTracks = min(count($tracks), 10); $numTracks = min(count($tracks), 10);
for($i = 0; $i < $numTracks; $i++) { for($i = 0; $i < $numTracks; $i++) {
@ -33,10 +42,10 @@ class SoundCloudBridge extends BridgeAbstract {
$item['author'] = $tracks[$i]->user->username; $item['author'] = $tracks[$i]->user->username;
$item['title'] = $tracks[$i]->user->username . ' - ' . $tracks[$i]->title; $item['title'] = $tracks[$i]->user->username . ' - ' . $tracks[$i]->title;
$item['timestamp'] = strtotime($tracks[$i]->created_at); $item['timestamp'] = strtotime($tracks[$i]->created_at);
$item['content'] = $tracks[$i]->description; $item['content'] = nl2br($tracks[$i]->description);
$item['enclosures'] = array($tracks[$i]->uri $item['enclosures'] = array($tracks[$i]->uri
. '/stream?client_id=' . '/stream?client_id='
. $this->getClientID()); . $this->getClientID());
$item['id'] = self::URI $item['id'] = self::URI
. urlencode($this->getInput('u')) . urlencode($this->getInput('u'))
@ -99,14 +108,14 @@ class SoundCloudBridge extends BridgeAbstract {
// Without url=http, this returns a 404 // Without url=http, this returns a 404
$playerHTML = getContents('https://w.soundcloud.com/player/?url=http') $playerHTML = getContents('https://w.soundcloud.com/player/?url=http')
or returnServerError('Unable to get player page.'); or returnServerError('Unable to get player page.');
$regex = '/widget-.+?\.js/'; $regex = '/widget-.+?\.js/';
if(preg_match($regex, $playerHTML, $matches) == false) if(preg_match($regex, $playerHTML, $matches) == false)
returnServerError('Unable to find widget JS URL.'); returnServerError('Unable to find widget JS URL.');
$widgetURL = 'https://widget.sndcdn.com/' . $matches[0]; $widgetURL = 'https://widget.sndcdn.com/' . $matches[0];
$widgetJS = getContents($widgetURL) $widgetJS = getContents($widgetURL)
or returnServerError('Unable to get widget JS page.'); or returnServerError('Unable to get widget JS page.');
$regex = '/client_id.*?"(.+?)"/'; $regex = '/client_id.*?"(.+?)"/';
if(preg_match($regex, $widgetJS, $matches) == false) if(preg_match($regex, $widgetJS, $matches) == false)
returnServerError('Unable to find client ID.'); returnServerError('Unable to find client ID.');
@ -117,7 +126,7 @@ class SoundCloudBridge extends BridgeAbstract {
} }
private function buildAPIURL($endpoint, $parameters){ private function buildAPIURL($endpoint, $parameters){
return 'https://api.soundcloud.com/' return 'https://api-v2.soundcloud.com/'
. $endpoint . $endpoint
. '?' . '?'
. http_build_query($parameters); . http_build_query($parameters);