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

scrobble-utils.php 6.6 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)) {   
clint's avatar
clint committed
37
		die("FAILED ufs " . $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)) {
clint's avatar
clint committed
59
		die("FAILED art " . $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)) {
clint's avatar
clint committed
66
			die("FAILED artc " . $res->getMessage() . "\n");
67 68 69 70 71 72 73
		}
	}
}

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

74
	$res = $mdb2->query("SELECT name FROM Album WHERE name = " . ($album) . " AND artist_name = " . ($artist));
75
	if(PEAR::isError($res)) {
clint's avatar
clint committed
76
		die("FAILED alb " . $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)) {
clint's avatar
clint committed
95
			die("FAILED albc " . $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

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

	if(!$res->numRows()) {
		// Create new track
117
		$res = $mdb2->exec("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)) {
clint's avatar
clint committed
123
			die("FAILED trkc " . $res->getMessage() . "\n");
124
		}
clint's avatar
clint committed
125 126 127 128 129 130 131 132 133
		return getTrackCreateIfNew($artist, $album, $track, $mbid);
	} else {
		return $res->fetchOne(0);
	}
}

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

clint's avatar
clint committed
134 135 136 137
	$res = $mdb2->query("SELECT id FROM Scrobble_Track WHERE name = lower("
		. ($track) . ") AND artist = lower(" . ($artist) . ") AND album "
		. (($album == 'NULL') ? "IS NULL" : ("= lower(" . ($album) . ")")) . " AND mbid "
		. (($mbid == 'NULL') ? "IS NULL" : ("= lower(" . ($mbid) . ")")));
clint's avatar
clint committed
138
	if(PEAR::isError($res)) {
clint's avatar
clint committed
139
		die("FAILED st " . $res->getMessage() . "\n");
clint's avatar
clint committed
140 141 142
	}

	if(!$res->numRows()) {
clint's avatar
clint committed
143
		$sql = "INSERT INTO Scrobble_Track (name, artist, album, mbid, track) VALUES ("
clint's avatar
clint committed
144 145
			. "lower(" . ($track) . "), "
			. "lower(" . ($artist) . "), "
clint's avatar
clint committed
146 147
			. (($album == 'NULL') ? "NULL" : "lower(" . ($album) . ")") . ", "
			. (($mbid == 'NULL') ? "NULL" : "lower(" . ($mbid) . ")") . ", "
clint's avatar
clint committed
148 149
			. ($tid) . ")";
		$res = $mdb2->exec($sql);
clint's avatar
clint committed
150
		if(PEAR::isError($res)) {
clint's avatar
clint committed
151 152 153
			$msg = $res->getMessage() . " - " . $res->getUserInfo();
			reportError($msg, $sql);

clint's avatar
clint committed
154
			die("FAILED stc " . $res->getMessage() . "\n");
clint's avatar
clint committed
155 156 157 158
		}
		return getScrobbleTrackCreateIfNew($artist, $album, $track, $mbid, $tid);
	} else {
		return $res->fetchOne(0);
159 160
	}
}
mattl's avatar
mattl committed
161

162 163 164 165 166
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)) {
clint's avatar
clint committed
167
		die("FAILED se " . $res->getMessage() . "\n");
168 169 170 171 172 173 174 175
	}

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

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

180
  $track = str_replace(" (PREVIEW: buy it at www.magnatune.com)", "", $track);
mattl's avatar
mattl committed
181 182 183 184 185

  return $track;
  
}

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
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;

206
$aws_xml = simplexml_load_file($request) or die("xml response not loading\n");
207 208 209 210 211

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

213 214
function validateMBID ($input) {

clint's avatar
clint committed
215
if(isset($input)) {
216 217 218 219 220 221 222 223 224
	$input = strtolower(rtrim($input));
	if(preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/', $input)) {
		return $mdb2->quote($input, "text");
	} else {
		return 'NULL'
	}
} else {
	return 'NULL';
}
225
?>