-
Notifications
You must be signed in to change notification settings - Fork 1k
/
SoundcloudBridge.php
234 lines (186 loc) · 6.35 KB
/
SoundcloudBridge.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
class SoundCloudBridge extends BridgeAbstract
{
const MAINTAINER = 'kranack, Roliga';
const NAME = 'Soundcloud Bridge';
const URI = 'https://soundcloud.com/';
const CACHE_TIMEOUT = 600; // 10min
const DESCRIPTION = 'Returns 10 newest music from user profile';
const PARAMETERS = [[
'u' => [
'name' => 'username',
'exampleValue' => 'thekidlaroi',
'required' => true
],
't' => [
'name' => 'Content',
'type' => 'list',
'defaultValue' => 'tracks',
'values' => [
'All (except likes)' => 'all',
'Tracks' => 'tracks',
'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 $feedIcon = null;
private $clientIdRegex = '/client_id.*?"(.+?)"/';
private $widgetRegex = '/widget-.+?\.js/';
public function collectData()
{
$res = $this->getUser($this->getInput('u'));
$this->feedTitle = $res->username;
$this->feedIcon = $res->avatar_url;
$apiItems = $this->getUserItems($res->id, $this->getInput('t'))
or returnServerError('No results for ' . $this->getInput('t'));
$hasTrackObject = ['all', 'reposts', 'likes'];
foreach ($apiItems->collection as $index => $apiItem) {
if (in_array($this->getInput('t'), $hasTrackObject) === true) {
$apiItem = $apiItem->playlist ?? $apiItem->track;
}
$item = [];
$item['author'] = $apiItem->user->username;
$item['title'] = $apiItem->user->username . ' - ' . $apiItem->title;
$item['timestamp'] = strtotime($apiItem->created_at);
$description = nl2br($apiItem->description ?? '');
$item['content'] = <<<HTML
<p>{$description}</p>
HTML;
if (isset($apiItem->tracks) && $apiItem->track_count > 0) {
$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;
if (count($this->items) >= 10) {
break;
}
}
}
public function getIcon()
{
if ($this->feedIcon) {
return $this->feedIcon;
}
return parent::getIcon();
}
public function getURI()
{
if ($this->getInput('u')) {
return self::URI . $this->getInput('u') . '/' . $this->getInput('t');
}
return parent::getURI();
}
public function getName()
{
if ($this->feedTitle) {
return $this->feedTitle . ' - ' . ucfirst($this->getInput('t')) . ' - ' . self::NAME;
}
return parent::getName();
}
private function getClientID()
{
$clientID = $this->cache->get('SoundCloudBridge_client_id');
if (!$clientID) {
return $this->refreshClientID();
} else {
return $clientID;
}
}
private function refreshClientID()
{
$playerHTML = getContents($this->playerUrl);
// Extract widget JS filenames from player page
if (preg_match_all($this->widgetRegex, $playerHTML, $matches) == false) {
returnServerError('Unable to find widget JS URL.');
}
$clientID = '';
// Loop widget js files and extract client ID
foreach ($matches[0] as $widgetFile) {
$widgetURL = $this->widgetUrl . $widgetFile;
$widgetJS = getContents($widgetURL);
if (preg_match($this->clientIdRegex, $widgetJS, $matches)) {
$clientID = $matches[1];
$this->cache->set('SoundCloudBridge_client_id', $clientID);
return $clientID;
}
}
if (empty($clientID)) {
returnServerError('Unable to find client ID.');
}
}
private function buildApiUrl($endpoint, $parameters)
{
return $this->apiUrl
. $endpoint
. '?'
. http_build_query($parameters);
}
private function getUser($username)
{
$parameters = ['url' => self::URI . $username];
return $this->getApi('resolve', $parameters);
}
private function getUserItems($userId, $type)
{
$parameters = ['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();
$url = $this->buildApiUrl($endpoint, $parameters);
try {
return json_decode(getContents($url));
} catch (Exception $e) {
// Retry once with refreshed client ID
$parameters['client_id'] = $this->refreshClientID();
$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',
['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;
}
}