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

Commit a5e574d8 authored by elleo's avatar elleo

Refactor scrobble based artist/album/track creation in to separate functions...

Refactor scrobble based artist/album/track creation in to separate functions so the now playing service can share them
parent e552ffa0
<?
require_once('database.php');
function createArtistIfNew($artist) {
global $mdb2;
$res = $mdb2->query("SELECT name FROM Artist WHERE name = " . $artist);
if(PEAR::isError($res)) {
die("FAILED " . $res->getMessage());
}
if(!$res->numRows()) {
// Artist doesn't exist, so we create them
$res = $mdb2->query("INSERT INTO Artist (name) VALUES (" . $artist . ")");
if(PEAR::isError($res)) {
die("FAILED " . $res->getMessage());
}
}
}
function createAlbumIfNew($artist, $album) {
global $mdb2;
$res = $mdb2->query("SELECT name FROM Album WHERE name = " . $album);
if(PEAR::isError($res)) {
die("FAILED " . $res->getMessage());
}
if(!$res->numRows()) {
// Album doesn't exist, so create it
$res = $mdb2->query("INSERT INTO Album (name, artist_name) VALUES (" . $album . ", " . $artist . ")");
if(PEAR::isError($res)) {
die("FAILED " . $res->getMessage());
}
}
}
function createTrackIfNew($artist, $album, $track, $mbid) {
global $mdb2;
$res = $mdb2->query("SELECT name FROM Track WHERE name = " . $track . " AND artist = " . $artist);
if(PEAR::isError($res)) {
die("FAILED " . $res->getMessage());
}
if(!$res->numRows()) {
// Create new track
$res = $mdb2->query("INSERT INTO Track (name, artist, album, mbid) VALUES ("
. $track . ", "
. $artist . ", "
. $album . ", "
. $mbid . ")");
if(PEAR::isError($res)) {
die("FAILED " . $res->getMessage());
}
}
}
?>
<? <?
require_once('../../database.php'); require_once('../../database.php');
require_once('../../scrobble-utils.php');
if(!isset($_POST['s']) || !isset($_POST['a']) || !isset($_POST['t']) || !isset($_POST['i']) || !isset($_POST['o'])) { if(!isset($_POST['s']) || !isset($_POST['a']) || !isset($_POST['t']) || !isset($_POST['i']) || !isset($_POST['o'])) {
die("FAILED Required POST parameters are not set"); die("FAILED Required POST parameters are not set");
...@@ -41,42 +42,13 @@ for($i = 0; $i < count($_POST['a']); $i++) { ...@@ -41,42 +42,13 @@ for($i = 0; $i < count($_POST['a']); $i++) {
$mbid = 'NULL'; $mbid = 'NULL';
} }
$res = $mdb2->query("SELECT name FROM Artist WHERE name = " . $artist); createArtistIfNew($artist);
if(PEAR::isError($res)) {
die("FAILED " . $res->getMessage());
}
if(!$res->numRows()) {
// Artist doesn't exist, so we create them
$mdb2->query("INSERT INTO Artist (name) VALUES (" . $artist . ")");
}
if($album != 'NULL') { if($album != 'NULL') {
$res = $mdb2->query("SELECT name FROM Artist WHERE name = " . $album); createAlbumIfNew($artist, $album);
if(PEAR::isError($res)) {
die("FAILED " . $res->getMessage());
}
if(!$res->numRows()) {
// Album doesn't exist, so create it
$mdb2->query("INSERT INTO Album (name, artist_name) VALUES (" . $album . ", " . $artist . ")");
}
}
$res = $mdb2->query("SELECT name FROM Track WHERE name = " . $track);
if(PEAR::isError($res)) {
die("FAILED " . $res->getMessage());
}
if(!$res->numRows()) {
// Create new track
$mdb2->query("INSERT INTO Track (name, artist, album, mbid) VALUES ("
. $track . ", "
. $artist . ", "
. $album . ", "
. $mbid . ")");
} }
createTrackIfNew($artist, $album, $track, $mbid);
// Scrobble!
$res = $mdb2->query("SELECT playcount FROM Scrobbles WHERE username = " . $username . " AND track = " . $track . " AND artist = " . $artist); $res = $mdb2->query("SELECT playcount FROM Scrobbles WHERE username = " . $username . " AND track = " . $track . " AND artist = " . $artist);
if(PEAR::isError($res)) { if(PEAR::isError($res)) {
die("FAILED " . $res->getMessage()); die("FAILED " . $res->getMessage());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment