Audioscrobbler.php
20 KB
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Service
* @subpackage Audioscrobbler
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Audioscrobbler.php 23775 2011-03-01 17:25:24Z ralph $
*/
/**
* @see Zend_Http_Client
*/
require_once 'Zend/Http/Client.php';
/**
* @category Zend
* @package Zend_Service
* @subpackage Audioscrobbler
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Service_Audioscrobbler
{
/**
* Zend_Http_Client Object
*
* @var Zend_Http_Client
* @access protected
*/
protected $_client;
/**
* Array that contains parameters being used by the webservice
*
* @var array
* @access protected
*/
protected $_params;
/**
* Holds error information (e.g., for handling simplexml_load_string() warnings)
*
* @var array
* @access protected
*/
protected $_error = null;
/**
* Sets up character encoding, instantiates the HTTP client, and assigns the web service version.
*/
public function __construct()
{
$this->set('version', '1.0');
iconv_set_encoding('output_encoding', 'UTF-8');
iconv_set_encoding('input_encoding', 'UTF-8');
iconv_set_encoding('internal_encoding', 'UTF-8');
}
/**
* Set Http Client
*
* @param Zend_Http_Client $client
*/
public function setHttpClient(Zend_Http_Client $client)
{
$this->_client = $client;
}
/**
* Get current http client.
*
* @return Zend_Http_Client
*/
public function getHttpClient()
{
if($this->_client == null) {
$this->lazyLoadHttpClient();
}
return $this->_client;
}
/**
* Lazy load Http Client if none is instantiated yet.
*
* @return void
*/
protected function lazyLoadHttpClient()
{
$this->_client = new Zend_Http_Client();
}
/**
* Returns a field value, or false if the named field does not exist
*
* @param string $field
* @return string|false
*/
public function get($field)
{
if (array_key_exists($field, $this->_params)) {
return $this->_params[$field];
} else {
return false;
}
}
/**
* Generic set action for a field in the parameters being used
*
* @param string $field name of field to set
* @param string $value value to assign to the named field
* @return Zend_Service_Audioscrobbler Provides a fluent interface
*/
public function set($field, $value)
{
$this->_params[$field] = urlencode($value);
return $this;
}
/**
* Protected method that queries REST service and returns SimpleXML response set
*
* @param string $service name of Audioscrobbler service file we're accessing
* @param string $params parameters that we send to the service if needded
* @throws Zend_Http_Client_Exception
* @throws Zend_Service_Exception
* @return SimpleXMLElement result set
* @access protected
*/
protected function _getInfo($service, $params = null)
{
$service = (string) $service;
$params = (string) $params;
if ($params === '') {
$this->getHttpClient()->setUri("http://ws.audioscrobbler.com{$service}");
} else {
$this->getHttpClient()->setUri("http://ws.audioscrobbler.com{$service}?{$params}");
}
$response = $this->getHttpClient()->request();
$responseBody = $response->getBody();
if (preg_match('/No such path/', $responseBody)) {
/**
* @see Zend_Http_Client_Exception
*/
require_once 'Zend/Http/Client/Exception.php';
throw new Zend_Http_Client_Exception('Could not find: ' . $this->_client->getUri());
} elseif (preg_match('/No user exists with this name/', $responseBody)) {
/**
* @see Zend_Http_Client_Exception
*/
require_once 'Zend/Http/Client/Exception.php';
throw new Zend_Http_Client_Exception('No user exists with this name');
} elseif (!$response->isSuccessful()) {
/**
* @see Zend_Http_Client_Exception
*/
require_once 'Zend/Http/Client/Exception.php';
throw new Zend_Http_Client_Exception('The web service ' . $this->_client->getUri() . ' returned the following status code: ' . $response->getStatus());
}
set_error_handler(array($this, '_errorHandler'));
if (!$simpleXmlElementResponse = simplexml_load_string($responseBody)) {
restore_error_handler();
/**
* @see Zend_Service_Exception
*/
require_once 'Zend/Service/Exception.php';
$exception = new Zend_Service_Exception('Response failed to load with SimpleXML');
$exception->error = $this->_error;
$exception->response = $responseBody;
throw $exception;
}
restore_error_handler();
return $simpleXmlElementResponse;
}
/**
* Utility function to get Audioscrobbler profile information (eg: Name, Gender)
*
* @return array containing information
*/
public function userGetProfileInformation()
{
$service = "/{$this->get('version')}/user/{$this->get('user')}/profile.xml";
return $this->_getInfo($service);
}
/**
* Utility function get this user's 50 most played artists
*
* @return array containing info
*/
public function userGetTopArtists()
{
$service = "/{$this->get('version')}/user/{$this->get('user')}/topartists.xml";
return $this->_getInfo($service);
}
/**
* Utility function to get this user's 50 most played albums
*
* @return SimpleXMLElement object containing result set
*/
public function userGetTopAlbums()
{
$service = "/{$this->get('version')}/user/{$this->get('user')}/topalbums.xml";
return $this->_getInfo($service);
}
/**
* Utility function to get this user's 50 most played tracks
* @return SimpleXML object containing resut set
*/
public function userGetTopTracks()
{
$service = "/{$this->get('version')}/user/{$this->get('user')}/toptracks.xml";
return $this->_getInfo($service);
}
/**
* Utility function to get this user's 50 most used tags
*
* @return SimpleXMLElement object containing result set
*/
public function userGetTopTags()
{
$service = "/{$this->get('version')}/user/{$this->get('user')}/tags.xml";
return $this->_getInfo($service);
}
/**
* Utility function that returns the user's top tags used most used on a specific artist
*
* @return SimpleXMLElement object containing result set
*/
public function userGetTopTagsForArtist()
{
$service = "/{$this->get('version')}/user/{$this->get('user')}/artisttags.xml";
$params = "artist={$this->get('artist')}";
return $this->_getInfo($service, $params);
}
/**
* Utility function that returns this user's top tags for an album
*
* @return SimpleXMLElement object containing result set
*/
public function userGetTopTagsForAlbum()
{
$service = "/{$this->get('version')}/user/{$this->get('user')}/albumtags.xml";
$params = "artist={$this->get('artist')}&album={$this->get('album')}";
return $this->_getInfo($service, $params);
}
/**
* Utility function that returns this user's top tags for a track
*
* @return SimpleXMLElement object containing result set
*/
public function userGetTopTagsForTrack()
{
$service = "/{$this->get('version')}/user/{$this->get('user')}/tracktags.xml";
$params = "artist={$this->get('artist')}&track={$this->get('track')}";
return $this->_getInfo($service, $params);
}
/**
* Utility function that retrieves this user's list of friends
* @return SimpleXMLElement object containing result set
*/
public function userGetFriends()
{
$service = "/{$this->get('version')}/user/{$this->get('user')}/friends.xml";
return $this->_getInfo($service);
}
/**
* Utility function that returns a list of people with similar listening preferences to this user
*
* @return SimpleXMLElement object containing result set
*/
public function userGetNeighbours()
{
$service = "/{$this->get('version')}/user/{$this->get('user')}/neighbours.xml";
return $this->_getInfo($service);
}
/**
* Utility function that returns a list of the 10 most recent tracks played by this user
*
* @return SimpleXMLElement object containing result set
*/
public function userGetRecentTracks()
{
$service = "/{$this->get('version')}/user/{$this->get('user')}/recenttracks.xml";
return $this->_getInfo($service);
}
/**
* Utility function that returns a list of the 10 tracks most recently banned by this user
*
* @return SimpleXMLElement object containing result set
*/
public function userGetRecentBannedTracks()
{
$service = "/{$this->get('version')}/user/{$this->get('user')}/recentbannedtracks.xml";
return $this->_getInfo($service);
}
/**
* Utility function that returns a list of the 10 tracks most recently loved by this user
*
* @return SimpleXMLElement object containing result set
*/
public function userGetRecentLovedTracks()
{
$service = "/{$this->get('version')}/user/{$this->get('user')}/recentlovedtracks.xml";
return $this->_getInfo($service);
}
/**
* Utility function that returns a list of dates of available weekly charts for a this user
*
* Should actually be named userGetWeeklyChartDateList() but we have to follow audioscrobbler's naming
*
* @return SimpleXMLElement object containing result set
*/
public function userGetWeeklyChartList()
{
$service = "/{$this->get('version')}/user/{$this->get('user')}/weeklychartlist.xml";
return $this->_getInfo($service);
}
/**
* Utility function that returns weekly album chart data for this user
*
* @param integer $from optional UNIX timestamp for start of date range
* @param integer $to optional UNIX timestamp for end of date range
* @return SimpleXMLElement object containing result set
*/
public function userGetWeeklyAlbumChart($from = NULL, $to = NULL)
{
$params = "";
if ($from != NULL && $to != NULL) {
$from = (int)$from;
$to = (int)$to;
$params = "from={$from}&to={$to}";
}
$service = "/{$this->get('version')}/user/{$this->get('user')}/weeklyalbumchart.xml";
return $this->_getInfo($service, $params);
}
/**
* Utility function that returns weekly artist chart data for this user
*
* @param integer $from optional UNIX timestamp for start of date range
* @param integer $to optional UNIX timestamp for end of date range
* @return SimpleXMLElement object containing result set
*/
public function userGetWeeklyArtistChart($from = NULL, $to = NULL)
{
$params = "";
if ($from != NULL && $to != NULL) {
$from = (int)$from;
$to = (int)$to;
$params = "from={$from}&to={$to}";
}
$service = "/{$this->get('version')}/user/{$this->get('user')}/weeklyartistchart.xml";
return $this->_getInfo($service, $params);
}
/**
* Utility function that returns weekly track chart data for this user
*
* @param integer $from optional UNIX timestamp for start of date range
* @param integer $to optional UNIX timestamp for end of date range
* @return SimpleXMLElement object containing result set
*/
public function userGetWeeklyTrackChart($from = NULL, $to = NULL)
{
$params = "";
if ($from != NULL && $to != NULL) {
$from = (int)$from;
$to = (int)$to;
$params = "from={$from}&to={$to}";
}
$service = "/{$this->get('version')}/user/{$this->get('user')}/weeklytrackchart.xml";
return $this->_getInfo($service, $params);
}
/**
* Utility function that returns a list of artists similiar to this artist
*
* @return SimpleXMLElement object containing result set
*/
public function artistGetRelatedArtists()
{
$service = "/{$this->get('version')}/artist/{$this->get('artist')}/similar.xml";
return $this->_getInfo($service);
}
/**
* Utility function that returns a list of this artist's top listeners
*
* @return SimpleXMLElement object containing result set
*/
public function artistGetTopFans()
{
$service = "/{$this->get('version')}/artist/{$this->get('artist')}/fans.xml";
return $this->_getInfo($service);
}
/**
* Utility function that returns a list of this artist's top-rated tracks
*
* @return SimpleXMLElement object containing result set
*/
public function artistGetTopTracks()
{
$service = "/{$this->get('version')}/artist/{$this->get('artist')}/toptracks.xml";
return $this->_getInfo($service);
}
/**
* Utility function that returns a list of this artist's top-rated albums
*
* @return SimpleXMLElement object containing result set
*/
public function artistGetTopAlbums()
{
$service = "/{$this->get('version')}/artist/{$this->get('artist')}/topalbums.xml";
return $this->_getInfo($service);
}
/**
* Utility function that returns a list of this artist's top-rated tags
*
* @return SimpleXMLElement object containing result set
*/
public function artistGetTopTags()
{
$service = "/{$this->get('version')}/artist/{$this->get('artist')}/toptags.xml";
return $this->_getInfo($service);
}
/**
* Get information about an album
*
* @return SimpleXMLElement
*/
public function albumGetInfo()
{
$service = "/{$this->get('version')}/album/{$this->get('artist')}/{$this->get('album')}/info.xml";
return $this->_getInfo($service);
}
/**
* Get top fans of the current track.
*
* @return SimpleXMLElement
*/
public function trackGetTopFans()
{
$service = "/{$this->get('version')}/track/{$this->get('artist')}/{$this->get('track')}/fans.xml";
return $this->_getInfo($service);
}
/**
* Get top tags of the current track.
*
* @return SimpleXMLElement
*/
public function trackGetTopTags()
{
$service = "/{$this->get('version')}/track/{$this->get('artist')}/{$this->get('track')}/toptags.xml";
return $this->_getInfo($service);
}
/**
* Get Top Tags.
*
* @return SimpleXMLElement
*/
public function tagGetTopTags()
{
$service = "/{$this->get('version')}/tag/toptags.xml";
return $this->_getInfo($service);
}
/**
* Get top albums by current tag.
*
* @return SimpleXMLElement
*/
public function tagGetTopAlbums()
{
$service = "/{$this->get('version')}/tag/{$this->get('tag')}/topalbums.xml";
return $this->_getInfo($service);
}
/**
* Get top artists by current tag.
*
* @return SimpleXMLElement
*/
public function tagGetTopArtists()
{
$service = "/{$this->get('version')}/tag/{$this->get('tag')}/topartists.xml";
return $this->_getInfo($service);
}
/**
* Get Top Tracks by currently set tag.
*
* @return SimpleXMLElement
*/
public function tagGetTopTracks()
{
$service = "/{$this->get('version')}/tag/{$this->get('tag')}/toptracks.xml";
return $this->_getInfo($service);
}
/**
* Get weekly chart list by current set group.
*
* @see set()
* @return SimpleXMLElement
*/
public function groupGetWeeklyChartList()
{
$service = "/{$this->get('version')}/group/{$this->get('group')}/weeklychartlist.xml";
return $this->_getInfo($service);
}
/**
* Retrieve weekly Artist Charts
*
* @param int $from
* @param int $to
* @return SimpleXMLElement
*/
public function groupGetWeeklyArtistChartList($from = NULL, $to = NULL)
{
if ($from != NULL && $to != NULL) {
$from = (int)$from;
$to = (int)$to;
$params = "from={$from}&$to={$to}";
} else {
$params = "";
}
$service = "/{$this->get('version')}/group/{$this->get('group')}/weeklyartistchart.xml";
return $this->_getInfo($service, $params);
}
/**
* Retrieve Weekly Track Charts
*
* @param int $from
* @param int $to
* @return SimpleXMLElement
*/
public function groupGetWeeklyTrackChartList($from = NULL, $to = NULL)
{
if ($from != NULL && $to != NULL) {
$from = (int)$from;
$to = (int)$to;
$params = "from={$from}&to={$to}";
} else {
$params = "";
}
$service = "/{$this->get('version')}/group/{$this->get('group')}/weeklytrackchart.xml";
return $this->_getInfo($service, $params);
}
/**
* Retrieve Weekly album charts.
*
* @param int $from
* @param int $to
* @return SimpleXMLElement
*/
public function groupGetWeeklyAlbumChartList($from = NULL, $to = NULL)
{
if ($from != NULL && $to != NULL) {
$from = (int)$from;
$to = (int)$to;
$params = "from={$from}&to={$to}";
} else {
$params = "";
}
$service = "/{$this->get('version')}/group/{$this->get('group')}/weeklyalbumchart.xml";
return $this->_getInfo($service, $params);
}
/**
* Saves the provided error information to this instance
*
* @param integer $errno
* @param string $errstr
* @param string $errfile
* @param integer $errline
* @param array $errcontext
* @return void
*/
protected function _errorHandler($errno, $errstr, $errfile, $errline, array $errcontext)
{
$this->_error = array(
'errno' => $errno,
'errstr' => $errstr,
'errfile' => $errfile,
'errline' => $errline,
'errcontext' => $errcontext
);
}
/**
* Call Intercept for set($name, $field)
*
* @param string $method
* @param array $args
* @return Zend_Service_Audioscrobbler
*/
public function __call($method, $args)
{
if(substr($method, 0, 3) !== "set") {
require_once "Zend/Service/Exception.php";
throw new Zend_Service_Exception(
"Method ".$method." does not exist in class Zend_Service_Audioscrobbler."
);
}
$field = strtolower(substr($method, 3));
if(!is_array($args) || count($args) != 1) {
require_once "Zend/Service/Exception.php";
throw new Zend_Service_Exception(
"A value is required for setting a parameter field."
);
}
$this->set($field, $args[0]);
return $this;
}
}