TagXML.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 . '/database.php');
  16. require_once($install_path . '/data/Tag.php');
  17. require_once($install_path . '/data/Server.php');
  18. require_once('xml.php');
  19. /**
  20. * Class with functions that returns XML-formatted data for tags.
  21. *
  22. * These functions are mainly used by web service methods.
  23. *
  24. * @package API
  25. */
  26. class TagXML {
  27. public static function getTopTags($limit, $cache) {
  28. try {
  29. $res = Tag::getTopTags($limit, 0, $cache);
  30. } catch (Exception $e) {
  31. return XML::error('error', '7', 'Invalid resource specified');
  32. }
  33. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  34. $root = $xml->addChild('toptags');
  35. foreach($res as &$row) {
  36. $tag_node = $root->addChild('tag');
  37. $tag_node->addChild('name', repamp($row['tag']));
  38. $tag_node->addChild('count', $row['freq']);
  39. $tag_node->addChild('url', repamp(Server::getTagURL($row['tag'])));
  40. }
  41. return $xml;
  42. }
  43. public static function getTopArtists($tag, $limit, $page, $streamable, $cache) {
  44. $offset = ($page - 1) * $limit;
  45. try {
  46. $res = Tag::getTopArtists($tag, $limit, $offset, $streamable, $cache);
  47. } catch (Exception $e) {
  48. return XML::error('error', '7', 'Invalid resource specified');
  49. }
  50. if(!$res) {
  51. return XML::error('error', '6', 'No tag with that name');
  52. }
  53. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  54. $root = $xml->addChild('topartists');
  55. $root->addAttribute('tag', repamp($tag));
  56. $i = $offset + 1;
  57. foreach($res as &$row) {
  58. $artist_node = $root->addChild('artist');
  59. $artist_node->addAttribute('rank', $i);
  60. $artist = new Artist($row['artist']);
  61. $artist_node->addChild('name', repamp($artist->name));
  62. $artist_node->addChild('mbid', $artist->mbid);
  63. $artist_node->addChild('url', $artist->getURL());
  64. $artist_node->addChild('streamable', $artist->streamable);
  65. $image_small = $artist_node->addChild('image', repamp($artist->image_small));
  66. $image_small->addAttribute('size', 'small');
  67. $image_medium = $artist_node->addChild('image', repamp($artist->image_medium));
  68. $image_medium->addAttribute('size', 'medium');
  69. $image_large = $artist_node->addChild('image', repamp($artist->image_large));
  70. $image_large->addAttribute('size', 'large');
  71. $i++;
  72. }
  73. return $xml;
  74. }
  75. public static function getTopAlbums($tag, $limit, $page, $streamable, $cache) {
  76. $offset = ($page - 1) * $limit;
  77. try {
  78. $res = Tag::getTopAlbums($tag, $limit, $offset, $streamable, $cache);
  79. } catch (Exception $e) {
  80. return XML::error('error', '7', 'Invalid resource specified');
  81. }
  82. if(!$res) {
  83. return XML::error('error', '6', 'No tag with that name');
  84. }
  85. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  86. $root = $xml->addChild('topalbums');
  87. $root->addAttribute('tag', repamp($tag));
  88. $i = $offset + 1;
  89. foreach($res as &$row) {
  90. $album_node = $root->addChild('album');
  91. $album_node->addAttribute('rank', $i);
  92. $album = new Album($row['album'], $row['artist']);
  93. $album_node->addChild('name', repamp($album->name));
  94. $album_node->addChild('mbid', $album->mbid);
  95. $album_node->addChild('url', $album->getURL());
  96. $artist = new Artist($album->artist_name);
  97. $artist_node = $album_node->addChild('artist');
  98. $artist_node->addChild('name', repamp($artist->name));
  99. $artist_node->addChild('mbid', $artist->mbid);
  100. $artist_node->addChild('url', repamp($artist->getURL()));
  101. $album_node->addChild('image', repamp($album->image));
  102. $i++;
  103. }
  104. return $xml;
  105. }
  106. public static function getTopTracks($tag, $limit, $page, $streamable, $cache) {
  107. $offset = ($page - 1) * $limit;
  108. try {
  109. $res = Tag::getTopTracks($tag, $limit, $offset, $streamable, $cache);
  110. } catch (Exception $e) {
  111. return XML::error('error', '7', 'Invalid resource specified');
  112. }
  113. if(!$res) {
  114. return XML::error('error', '6', 'No tag with that name');
  115. }
  116. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  117. $root = $xml->addChild('toptracks');
  118. $root->addAttribute('tag', repamp($tag));
  119. $i = $offset + 1;
  120. foreach($res as &$row) {
  121. $track_node = $root->addChild('track');
  122. $track_node->addAttribute('rank', $i);
  123. $track = new Track($row['track'], $row['artist']);
  124. $track_node->addChild('name', repamp($track->name));
  125. $track_node->addChild('duration', $track->duration);
  126. $track_node->addChild('mbid', $track->mbid);
  127. $track_node->addChild('url', $track->getURL());
  128. $track_node->addChild('streamable', $track->streamable);
  129. $artist = new Artist($track->artist_name);
  130. $artist_node = $track_node->addChild('artist');
  131. $artist_node->addChild('name', repamp($artist->name));
  132. $artist_node->addChild('mbid', $artist->mbid);
  133. $artist_node->addChild('url', $artist->getURL());
  134. $image_small = $track_node->addchild('image', $artist->image_small);
  135. $image_small->addAttribute('size', 'small');
  136. $image_medium = $track_node->addchild('image', $artist->image_medium);
  137. $image_medium->addAttribute('size', 'medium');
  138. $image_large = $track_node->addchild('image', $artist->image_large);
  139. $image_large->addAttribute('size', 'large');
  140. $i++;
  141. }
  142. return $xml;
  143. }
  144. public static function getInfo($tag, $cache) {
  145. try {
  146. $res = Tag::getInfo($tag, $cache);
  147. } catch (Exception $e) {
  148. return XML::error('error', '7', 'Invalid resource specified');
  149. }
  150. if(!$res) {
  151. return XML::error('error', '6', 'No tag with that name');
  152. }
  153. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  154. $root = $xml->addChild('tag');
  155. $root->addChild('name', repamp($tag));
  156. $root->addChild('url', repamp(Server::getTagURL($tag)));
  157. $root->addChild('taggings', $res[0]['freq']);
  158. return $xml;
  159. }
  160. }