musicbrainz.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /* GNUkebox -- a free software server for recording your 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. @apache_setenv('no-gzip', 1);
  16. @ini_set('zlib.output_compression', 0);
  17. @ini_set('implicit_flush', 1);
  18. header('Content-type: text/html; charset=utf-8');
  19. require_once('database.php');
  20. require_once('utils/human-time.php');
  21. $adodb->SetFetchMode(ADODB_FETCH_ASSOC);
  22. $res = $adodb->GetAll('SELECT artist, track from Scrobbles where album is null LIMIT 20;');
  23. echo '<ul>';
  24. if (!$res) {
  25. die('sql error');
  26. }
  27. foreach ($res as &$row) {
  28. echo '<li>' . $row['artist'] . '&mdash;' . $row['track'] . '</li>';
  29. echo 'Finding album...';
  30. echo doABunchOfShit($row['artist'], $row['track']);
  31. for ($i = 0; $i < ob_get_level(); $i++) {
  32. ob_end_flush();
  33. }
  34. ob_implicit_flush(1);
  35. }
  36. function doABunchOfShit($artist, $track) {
  37. $album = ScrobbleLookup($artist, $track);
  38. if ($album) {
  39. return $album;
  40. } else {
  41. $album = BrainzLookup($artist, $track);
  42. return $album;
  43. }
  44. }
  45. function ScrobbleLookup($artist, $track) {
  46. global $adodb;
  47. $sql = 'SELECT album from Scrobbles where artist = ' . $adodb->qstr($artist) . ' and track = ' . $adodb->qstr($track) . ' LIMIT 1;';
  48. $adodb->SetFetchMode(ADODB_FETCH_ASSOC);
  49. $album = $adodb->GetOne($sql);
  50. if (!$album) {
  51. die('sql error');
  52. }
  53. return $album;
  54. }
  55. function BrainzLookup($artist, $track) {
  56. global $adodb;
  57. $sql = 'select a.name as artist,l.name as album, t.name as track,t.gid as mbid from brainz.track t left join brainz.artist a on t.artist=a.id left join brainz.albumjoin j on j.track=t.id left join brainz.album l on l.id=j.album where lower(t.name) = lower(' . $adodb->qstr($track) . ') and lower(a.name) = lower(' . $adodb->qstr($artist) . ') LIMIT 1;';
  58. $adodb->SetFetchMode(ADODB_FETCH_ASSOC);
  59. $albumData = $adodb->GetRow($sql);
  60. if (!$albumData)) {
  61. die('sql error');
  62. }
  63. return $albumData['album'];
  64. }
  65. ?>
  66. </ul>