scrobble-utils.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. require_once('database.php'); // include the database connection string
  16. require_once('temp-utils.php');
  17. function useridFromSID($session_id) {
  18. //derive the username from a session ID
  19. global $adodb; // include the Database connector
  20. // Delete any expired session ids
  21. $adodb->Execute('DELETE FROM Scrobble_Sessions WHERE expires < ' . time());
  22. try {
  23. $res = $adodb->GetOne('SELECT userid FROM Scrobble_Sessions WHERE sessionid = ' . $adodb->qstr($session_id)); // get the username from the table
  24. } catch (Exception $e) {
  25. die('FAILED ufs ' . $e->getMessage() . '\n');
  26. // die is there is an error, printing the error
  27. }
  28. if (!$res) {
  29. die("BADSESSION\n");
  30. // the user has no session
  31. }
  32. return $res;
  33. // return the first user
  34. }
  35. function createArtistIfNew($artist) {
  36. global $adodb;
  37. $id = $adodb->GetOne('SELECT id FROM Artist WHERE lower(name) = lower(' . $artist . ')');
  38. if (!$id) {
  39. // Artist doesn't exist, so we create them
  40. $res = $adodb->Execute('INSERT INTO Artist (name) VALUES (' . $artist . ')');
  41. }
  42. }
  43. function createAlbumIfNew($artist, $album) {
  44. global $adodb;
  45. $id = $adodb->GetOne('SELECT id FROM Album WHERE lower(name) = lower(' . $album . ') AND lower(artist_name) = lower(' . $artist . ')');
  46. if (!$id) {
  47. // Album doesn't exist, so create it
  48. // First check if artist exist, if not create it
  49. createArtistIfNew($artist);
  50. // Disable to fix scrobble breakage
  51. //$art = $adodb->qstr(getAlbumArt($artist, $album));
  52. $art = '';
  53. if ($art != '') {
  54. $license = $adodb->qstr('amazon');
  55. $sql = 'INSERT INTO Album (name, artist_name, image, artwork_license) VALUES (' . $album . ', ' . $artist . ', ' . $art . ', ' . $license .')';
  56. } else {
  57. $sql = 'INSERT INTO Album (name, artist_name) VALUES (' . $album . ', ' . $artist . ')';
  58. }
  59. $adodb->Execute($sql);
  60. }
  61. }
  62. function getTrackCreateIfNew($artist, $album, $track, $mbid) {
  63. global $adodb;
  64. if ($album != 'NULL') {
  65. $res = $adodb->GetOne('SELECT id FROM Track WHERE name = ' . $track . ' AND artist_name = ' . $artist . ' AND album_name = ' . $album);
  66. } else {
  67. $res = $adodb->GetOne('SELECT id FROM Track WHERE name = ' . $track . ' AND artist_name = ' . $artist . ' AND album_name IS NULL');
  68. }
  69. if (!$res) {
  70. // First check if artist and album exists, if not create them
  71. if ($album != 'NULL') {
  72. createAlbumIfNew($artist, $album);
  73. } else {
  74. createArtistIfNew($artist);
  75. }
  76. // Create new track
  77. $res = $adodb->Execute('INSERT INTO Track (name, artist_name, album_name, mbid) VALUES ('
  78. . $track . ', '
  79. . $artist . ', '
  80. . $album . ', '
  81. . $mbid . ')');
  82. return getTrackCreateIfNew($artist, $album, $track, $mbid);
  83. } else {
  84. return $res;
  85. }
  86. }
  87. function getScrobbleTrackCreateIfNew($artist, $album, $track, $mbid) {
  88. global $adodb;
  89. $res = $adodb->GetOne('SELECT id FROM Scrobble_Track WHERE name = lower('
  90. . $track . ') AND artist = lower(' . $artist . ') AND album '
  91. . (($album == 'NULL') ? 'IS NULL' : ('= lower(' . $album . ')')) . ' AND mbid '
  92. . (($mbid == 'NULL') ? 'IS NULL' : ('= lower(' . $mbid . ')')));
  93. if (!$res) {
  94. // First check if track exists, if not create it
  95. $tid = getTrackCreateIfNew($artist, $album, $track, $mbid);
  96. $sql = 'INSERT INTO Scrobble_Track (name, artist, album, mbid, track) VALUES ('
  97. . 'lower(' . $track . '), '
  98. . 'lower(' . $artist . '), '
  99. . (($album == 'NULL') ? 'NULL' : 'lower(' . $album . ')') . ', '
  100. . (($mbid == 'NULL') ? 'NULL' : 'lower(' . $mbid . ')') . ', '
  101. . $tid . ')';
  102. $res = $adodb->Execute($sql);
  103. return getScrobbleTrackCreateIfNew($artist, $album, $track, $mbid);
  104. } else {
  105. return $res;
  106. }
  107. }
  108. function scrobbleExists($userid, $artist, $track, $time) {
  109. global $adodb;
  110. $res = $adodb->GetOne('SELECT time FROM Scrobbles WHERE userid = ' . $userid . ' AND artist = ' . $artist . ' AND track = ' . $track . ' AND time = ' . $time);
  111. if (!$res) {
  112. return false;
  113. } else {
  114. return true;
  115. }
  116. }
  117. function noSpamTracks($track) {
  118. // This function exists to remove things like '(PREVIEW: buy it at www.magnatune.com)' from track names.
  119. $track = str_replace(' (PREVIEW: buy it at www.magnatune.com)', '', $track);
  120. return $track;
  121. }
  122. function getAlbumArt($artist, $album) {
  123. $Access_Key_ID = '1EST86JB355JBS3DFE82'; // this is mattl's personal key :)
  124. $SearchIndex = 'Music';
  125. $Keywords = rawurlencode($artist . ' ' . $album);
  126. $Operation = 'ItemSearch';
  127. $Version = '2007-07-16';
  128. $ResponseGroup = 'ItemAttributes,Images';
  129. $request = 'http://ecs.amazonaws.com/onca/xml'
  130. . '?Service=AWSECommerceService'
  131. . '&AssociateTag=' . $Associate_tag
  132. . '&AWSAccessKeyId=' . $Access_Key_ID
  133. . '&Operation=' . $Operation
  134. . '&Version=' . $Version
  135. . '&SearchIndex=' . $SearchIndex
  136. . '&Keywords=' . $Keywords
  137. . '&ResponseGroup=' . $ResponseGroup;
  138. $aws_xml = simplexml_load_file($request) or die('xml response not loading\n');
  139. $image = $aws_xml->Items->Item->MediumImage->URL;
  140. $URI = $aws_xml->Items->Item->DetailPageURL;
  141. return $image;
  142. }
  143. function validateMBID($input) {
  144. if (isset($input)) {
  145. $input = strtolower(rtrim($input));
  146. if (preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/', $input)) {
  147. return $input;
  148. } else {
  149. return null;
  150. }
  151. } else {
  152. return null;
  153. }
  154. }
  155. /**
  156. * Sends a scrobble on to any other services the user has connected to their account
  157. */
  158. function forwardScrobble($userid, $artist, $album, $track, $time, $mbid, $source, $rating, $length) {
  159. global $adodb, $lastfm_key, $lastfm_secret;
  160. $artist = rawurlencode($artist);
  161. $track = rawurlencode($track);
  162. $album = rawurlencode($album);
  163. $mbid = rawurlencode($mbid);
  164. $source = rawurlencode($source);
  165. $rating = rawurlencode($rating);
  166. $length = rawurlencode($length);
  167. $res = $adodb->CacheGetAll(600, 'SELECT * FROM Service_Connections WHERE userid = ' . $userid . ' AND forward = 1');
  168. foreach ($res as &$row) {
  169. $remote_key = $row['remote_key'];
  170. $ws_url = $row['webservice_url'];
  171. $curl_session = curl_init($ws_url);
  172. $post_vars = '';
  173. if ($album) {
  174. $post_vars .= 'album[0]=' . $album . '&';
  175. }
  176. $post_vars .= 'api_key=' . $lastfm_key . '&artist[0]=' . $artist;
  177. if ($length) {
  178. $post_vars .= '&length[0]=' . $length;
  179. }
  180. if ($mbid) {
  181. $post_vars .= '&mbid[0]=' . $mbid;
  182. }
  183. $post_vars .= '&method=track.scrobble';
  184. if ($rating) {
  185. $post_vars .= '&rating[0]=' . $rating;
  186. }
  187. $post_vars .= '&sk=' . $remote_key;
  188. if ($source) {
  189. $post_vars .= '&source[0]='. $source;
  190. }
  191. $post_vars .= '&timestamp[0]=' . $time . '&track[0]=' . $track;
  192. $sig = urldecode(str_replace('&', '', $post_vars));
  193. $sig = str_replace('=', '', $sig);
  194. $sig = md5($sig . $lastfm_secret);
  195. $post_vars .= '&api_sig=' . $sig;
  196. curl_setopt($curl_session, CURLOPT_POST, true);
  197. curl_setopt($curl_session, CURLOPT_POSTFIELDS, $post_vars);
  198. curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
  199. curl_setopt($curl_session, CURLOPT_CONNECTTIMEOUT, 1);
  200. curl_setopt($curl_session, CURLOPT_TIMEOUT, 1);
  201. $response = curl_exec($curl_session);
  202. curl_close($curl_session);
  203. }
  204. }