PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/src/lastfmapi/Api/AlbumApi.php

https://github.com/matto1990/PHP-Last.fm-API
PHP | 270 lines | 194 code | 23 blank | 53 comment | 36 complexity | 4d9756e56f11718f85494edcc1f5db64 MD5 | raw file
  1. <?php
  2. namespace LastFmApi\Api;
  3. use LastFmApi\Exception\InvalidArgumentException;
  4. use LastFmApi\Exception\NotAuthenticatedException;
  5. use LastFmApi\Exception\NoResultsException;
  6. use SimpleXMLElement;
  7. /**
  8. * File that stores api calls for album api calls
  9. * @package apicalls
  10. */
  11. /**
  12. * Allows access to the api requests relating to albums
  13. */
  14. class AlbumApi extends BaseApi
  15. {
  16. /**
  17. * Tag an album using a list of user supplied tags. (Requires full auth)
  18. * @param array $methodVars An array with the following required values: <i>album</i>, <i>artist</i>, <i>tags</i>
  19. * @return boolean
  20. */
  21. public function addTags($methodVars)
  22. {
  23. // Only allow full authed calls
  24. if ($this->getFullAuth() === true) {
  25. // Check for required variables
  26. if (!empty($methodVars['album']) && !empty($methodVars['artist']) && !empty($methodVars['tags'])) {
  27. // If the tags variables is an array build a CS list
  28. if (is_array($methodVars['tags'])) {
  29. $tags = '';
  30. foreach ($methodVars['tags'] as $tag) {
  31. $tags .= $tag . ',';
  32. }
  33. $tags = substr($tags, 0, -1);
  34. } else {
  35. $tags = $methodVars['tags'];
  36. }
  37. $methodVars['tags'] = $tags;
  38. // Set the call variables
  39. $vars = array(
  40. 'method' => 'album.addtags',
  41. 'api_key' => $this->getAuth()->apiKey,
  42. 'sk' => $this->getAuth()->sessionKey
  43. );
  44. $vars = array_merge($vars, $methodVars);
  45. // Generate a call signiture
  46. $sig = $this->apiSig($this->getAuth()->apiSecret, $vars);
  47. $vars['api_sig'] = $sig;
  48. // Do the call and check for errors
  49. if ($call = $this->apiPostCall($vars)) {
  50. // If none return true
  51. return true;
  52. } else {
  53. // If there is return false
  54. return false;
  55. }
  56. } else {
  57. throw new InvalidArgumentException('You must include album, artist and tags variables in the call for this method');
  58. }
  59. } else {
  60. throw new NotAuthenticatedException('Method requires full auth. Call auth.getSession using lastfmApiAuth class');
  61. }
  62. }
  63. /**
  64. * Get the metadata for an album on Last.fm using the album name or a musicbrainz id
  65. * @param array $methodVars An array with the following required values: <i>album</i> and optional values: <i>artist</i>, <i>mbid</i>
  66. * @return array
  67. */
  68. public function getInfo($methodVars)
  69. {
  70. // Set the call variables
  71. $vars = array(
  72. 'method' => 'album.getinfo',
  73. 'api_key' => $this->getAuth()->apiKey
  74. );
  75. $vars = array_merge($vars, $methodVars);
  76. $info = array();
  77. $i = 0;
  78. if ($call = $this->apiGetCall($vars)) {
  79. $info['name'] = (string) $call->album->name;
  80. $info['artist'] = (string) $call->album->artist;
  81. $info['lastfmid'] = (string) $call->album->id;
  82. $info['mbid'] = (string) $call->album->mbid;
  83. $info['url'] = (string) $call->album->url;
  84. $info['releasedate'] = strtotime(trim((string) $call->album->releasedate));
  85. $info['image']['small'] = (string) $call->album->image;
  86. $info['image']['medium'] = (string) $call->album->image[1];
  87. $info['image']['large'] = (string) $call->album->image[2];
  88. $info['listeners'] = (string) $call->album->listeners;
  89. $info['playcount'] = (string) $call->album->playcount;
  90. foreach ($call->album->tags->tag as $tags) {
  91. $info['toptags'][$i]['name'] = (string) $tags->name;
  92. $info['toptags'][$i]['url'] = (string) $tags->url;
  93. $i++;
  94. }
  95. $i = 0;
  96. foreach ($call->album->tracks->track as $track) {
  97. $info['tracks'][$i]['name'] = (string) $track->name;
  98. $info['tracks'][$i]['url'] = (string) $track->url;
  99. $info['tracks'][$i]['duration'] = (string) $track->duration;
  100. $info['tracks'][$i]['streamable'] = (string) $track->streamable;
  101. $info['tracks'][$i]['rank'] = (string) $track['rank'];
  102. $info['tracks'][$i]['artist'] = array();
  103. $info['tracks'][$i]['artist']['name'] = $track->artist->name;
  104. $info['tracks'][$i]['artist']['mbid'] = $track->artist->mbid;
  105. $info['tracks'][$i]['artist']['url'] = $track->artist->url;
  106. $i++;
  107. }
  108. $info['wiki'] = array(
  109. 'summary' => (string) $call->album->wiki->summary,
  110. 'content' => (string) $call->album->wiki->content
  111. );
  112. return $info;
  113. } else {
  114. return false;
  115. }
  116. }
  117. /**
  118. * Get the tags applied by an individual user to an album on Last.fm
  119. * @param array $methodVars An array with the following required values: <i>album</i>, <i>artist</i>
  120. * @return array
  121. */
  122. public function getTags($methodVars)
  123. {
  124. // Only allow full authed calls
  125. if ($this->getFullAuth() === true) {
  126. // Check for required variables
  127. if (!empty($methodVars['album']) && !empty($methodVars['artist'])) {
  128. // Set the variables
  129. $vars = array(
  130. 'method' => 'album.gettags',
  131. 'api_key' => $this->getAuth()->apiKey,
  132. 'sk' => $this->getAuth()->sessionKey
  133. );
  134. $vars = array_merge($vars, $methodVars);
  135. // Generate a call signiture
  136. $sig = $this->apiSig($this->getAuth()->apiSecret, $vars);
  137. $vars['api_sig'] = $sig;
  138. $tags = array();
  139. // Make the call
  140. if ($call = $this->apiGetCall($vars)) {
  141. if (count($call->tags->tag) > 0) {
  142. $i = 0;
  143. foreach ($call->tags->tag as $tag) {
  144. $tags[$i]['name'] = (string) $tag->name;
  145. $tags[$i]['url'] = (string) $tag->url;
  146. $i++;
  147. }
  148. return $tags;
  149. } else {
  150. throw new NoResultsException('User has no tags for this artist');
  151. }
  152. } else {
  153. return false;
  154. }
  155. } else {
  156. throw new InvalidArgumentException('You must include album, artist and tags variables in the call for this method');
  157. }
  158. } else {
  159. throw new NotAuthenticatedException('Method requires full auth. Call auth.getSession using lastfmApiAuth class');
  160. }
  161. }
  162. /**
  163. * Remove a user's tag from an album. (Requires full auth)
  164. * @param array $methodVars An array with the following required values: <i>album</i>, <i>artist</i>, <i>tag</i>
  165. * @return boolean
  166. */
  167. public function removeTag($methodVars)
  168. {
  169. // Only allow full authed calls
  170. if ($this->getFullAuth() === true) {
  171. // Check for required variables
  172. if (!empty($methodVars['album']) && !empty($methodVars['artist']) && !empty($methodVars['tag'])) {
  173. // Set the variables
  174. $vars = array(
  175. 'method' => 'album.removetag',
  176. 'api_key' => $this->getAuth()->apiKey,
  177. 'sk' => $this->getAuth()->sessionKey
  178. );
  179. $vars = array_merge($vars, $methodVars);
  180. // Generate a call signature
  181. $sig = $this->apiSig($this->getAuth()->apiSecret, $vars);
  182. $vars['api_sig'] = $sig;
  183. // Do the call
  184. if ($call = $this->apiPostCall($vars)) {
  185. return true;
  186. } else {
  187. return false;
  188. }
  189. } else {
  190. throw new InvalidArgumentException('You must include album, artist and tags variables in the call for this method');
  191. }
  192. } else {
  193. throw new NotAuthenticatedException('Method requires full auth. Call auth.getSession using lastfmApiAuth class');
  194. }
  195. }
  196. /**
  197. * Search for an album by name. Returns album matches sorted by relevance
  198. * @param array $methodVars An array with the following required values: <i>album</i>
  199. * @return array
  200. */
  201. public function search($methodVars)
  202. {
  203. // Check for required variables
  204. if (!empty($methodVars['album'])) {
  205. $vars = array(
  206. 'method' => 'album.search',
  207. 'api_key' => $this->getAuth()->apiKey
  208. );
  209. $vars = array_merge($vars, $methodVars);
  210. $searchresults = array();
  211. if ($call = $this->apiGetCall($vars)) {
  212. $callNamespaces = $call->getDocNamespaces(true);
  213. // fix missing namespace (sic)
  214. if (!isset($callNamespaces['opensearch'])) {
  215. $call->results->addAttribute('xmlns:xmlns:opensearch', 'http://a9.com/-/spec/opensearch/1.1/');
  216. $call = new SimpleXMLElement($call->asXML());
  217. }
  218. $opensearch = $call->results->children('http://a9.com/-/spec/opensearch/1.1/');
  219. if ($opensearch->totalResults > 0) {
  220. $searchresults['totalResults'] = (string) $opensearch->totalResults;
  221. $searchresults['startIndex'] = (string) $opensearch->startIndex;
  222. $searchresults['itemsPerPage'] = (string) $opensearch->itemsPerPage;
  223. $i = 0;
  224. foreach ($call->results->albummatches->album as $album) {
  225. $searchresults['results'][$i]['name'] = (string) $album->name;
  226. $searchresults['results'][$i]['artist'] = (string) $album->artist;
  227. $searchresults['results'][$i]['id'] = (string) $album->id;
  228. $searchresults['results'][$i]['mbid'] = (string) $album->mbid;
  229. $searchresults['results'][$i]['url'] = (string) $album->url;
  230. $searchresults['results'][$i]['streamable'] = (string) $album->streamable;
  231. $searchresults['results'][$i]['image']['small'] = (string) $album->image[0];
  232. $searchresults['results'][$i]['image']['medium'] = (string) $album->image[1];
  233. $searchresults['results'][$i]['image']['large'] = (string) $album->image[2];
  234. $i++;
  235. }
  236. return $searchresults;
  237. } else {
  238. throw new NoResultsException('No results', 90);
  239. }
  240. } else {
  241. return false;
  242. }
  243. } else {
  244. throw new InvalidArgumentException('You must include album variable in the call for this method');
  245. }
  246. }
  247. }