Commit e552ffa0 authored by elleo's avatar elleo

Basic scrobbling support

Create tables for storing tracks, scrobbles and scrobble sessions
Fake now playing support
Add unit test to check scrobbling
parent 933dd659
......@@ -65,6 +65,32 @@ if (isset($_POST['install'])) {
name_b VARCHAR(255) REFERENCES Artist(name),
PRIMARY KEY(name_a, name_b))");
$mdb2->query("CREATE TABLE Track(
name VARCHAR(255),
artist VARCHAR(255) REFERENCES Artist(name),
album VARCHAR(255) REFERENCES Album(name),
mbid VARCHAR(36),
duration int,
streamable int,
license VARCHAR(255),
downloadurl VARCHAR(255),
PRIMARY KEY(name, artist))");
$mdb2->query("CREATE TABLE Scrobbles(
username VARCHAR(255) REFERENCES User(username),
track VARCHAR(255) REFERENCES Track(name),
artist VARCHAR(255) REFERENCES Artist(name),
rating VARCHAR(1),
playcount int,
lastplayed TIMESTAMP,
PRIMARY KEY(username, track, artist))");
$mdb2->query("CREATE TABLE Scrobble_Sessions(
username VARCHAR(255) REFERENCES User(username),
sessionid VARCHAR(32),
expires TIMESTAMP,
PRIMARY KEY(username, sessionid))");
// Test user configuration
$res = $mdb2->query("INSERT INTO Users
(username, password, created)
......
<?
// Close your eyes and pretend that everything's okay...
die("OK");
?>
......@@ -29,8 +29,14 @@ if(isset($_GET['hs'])) {
die("BADAUTH");
}
$session_id = md5($auth_token . time());
$mdb2->query("INSERT INTO Scrobble_Sessions(username, sessionid, expires) VALUES ("
. $mdb2->quote($username, "text") . ","
. $mdb2->quote($session_id, "text") . ","
. $mdb2->quote(time() + 86400) . ")");
echo "OK\n";
echo "1\n"; // TODO: Real session ids
echo $session_id . "\n";
echo $submissions_server . "/nowplaying/1.2/\n";
echo $submissions_server . "/submissions/1.2/\n";
}
......
<?
require_once('../../database.php');
if(!isset($_POST['s']) || !isset($_POST['a']) || !isset($_POST['t']) || !isset($_POST['i']) || !isset($_POST['o'])) {
die("FAILED Required POST parameters are not set");
}
if(!is_array($_POST['a']) || !is_array($_POST['t']) || !is_array($_POST['i']) || !is_array($_POST['o'])) {
die("FAILED Track parameters must be arrays");
}
$session_id = $_POST['s'];
// 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)) {
die("FAILED " . $res->getMessage());
}
if(!$res->numRows()) {
die("BADSESSION");
}
$username = $mdb2->quote($res->fetchOne(0), "text");
for($i = 0; $i < count($_POST['a']); $i++) {
$artist = $mdb2->quote($_POST['a'][$i], "text");
if(isset($_POST['b'][$i])) {
$album = $mdb2->quote($_POST['b'][$i], "text");
} else {
$album = 'NULL';
}
$track = $mdb2->quote($_POST['t'][$i], "text");
$time = (int) $_POST['i'][$i];
$origin = $mdb2->quote($_POST['o'][$i], "text");
if(isset($_POST['m'][$i])) {
$mbid = $mdb2->quote($_POST['m'][$i], "text");
} else {
$mbid = 'NULL';
}
$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
$mdb2->query("INSERT INTO Artist (name) VALUES (" . $artist . ")");
}
if($album != 'NULL') {
$res = $mdb2->query("SELECT name FROM Artist WHERE name = " . $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 . ")");
}
$res = $mdb2->query("SELECT playcount FROM Scrobbles WHERE username = " . $username . " AND track = " . $track . " AND artist = " . $artist);
if(PEAR::isError($res)) {
die("FAILED " . $res->getMessage());
}
if(!$res->numRows()) {
// New scrobble entry
$mdb2->query("INSERT INTO Scrobbles (username, artist, track, lastplayed, playcount) VALUES ("
. $username . ", "
. $artist . ", "
. $track . ", "
. $time . ", "
. "1)");
} else {
$playcount = $res->fetchOne(0);
$playcount++;
$mdb2->query("UPDATE Scrobbles SET "
. "playcount = " . $playcount . ", "
. "lastplayed = " . $time
. " WHERE username = " . $username
. " AND artist = " . $artist
. " AND track = " . $track);
}
}
die("OK");
?>
<?php
require_once('PHPUnit/Framework.php');
require_once('HTTP/Request.php');
class SubmissionsTest extends PHPUnit_Framework_TestCase
{
......@@ -16,6 +17,19 @@ class SubmissionsTest extends PHPUnit_Framework_TestCase
$this->assertEquals("BADAUTH", trim($result[0]));
}
public function testScrobble() {
$result = $this->standardLogin("testuser", "password");
$this->assertEquals("OK", trim($result[0]));
$session_id = trim($result[1]);
$scrobble_server = trim($result[3]);
$result = $this->scrobble($scrobble_server, $session_id, "Richard Stallman", "The Free Software Song");
echo $result;
$this->assertEquals("OK", $result);
}
private function standardLogin($username, $password) {
require("../config.php");
......@@ -25,4 +39,16 @@ class SubmissionsTest extends PHPUnit_Framework_TestCase
return $response;
}
private function scrobble($server, $session_id, $artist, $track) {
$r = new HTTP_Request($server);
$r->setMethod(HTTP_REQUEST_METHOD_POST);
$r->addPostData('s', $session_id);
$r->addPostData('a[0]', $artist);
$r->addPostData('t[0]', $track);
$r->addPostData('i[0]', time());
$r->addPostData('o[0]', 'U');
$r->sendRequest();
return $r->getResponseBody();
}
}
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