Skip to content

Commit

Permalink
Add scrobble & nowPlaying methods to lastfm JS
Browse files Browse the repository at this point in the history
  • Loading branch information
anantn committed May 26, 2011
1 parent d74cfcb commit e58fc24
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
75 changes: 67 additions & 8 deletions lib/lastfm.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var api_base = "http://ws.audioscrobbler.com/2.0/";
var api_auth = "http://www.last.fm/api/auth/";

function LFMRequest() {
this._params = {};
this._uri = api_base;
}
LFMRequest.prototype = {
Expand Down Expand Up @@ -45,7 +46,7 @@ LFMRequest.prototype = {
return hexed;
},

_doCall: function(cb) {
_doGet: function(cb) {
this._uri += "?";
for (let prop in this._params) {
this._uri = this._uri + prop + "=" + this._params[prop] + "&";
Expand All @@ -55,35 +56,80 @@ LFMRequest.prototype = {
/* We were including format=json before forming the signature, but
* last.fm doesn't like it :( */
this._uri += "&format=json";
request.Request({
url: this._uri,
onComplete: function(response) {
cb(response.json);
}
}).get();
},

let req = request.Request({
_doPost: function(cb) {
let sig = this._makeSig();
this._uri += "?format=json";
this._params["api_sig"] = sig;

request.Request({
url: this._uri,
content: this._params,
onComplete: function(response) {
cb(response.json);
}
});
req.get();
}).post();
},

getToken: function(cb) {
this._params = {};
this._method("auth.gettoken");
this._doCall(function(result) {
this._doGet(function(result) {
cb(result.token);
});
},

getSession: function(token, cb) {
this._params = {};
this._method("auth.getsession");
this._params["token"] = token;
this._doCall(function(result) {
this._doGet(function(result) {
if ("error" in result) {
cb(false);
} else {
cb(result.session);
}
});
},

scrobble: function(song, key, cb) {
this._method("track.scrobble");

this._params["sk"] = key;
this._params["track"] = song.title;
this._params["artist"] = song.artist;
this._params["timestamp"] = song.started;

this._doPost(function(result) {
if ("error" in result) {
cb(false);
console.log("Error in scrobble: " + result.message);
} else {
cb(result);
}
});
},

nowPlaying: function(song, key, cb) {
this._method("track.updatenowplaying");

this._params["sk"] = key;
this._params["track"] = song.title;
this._params["artist"] = song.artist;

this._doPost(function(result) {
if ("error" in result) {
cb(false);
console.log("Error in scrobble: " + result.message);
} else {
cb(result);
}
});
}
};

Expand All @@ -108,4 +154,17 @@ function authorize(cb) {
});
}

function scrobble(song, key, cb) {
let sreq = new LFMRequest();
sreq.scrobble(song, key, cb);
}

function nowPlaying(song, key, cb) {
let nreq = new LFMRequest();
nreq.nowPlaying(song, key, cb);
}

exports.scrobble = scrobble;
exports.authorize = authorize;
exports.nowPlaying = nowPlaying;

6 changes: 4 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ const simple = require("simple-storage");

/* Scrobble function */
function scrobbleIt(song) {
console.log("Gonna scrobbleIt! " + song.title + "\n");
console.log("Gonna scrobbleIt! " + song.title);
lastfm.scrobble(song, simple.storage.lastfm.key, function() {});
}
function nowPlaying(song) {
console.log("Gonna update nowPlaying! " + song.title + "\n");
console.log("Gonna update nowPlaying! " + song.title);
lastfm.nowPlaying(song, simple.storage.lastfm.key, function() {});
}

/* Setup pageMods */
Expand Down
2 changes: 1 addition & 1 deletion lib/pandora.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Pandora.prototype = {
this._scrobble({
'title': song.title,
'artist': song.artist,
'started': Math.round(Date.now() - this._playedFor)
'started': Math.round(Date.now() / 1000 - this._playedFor)
});
}
this._playedFor = 0;
Expand Down

0 comments on commit e58fc24

Please sign in to comment.