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

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

mattl's avatar
mattl committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Libre.fm -- a free network service for sharing your music listening habits

   Copyright (C) 2009 Libre.fm Project

   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 23
require_once('database.php');

elleo's avatar
elleo committed
24 25 26 27 28 29 30 31
function usernameFromSID($session_id) {
	global $mdb2;

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

	$res = $mdb2->query("SELECT username FROM Scrobble_Sessions WHERE sessionid = " . $mdb2->quote($session_id, "text"));
	if(PEAR::isError($res)) {
32
		die("FAILED " . $res->getMessage() . "\n");
elleo's avatar
elleo committed
33 34 35
	}

	if(!$res->numRows()) {
36
		die("BADSESSION\n");
elleo's avatar
elleo committed
37 38 39 40 41
	}

	return $res->fetchOne(0);
}

42 43 44
function createArtistIfNew($artist) {
	global $mdb2;

mattl's avatar
mattl committed
45 46
	$artist = NoSpamTracks($artist);

jurgbohn's avatar
jurgbohn committed
47
	$res = $mdb2->query("SELECT name FROM Artist WHERE name = " . ($artist));
48
	if(PEAR::isError($res)) {
49
		die("FAILED " . $res->getMessage() . "\n");
50 51 52 53
	}

	if(!$res->numRows()) {
		// Artist doesn't exist, so we create them
jurgbohn's avatar
jurgbohn committed
54
		$res = $mdb2->query("INSERT INTO Artist (name) VALUES (" . ($artist) . ")");
55
		if(PEAR::isError($res)) {
56
			die("FAILED " . $res->getMessage() . "\n");
57 58 59 60 61 62 63
		}
	}
}

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

jurgbohn's avatar
jurgbohn committed
64
	$res = $mdb2->query("SELECT name FROM Album WHERE name = " . ($album));
65
	if(PEAR::isError($res)) {
66
		die("FAILED " . $res->getMessage() . "\n");
67 68 69 70
	}

	if(!$res->numRows()) {
		// Album doesn't exist, so create it
71
	        
mattl's avatar
mattl committed
72
	  $art = $mdb2->quote(getAlbumArt($artist, $album));
mattl's avatar
mattl committed
73
	  if ($art !="") {
mattl's avatar
mattl committed
74
	    $license = $mdb2->quote("amazon");
75

76
	    $res = $mdb2->query("INSERT INTO Album (name, artist_name, image, artwork_license) VALUES (" . ($album) . ", " . ($artist) . ", " . ($art) . ", " . ($license) .")");
77 78 79

	  } else {

jurgbohn's avatar
jurgbohn committed
80
		$res = $mdb2->query("INSERT INTO Album (name, artist_name) VALUES (" . ($album) . ", " . ($artist) . ")");
81 82 83

	  }

84
		if(PEAR::isError($res)) {
85
			die("FAILED " . $res->getMessage() . "\n");
86 87 88 89 90 91 92
		}
	}
}

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

mattl's avatar
mattl committed
93
	$track = NoSpamTracks($track);
mattl's avatar
mattl committed
94
	$artist = NoSpamTracks($artist);
mattl's avatar
mattl committed
95

clint's avatar
clint committed
96 97 98 99 100
	if($album) {
	$res = $mdb2->query("SELECT name FROM Track WHERE lower(name) = " . (strtolower($track)) . " AND lower(artist) = " . (strtolower($artist)) . " AND lower(album) = " . strtolower($album));
	} else {
	$res = $mdb2->query("SELECT name FROM Track WHERE lower(name) = " . (strtolower($track)) . " AND lower(artist) = " . (strtolower($artist)) . "AND album IS NULL");
	}
101
	if(PEAR::isError($res)) {
102
		die("FAILED " . $res->getMessage() . "\n");
103 104 105 106 107
	}

	if(!$res->numRows()) {
		// Create new track
		$res = $mdb2->query("INSERT INTO Track (name, artist, album, mbid) VALUES ("
jurgbohn's avatar
jurgbohn committed
108 109 110 111
			. ($track) . ", "
			. ($artist) . ", "
			. ($album) . ", "
			. ($mbid) . ")");
112
		if(PEAR::isError($res)) {
113
			die("FAILED " . $res->getMessage() . "\n");
114 115 116
		}
	}
}
mattl's avatar
mattl committed
117

118 119 120 121 122
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)) {
123
		die("FAILED " . $res->getMessage() . "\n");
124 125 126 127 128 129 130 131
	}

	if(!$res->numRows()) {
		return false;
	} else {
		return true;
	}
}
mattl's avatar
mattl committed
132 133 134 135 136 137 138 139 140 141
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;
  
}

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
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;

162
$aws_xml = simplexml_load_file($request) or die("xml response not loading\n");
163 164 165 166 167

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

169
?>