We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

scrobble-utils.php 6.03 KB
Newer Older
mattl's avatar
mattl committed
1 2
<?php

mattl's avatar
mattl committed
3 4
/* Libre.fm -- a free network service for sharing your music listening habits

5
   Copyright (C) 2009 Free Software Foundation, Inc
mattl's avatar
mattl committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

22
require_once('database.php');	// include the database connection string
23

24 25 26 27 28 29
function usernameFromSID($session_id) 
{

//derive the username from a session ID

	global $mdb2; 	       // include the Database connector
elleo's avatar
elleo committed
30 31 32 33

	// Delete any expired session ids
	$mdb2->query("DELETE FROM Scrobble_Sessions WHERE expires < " . time());

34 35 36
	$res = $mdb2->query("SELECT username FROM Scrobble_Sessions WHERE sessionid = " . $mdb2->quote($session_id, "text")); // get the username from the table

	if(PEAR::isError($res)) {   
37
		die("FAILED " . $res->getMessage() . "\n");
38
		// die is there is an error, printing the error
elleo's avatar
elleo committed
39 40 41
	}

	if(!$res->numRows()) {
42
		die("BADSESSION\n");
43 44

		// the user has no session
elleo's avatar
elleo committed
45 46 47
	}

	return $res->fetchOne(0);
48 49

	       // return the first user
elleo's avatar
elleo committed
50 51
}

52 53 54
function createArtistIfNew($artist) {
	global $mdb2;

mattl's avatar
mattl committed
55 56
	$artist = NoSpamTracks($artist);

jurgbohn's avatar
jurgbohn committed
57
	$res = $mdb2->query("SELECT name FROM Artist WHERE name = " . ($artist));
58
	if(PEAR::isError($res)) {
59
		die("FAILED " . $res->getMessage() . "\n");
60 61 62 63
	}

	if(!$res->numRows()) {
		// Artist doesn't exist, so we create them
jurgbohn's avatar
jurgbohn committed
64
		$res = $mdb2->query("INSERT INTO Artist (name) VALUES (" . ($artist) . ")");
65
		if(PEAR::isError($res)) {
66
			die("FAILED " . $res->getMessage() . "\n");
67 68 69 70 71 72 73
		}
	}
}

function createAlbumIfNew($artist, $album) {
	global $mdb2;

jurgbohn's avatar
jurgbohn committed
74
	$res = $mdb2->query("SELECT name FROM Album WHERE name = " . ($album));
75
	if(PEAR::isError($res)) {
76
		die("FAILED " . $res->getMessage() . "\n");
77 78 79 80
	}

	if(!$res->numRows()) {
		// Album doesn't exist, so create it
81
	        
mattl's avatar
mattl committed
82
	  $art = $mdb2->quote(getAlbumArt($artist, $album));
mattl's avatar
mattl committed
83
	  if ($art !="") {
mattl's avatar
mattl committed
84
	    $license = $mdb2->quote("amazon");
85

86
	    $res = $mdb2->query("INSERT INTO Album (name, artist_name, image, artwork_license) VALUES (" . ($album) . ", " . ($artist) . ", " . ($art) . ", " . ($license) .")");
87 88 89

	  } else {

jurgbohn's avatar
jurgbohn committed
90
		$res = $mdb2->query("INSERT INTO Album (name, artist_name) VALUES (" . ($album) . ", " . ($artist) . ")");
91 92 93

	  }

94
		if(PEAR::isError($res)) {
95
			die("FAILED " . $res->getMessage() . "\n");
96 97 98 99
		}
	}
}

clint's avatar
clint committed
100
function getTrackCreateIfNew($artist, $album, $track, $mbid) {
101 102
	global $mdb2;

mattl's avatar
mattl committed
103
	$track = NoSpamTracks($track);
mattl's avatar
mattl committed
104
	$artist = NoSpamTracks($artist);
mattl's avatar
mattl committed
105

clint's avatar
clint committed
106
	if($album) {
clint's avatar
clint committed
107
	$res = $mdb2->query("SELECT id FROM Track WHERE lower(name) = " . (strtolower($track)) . " AND lower(artist) = " . (strtolower($artist)) . " AND lower(album) = " . strtolower($album));
clint's avatar
clint committed
108
	} else {
clint's avatar
clint committed
109
	$res = $mdb2->query("SELECT id FROM Track WHERE lower(name) = " . (strtolower($track)) . " AND lower(artist) = " . (strtolower($artist)) . "AND album IS NULL");
clint's avatar
clint committed
110
	}
111
	if(PEAR::isError($res)) {
112
		die("FAILED " . $res->getMessage() . "\n");
113 114 115 116 117
	}

	if(!$res->numRows()) {
		// Create new track
		$res = $mdb2->query("INSERT INTO Track (name, artist, album, mbid) VALUES ("
jurgbohn's avatar
jurgbohn committed
118 119 120 121
			. ($track) . ", "
			. ($artist) . ", "
			. ($album) . ", "
			. ($mbid) . ")");
122
		if(PEAR::isError($res)) {
123
			die("FAILED " . $res->getMessage() . "\n");
124
		}
clint's avatar
clint committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
		return getTrackCreateIfNew($artist, $album, $track, $mbid);
	} else {
		return $res->fetchOne(0);
	}
}

function getScrobbleTrackCreateIfNew($artist, $album, $track, $mbid, $tid) {
	global $mdb2;

	$res = $mdb2->query("SELECT id FROM Scrobbles_Track WHERE name = " . (strtolower($track)) . " AND artist = " . (strtolower($artist)) . " AND album = " . strtolower($album) . " AND mbid = " . strtolower($mbid));
	if(PEAR::isError($res)) {
		die("FAILED " . $res->getMessage() . "\n");
	}

	if(!$res->numRows()) {
		// Create new track
		$res = $mdb2->exec("INSERT INTO Scrobbles_Track (name, artist, album, mbid, track) VALUES ("
			. strtolower($track) . ", "
			. strtolower($artist) . ", "
			. strtolower($album) . ", "
			. strtolower($mbid) . ","
			. strtolower($tid) . ")");
		if(PEAR::isError($res)) {
			die("FAILED " . $res->getMessage() . "\n");
		}
		return getScrobbleTrackCreateIfNew($artist, $album, $track, $mbid, $tid);
	} else {
		return $res->fetchOne(0);
153 154
	}
}
mattl's avatar
mattl committed
155

156 157 158 159 160
function scrobbleExists($username, $artist, $track, $time) {
	global $mdb2;

	$res = $mdb2->query("SELECT time FROM Scrobbles WHERE username = " . ($username) . " AND artist = " . ($artist) . " AND track = " . ($track) . " AND time = " . ($time));
	if(PEAR::isError($res)) {
161
		die("FAILED " . $res->getMessage() . "\n");
162 163 164 165 166 167 168 169
	}

	if(!$res->numRows()) {
		return false;
	} else {
		return true;
	}
}
mattl's avatar
mattl committed
170 171 172 173 174 175 176 177 178 179
function NoSpamTracks ($track) {

  // This function exists to remove things like '(PREVIEW: buy it at www.magnatune.com)' from track names.

  $track = str_replace("(PREVIEW: buy it at www.magnatune.com)", "", $track);

  return $track;
  
}

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
function getAlbumArt($artist, $album) {

  $Access_Key_ID = "1EST86JB355JBS3DFE82"; // this is mattl's personal key :)

        $SearchIndex='Music';
$Keywords=urlencode($artist.' '.$album);
        $Operation = "ItemSearch";
$Version = "2007-07-16";
        $ResponseGroup = "ItemAttributes,Images";
$request=
        "http://ecs.amazonaws.com/onca/xml"
                . "?Service=AWSECommerceService"
. "&AssociateTag=" . $Associate_tag
. "&AWSAccessKeyId=" . $Access_Key_ID
. "&Operation=" . $Operation
. "&Version=" . $Version
. "&SearchIndex=" . $SearchIndex
. "&Keywords=" . $Keywords
. "&ResponseGroup=" . $ResponseGroup;

200
$aws_xml = simplexml_load_file($request) or die("xml response not loading\n");
201 202 203 204 205

$image = $aws_xml->Items->Item->MediumImage->URL;
        $URI = $aws_xml->Items->Item->DetailPageURL;
        return $image;
}
mattl's avatar
mattl committed
206

207
?>