|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00028 require_once 'Zend/Http/Client.php'; 00029 00030 00038 class Zend_Service_Audioscrobbler 00039 { 00046 protected $_client; 00047 00054 protected $_params; 00055 00062 protected $_error = null; 00063 00064 00068 public function __construct() 00069 { 00070 $this->set('version', '1.0'); 00071 00072 iconv_set_encoding('output_encoding', 'UTF-8'); 00073 iconv_set_encoding('input_encoding', 'UTF-8'); 00074 iconv_set_encoding('internal_encoding', 'UTF-8'); 00075 } 00076 00082 public function setHttpClient(Zend_Http_Client $client) 00083 { 00084 $this->_client = $client; 00085 } 00086 00092 public function getHttpClient() 00093 { 00094 if($this->_client == null) { 00095 $this->lazyLoadHttpClient(); 00096 } 00097 return $this->_client; 00098 } 00099 00105 protected function lazyLoadHttpClient() 00106 { 00107 $this->_client = new Zend_Http_Client(); 00108 } 00109 00116 public function get($field) 00117 { 00118 if (array_key_exists($field, $this->_params)) { 00119 return $this->_params[$field]; 00120 } else { 00121 return false; 00122 } 00123 } 00124 00132 public function set($field, $value) 00133 { 00134 $this->_params[$field] = urlencode($value); 00135 00136 return $this; 00137 } 00138 00149 protected function _getInfo($service, $params = null) 00150 { 00151 $service = (string) $service; 00152 $params = (string) $params; 00153 00154 if ($params === '') { 00155 $this->getHttpClient()->setUri("http://ws.audioscrobbler.com{$service}"); 00156 } else { 00157 $this->getHttpClient()->setUri("http://ws.audioscrobbler.com{$service}?{$params}"); 00158 } 00159 00160 $response = $this->getHttpClient()->request(); 00161 $responseBody = $response->getBody(); 00162 00163 if (preg_match('/No such path/', $responseBody)) { 00167 require_once 'Zend/Http/Client/Exception.php'; 00168 throw new Zend_Http_Client_Exception('Could not find: ' . $this->_client->getUri()); 00169 } elseif (preg_match('/No user exists with this name/', $responseBody)) { 00173 require_once 'Zend/Http/Client/Exception.php'; 00174 throw new Zend_Http_Client_Exception('No user exists with this name'); 00175 } elseif (!$response->isSuccessful()) { 00179 require_once 'Zend/Http/Client/Exception.php'; 00180 throw new Zend_Http_Client_Exception('The web service ' . $this->_client->getUri() . ' returned the following status code: ' . $response->getStatus()); 00181 } 00182 00183 set_error_handler(array($this, '_errorHandler')); 00184 00185 if (!$simpleXmlElementResponse = simplexml_load_string($responseBody)) { 00186 restore_error_handler(); 00190 require_once 'Zend/Service/Exception.php'; 00191 $exception = new Zend_Service_Exception('Response failed to load with SimpleXML'); 00192 $exception->error = $this->_error; 00193 $exception->response = $responseBody; 00194 throw $exception; 00195 } 00196 00197 restore_error_handler(); 00198 00199 return $simpleXmlElementResponse; 00200 } 00201 00207 public function userGetProfileInformation() 00208 { 00209 $service = "/{$this->get('version')}/user/{$this->get('user')}/profile.xml"; 00210 return $this->_getInfo($service); 00211 } 00212 00218 public function userGetTopArtists() 00219 { 00220 $service = "/{$this->get('version')}/user/{$this->get('user')}/topartists.xml"; 00221 return $this->_getInfo($service); 00222 } 00223 00229 public function userGetTopAlbums() 00230 { 00231 $service = "/{$this->get('version')}/user/{$this->get('user')}/topalbums.xml"; 00232 return $this->_getInfo($service); 00233 } 00234 00239 public function userGetTopTracks() 00240 { 00241 $service = "/{$this->get('version')}/user/{$this->get('user')}/toptracks.xml"; 00242 return $this->_getInfo($service); 00243 } 00244 00250 public function userGetTopTags() 00251 { 00252 $service = "/{$this->get('version')}/user/{$this->get('user')}/tags.xml"; 00253 return $this->_getInfo($service); 00254 } 00255 00261 public function userGetTopTagsForArtist() 00262 { 00263 $service = "/{$this->get('version')}/user/{$this->get('user')}/artisttags.xml"; 00264 $params = "artist={$this->get('artist')}"; 00265 return $this->_getInfo($service, $params); 00266 } 00267 00273 public function userGetTopTagsForAlbum() 00274 { 00275 $service = "/{$this->get('version')}/user/{$this->get('user')}/albumtags.xml"; 00276 $params = "artist={$this->get('artist')}&album={$this->get('album')}"; 00277 return $this->_getInfo($service, $params); 00278 } 00279 00285 public function userGetTopTagsForTrack() 00286 { 00287 $service = "/{$this->get('version')}/user/{$this->get('user')}/tracktags.xml"; 00288 $params = "artist={$this->get('artist')}&track={$this->get('track')}"; 00289 return $this->_getInfo($service, $params); 00290 } 00291 00296 public function userGetFriends() 00297 { 00298 $service = "/{$this->get('version')}/user/{$this->get('user')}/friends.xml"; 00299 return $this->_getInfo($service); 00300 } 00301 00307 public function userGetNeighbours() 00308 { 00309 $service = "/{$this->get('version')}/user/{$this->get('user')}/neighbours.xml"; 00310 return $this->_getInfo($service); 00311 } 00312 00318 public function userGetRecentTracks() 00319 { 00320 $service = "/{$this->get('version')}/user/{$this->get('user')}/recenttracks.xml"; 00321 return $this->_getInfo($service); 00322 } 00323 00329 public function userGetRecentBannedTracks() 00330 { 00331 $service = "/{$this->get('version')}/user/{$this->get('user')}/recentbannedtracks.xml"; 00332 return $this->_getInfo($service); 00333 } 00334 00340 public function userGetRecentLovedTracks() 00341 { 00342 $service = "/{$this->get('version')}/user/{$this->get('user')}/recentlovedtracks.xml"; 00343 return $this->_getInfo($service); 00344 } 00345 00353 public function userGetWeeklyChartList() 00354 { 00355 $service = "/{$this->get('version')}/user/{$this->get('user')}/weeklychartlist.xml"; 00356 return $this->_getInfo($service); 00357 } 00358 00359 00367 public function userGetWeeklyAlbumChart($from = NULL, $to = NULL) 00368 { 00369 $params = ""; 00370 00371 if ($from != NULL && $to != NULL) { 00372 $from = (int)$from; 00373 $to = (int)$to; 00374 $params = "from={$from}&to={$to}"; 00375 } 00376 00377 $service = "/{$this->get('version')}/user/{$this->get('user')}/weeklyalbumchart.xml"; 00378 return $this->_getInfo($service, $params); 00379 } 00380 00388 public function userGetWeeklyArtistChart($from = NULL, $to = NULL) 00389 { 00390 $params = ""; 00391 00392 if ($from != NULL && $to != NULL) { 00393 $from = (int)$from; 00394 $to = (int)$to; 00395 $params = "from={$from}&to={$to}"; 00396 } 00397 00398 $service = "/{$this->get('version')}/user/{$this->get('user')}/weeklyartistchart.xml"; 00399 return $this->_getInfo($service, $params); 00400 } 00401 00409 public function userGetWeeklyTrackChart($from = NULL, $to = NULL) 00410 { 00411 $params = ""; 00412 00413 if ($from != NULL && $to != NULL) { 00414 $from = (int)$from; 00415 $to = (int)$to; 00416 $params = "from={$from}&to={$to}"; 00417 } 00418 00419 $service = "/{$this->get('version')}/user/{$this->get('user')}/weeklytrackchart.xml"; 00420 return $this->_getInfo($service, $params); 00421 } 00422 00423 00429 public function artistGetRelatedArtists() 00430 { 00431 $service = "/{$this->get('version')}/artist/{$this->get('artist')}/similar.xml"; 00432 return $this->_getInfo($service); 00433 } 00434 00440 public function artistGetTopFans() 00441 { 00442 $service = "/{$this->get('version')}/artist/{$this->get('artist')}/fans.xml"; 00443 return $this->_getInfo($service); 00444 } 00445 00451 public function artistGetTopTracks() 00452 { 00453 $service = "/{$this->get('version')}/artist/{$this->get('artist')}/toptracks.xml"; 00454 return $this->_getInfo($service); 00455 } 00456 00462 public function artistGetTopAlbums() 00463 { 00464 $service = "/{$this->get('version')}/artist/{$this->get('artist')}/topalbums.xml"; 00465 return $this->_getInfo($service); 00466 } 00467 00473 public function artistGetTopTags() 00474 { 00475 $service = "/{$this->get('version')}/artist/{$this->get('artist')}/toptags.xml"; 00476 return $this->_getInfo($service); 00477 } 00478 00479 00485 public function albumGetInfo() 00486 { 00487 $service = "/{$this->get('version')}/album/{$this->get('artist')}/{$this->get('album')}/info.xml"; 00488 return $this->_getInfo($service); 00489 } 00490 00496 public function trackGetTopFans() 00497 { 00498 $service = "/{$this->get('version')}/track/{$this->get('artist')}/{$this->get('track')}/fans.xml"; 00499 return $this->_getInfo($service); 00500 } 00501 00507 public function trackGetTopTags() 00508 { 00509 $service = "/{$this->get('version')}/track/{$this->get('artist')}/{$this->get('track')}/toptags.xml"; 00510 return $this->_getInfo($service); 00511 } 00512 00518 public function tagGetTopTags() 00519 { 00520 $service = "/{$this->get('version')}/tag/toptags.xml"; 00521 return $this->_getInfo($service); 00522 } 00523 00529 public function tagGetTopAlbums() 00530 { 00531 $service = "/{$this->get('version')}/tag/{$this->get('tag')}/topalbums.xml"; 00532 return $this->_getInfo($service); 00533 } 00534 00540 public function tagGetTopArtists() 00541 { 00542 $service = "/{$this->get('version')}/tag/{$this->get('tag')}/topartists.xml"; 00543 return $this->_getInfo($service); 00544 } 00545 00551 public function tagGetTopTracks() 00552 { 00553 $service = "/{$this->get('version')}/tag/{$this->get('tag')}/toptracks.xml"; 00554 return $this->_getInfo($service); 00555 } 00556 00563 public function groupGetWeeklyChartList() 00564 { 00565 $service = "/{$this->get('version')}/group/{$this->get('group')}/weeklychartlist.xml"; 00566 return $this->_getInfo($service); 00567 } 00568 00576 public function groupGetWeeklyArtistChartList($from = NULL, $to = NULL) 00577 { 00578 00579 if ($from != NULL && $to != NULL) { 00580 $from = (int)$from; 00581 $to = (int)$to; 00582 $params = "from={$from}&$to={$to}"; 00583 } else { 00584 $params = ""; 00585 } 00586 00587 $service = "/{$this->get('version')}/group/{$this->get('group')}/weeklyartistchart.xml"; 00588 return $this->_getInfo($service, $params); 00589 } 00590 00598 public function groupGetWeeklyTrackChartList($from = NULL, $to = NULL) 00599 { 00600 if ($from != NULL && $to != NULL) { 00601 $from = (int)$from; 00602 $to = (int)$to; 00603 $params = "from={$from}&to={$to}"; 00604 } else { 00605 $params = ""; 00606 } 00607 00608 $service = "/{$this->get('version')}/group/{$this->get('group')}/weeklytrackchart.xml"; 00609 return $this->_getInfo($service, $params); 00610 } 00611 00619 public function groupGetWeeklyAlbumChartList($from = NULL, $to = NULL) 00620 { 00621 if ($from != NULL && $to != NULL) { 00622 $from = (int)$from; 00623 $to = (int)$to; 00624 $params = "from={$from}&to={$to}"; 00625 } else { 00626 $params = ""; 00627 } 00628 00629 $service = "/{$this->get('version')}/group/{$this->get('group')}/weeklyalbumchart.xml"; 00630 return $this->_getInfo($service, $params); 00631 } 00632 00643 protected function _errorHandler($errno, $errstr, $errfile, $errline, array $errcontext) 00644 { 00645 $this->_error = array( 00646 'errno' => $errno, 00647 'errstr' => $errstr, 00648 'errfile' => $errfile, 00649 'errline' => $errline, 00650 'errcontext' => $errcontext 00651 ); 00652 } 00653 00661 public function __call($method, $args) 00662 { 00663 if(substr($method, 0, 3) !== "set") { 00664 require_once "Zend/Service/Exception.php"; 00665 throw new Zend_Service_Exception( 00666 "Method ".$method." does not exist in class Zend_Service_Audioscrobbler." 00667 ); 00668 } 00669 $field = strtolower(substr($method, 3)); 00670 00671 if(!is_array($args) || count($args) != 1) { 00672 require_once "Zend/Service/Exception.php"; 00673 throw new Zend_Service_Exception( 00674 "A value is required for setting a parameter field." 00675 ); 00676 } 00677 $this->set($field, $args[0]); 00678 00679 return $this; 00680 } 00681 }