ArtistXML.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /* GNU FM -- a free network service for sharing your music listening habits
  3. Copyright (C) 2009 Free Software Foundation, Inc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. require_once($install_path . '/data/Artist.php');
  16. require_once('xml.php');
  17. /**
  18. * Class with functions that returns XML-formatted data for artists.
  19. *
  20. * These functions are mainly used by web service methods.
  21. *
  22. * @package API
  23. */
  24. class ArtistXML {
  25. /**
  26. * Provides artist information in XML format
  27. *
  28. * @param string $api_key A 32 character API key (currently not checked)
  29. * @param string $artistName The name of the artist to retrieve info for
  30. * @param string $mbid A music brainz ID (optional), if supplied this will be preferred to the artist name
  31. * @param string $lang A 2 character ISO 639 alpha-2 code indicating the language to return the information in
  32. * @return A SimpleXMLElement containing the artist's information
  33. */
  34. public static function getInfo($artistName, $api_key = false, $mbid = false, $lang = 'en') {
  35. // We assume $api_key is valid and set at this point
  36. if (!isset($artistName) && !isset($mbid)) {
  37. echo XML::error('failed', '7', 'Invalid resource specified');
  38. return;
  39. }
  40. try {
  41. $artist = new Artist($artistName, $mbid);
  42. } catch (Exception $e) {
  43. return XML::error('failed', '7', 'Invalid resource specified');
  44. }
  45. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  46. $artistXml = $xml->addChild('artist', null);
  47. $artistXml->addChild('name', $artist->name);
  48. $artistXml->addChild('mbid', $artist->mbid);
  49. $artistXml->addChild('url', $artist->getURL());
  50. $artistXml->addChild('streamable', $artist->streamable);
  51. $bio = $artistXml->addChild('bio', null);
  52. $bio->addChild('published', $artist->bio_published);
  53. $bio->addChild('summary', $artist->bio_summary);
  54. $bio->addChild('content', $artist->bio_content);
  55. return $xml;
  56. }
  57. public static function getTopTracks($artistname, $limit, $streamable, $page, $cache) {
  58. global $adodb;
  59. $offset = ($page - 1) * $limit;
  60. try {
  61. $artist = new Artist($artistname);
  62. $res = $artist->getTopTracks($limit, $offset, $streamable, null, null, $cache);
  63. } catch (Exception $e) {
  64. return XML::error('error', '7', 'Invalid resource specified');
  65. }
  66. // Get total track count, using subquery to get distinct row(artist, track) count
  67. $query = 'SELECT count(*) FROM (SELECT count(*) FROM Scrobbles s';
  68. if($streamable) {
  69. $query .= ' WHERE ROW(s.artist, s.track) IN (SELECT artist_name, name FROM Track WHERE streamable=1)';
  70. $andquery = True;
  71. } else {
  72. $query .= ' WHERE';
  73. $andquery = False;
  74. }
  75. $andquery ? $query .= ' AND' : null;
  76. $query .= ' artist=' . $adodb->qstr($artist->name) . ' GROUP BY s.track, s.artist) c';
  77. $total = $adodb->CacheGetOne($cache, $query);
  78. $totalPages = ceil($total/$limit);
  79. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  80. $root = $xml->addChild('toptracks', null);
  81. $root->addAttribute('artist', $artist->name);
  82. $root->addAttribute('page', $page);
  83. $root->addAttribute('perPage', $limit);
  84. $root->addAttribute('totalPages', $totalPages);
  85. $root->addAttribute('total', $total);
  86. $i = $offset + 1;
  87. foreach($res as &$row) {
  88. try {
  89. $track = new Track($row['track'], $row['artist']);
  90. $track_node = $root->addChild('track', null);
  91. $track_node->addAttribute('rank', $i);
  92. $track_node->addChild('name', repamp($track->name));
  93. $track_node->addChild('duration', $track->duration);
  94. $track_node->addChild('playcount', $row['freq']);
  95. $track_node->addChild('listeners', $row['listeners']);
  96. $track_node->addChild('mbid', $track->mbid);
  97. $track_node->addChild('url', repamp($row['trackurl']));
  98. $track_node->addChild('streamable', $track->streamable);
  99. $artist_node = $track_node->addChild('artist', null);
  100. $artist_node->addChild('name', repamp($artist->name));
  101. $artist_node->addChild('mbid', $artist->mbid);
  102. $artist_node->addChild('url', repamp($row['artisturl']));
  103. $image_small = $track_node->addChild('image', $artist->image_small);
  104. $image_small->addAttribute('size', 'small');
  105. $image_medium = $track_node->addChild('image', $artist->image_medium);
  106. $image_medium->addAttribute('size', 'medium');
  107. $image_large = $track_node->addChild('image', $artist->image_large);
  108. $image_large->addAttribute('size', 'large');
  109. } catch (Exception $e) {}
  110. $i++;
  111. }
  112. return $xml;
  113. }
  114. public static function getTopFans($artistname, $limit, $cache) {
  115. global $adodb;
  116. try {
  117. $artist = new Artist($artistname);
  118. $res = $artist->getTopListeners($limit, 0, False, null, null, $cache);
  119. } catch (Exception $e) {
  120. return XML::error('error', '7', 'Invalid resource specified');
  121. }
  122. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  123. $root = $xml->addChild('topfans', null);
  124. $root->addAttribute('artist', $artist->name);
  125. $i = $offset + 1;
  126. foreach($res as &$row) {
  127. try {
  128. $user = new User($row['username']);
  129. $user_node = $root->addChild('user', null);
  130. $user_node->addChild('name', $user->name);
  131. $user_node->addChild('realname', $user->fullname);
  132. $user_node->addChild('url', repamp($user->getURL()));
  133. $image_small = $user_node->addChild('image', null);
  134. $image_small->addAttribute('size', 'small');
  135. $image_medium = $user_node->addChild('image', null);
  136. $image_medium->addAttribute('size', 'medium');
  137. $image_large = $user_node->addChild('image', null);
  138. $image_large->addAttribute('size', 'large');
  139. $user_node->addChild('weight', $row['freq']);
  140. } catch (Exception $e) {}
  141. $i++;
  142. }
  143. return $xml;
  144. }
  145. public static function getTopTags($artistName, $limit, $cache) {
  146. try {
  147. $artist = new Artist($artistName);
  148. $res = $artist->getTopTags($limit, 0, $cache);
  149. } catch (Exception $e) {
  150. return(XML::error('failed', '7', 'Invalid resource specified'));
  151. }
  152. if(!$res) {
  153. return(XML::error('failed', '6', 'No tags for this artist'));
  154. }
  155. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  156. $root = $xml->addChild('toptags', null);
  157. $root->addAttribute('artist', $artist->name);
  158. foreach ($res as &$row) {
  159. $tag_node = $root->addChild('tag', null);
  160. $tag_node->addChild('name', repamp($row['tag']));
  161. $tag_node->addChild('count', $row['freq']);
  162. $tag_node->addChild('url', Server::getTagURL($row['tag']));
  163. }
  164. return $xml;
  165. }
  166. public static function getTags($artistName, $userid, $limit, $cache) {
  167. try {
  168. $artist = new Artist($artistName);
  169. $res = $artist->getTags($userid, $limit, 0, $cache);
  170. } catch (Exception $e) {
  171. return(XML::error('failed', '7', 'Invalid resource specified'));
  172. }
  173. if(!$res) {
  174. return(XML::error('failed', '6', 'No tags for this artist'));
  175. }
  176. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  177. $root = $xml->addChild('tags', null);
  178. $root->addAttribute('artist', repamp($artist->name));
  179. foreach($res as &$row) {
  180. $tag_node = $root->addChild('tag', null);
  181. $tag_node->addChild('name', repamp($row['tag']));
  182. $tag_node->addChild('url', Server::getTagURL($row['tag']));
  183. }
  184. return $xml;
  185. }
  186. public static function getFlattr($artistName) {
  187. try {
  188. $artist = new Artist($artistName);
  189. } catch (Exception $e) {
  190. return(XML::error('failed', '7', 'Invalid resource specified'));
  191. }
  192. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  193. $root = $xml->addChild('flattr', null);
  194. $root->addAttribute('artist', $artist->name);
  195. $root->addChild('flattr_uid', $artist->flattr_uid);
  196. return $xml;
  197. }
  198. public static function search($artistQuery, $streamable) {
  199. $res = Server::search($artistQuery, "artist", 40, $streamable);
  200. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  201. $root = $xml->addChild('results', null);
  202. $root->addAttribute('xmlns:opensearch', 'http://a9.com/-/spec/opensearch/1.1/');
  203. $root->addAttribute('for', $artistQuery);
  204. $matches = $root->addChild('artistmatches', null);
  205. foreach($res as &$row) {
  206. $artist = $matches->addChild('artist', null);
  207. $artist->addChild('name', repamp($row['name']));
  208. $artist->addChild('mbid', $row['mbid']);
  209. $artist->addChild('url', $row['url']);
  210. $artist->addChild('streamable', $row['streamable'] ? "1" : "0");
  211. $image_small = $artist->addChild('image', $row['image_small']);
  212. $image_small->addAttribute('size', 'small');
  213. $image_medium = $artist->addChild('image', $row['image_medium']);
  214. $image_medium->addAttribute('size', 'medium');
  215. $image_large = $artist->addChild('image', $row['image_large']);
  216. $image_large->addAttribute('size', 'large');
  217. }
  218. return $xml;
  219. }
  220. }