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

scrobble-utils.php 6.61 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/>.

*/

clint's avatar
clint committed
22
require_once('database.php');	// include the database connection string
23

clint's avatar
clint committed
24
function usernameFromSID($session_id)
25 26 27 28
{

//derive the username from a session ID

clint's avatar
clint committed
29
	global $adodb; 	       // include the Database connector
elleo's avatar
elleo committed
30 31

	// Delete any expired session ids
clint's avatar
clint committed
32
	$adodb->Execute('DELETE FROM Scrobble_Sessions WHERE expires < ' . time());
elleo's avatar
elleo committed
33

34
	try {
clint's avatar
clint committed
35
		$res = $adodb->GetOne('SELECT username FROM Scrobble_Sessions WHERE sessionid = ' . $adodb->qstr($session_id)); // get the username from the table
36 37
	}
	catch (exception $e) {
clint's avatar
clint committed
38
		die('FAILED ufs ' . $e->getMessage() . '\n');
39
		// die is there is an error, printing the error
elleo's avatar
elleo committed
40 41
	}

clint's avatar
clint committed
42
	if(!$res) {
clint's avatar
clint committed
43
		die('BADSESSION\n');
44 45

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

clint's avatar
clint committed
48
	return $res;
49 50

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

53
function createArtistIfNew($artist) {
clint's avatar
clint committed
54
	global $adodb;
55

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

58
	try {
clint's avatar
clint committed
59
		$res = $adodb->GetOne('SELECT name FROM Artist WHERE lower(name) = lower(' . ($artist) . ')');
60 61
	}
	catch (exception $e) {
clint's avatar
clint committed
62
		die('FAILED art ' . $e->getMessage() . '\n');
63 64
	}

clint's avatar
clint committed
65
	if(!$res) {
66
		// Artist doesn't exist, so we create them
67
		try {
clint's avatar
clint committed
68
			$res = $adodb->Execute('INSERT INTO Artist (name) VALUES (' . ($artist) . ')');
69 70
		}
		catch (exception $e) {
clint's avatar
clint committed
71
			die('FAILED artc ' . $e->getMessage() . '\n');
72 73 74 75 76
		}
	}
}

function createAlbumIfNew($artist, $album) {
clint's avatar
clint committed
77
	global $adodb;
78

79
	try {
80
		$name = $adodb->GetOne('SELECT name FROM Album WHERE lower(name) = lower(' . ($album) . ') AND lower(artist_name) = lower(' . ($artist) . ')');
81 82
	}
	catch (exception $e) {
clint's avatar
clint committed
83
		die('FAILED alb ' . $e->getMessage() . '\n');
84 85
	}

clint's avatar
clint committed
86
	if(!$name) {
87
		// Album doesn't exist, so create it
88

89
		$art = $adodb->qstr(getAlbumArt($artist, $album));
90

91
		if ($art !="") {
clint's avatar
clint committed
92
			$license = $adodb->qstr('amazon');
93

clint's avatar
clint committed
94
			$sql = 'INSERT INTO Album (name, artist_name, image, artwork_license) VALUES (' . ($album) . ', ' . ($artist) . ', ' . ($art) . ', ' . ($license) .')';
95

96
		} else {
clint's avatar
clint committed
97
			$sql = 'INSERT INTO Album (name, artist_name) VALUES (' . ($album) . ', ' . ($artist) . ')';
98 99 100 101 102
		}
		try {
			$adodb->Execute($sql);
		}
		catch (exception $e) {
clint's avatar
clint committed
103
			die('FAILED albc ' . $e->getMessage() . '\n');
104
		}
105 106 107
	}
}

clint's avatar
clint committed
108
function getTrackCreateIfNew($artist, $album, $track, $mbid) {
clint's avatar
clint committed
109
	global $adodb;
110

mattl's avatar
mattl committed
111
	$track = NoSpamTracks($track);
mattl's avatar
mattl committed
112
	$artist = NoSpamTracks($artist);
mattl's avatar
mattl committed
113

114
	try {
115
	if($album != 'NULL') {
clint's avatar
clint committed
116
	$res = $adodb->GetOne('SELECT id FROM Track WHERE lower(name) = lower(' . ($track) . ') AND lower(artist_name) = lower(' . ($artist) . ') AND lower(album_name) = lower(' . ($album) . ')');
clint's avatar
clint committed
117
	} else {
clint's avatar
clint committed
118
	$res = $adodb->GetOne('SELECT id FROM Track WHERE lower(name) = lower(' . ($track) . ') AND lower(artist_name) = lower(' . ($artist) . ') AND album_name IS NULL');
clint's avatar
clint committed
119
	}
120 121
	}
	catch (exception $e) {
clint's avatar
clint committed
122
		die('FAILED trk ' . $e->getMessage() . '\n');
123 124
	}

clint's avatar
clint committed
125
	if(!$res) {
126
		// Create new track
127
		try {
clint's avatar
clint committed
128 129 130 131 132
		$res = $adodb->Execute('INSERT INTO Track (name, artist_name, album_name, mbid) VALUES ('
			. ($track) . ', '
			. ($artist) . ', '
			. ($album) . ', '
			. ($mbid) . ')');
133 134
		}
		catch (exception $e) {
clint's avatar
clint committed
135
			die('FAILED trkc ' . $e->getMessage() . '\n');
136
		}
clint's avatar
clint committed
137 138
		return getTrackCreateIfNew($artist, $album, $track, $mbid);
	} else {
clint's avatar
clint committed
139
		return $res;
clint's avatar
clint committed
140 141 142 143
	}
}

function getScrobbleTrackCreateIfNew($artist, $album, $track, $mbid, $tid) {
clint's avatar
clint committed
144
	global $adodb;
clint's avatar
clint committed
145

146
	try {
clint's avatar
clint committed
147 148 149 150
	$res = $adodb->GetOne('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) . ')')));
151 152
	}
	catch (exception $e) {
clint's avatar
clint committed
153
		die('FAILED st ' . $e->getMessage() . '\n');
clint's avatar
clint committed
154 155
	}

clint's avatar
clint committed
156
	if(!$res) {
clint's avatar
clint committed
157 158 159 160 161 162
		$sql = 'INSERT INTO Scrobble_Track (name, artist, album, mbid, track) VALUES ('
			. 'lower(' . ($track) . '), '
			. 'lower(' . ($artist) . '), '
			. (($album == 'NULL') ? 'NULL' : 'lower(' . ($album) . ')') . ', '
			. (($mbid == 'NULL') ? 'NULL' : 'lower(' . ($mbid) . ')') . ', '
			. ($tid) . ')';
163 164 165 166
		try {
			$res = $adodb->Execute($sql);
		}
		catch (exception $e) {
clint's avatar
clint committed
167
			$msg = $e->getMessage() . ' - ' . $e->getUserInfo();
clint's avatar
clint committed
168 169
			reportError($msg, $sql);

clint's avatar
clint committed
170
			die('FAILED stc ' . $res->getMessage() . '\n');
clint's avatar
clint committed
171 172 173
		}
		return getScrobbleTrackCreateIfNew($artist, $album, $track, $mbid, $tid);
	} else {
clint's avatar
clint committed
174
		return $res;
175 176
	}
}
mattl's avatar
mattl committed
177

178
function scrobbleExists($username, $artist, $track, $time) {
clint's avatar
clint committed
179
	global $adodb;
180

181
	try {
clint's avatar
clint committed
182
	$res = $adodb->GetOne('SELECT time FROM Scrobbles WHERE username = ' . ($username) . ' AND artist = ' . ($artist) . ' AND track = ' . ($track) . ' AND time = ' . ($time));
183 184
	}
	catch (exception $e) {
clint's avatar
clint committed
185
		die('FAILED se ' . $e->getMessage() . '\n');
186 187
	}

clint's avatar
clint committed
188
	if(!$res) {
189 190 191 192 193
		return false;
	} else {
		return true;
	}
}
mattl's avatar
mattl committed
194 195 196 197
function NoSpamTracks ($track) {

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

clint's avatar
clint committed
198
  $track = str_replace(' (PREVIEW: buy it at www.magnatune.com)', "", $track);
mattl's avatar
mattl committed
199 200

  return $track;
clint's avatar
clint committed
201

mattl's avatar
mattl committed
202 203
}

204 205
function getAlbumArt($artist, $album) {

clint's avatar
clint committed
206
  $Access_Key_ID = '1EST86JB355JBS3DFE82'; // this is mattl's personal key :)
207 208 209

        $SearchIndex='Music';
$Keywords=urlencode($artist.' '.$album);
clint's avatar
clint committed
210 211 212
        $Operation = 'ItemSearch';
$Version = '2007-07-16';
        $ResponseGroup = 'ItemAttributes,Images';
213
$request=
clint's avatar
clint committed
214 215 216 217 218 219 220 221 222 223 224
        '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;

$aws_xml = simplexml_load_file($request) or die('xml response not loading\n');
225 226 227 228 229

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

231
function validateMBID ($input) {
clint's avatar
clint committed
232
if(isset($input)) {
233 234
	$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)) {
235
		return $input;
236
	} else {
237
		return null;
238 239
	}
} else {
240
	return null;
241
}
clint's avatar
clint committed
242 243

}
244
?>