UserXML.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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/User.php');
  17. require_once('xml.php');
  18. /**
  19. * Class with functions that returns XML-formatted data for users.
  20. *
  21. * These functions are mainly used by web service methods.
  22. *
  23. * @package API
  24. */
  25. class UserXML {
  26. public static function getInfo($username) {
  27. try {
  28. $user = new User($username);
  29. } catch (Exception $e) {
  30. return XML::error('failed', '7', 'Invalid resource specified');
  31. }
  32. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  33. $user_node = $xml->addChild('user', null);
  34. $user_node->addChild('name', $user->name);
  35. $user_node->addChild('realname', $user->fullname);
  36. $user_node->addChild('homepage', $user->homepage);
  37. $user_node->addChild('location', $user->location);
  38. $ic = $user_node->addChild('image', repamp($user->getAvatar(34)));
  39. $ic->addAttribute('size', 'small');
  40. $ic = $user_node->addChild('image', repamp($user->getAvatar(64)));
  41. $ic->addAttribute('size', 'medium');
  42. $ic = $user_node->addChild('image', repamp($user->getAvatar(126)));
  43. $ic->addAttribute('size', 'large');
  44. $ic = $user_node->addChild('image', repamp($user->getAvatar(252)));
  45. $ic->addAttribute('size', 'extralarge');
  46. $user_node->addChild('bio', $user->bio);
  47. $user_node->addChild('url', $user->getURL());
  48. $user_node->addChild('playcount', $user->getTotalTracks());
  49. $user_node->addChild('profile_created', strftime('%c', $user->created));
  50. if (isset($user->modified)) {
  51. $user_node->addChild('profile_updated', strftime('%c', $user->modified));
  52. }
  53. return $xml;
  54. }
  55. public static function getTopArtists($username, $limit, $streamable, $page, $period, $cache) {
  56. global $adodb;
  57. try {
  58. $timestamp = UserXML::_periodToTimestamp($period);
  59. } catch (Exception $e) {
  60. return(XML::error('error', '13', 'Invalid method signature supplied'));
  61. }
  62. $offset = ($page - 1) * $limit;
  63. $begin = $timestamp - ($timestamp % 3600);
  64. try {
  65. $user = new User($username);
  66. $res = $user->getTopArtists($limit, $offset, $streamable, $begin, null, $cache);
  67. } catch (Exception $e) {
  68. return XML::error('error', '7', 'Invalid resource specified');
  69. }
  70. $query = 'SELECT COUNT(DISTINCT(artist)) FROM Scrobbles s';
  71. if($streamable) {
  72. $query .= ' INNER JOIN Artist a ON s.artist=a.name WHERE a.streamable=1';
  73. $andquery = True;
  74. } else {
  75. $query .= ' WHERE';
  76. $andquery = False;
  77. }
  78. if ($begin) {
  79. $andquery ? $query .= ' AND' : $andquery = True;
  80. $query .= ' time>' . $begin;
  81. }
  82. $andquery ? $query .= ' AND' : null;
  83. $query .= ' userid=' . $user->uniqueid;
  84. $total = $adodb->CacheGetOne($cache, $query);
  85. $totalPages = ceil($total/$limit);
  86. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  87. $root = $xml->addChild('topartists', null);
  88. $root->addAttribute('user', $user->name);
  89. $root->addAttribute('type', $period);
  90. $root->addAttribute('page', $page);
  91. $root->addAttribute('perPage', $limit);
  92. $root->addAttribute('totalPages', $totalPages);
  93. $root->addAttribute('total', $total);
  94. $i = $offset + 1;
  95. foreach($res as &$row) {
  96. $artist_node = $root->addChild('artist', null);
  97. $artist_node->addAttribute('rank', $i);
  98. $artist_node->addChild('name', repamp($row['artist']));
  99. $artist_node->addChild('playcount', $row['freq']);
  100. try {
  101. $artist = new Artist($row['artist']);
  102. $artist_node->addChild('mbid', $artist->mbid);
  103. $artist_node->addChild('url', $artist->geturl());
  104. $artist_node->addChild('streamable', $artist->streamable);
  105. $image_small = $artist_node->addChild('image', $artist->image_small);
  106. $image_small->addAttribute('size', 'small');
  107. $image_medium = $artist_node->addChild('image', $artist->image_medium);
  108. $image_medium->addAttribute('size', 'medium');
  109. $image_large = $artist_node->addChild('image', $artist->image_large);
  110. $image_large->addAttribute('size', 'large');
  111. } catch (Exception $e) {}
  112. $i++;
  113. }
  114. return $xml;
  115. }
  116. public static function getTopTracks($username, $limit, $streamable, $page, $period, $cache) {
  117. global $adodb;
  118. try {
  119. $timestamp = UserXML::_periodToTimestamp($period);
  120. } catch (Exception $e) {
  121. return(XML::error('error', '13', 'Invalid method signature supplied'));
  122. }
  123. $offset = ($page - 1) * $limit;
  124. $begin = $timestamp - ($timestamp % 3600);
  125. try {
  126. $user = new User($username);
  127. $res = $user->getTopTracks($limit, $offset, $streamable, $begin, null, $cache);
  128. } catch (Exception $e) {
  129. return XML::error('error', '7', 'Invalid resource specified');
  130. }
  131. // Get total track count, using subquery to get distinct row(artist, track) count
  132. $query = 'SELECT count(*) FROM (SELECT count(*) FROM Scrobbles s';
  133. if($streamable) {
  134. $query .= ' WHERE ROW(s.artist, s.track) IN (SELECT artist_name, name FROM Track WHERE streamable=1)';
  135. $andquery = True;
  136. } else {
  137. $query .= ' WHERE';
  138. $andquery = False;
  139. }
  140. if ($begin) {
  141. $andquery ? $query .= ' AND' : $andquery = True;
  142. $query .= ' time>' . $begin;
  143. }
  144. $andquery ? $query .= ' AND' : null;
  145. $query .= ' userid=' . $user->uniqueid . ' GROUP BY s.track, s.artist) c';
  146. $total = $adodb->CacheGetOne($cache, $query);
  147. $totalPages = ceil($total/$limit);
  148. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  149. $root = $xml->addChild('toptracks', null);
  150. $root->addAttribute('user', $user->name);
  151. $root->addAttribute('type', $period);
  152. $root->addAttribute('page', $page);
  153. $root->addAttribute('perPage', $limit);
  154. $root->addAttribute('totalPages', $totalPages);
  155. $root->addAttribute('total', $total);
  156. $i = $offset + 1;
  157. foreach($res as &$row) {
  158. try {
  159. $track = new Track($row['track'], $row['artist']);
  160. $artist = $track->getArtist();
  161. $track_node = $root->addChild('track', null);
  162. $track_node->addAttribute('rank', $i);
  163. $track_node->addChild('name', repamp($track->name));
  164. $track_node->addChild('duration', $track->duration);
  165. $track_node->addChild('playcount', $row['freq']);
  166. $track_node->addChild('mbid', $track->mbid);
  167. $track_node->addChild('url', repamp($row['trackurl']));
  168. $track_node->addChild('streamable', $track->streamable);
  169. $artist_node = $track_node->addChild('artist', null);
  170. $artist_node->addChild('name', repamp($artist->name));
  171. $artist_node->addChild('mbid', $artist->mbid);
  172. $artist_node->addChild('url', repamp($row['artisturl']));
  173. $image_small = $track_node->addChild('image', $artist->image_small);
  174. $image_small->addAttribute('size', 'small');
  175. $image_medium = $track_node->addChild('image', $artist->image_medium);
  176. $image_medium->addAttribute('size', 'medium');
  177. $image_large = $track_node->addChild('image', $artist->image_large);
  178. $image_large->addAttribute('size', 'large');
  179. } catch (Exception $e) {}
  180. $i++;
  181. }
  182. return $xml;
  183. }
  184. public static function getRecentTracks($u, $limit, $page, $from, $to) {
  185. global $adodb;
  186. if (!isset($limit)) {
  187. $limit = 10;
  188. }
  189. $offset = ($page - 1) * $limit;
  190. $err = 0;
  191. try {
  192. $user = new User($u);
  193. if ($page == 1) {
  194. $npres = $user->getNowPlaying(1);
  195. }
  196. $res = $user->getScrobbles($limit, $offset, $from, $to);
  197. } catch (Exception $e) {
  198. $err = 1;
  199. }
  200. if ($err || !$res) {
  201. return(XML::error('error', '7', 'Invalid resource specified'));
  202. }
  203. $totalTracks = $user->getTotalTracks();
  204. $totalPages = ceil($totalTracks / $limit);
  205. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  206. $root = $xml->addChild('recenttracks', null);
  207. $root->addAttribute('user', $user->name);
  208. $root->addAttribute('page', $page);
  209. $root->addAttribute('perPage', $limit);
  210. $root->addAttribute('totalPages', $totalPages);
  211. if ($npres) {
  212. foreach ($npres as &$row) {
  213. $track = $root->addChild('track');
  214. $track->addAttribute('nowplaying', 'true');
  215. $row['time'] = time();
  216. UserXML::_addTrackDetails($track, $row);
  217. }
  218. }
  219. foreach ($res as &$row) {
  220. $track = $root->addChild('track', null);
  221. UserXML::_addTrackDetails($track, $row);
  222. }
  223. return $xml;
  224. }
  225. private static function _addTrackDetails($track, $row) {
  226. $artist = $track->addChild('artist', repamp($row['artist']));
  227. $artist->addAttribute('mbid', $row['artist_mbid']);
  228. $name = $track->addChild('name', repamp($row['track']));
  229. $track->addChild('mbid', $row['mbid']);
  230. $album = $track->addChild('album', repamp($row['album']));
  231. $album->addAttribute('mbid', $row['album_mbid']);
  232. $track->addChild('url', repamp(Server::getTrackURL($row['artist'], $row['album'], $row['track'])));
  233. $date = $track->addChild('date', gmdate('d M Y H:i', $row['time']) . ' GMT');
  234. $date->addAttribute('uts', $row['time']);
  235. $track->addChild('streamable', null);
  236. }
  237. private static function _periodToTimestamp($period) {
  238. //TODO: Do better, this is too ugly :\
  239. if (strcmp($period, 'overall') == 0) {
  240. $timestamp = 0;
  241. } else if (strcmp($period, '7day') == 0) {
  242. $timestamp = strtotime('-7 days');
  243. } else if (strcmp($period, '1month') == 0) {
  244. $timestamp = strtotime('-1 month');
  245. } else if (strcmp($period, '3month') == 0) {
  246. $timestamp = strtotime('-3 months');
  247. } else if (strcmp($period, '6month') == 0) {
  248. $timestamp = strtotime('-6 months');
  249. } else if (strcmp($period, '12month') == 0) {
  250. $timestamp = strtotime('-12 months');
  251. } else {
  252. throw new Exception("Not a valid period");
  253. }
  254. return $timestamp;
  255. }
  256. public static function getTopTags($u, $limit, $cache) {
  257. global $base_url;
  258. try {
  259. $user = new User($u);
  260. $res = $user->getTopTags($limit, 0, $cache);
  261. } catch (Exception $e) {
  262. return XML::error('error', '7', 'Invalid resource specified');
  263. }
  264. if(!$res) {
  265. return XML::error('error', '6', 'No tags for this user');
  266. }
  267. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  268. $root = $xml->addChild('toptags');
  269. $root->addAttribute('user', $user->name);
  270. foreach ($res as &$row) {
  271. $tag = $root->addChild('tag', null);
  272. $tag->addChild('name', repamp($row['tag']));
  273. $tag->addChild('count', repamp($row['freq']));
  274. $tag->addChild('url', Server::getTagURL($row['tag']));
  275. }
  276. return $xml;
  277. }
  278. public static function getPersonalTags($u, $tag, $taggingtype, $limit, $page, $cache, $streamable) {
  279. $offset = ($page - 1) * $limit;
  280. try {
  281. $user = new User($u);
  282. $res = $user->getPersonalTags($tag, $taggingtype, $limit, $offset, $cache, $streamable);
  283. } catch (Exception $e) {
  284. return XML::error('error', '7', 'Invalid resource specified');
  285. }
  286. if(!$res) {
  287. return XML::error('error', '6', 'No tag with this name');
  288. }
  289. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  290. $root = $xml->addChild('taggings');
  291. $root->addAttribute('user', repamp($user->name));
  292. $root->addAttribute('tag', repamp($tag));
  293. $root->addAttribute('page', $page);
  294. $root->addAttribute('perPage', $limit);
  295. $root->addAttribute('totalPages', null); //TODO
  296. $root->addAttribute('total', null); //TODO
  297. if(strtolower($taggingtype) == 'artist') {
  298. $artists = $root->addChild('artists', null);
  299. foreach($res as &$row) {
  300. $artist = new Artist($row['artist']);
  301. $artist_node = $artists->addChild('artist', null);
  302. $artist_node->addChild('name', repamp($artist->name));
  303. $artist_node->addChild('mbid', $artist->mbid);
  304. $artist_node->addChild('url', $artist->getURL());
  305. $artist_node->addChild('streamable', $artist->streamable);
  306. $image_small = $artist_node->addchild('image', $artist->image_small);
  307. $image_small->addAttribute('size', 'small');
  308. $image_medium = $artist_node->addchild('image', $artist->image_medium);
  309. $image_medium->addAttribute('size', 'medium');
  310. $image_large = $artist_node->addchild('image', $artist->image_large);
  311. $image_large->addAttribute('size', 'large');
  312. }
  313. }elseif(strtolower($taggingtype) == 'album') {
  314. $albums = $root->addChild('albums', null);
  315. foreach($res as &$row) {
  316. $album = new Album($row['album'], $row['artist']);
  317. $album_node = $albums->addChild('album', null);
  318. $album_node->addChild('name', repamp($album->name));
  319. $album_node->addChild('mbid', $album->mbid);
  320. $album_node->addChild('url', $album->getURL());
  321. $artist = new Artist($row['artist']);
  322. $artist_node = $album_node->addChild('artist', null);
  323. $artist_node->addChild('name', repamp($artist->name));
  324. $artist_node->addChild('mbid', $artist->mbid);
  325. $artist_node->addChild('url', $artist->getURL());
  326. $album_node->addChild('image', $album->image);
  327. }
  328. }elseif(strtolower($taggingtype) == 'track') {
  329. $tracks = $root->addChild('tracks', null);
  330. foreach($res as &$row) {
  331. $track = new Track($row['track'], $row['artist']);
  332. $track_node = $tracks->addChild('track', null);
  333. $track_node->addChild('name', repamp($track->name));
  334. $track_node->addChild('duration', $track->duration);
  335. $track_node->addChild('mbid', $track->mbid);
  336. $track_node->addChild('url', $track->getURL());
  337. $track_node->addChild('streamable', $track->streamable);
  338. $artist = new Artist($row['artist']);
  339. $artist_node = $track_node->addChild('artist', null);
  340. $artist_node->addChild('name', repamp($artist->name));
  341. $artist_node->addChild('mbid', $artist->mbid);
  342. $artist_node->addChild('url', $artist->getURL());
  343. $image_small = $track_node->addchild('image', $artist->image_small);
  344. $image_small->addAttribute('size', 'small');
  345. $image_medium = $track_node->addchild('image', $artist->image_medium);
  346. $image_medium->addAttribute('size', 'medium');
  347. $image_large = $track_node->addchild('image', $artist->image_large);
  348. $image_large->addAttribute('size', 'large');
  349. }
  350. } else {
  351. return XML::error('error', '7', 'Invalid resource specified');
  352. }
  353. return $xml;
  354. }
  355. public static function getTagInfo($u, $tag, $cache) {
  356. try {
  357. $user = new User($u);
  358. $res = $user->getTagInfo($tag, $cache);
  359. } catch (Exception $e) {
  360. return XML::error('error', '7', 'Invalid resource specified');
  361. }
  362. if(!$res) {
  363. return XML::error('error', '6', 'No tag with that name');
  364. }
  365. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  366. $root = $xml->addChild('tag');
  367. $root->addChild('name', repamp($tag));
  368. $root->addChild('url', Server::getTagURL($tag));
  369. $root->addChild('taggings', $res[0]['freq']);
  370. return $xml;
  371. }
  372. public static function getLovedTracks($username, $limit = 50, $page = 1, $streamable = False, $cache = 600) {
  373. global $adodb;
  374. $offset = ($page - 1) * $limit;
  375. try {
  376. $user = new User($username);
  377. $res = $user->getLovedTracks($limit, $offset, $streamable, null, $cache);
  378. } catch (Exception $ex) {
  379. return XML::error('error', '7', 'Invalid resource specified');
  380. }
  381. // Get total track count, using subquery to get distinct row(artist, track) count
  382. $query = 'SELECT COUNT(*) FROM (SELECT count(*) FROM Loved_Tracks lt';
  383. if($streamable) {
  384. $query .= ' WHERE ROW(lt.artist, lt.track) IN (SELECT artist_name, name FROM Track WHERE streamable=1)';
  385. $andquery = True;
  386. } else {
  387. $query .= ' WHERE';
  388. $andquery = False;
  389. }
  390. $andquery ? $query .= ' AND' : null;
  391. $query .= ' userid=' . $user->uniqueid . ' GROUP BY lt.track, lt.artist) c';
  392. $total = $adodb->CacheGetOne($cache, $query);
  393. $totalPages = ceil($total/$limit);
  394. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  395. $root = $xml->addChild('lovedtracks');
  396. $root->addAttribute('user', $user->name);
  397. $root->addAttribute('page', $page);
  398. $root->addAttribute('perPage', $limit);
  399. $root->addAttribute('totalPages', $totalPages);
  400. $root->addAttribute('total', $total);
  401. foreach ($res as &$row) {
  402. $track_node = $root->addChild('track', null);
  403. UserXML::_addLBTrackDetails($track_node, $row);
  404. }
  405. return $xml;
  406. }
  407. public static function getBannedTracks($u, $limit = 50, $page = 1) {
  408. global $adodb;
  409. $offset = ($page - 1) * $limit;
  410. try {
  411. $user = new User($u);
  412. $res = $user->getBannedTracks($limit, $offset);
  413. } catch (Exception $ex) {
  414. return XML::error('error', '7', 'Invalid resource specified');
  415. }
  416. $totalPages = $adodb->GetOne('SELECT COUNT(track) FROM Banned_Tracks WHERE userid = ' . $user->uniqueid);
  417. $totalPages = ceil($totalPages / $limit);
  418. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  419. $root = $xml->addChild('bannedtracks');
  420. $root->addAttribute('user', $user->name);
  421. $root->addAttribute('page', $page);
  422. $root->addAttribute('perPage', $limit);
  423. $root->addAttribute('totalPages', $totalPages);
  424. foreach ($res as &$row) {
  425. $track_node = $root->addChild('track', null);
  426. UserXML::_addLBTrackDetails($track_node, $row);
  427. }
  428. return $xml;
  429. }
  430. private static function _addLBTrackDetails($track_node, $row) {
  431. $track = new Track($row['track'], $row['artist']);
  432. $track_node->addChild('name', repamp($track->name));
  433. $track_node->addChild('mbid', $track->mbid);
  434. $track_node->addChild('url', $track->getURL());
  435. $date = $track_node->addChild('date', gmdate('d M Y H:i', $row['time']) . ' GMT');
  436. $date->addAttribute('uts', $row['time']);
  437. try {
  438. $artist = new Artist($row['artist']);
  439. $artist_node = $track_node->addChild('artist', null);
  440. $artist_node->addChild('name', repamp($artist->name));
  441. $artist_node->addChild('mbid', $artist->mbid);
  442. $artist_node->addChild('url', $artist->getURL());
  443. } catch (Exception $e) {}
  444. }
  445. public static function getNeighbours($u, $limit = 50) {
  446. try {
  447. $user = new User($u);
  448. $res = $user->getNeighbours($limit);
  449. } catch (Exception $e) {
  450. return XML::error('error', '7', 'Invalid resource specified');
  451. }
  452. $xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
  453. $root = $xml->addChild('neighbours');
  454. $root->addAttribute('user', $user->name);
  455. if (empty($res)) {
  456. return $xml;
  457. }
  458. $highest_match = $res[0]['shared_artists'];
  459. foreach ($res as $row) {
  460. $neighbour = $row['user'];
  461. $user_node = $root->addChild('user', null);
  462. $user_node->addChild('name', repamp($neighbour->name));
  463. $user_node->addChild('fullname', repamp($neighbour->fullname));
  464. $user_node->addChild('url', repamp($neighbour->getURL()));
  465. // Give a normalised value
  466. $user_node->addChild('match', $row['shared_artists'] / $highest_match);
  467. }
  468. return $xml;
  469. }
  470. }