-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGSAPI.php
392 lines (364 loc) · 13.6 KB
/
GSAPI.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
/**
* GSAPI - Interface to the Grooveshark API used in the Wordpress plugin
*
* PHP Version 5
*
* @author Roberto Sanchez <[email protected]>
*/
// These functions added for php versions that don't include json functions
function gs_json_decode($content, $arg) {
if (!extension_loaded('json')) {
if (!class_exists('GS_Services_JSON')) {
require_once 'GSJSON.php';
}
$json = new GS_Services_JSON(SERVICES_JSON_LOOSE_TYPE);
return $json->decode($content);
} else {
// just use php's json if it is available
return json_decode($content, $arg);
}
}
class GSAPI
{
protected $sessionID = '';
protected $userID = 0;
protected $callCount = 0;
//private static $host = 'staging.api.grooveshark.com';
private static $host = 'api.grooveshark.com';
private static $instance;
public function __construct($sessionID = '')
{
if (empty($sessionID)) {
$result = self::callRemote('startSession', array());
$this->sessionID = $result['result']['sessionID'];
} else {
$this->sessionID = $sessionID;
}
}
private static function createMessageSig($method, $params, $secret)
{
ksort($params);
$data = '';
foreach ($params as $key => $value) {
$data .= "$key{$value}";
}
$data = "$method{$data}";
$sig = hash_hmac('md5', $data, $secret);
return $sig;
}
private static function callRemote($method, $params = array())
{
$url = sprintf('http://%s/ws/2.0/?method=%s&%s&wsKey=wordpress&sig=%s&format=json', self::$host, $method, http_build_query($params), self::createMessageSig($method, $params, 'd6c59291620c6eaa5bf94da08fae0ecc'));
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
$result = curl_exec($curl);
curl_close($curl);
return gs_json_decode($result, true);
}
/*
* Gets an instance of the GSAPI object
*
* @param mixed[] Array containing either a sessionID element or an APIKey element
* @return GSAPI Instance of the GSAPI object
*/
public static function getInstance($sessionID = '')
{
if (!(self::$instance instanceof GSAPI)) {
self::$instance = new GSAPI($sessionID);
}
return self::$instance;
}
public function getUserID() {return $this->userID;}
public function getSessionID() {return $this->sessionID;}
public function getApiCallsCount() {return $this->callCount;}
/**
* Authenticates the user for current API session
*
* @param string username
* @param string token
*/
public function authenticateUser($username, $token)
{
$result = self::callRemote('authenticateUser', array('sessionID' => $this->sessionID, 'username' => $username, 'token' => $token));
if (!isset($result['result']) && !isset($result['result']['UserID'])) {
return false;
} else {
$this->userID = $result['result']['UserID'];
return true;
}
}
/**
* Logs the user out (just to make sure songs are not added to their account)
*
*/
public function logout()
{
self::callRemote('logout', array('sessionID' => $this->sessionID));
}
/**
* Gets the logged-in user's username, returns an empty string if no username is found
* @return string username (empty string on failure)
*/
public function getUsername()
{
if ($this->userID != 0) {
$result = self::callRemote('getUserInfoFromSessionID', array('sessionID' => $this->sessionID));
if (isset($result['result']) && isset($result['result'][0]) && isset($result['result'][0]['username'])) {
return $result['result'][0]['username'];
} else {
return '';
}
}
return '';
}
/**
* Performs an API search for songs
*
* @param string Search query
* @param int Search limit
* @return mixed Songs array or error
*/
public function searchSongs($query, $limit)
{
$result = self::callRemote('getSongSearchResultsEx', array('query' => $query, 'limit' => $limit));
if (isset($result['result'])) {
return $result['result']['songs'];
} elseif (isset($result['errors'])) {
return array('error' => $result['errors'][0]['code']);
} else {
return array('error' => -4);
}
}
/**
* Gets song information
*
* @param int songID
* @return mixed Song information or error
*/
public function songAbout($songID)
{
$result = self::callRemote('getSongInfoEx', array('songID' => $songID));
if (isset($result['result'])) {
return $result['result'];
} elseif (isset($result['errors'])) {
return array('error' => $result['errors'][0]['code']);
} else {
return array('error' => -8);
}
}
/**
* Get the url to link to a song on Grooveshark
*
* @param int songID
* @return string the song's url, or empty string on failure
*/
public function getSongUrl($songID)
{
$result = self::callRemote('getSongURLFromSongID', array('songID' => $songID));
if (isset($result['result'])) {
return $result['result']['url'];
} else {
return '';
}
}
/**
* Gets the favorite songs of the logged-in user
*
* @return mixed Songs list or error
*/
public function userGetFavoriteSongs()
{
if ($this->userID == 0) {
return array('error' => 'User Not Logged In');
}
$result = self::callRemote('getUserFavoriteSongs', array('sessionID' => $this->sessionID));
if (isset($result['result'])) {
return $result['result'];
} elseif (isset($result['errors'])) {
return array('error' => $result['errors'][0]['code']);
} else {
return array('error' => -32);
}
}
/**
* Gets the playlists of the logged-in user
*
* @return mixed playlistis or error
*/
public function userGetPlaylists()
{
if ($this->userID == 0) {
return array('error' => 'User Not Logged In');
}
$result = self::callRemote('getUserPlaylists', array('sessionID' => $this->sessionID));
if (isset($result['result'])) {
return $result['result'];
} elseif (isset($result['errors'])) {
return array('error' => $result['errors'][0]['code']);
} else {
return array('error' => -64);
}
}
/**
* Gets playlist information
*
* @param int playlistID
* @return mixed playlist information or error
*/
public function playlistAbout($playlistID)
{
$result = self::callRemote('getPlaylistInfo', array('playlistID' => $playlistID));
if (isset($result['result'])) {
return $result['result'];
} elseif (isset($result['errors'])) {
return array('error' => $result['errors'][0]['code']);
} else {
return array('error' => -128);
}
}
/**
* Creates a playlist with songs
*
* @param string playlist name
* @param int[] Array consisting of songIDs for the songs to be added to the new playlist
* @return int playlistID (or 0 on failure, -1 on duplicate)
*/
public function playlistCreate($name, $songIDs)
{
$result = self::callRemote('createPlaylist', array('name' => $name, 'songIDs' => $songIDs, 'sessionID' => $this->sessionID));
if (isset($result['result']) && ($result['result']['success'] == 1)) {
return $result['result']['playlistID'];
} elseif (isset($result['errors'])) {
$duplicate = false;
foreach($result['errors'] as $error) {
if ($error['code'] == 800) {
$duplicate = true;
}
}
if ($duplicate) {
return -1;
} else {
return 0;
}
} else {
return 0;
}
}
/**
* Sets the songs for a playlist
*
* @param int playlistID for the playlist to be modified
* @param int[] Array consisting of songIDs for the songs to be added to the new playlist
* @return bool true on success, false on failure
*/
public function playlistSetSongs($playlistID, $songIDs)
{
$result = self::callRemote('setPlaylistSongs', array('playlistID' => $playlistID, 'songIDs' => $songIDs, 'sessionID' => $this->sessionID));
if (isset($result['result']) && ($result['result']['success'] == 1)) {
return true;
} else {
return false;
}
}
/**
* Gets the songs of a playlist
*
* @param int playlistID
* @return mixed list of songs or error
*/
public function playlistGetSongs($playlistID)
{
$result = self::callRemote('getPlaylistSongs', array('playlistID' => $playlistID));
if (isset($result['result'])) {
return $result['result'];
} elseif (isset($result['errors'])) {
return array('errors' => $result['errors'][0]['code']);
} else {
return array('error' => -512);
}
}
/**
* Get the url for a playlist
*
* @param int playlistID
* @return string playlist url (or empty string on failure)
*/
public function getPlaylistUrl($playlistID)
{
$result = self::callRemote('getPlaylistURLFromPlaylistID', array('playlistID' => $playlistID));
if (isset($result['result'])) {
return $result['result'];
} else {
return '';
}
}
/**
* Gets the widget embed code for a song
*
* @param int songID
* @param int width of widget in pixels
* @return string Widget's embed code (or error string)
*/
public function songGetWidgetEmbedCode($songID, $width, $theme = 'metal')
{
return self::getWidgetEmbedCode('songWidget.swf', $width, 40, array('songIDs' => $songID), 0, null, $theme);
}
/**
* Gets widget embed code for an autoplay widget
*
* @param int songID
* @return string widget's embed code or error string
*/
public function songGetApWidgetEmbedCode($songID) {
return self::getWidgetEmbedCode('songWidget.swf', 0, 0, array('songIDs' => $songID), 1, null, 'metal');
}
/**
* Gets the widget embed code (hex codes do not require prefix)
*
* @param int[] $songIDs: array consisting of the songIDs of the songs for the widget
* @param int $width in pixels
* @param int $height in pixels
* @param string $name: widget name
* @param string $bt: body text color hex code
* @param string $bth: body text hover color hex code
* @param string $bbg: body background color hex code
* @param string $bfg: body foreground color hex code
* @param string $pbg: player background color hex code
* @param string $pfg: player foreground color hex code
* @param string $pbgh: player background hover color hex code
* @param string $pfgh: player foreground hover color hex code
* @param string $lbg: list background color hex code
* @param string $lfg: list foreground color hex code
* @param string $lbgh: list background hover color hex code
* @param string $lfgh: list foreground hover color hex code
* @param string $sb: scrollbar color hex code
* @param string $sbh: scrollbar hover color hex code
* @param string $secondaryIcon color hex code
* @return string widget embed code
*/
public function playlistGetWidgetEmbedCode($songIDs, $width, $height, $name, $bt, $bth, $bbg, $bfg,
$pbg, $pfg, $pbgh, $pfgh, $lbg, $lfg, $lbgh, $lfgh, $sb, $sbh, $secondaryIcon)
{
$colors = array('bt' => $bt, 'bth' => $bth, 'bbg' => $bbg, 'bfg' => $bfg, 'pbg' => $pbg, 'pfg' => $pfg, 'pbgh' => $pbgh, 'pfgh' => $pfgh, 'lbg' => $lbg, 'lfg' => $lfg, 'lbgh' => $lbgh, 'lfgh' => $lfgh, 'sb' => $sb, 'sbh' => $sbh, 'si' => $secondaryIcon);
return self::getWidgetEmbedCode('widget.swf', $width, $height, array('songIDs' => implode(',',$songIDs)), 0, $colors);
}
private static function getWidgetEmbedCode($swfName, $width, $height, $ids, $ap = 0, $colors = null, $style = null)
{
$ids = 'songIDs=' . $ids['songIDs'];
$colors = is_array($colors) && !empty($colors) ? '&' . http_build_query($colors) : '';
$ap = ($ap != 0) ? "&p=$ap" : '';
$style = '&style=' . $style;
$embed = "
<object width='$width' height='$height'>
<param name='movie' value='http://listen.grooveshark.com/$swfName'></param>
<param name='wmode' value='window'></param>
<param name='allowScriptAccess' value='always'></param>
<param name='flashvars' value='hostname=cowbell.grooveshark.com&{$ids}{$ap}{$colors}{$style}'></param>
<embed src='http://listen.grooveshark.com/$swfName' type='application/x-shockwave-flash' width='$width' height='$height' flashvars='hostname=cowbell.grooveshark.com&{$ids}{$ap}{$colors}{$style}' allowScriptAccess='always' wmode='window'></embed>
</object>";
return $embed;
}
}