Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 76 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,21 @@ var Lastfm = function(options) {
error: {'#': 'Artist not specified.'}
});
} else {
var method = 'artist'
if (opt.track != undefined && opt.track != '') method = 'track'
var extraParams = '&artist=' + encodeURIComponent(opt.artist) + (opt.track == undefined || opt.track == '' ? '' : '&track=' + encodeURIComponent(opt.track))
if (opt.album != undefined && opt.album != '') {
method = 'album'
extraParams = '&artist=' + encodeURIComponent(opt.artist) + '&album=' + encodeURIComponent(opt.album)
}
if (opt.mbid != undefined && opt.mbid != '') {
method = 'album'
extraParams = '&mbid=' + encodeURIComponent(opt.mbid)
}
http.get({
host: 'ws.audioscrobbler.com',
port: 80,
path: '/2.0/?method=' + (opt.track != undefined && opt.track != '' ? 'track' : 'artist') + '.getinfo&api_key=' + this.api_key + '&autocorrect=1&username=' + this.username + '&artist=' + encodeURIComponent(opt.artist) + (opt.track == undefined || opt.track == '' ? '' : '&track=' + encodeURIComponent(opt.track))
path: '/2.0/?method=' + method + '.getinfo&api_key=' + this.api_key + '&autocorrect=1&username=' + this.username + extraParams
}, function(res) {
var body = '';
res.on('data', function(chunk) {
Expand Down Expand Up @@ -173,7 +184,7 @@ Lastfm.prototype.doScrobble = function(options) {
if(this.debug)
console.log("Using session key: " + this.session_key + "\n\n");
var authToken = this.authToken ? this.authToken : md5(this.username + md5(this.password));
var sig = 'api_key' + this.api_key + 'artist' + options.artist + 'method' + options.method + 'sk' + this.session_key + (options.tags != null ? 'tags' + options.tags : '') + 'timestamp' + options.timestamp + 'track' + options.track + this.api_secret;
var sig = ((options.album != null && options.album != undefined) ? 'album' + options.album : '') + 'api_key' + this.api_key + 'artist' + options.artist + 'method' + options.method + 'sk' + this.session_key + (options.tags != null ? 'tags' + options.tags : '') + 'timestamp' + options.timestamp + 'track' + options.track + this.api_secret;
var api_sig = md5(sig);

var post_obj = {
Expand All @@ -183,8 +194,12 @@ Lastfm.prototype.doScrobble = function(options) {
api_sig: api_sig,
timestamp: options.timestamp,
artist: options.artist,
track: options.track
track: options.track,
};

if(options.album) {
post_obj.album = options.album;
}

if(options.tags != null)
post_obj.tags = options.tags;
Expand Down Expand Up @@ -300,6 +315,64 @@ Lastfm.prototype.getArtistInfo = function(opt) {
}
};

Lastfm.prototype.getAlbumInfo = function(opt) {
opt = opt || {};
if(opt.album == undefined || opt.album == '' && typeof opt.callback == 'function') {
opt.callback({
success: false,
error: 'Album not specified.'
});
} else if(typeof opt.callback == 'function') {
var the_callback = opt.callback;
this._isTheMethodCaller = true;
this._getInfo(Object.assign(opt, {
callback: function(result) {
this._isTheMethodCaller = false;
if(result['@'].status == 'ok') {
the_callback({
success: true,
albumInfo: result.album
});
} else {
the_callback({
success: false,
error: result.error['#']
});
}
}
}));
}
};

Lastfm.prototype.getAlbumInfoByMbid = function(opt) {
opt = opt || {};
if(opt.mbid == undefined || opt.mbid == '' && typeof opt.callback == 'function') {
opt.callback({
success: false,
error: 'Album MBID not specified.'
});
} else if(typeof opt.callback == 'function') {
var the_callback = opt.callback;
this._isTheMethodCaller = true;
this._getInfo(Object.assign(opt, {
callback: function(result) {
this._isTheMethodCaller = false;
if(result['@'].status == 'ok') {
the_callback({
success: true,
albumInfo: result.album
});
} else {
the_callback({
success: false,
error: result.error['#']
});
}
}
}));
}
};

Lastfm.prototype.getTags = function(opt) {
var the_callback = opt.callback;
this._isTheMethodCaller = true;
Expand Down
92 changes: 92 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "simple-lastfm",
"description": "simple last.fm api for node.js",
"version": "1.0.6",
"version": "1.2.1",
"repository": {
"type": "git",
"url": "git://github.com/atomjack/simple-lastfm.git"
Expand Down