2
0

Audioscrobbler.php 20 KB

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