Audioscrobbler.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage Audioscrobbler
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Http_Client
  24. */
  25. require_once 'Zend/Http/Client.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Service
  29. * @subpackage Audioscrobbler
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Service_Audioscrobbler
  34. {
  35. /**
  36. * Zend_Http_Client Object
  37. *
  38. * @var Zend_Http_Client
  39. * @access protected
  40. */
  41. protected $_client;
  42. /**
  43. * Array that contains parameters being used by the webservice
  44. *
  45. * @var array
  46. * @access protected
  47. */
  48. protected $_params;
  49. /**
  50. * Holds error information (e.g., for handling simplexml_load_string() warnings)
  51. *
  52. * @var array
  53. * @access protected
  54. */
  55. protected $_error = null;
  56. /**
  57. * Sets up character encoding, instantiates the HTTP client, and assigns the web service version.
  58. */
  59. public function __construct()
  60. {
  61. $this->set('version', '1.0');
  62. iconv_set_encoding('output_encoding', 'UTF-8');
  63. iconv_set_encoding('input_encoding', 'UTF-8');
  64. iconv_set_encoding('internal_encoding', 'UTF-8');
  65. }
  66. /**
  67. * Set Http Client
  68. *
  69. * @param Zend_Http_Client $client
  70. */
  71. public function setHttpClient(Zend_Http_Client $client)
  72. {
  73. $this->_client = $client;
  74. }
  75. /**
  76. * Get current http client.
  77. *
  78. * @return Zend_Http_Client
  79. */
  80. public function getHttpClient()
  81. {
  82. if($this->_client == null) {
  83. $this->lazyLoadHttpClient();
  84. }
  85. return $this->_client;
  86. }
  87. /**
  88. * Lazy load Http Client if none is instantiated yet.
  89. *
  90. * @return void
  91. */
  92. protected function lazyLoadHttpClient()
  93. {
  94. $this->_client = new Zend_Http_Client();
  95. }
  96. /**
  97. * Returns a field value, or false if the named field does not exist
  98. *
  99. * @param string $field
  100. * @return string|false
  101. */
  102. public function get($field)
  103. {
  104. if (array_key_exists($field, $this->_params)) {
  105. return $this->_params[$field];
  106. } else {
  107. return false;
  108. }
  109. }
  110. /**
  111. * Generic set action for a field in the parameters being used
  112. *
  113. * @param string $field name of field to set
  114. * @param string $value value to assign to the named field
  115. * @return Zend_Service_Audioscrobbler Provides a fluent interface
  116. */
  117. public function set($field, $value)
  118. {
  119. $this->_params[$field] = urlencode($value);
  120. return $this;
  121. }
  122. /**
  123. * Protected method that queries REST service and returns SimpleXML response set
  124. *
  125. * @param string $service name of Audioscrobbler service file we're accessing
  126. * @param string $params parameters that we send to the service if needded
  127. * @throws Zend_Http_Client_Exception
  128. * @throws Zend_Service_Exception
  129. * @return SimpleXMLElement result set
  130. * @access protected
  131. */
  132. protected function _getInfo($service, $params = null)
  133. {
  134. $service = (string) $service;
  135. $params = (string) $params;
  136. if ($params === '') {
  137. $this->getHttpClient()->setUri("http://ws.audioscrobbler.com{$service}");
  138. } else {
  139. $this->getHttpClient()->setUri("http://ws.audioscrobbler.com{$service}?{$params}");
  140. }
  141. $response = $this->getHttpClient()->request();
  142. $responseBody = $response->getBody();
  143. if (preg_match('/No such path/', $responseBody)) {
  144. /**
  145. * @see Zend_Http_Client_Exception
  146. */
  147. require_once 'Zend/Http/Client/Exception.php';
  148. throw new Zend_Http_Client_Exception('Could not find: ' . $this->_client->getUri());
  149. } elseif (preg_match('/No user exists with this name/', $responseBody)) {
  150. /**
  151. * @see Zend_Http_Client_Exception
  152. */
  153. require_once 'Zend/Http/Client/Exception.php';
  154. throw new Zend_Http_Client_Exception('No user exists with this name');
  155. } elseif (!$response->isSuccessful()) {
  156. /**
  157. * @see Zend_Http_Client_Exception
  158. */
  159. require_once 'Zend/Http/Client/Exception.php';
  160. throw new Zend_Http_Client_Exception('The web service ' . $this->_client->getUri() . ' returned the following status code: ' . $response->getStatus());
  161. }
  162. set_error_handler(array($this, '_errorHandler'));
  163. if (!$simpleXmlElementResponse = simplexml_load_string($responseBody)) {
  164. restore_error_handler();
  165. /**
  166. * @see Zend_Service_Exception
  167. */
  168. require_once 'Zend/Service/Exception.php';
  169. $exception = new Zend_Service_Exception('Response failed to load with SimpleXML');
  170. $exception->error = $this->_error;
  171. $exception->response = $responseBody;
  172. throw $exception;
  173. }
  174. restore_error_handler();
  175. return $simpleXmlElementResponse;
  176. }
  177. /**
  178. * Utility function to get Audioscrobbler profile information (eg: Name, Gender)
  179. *
  180. * @return array containing information
  181. */
  182. public function userGetProfileInformation()
  183. {
  184. $service = "/{$this->get('version')}/user/{$this->get('user')}/profile.xml";
  185. return $this->_getInfo($service);
  186. }
  187. /**
  188. * Utility function get this user's 50 most played artists
  189. *
  190. * @return array containing info
  191. */
  192. public function userGetTopArtists()
  193. {
  194. $service = "/{$this->get('version')}/user/{$this->get('user')}/topartists.xml";
  195. return $this->_getInfo($service);
  196. }
  197. /**
  198. * Utility function to get this user's 50 most played albums
  199. *
  200. * @return SimpleXMLElement object containing result set
  201. */
  202. public function userGetTopAlbums()
  203. {
  204. $service = "/{$this->get('version')}/user/{$this->get('user')}/topalbums.xml";
  205. return $this->_getInfo($service);
  206. }
  207. /**
  208. * Utility function to get this user's 50 most played tracks
  209. * @return SimpleXML object containing resut set
  210. */
  211. public function userGetTopTracks()
  212. {
  213. $service = "/{$this->get('version')}/user/{$this->get('user')}/toptracks.xml";
  214. return $this->_getInfo($service);
  215. }
  216. /**
  217. * Utility function to get this user's 50 most used tags
  218. *
  219. * @return SimpleXMLElement object containing result set
  220. */
  221. public function userGetTopTags()
  222. {
  223. $service = "/{$this->get('version')}/user/{$this->get('user')}/tags.xml";
  224. return $this->_getInfo($service);
  225. }
  226. /**
  227. * Utility function that returns the user's top tags used most used on a specific artist
  228. *
  229. * @return SimpleXMLElement object containing result set
  230. */
  231. public function userGetTopTagsForArtist()
  232. {
  233. $service = "/{$this->get('version')}/user/{$this->get('user')}/artisttags.xml";
  234. $params = "artist={$this->get('artist')}";
  235. return $this->_getInfo($service, $params);
  236. }
  237. /**
  238. * Utility function that returns this user's top tags for an album
  239. *
  240. * @return SimpleXMLElement object containing result set
  241. */
  242. public function userGetTopTagsForAlbum()
  243. {
  244. $service = "/{$this->get('version')}/user/{$this->get('user')}/albumtags.xml";
  245. $params = "artist={$this->get('artist')}&album={$this->get('album')}";
  246. return $this->_getInfo($service, $params);
  247. }
  248. /**
  249. * Utility function that returns this user's top tags for a track
  250. *
  251. * @return SimpleXMLElement object containing result set
  252. */
  253. public function userGetTopTagsForTrack()
  254. {
  255. $service = "/{$this->get('version')}/user/{$this->get('user')}/tracktags.xml";
  256. $params = "artist={$this->get('artist')}&track={$this->get('track')}";
  257. return $this->_getInfo($service, $params);
  258. }
  259. /**
  260. * Utility function that retrieves this user's list of friends
  261. * @return SimpleXMLElement object containing result set
  262. */
  263. public function userGetFriends()
  264. {
  265. $service = "/{$this->get('version')}/user/{$this->get('user')}/friends.xml";
  266. return $this->_getInfo($service);
  267. }
  268. /**
  269. * Utility function that returns a list of people with similar listening preferences to this user
  270. *
  271. * @return SimpleXMLElement object containing result set
  272. */
  273. public function userGetNeighbours()
  274. {
  275. $service = "/{$this->get('version')}/user/{$this->get('user')}/neighbours.xml";
  276. return $this->_getInfo($service);
  277. }
  278. /**
  279. * Utility function that returns a list of the 10 most recent tracks played by this user
  280. *
  281. * @return SimpleXMLElement object containing result set
  282. */
  283. public function userGetRecentTracks()
  284. {
  285. $service = "/{$this->get('version')}/user/{$this->get('user')}/recenttracks.xml";
  286. return $this->_getInfo($service);
  287. }
  288. /**
  289. * Utility function that returns a list of the 10 tracks most recently banned by this user
  290. *
  291. * @return SimpleXMLElement object containing result set
  292. */
  293. public function userGetRecentBannedTracks()
  294. {
  295. $service = "/{$this->get('version')}/user/{$this->get('user')}/recentbannedtracks.xml";
  296. return $this->_getInfo($service);
  297. }
  298. /**
  299. * Utility function that returns a list of the 10 tracks most recently loved by this user
  300. *
  301. * @return SimpleXMLElement object containing result set
  302. */
  303. public function userGetRecentLovedTracks()
  304. {
  305. $service = "/{$this->get('version')}/user/{$this->get('user')}/recentlovedtracks.xml";
  306. return $this->_getInfo($service);
  307. }
  308. /**
  309. * Utility function that returns a list of dates of available weekly charts for a this user
  310. *
  311. * Should actually be named userGetWeeklyChartDateList() but we have to follow audioscrobbler's naming
  312. *
  313. * @return SimpleXMLElement object containing result set
  314. */
  315. public function userGetWeeklyChartList()
  316. {
  317. $service = "/{$this->get('version')}/user/{$this->get('user')}/weeklychartlist.xml";
  318. return $this->_getInfo($service);
  319. }
  320. /**
  321. * Utility function that returns weekly album chart data for this user
  322. *
  323. * @param integer $from optional UNIX timestamp for start of date range
  324. * @param integer $to optional UNIX timestamp for end of date range
  325. * @return SimpleXMLElement object containing result set
  326. */
  327. public function userGetWeeklyAlbumChart($from = NULL, $to = NULL)
  328. {
  329. $params = "";
  330. if ($from != NULL && $to != NULL) {
  331. $from = (int)$from;
  332. $to = (int)$to;
  333. $params = "from={$from}&to={$to}";
  334. }
  335. $service = "/{$this->get('version')}/user/{$this->get('user')}/weeklyalbumchart.xml";
  336. return $this->_getInfo($service, $params);
  337. }
  338. /**
  339. * Utility function that returns weekly artist chart data for this user
  340. *
  341. * @param integer $from optional UNIX timestamp for start of date range
  342. * @param integer $to optional UNIX timestamp for end of date range
  343. * @return SimpleXMLElement object containing result set
  344. */
  345. public function userGetWeeklyArtistChart($from = NULL, $to = NULL)
  346. {
  347. $params = "";
  348. if ($from != NULL && $to != NULL) {
  349. $from = (int)$from;
  350. $to = (int)$to;
  351. $params = "from={$from}&to={$to}";
  352. }
  353. $service = "/{$this->get('version')}/user/{$this->get('user')}/weeklyartistchart.xml";
  354. return $this->_getInfo($service, $params);
  355. }
  356. /**
  357. * Utility function that returns weekly track chart data for this user
  358. *
  359. * @param integer $from optional UNIX timestamp for start of date range
  360. * @param integer $to optional UNIX timestamp for end of date range
  361. * @return SimpleXMLElement object containing result set
  362. */
  363. public function userGetWeeklyTrackChart($from = NULL, $to = NULL)
  364. {
  365. $params = "";
  366. if ($from != NULL && $to != NULL) {
  367. $from = (int)$from;
  368. $to = (int)$to;
  369. $params = "from={$from}&to={$to}";
  370. }
  371. $service = "/{$this->get('version')}/user/{$this->get('user')}/weeklytrackchart.xml";
  372. return $this->_getInfo($service, $params);
  373. }
  374. /**
  375. * Utility function that returns a list of artists similiar to this artist
  376. *
  377. * @return SimpleXMLElement object containing result set
  378. */
  379. public function artistGetRelatedArtists()
  380. {
  381. $service = "/{$this->get('version')}/artist/{$this->get('artist')}/similar.xml";
  382. return $this->_getInfo($service);
  383. }
  384. /**
  385. * Utility function that returns a list of this artist's top listeners
  386. *
  387. * @return SimpleXMLElement object containing result set
  388. */
  389. public function artistGetTopFans()
  390. {
  391. $service = "/{$this->get('version')}/artist/{$this->get('artist')}/fans.xml";
  392. return $this->_getInfo($service);
  393. }
  394. /**
  395. * Utility function that returns a list of this artist's top-rated tracks
  396. *
  397. * @return SimpleXMLElement object containing result set
  398. */
  399. public function artistGetTopTracks()
  400. {
  401. $service = "/{$this->get('version')}/artist/{$this->get('artist')}/toptracks.xml";
  402. return $this->_getInfo($service);
  403. }
  404. /**
  405. * Utility function that returns a list of this artist's top-rated albums
  406. *
  407. * @return SimpleXMLElement object containing result set
  408. */
  409. public function artistGetTopAlbums()
  410. {
  411. $service = "/{$this->get('version')}/artist/{$this->get('artist')}/topalbums.xml";
  412. return $this->_getInfo($service);
  413. }
  414. /**
  415. * Utility function that returns a list of this artist's top-rated tags
  416. *
  417. * @return SimpleXMLElement object containing result set
  418. */
  419. public function artistGetTopTags()
  420. {
  421. $service = "/{$this->get('version')}/artist/{$this->get('artist')}/toptags.xml";
  422. return $this->_getInfo($service);
  423. }
  424. /**
  425. * Get information about an album
  426. *
  427. * @return SimpleXMLElement
  428. */
  429. public function albumGetInfo()
  430. {
  431. $service = "/{$this->get('version')}/album/{$this->get('artist')}/{$this->get('album')}/info.xml";
  432. return $this->_getInfo($service);
  433. }
  434. /**
  435. * Get top fans of the current track.
  436. *
  437. * @return SimpleXMLElement
  438. */
  439. public function trackGetTopFans()
  440. {
  441. $service = "/{$this->get('version')}/track/{$this->get('artist')}/{$this->get('track')}/fans.xml";
  442. return $this->_getInfo($service);
  443. }
  444. /**
  445. * Get top tags of the current track.
  446. *
  447. * @return SimpleXMLElement
  448. */
  449. public function trackGetTopTags()
  450. {
  451. $service = "/{$this->get('version')}/track/{$this->get('artist')}/{$this->get('track')}/toptags.xml";
  452. return $this->_getInfo($service);
  453. }
  454. /**
  455. * Get Top Tags.
  456. *
  457. * @return SimpleXMLElement
  458. */
  459. public function tagGetTopTags()
  460. {
  461. $service = "/{$this->get('version')}/tag/toptags.xml";
  462. return $this->_getInfo($service);
  463. }
  464. /**
  465. * Get top albums by current tag.
  466. *
  467. * @return SimpleXMLElement
  468. */
  469. public function tagGetTopAlbums()
  470. {
  471. $service = "/{$this->get('version')}/tag/{$this->get('tag')}/topalbums.xml";
  472. return $this->_getInfo($service);
  473. }
  474. /**
  475. * Get top artists by current tag.
  476. *
  477. * @return SimpleXMLElement
  478. */
  479. public function tagGetTopArtists()
  480. {
  481. $service = "/{$this->get('version')}/tag/{$this->get('tag')}/topartists.xml";
  482. return $this->_getInfo($service);
  483. }
  484. /**
  485. * Get Top Tracks by currently set tag.
  486. *
  487. * @return SimpleXMLElement
  488. */
  489. public function tagGetTopTracks()
  490. {
  491. $service = "/{$this->get('version')}/tag/{$this->get('tag')}/toptracks.xml";
  492. return $this->_getInfo($service);
  493. }
  494. /**
  495. * Get weekly chart list by current set group.
  496. *
  497. * @see set()
  498. * @return SimpleXMLElement
  499. */
  500. public function groupGetWeeklyChartList()
  501. {
  502. $service = "/{$this->get('version')}/group/{$this->get('group')}/weeklychartlist.xml";
  503. return $this->_getInfo($service);
  504. }
  505. /**
  506. * Retrieve weekly Artist Charts
  507. *
  508. * @param int $from
  509. * @param int $to
  510. * @return SimpleXMLElement
  511. */
  512. public function groupGetWeeklyArtistChartList($from = NULL, $to = NULL)
  513. {
  514. if ($from != NULL && $to != NULL) {
  515. $from = (int)$from;
  516. $to = (int)$to;
  517. $params = "from={$from}&$to={$to}";
  518. } else {
  519. $params = "";
  520. }
  521. $service = "/{$this->get('version')}/group/{$this->get('group')}/weeklyartistchart.xml";
  522. return $this->_getInfo($service, $params);
  523. }
  524. /**
  525. * Retrieve Weekly Track Charts
  526. *
  527. * @param int $from
  528. * @param int $to
  529. * @return SimpleXMLElement
  530. */
  531. public function groupGetWeeklyTrackChartList($from = NULL, $to = NULL)
  532. {
  533. if ($from != NULL && $to != NULL) {
  534. $from = (int)$from;
  535. $to = (int)$to;
  536. $params = "from={$from}&to={$to}";
  537. } else {
  538. $params = "";
  539. }
  540. $service = "/{$this->get('version')}/group/{$this->get('group')}/weeklytrackchart.xml";
  541. return $this->_getInfo($service, $params);
  542. }
  543. /**
  544. * Retrieve Weekly album charts.
  545. *
  546. * @param int $from
  547. * @param int $to
  548. * @return SimpleXMLElement
  549. */
  550. public function groupGetWeeklyAlbumChartList($from = NULL, $to = NULL)
  551. {
  552. if ($from != NULL && $to != NULL) {
  553. $from = (int)$from;
  554. $to = (int)$to;
  555. $params = "from={$from}&to={$to}";
  556. } else {
  557. $params = "";
  558. }
  559. $service = "/{$this->get('version')}/group/{$this->get('group')}/weeklyalbumchart.xml";
  560. return $this->_getInfo($service, $params);
  561. }
  562. /**
  563. * Saves the provided error information to this instance
  564. *
  565. * @param integer $errno
  566. * @param string $errstr
  567. * @param string $errfile
  568. * @param integer $errline
  569. * @param array $errcontext
  570. * @return void
  571. */
  572. protected function _errorHandler($errno, $errstr, $errfile, $errline, array $errcontext)
  573. {
  574. $this->_error = array(
  575. 'errno' => $errno,
  576. 'errstr' => $errstr,
  577. 'errfile' => $errfile,
  578. 'errline' => $errline,
  579. 'errcontext' => $errcontext
  580. );
  581. }
  582. /**
  583. * Call Intercept for set($name, $field)
  584. *
  585. * @param string $method
  586. * @param array $args
  587. * @return Zend_Service_Audioscrobbler
  588. */
  589. public function __call($method, $args)
  590. {
  591. if(substr($method, 0, 3) !== "set") {
  592. require_once "Zend/Service/Exception.php";
  593. throw new Zend_Service_Exception(
  594. "Method ".$method." does not exist in class Zend_Service_Audioscrobbler."
  595. );
  596. }
  597. $field = strtolower(substr($method, 3));
  598. if(!is_array($args) || count($args) != 1) {
  599. require_once "Zend/Service/Exception.php";
  600. throw new Zend_Service_Exception(
  601. "A value is required for setting a parameter field."
  602. );
  603. }
  604. $this->set($field, $args[0]);
  605. return $this;
  606. }
  607. }