Audioscrobbler.php 20 KB

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