git.gnu.io has moved to IP address 209.51.188.249 -- please double check where you are logging in.

Commit 9024bd0f authored by Jonas Haraldsson's avatar Jonas Haraldsson

user.getrecenttracks from/to params

user.getrecenttracks&user=foo&from=1234567890 will only show scrobbles newer
than unix time 1234567890

user.getrecenttracks&user=foo&to=1234567890 will only show scrobbles older
than unix time 1234567890
parent 6673c78a
......@@ -169,6 +169,8 @@ function method_user_getTopArtists() {
* * **user** (required) : Name of the user.
* * **limit** (optional) : How many items to show. Defaults to 50.
* * **page** (optional) : The page to show. Defaults to 1.
* * **from** (optional) : Show items newer than this timestamp (UNIX format)
* * **to** (optional) : Show items older than this timestamp (UNIX format)
* * **format** (optional) : Format of response, **xml** or **json**. Default is xml.
* - - -
*
......@@ -184,7 +186,7 @@ function method_user_getRecentTracks() {
$limit = get_with_default('limit', 50);
$page = get_with_default('page', 1);
$xml = UserXML::getRecentTracks($_REQUEST['user'], $limit, $page);
$xml = UserXML::getRecentTracks($_REQUEST['user'], $limit, $page, $_REQUEST['from'], $_REQUEST['to']);
respond($xml);
}
......
......@@ -215,7 +215,7 @@ class UserXML {
return $xml;
}
public static function getRecentTracks($u, $limit, $page) {
public static function getRecentTracks($u, $limit, $page, $from, $to) {
global $adodb;
if (!isset($limit)) {
......@@ -229,7 +229,7 @@ class UserXML {
if ($page == 1) {
$npres = $user->getNowPlaying(1);
}
$res = $user->getScrobbles($limit, $offset);
$res = $user->getScrobbles($limit, $offset, $from, $to);
} catch (Exception $e) {
$err = 1;
}
......
......@@ -46,71 +46,57 @@ class Server {
* @param int $number The number of scrobbles to return
* @param int $userid The user id to return scrobbles for
* @param int $offset Amount of entries to skip before returning scrobbles
* @param int $from Only return scrobbles with time higher than this timestamp
* @param int $to Only return scrobbles with time lower than this timestamp
* @return array Scrobbles or null in case of failure
*/
static function getRecentScrobbles($number = 10, $userid = false, $offset = 0) {
static function getRecentScrobbles($number = 10, $userid = false, $offset = 0, $from = false, $to = false) {
global $adodb;
$adodb->SetFetchMode(ADODB_FETCH_ASSOC);
try {
if ($userid) {
$res = $adodb->CacheGetAll(60,
'SELECT Scrobbles.*, Loved_Tracks.userid as loved
FROM Scrobbles LEFT JOIN Loved_Tracks ON (Scrobbles.track=Loved_Tracks.track AND Scrobbles.artist=Loved_Tracks.artist AND Scrobbles.userid=Loved_Tracks.userid) WHERE Scrobbles.userid = ' . ($userid) . ' ORDER BY Scrobbles.time DESC LIMIT ' . (int)($number) . ' OFFSET ' . $offset);
/**
s.userid,
s.artist,
s.track,
s.album,
s.time,
s.mbid,
a.mbid AS artist_mbid,
l.mbid AS album_mbid,
l.image AS album_image,
l.artwork_license,
t.license,
t.mbid AS track_mbid
removed this.
LEFT JOIN Artist a
ON s.artist=a.name
LEFT JOIN Album l
ON l.artist_name=s.artist
AND l.name=s.album
LEFT JOIN Scrobble_Track st
ON s.stid = st.id
LEFT JOIN Track t
ON st.track = t.id
if ($userid) {
$params = array();
$query = 'SELECT s.*, lt.userid as loved FROM Scrobbles s LEFT JOIN Loved_Tracks lt ON (s.track=lt.track AND s.artist=lt.artist AND s.userid=lt.userid) WHERE s.userid=?';
$params[] = $userid;
*/
if ($from) {
$query .= ' AND s.time>?';
$params[] = (int) $from;
}
if ($to) {
$query .= ' AND s.time<?';
$params[] = (int) $to;
}
} else {
$params = array();
$query = 'SELECT s.* FROM Scrobbles s';
if ($from) {
$query .= ' WHERE s.time>?';
$params[] = (int) $from;
if ($to) {
$query .= ' AND s.time<?';
$params[] = (int) $to;
}
} else {
$res = $adodb->CacheGetAll(60,
'SELECT *
FROM Scrobbles ORDER BY time DESC
LIMIT ' . (int)($number) . ' OFFSET ' . $offset);
/**
if ($to) {
$query .= ' WHERE s.time<?';
$params[] = (int) $to;
}
}
}
LEFT JOIN Artist a
ON s.artist=a.name
LEFT JOIN Album l
ON l.artist_name=s.artist
AND l.name=s.album
LEFT JOIN Scrobble_Track st
ON s.stid = st.id
LEFT JOIN Track t
ON st.track = t.id
$query .= ' ORDER BY s.time DESC LIMIT ? OFFSET ?';
$params[] = (int) $number;
$params[] = (int) $offset;
*/
}
try {
$res = $adodb->CacheGetAll(60, $query, $params);
} catch (Exception $e) {
reportError($e->getMessage(), $e->getTraceAsString());
return null;
}
......
......@@ -182,10 +182,12 @@ class User {
*
* @param int $number The number of scrobbles to return
* @param int $offset The position of the first scrobble to return
* @param int $from Only return scrobbles with time higher than this timestamp
* @param int $to Only return scrobbles with time lower than this timestamp
* @return array An array of scrobbles with human time
*/
function getScrobbles($number, $offset = 0) {
$data = Server::getRecentScrobbles($number, $this->uniqueid, $offset);
function getScrobbles($number, $offset = 0, $from = false, $to = false) {
$data = Server::getRecentScrobbles($number, $this->uniqueid, $offset, $from, $to);
if ($data == null) {
return array();
......
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