[SoundcloudBridge] Add support for albums, reposts & likes (#2236)

This commit is contained in:
Joseph 2021-09-20 12:53:41 +00:00 committed by GitHub
parent ccb2e64fd0
commit 927cb17dbf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,28 +1,36 @@
<?php <?php
class SoundCloudBridge extends BridgeAbstract { class SoundCloudBridge extends BridgeAbstract {
const MAINTAINER = 'kranack, Roliga'; 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
const DESCRIPTION = 'Returns 10 newest music from user profile'; const DESCRIPTION = 'Returns 10 newest music from user profile';
const PARAMETERS = array( array( const PARAMETERS = array(array(
'u' => array( 'u' => array(
'name' => 'username', 'name' => 'username',
'required' => true 'required' => true
), ),
't' => array( 't' => array(
'name' => 'type', 'name' => 'Content',
'type' => 'list', 'type' => 'list',
'defaultValue' => 'tracks', 'defaultValue' => 'tracks',
'values' => array( 'values' => array(
'All (except likes)' => 'all',
'Tracks' => 'tracks', 'Tracks' => 'tracks',
'Playlists' => 'playlists' 'Albums' => 'albums',
'Playlists' => 'playlists',
'Reposts' => 'reposts',
'Likes' => 'likes'
) )
) )
)); ));
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/';
private $feedTitle = null; private $feedTitle = null;
private $feedIcon = null; private $feedIcon = null;
private $clientIDCache = null; private $clientIDCache = null;
@ -30,35 +38,46 @@ class SoundCloudBridge extends BridgeAbstract {
private $clientIdRegex = '/client_id.*?"(.+?)"/'; private $clientIdRegex = '/client_id.*?"(.+?)"/';
private $widgetRegex = '/widget-.+?\.js/'; private $widgetRegex = '/widget-.+?\.js/';
public function collectData(){ public function collectData() {
$res = $this->apiGet('resolve', array( $res = $this->getUser($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->feedTitle = $res->username; $this->feedTitle = $res->username;
$this->feedIcon = $res->avatar_url; $this->feedIcon = $res->avatar_url;
$tracks = $this->apiGet( $apiItems = $this->getUserItems($res->id, $this->getInput('t'))
'users/' . urlencode($res->id) . '/' . $this->getInput('t'), or returnServerError('No results for ' . $this->getInput('t'));
array('limit' => 31)
) or returnServerError('No results for this user/playlist'); $hasTrackObject = array('all', 'reposts', 'likes');
foreach ($apiItems->collection as $index => $apiItem) {
if (in_array($this->getInput('t'), $hasTrackObject) === true) {
$apiItem = $apiItem->track;
}
foreach ($tracks->collection as $index => $track) {
$item = array(); $item = array();
$item['author'] = $track->user->username; $item['author'] = $apiItem->user->username;
$item['title'] = $track->user->username . ' - ' . $track->title; $item['title'] = $apiItem->user->username . ' - ' . $apiItem->title;
$item['timestamp'] = strtotime($track->created_at); $item['timestamp'] = strtotime($apiItem->created_at);
$item['content'] = nl2br($track->description);
$item['enclosures'][] = $track->artwork_url;
$item['id'] = self::URI $description = nl2br($apiItem->description);
. urlencode($this->getInput('u'))
. '/' $item['content'] = <<<HTML
. urlencode($track->permalink); <p>{$description}</p>
$item['uri'] = self::URI HTML;
. urlencode($this->getInput('u'))
. '/' if (isset($apiItem->tracks) && $apiItem->track_count > 0) {
. urlencode($track->permalink); $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;
$this->items[] = $item; $this->items[] = $item;
if (count($this->items) >= 10) { if (count($this->items) >= 10) {
@ -75,13 +94,17 @@ class SoundCloudBridge extends BridgeAbstract {
return parent::getIcon(); return parent::getIcon();
} }
public function getURI(){ public function getURI() {
return 'https://soundcloud.com/' . $this->getInput('u'); if ($this->getInput('u')) {
return self::URI . $this->getInput('u') . '/' . $this->getInput('t');
}
return parent::getURI();
} }
public function getName(){ public function getName() {
if($this->feedTitle) { if($this->feedTitle) {
return $this->feedTitle . ' - ' . self::NAME; return $this->feedTitle . ' - ' . ucfirst($this->getInput('t')) . ' - ' . self::NAME;
} }
return parent::getName(); return parent::getName();
@ -113,8 +136,7 @@ class SoundCloudBridge extends BridgeAbstract {
private function refreshClientID(){ private function refreshClientID(){
$this->initClientIDCache(); $this->initClientIDCache();
// Without url=http, this returns a 404 $playerHTML = getContents($this->playerUrl)
$playerHTML = getContents('https://w.soundcloud.com/player/?url=http')
or returnServerError('Unable to get player page.'); or returnServerError('Unable to get player page.');
// Extract widget JS filenames from player page // Extract widget JS filenames from player page
@ -125,7 +147,7 @@ class SoundCloudBridge extends BridgeAbstract {
// Loop widget js files and extract client ID // Loop widget js files and extract client ID
foreach ($matches[0] as $widgetFile) { foreach ($matches[0] as $widgetFile) {
$widgetURL = 'https://widget.sndcdn.com/' . $widgetFile; $widgetURL = $this->widgetUrl . $widgetFile;
$widgetJS = getContents($widgetURL) $widgetJS = getContents($widgetURL)
or returnServerError('Unable to get widget JS page.'); or returnServerError('Unable to get widget JS page.');
@ -143,22 +165,75 @@ class SoundCloudBridge extends BridgeAbstract {
} }
} }
private function buildAPIURL($endpoint, $parameters){ private function buildApiUrl($endpoint, $parameters) {
return 'https://api-v2.soundcloud.com/' return $this->apiUrl
. $endpoint . $endpoint
. '?' . '?'
. http_build_query($parameters); . http_build_query($parameters);
} }
private function apiGet($endpoint, $parameters = array()) { 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) {
$parameters['client_id'] = $this->getClientID(); $parameters['client_id'] = $this->getClientID();
$url = $this->buildApiUrl($endpoint, $parameters);
try { try {
return json_decode(getContents($this->buildAPIURL($endpoint, $parameters))); return json_decode(getContents($url));
} catch (Exception $e) { } catch (Exception $e) {
// Retry once with refreshed client ID // Retry once with refreshed client ID
$parameters['client_id'] = $this->refreshClientID(); $parameters['client_id'] = $this->refreshClientID();
return json_decode(getContents($this->buildAPIURL($endpoint, $parameters))); $url = $this->buildApiUrl($endpoint, $parameters);
return json_decode(getContents($url));
} }
} }
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;
}
} }