simple-lastfm
Advanced tools
Comparing version 1.0.5 to 1.0.6
135
lib/index.js
@@ -5,3 +5,2 @@ var http = require('http'); | ||
var querystring = require('querystring'); | ||
var $ = require('jquery'); | ||
@@ -14,2 +13,3 @@ var Lastfm = function(options) { | ||
var password; | ||
var authToken; | ||
var session_key; | ||
@@ -25,8 +25,10 @@ | ||
this.api_secret = options.api_secret; | ||
if(options.username != undefined && options.username != '') | ||
this.username = options.username; | ||
if(options.password != undefined && options.password != '') | ||
this.password = options.password; | ||
if(options.username != undefined && options.username != '') | ||
this.username = options.username; | ||
if(options.password != undefined && options.password != '') | ||
this.password = options.password; | ||
if(options.session_key != undefined && options.session_key != '') | ||
this.session_key = options.session_key; | ||
if(options.authToken != undefined && options.authToken != '') | ||
this.authToken = options.authToken; | ||
@@ -83,3 +85,3 @@ // Privileged method - available to public methods but not to the instance itself. | ||
Lastfm.prototype.getSessionKey = function(callback) { | ||
var authToken = md5(this.username + md5(this.password)); | ||
var authToken = this.authToken ? this.authToken : md5(this.username + md5(this.password)); | ||
var sig = 'api_key' + this.api_key + 'authToken' + authToken + 'methodauth.getMobileSessionusername' + this.username + this.api_secret; | ||
@@ -126,3 +128,3 @@ var api_sig = md5(sig); | ||
Lastfm.prototype.scrobbleTrack = function(opt) { | ||
var options = $.extend(opt || {}, {method: 'track.scrobble'}); | ||
var options = Object.assign(opt || {}, {method: 'track.scrobble'}); | ||
this.doScrobble(options); | ||
@@ -132,3 +134,3 @@ }; | ||
Lastfm.prototype.loveTrack = function(opt) { | ||
var options = $.extend(opt || {}, {method: 'track.love'}); | ||
var options = Object.assign(opt || {}, {method: 'track.love'}); | ||
this.doScrobble(options); | ||
@@ -138,3 +140,3 @@ }; | ||
Lastfm.prototype.unloveTrack = function(opt) { | ||
var options = $.extend(opt || {}, {method: 'track.unlove'}); | ||
var options = Object.assign(opt || {}, {method: 'track.unlove'}); | ||
this.doScrobble(options); | ||
@@ -144,6 +146,11 @@ }; | ||
Lastfm.prototype.scrobbleNowPlayingTrack = function(opt) { | ||
var options = $.extend(opt || {}, {method: 'track.updateNowPlaying'}); | ||
var options = Object.assign(opt || {}, {method: 'track.updateNowPlaying'}); | ||
this.doScrobble(options); | ||
}; | ||
Lastfm.prototype.addTrackTags = function(opt) { | ||
var options = Object.assign(opt || {}, {method: 'track.addTags'}); | ||
this.doScrobble(options); | ||
}; | ||
Lastfm.prototype.doScrobble = function(options) { | ||
@@ -165,3 +172,3 @@ if(this.debug) | ||
} | ||
if((this.password == undefined || this.password == '') && typeof options.callback == 'function') { | ||
if(((this.password == undefined || this.password == '') && !this.authToken) && typeof options.callback == 'function') { | ||
options.callback({ | ||
@@ -173,17 +180,11 @@ success: false, | ||
// session.scrobbled = true; | ||
options.timestamp = options.timestamp != undefined ? Math.floor(options.timestamp) : Math.floor(now() / 1000); | ||
//var timestamp = | ||
if(this.debug) | ||
console.log("Using session key: " + this.session_key + "\n\n"); | ||
var authToken = md5(this.username + md5(this.password)); | ||
// console.log("authToken = " + authToken); | ||
var sig = 'api_key' + this.api_key + 'artist' + options.artist + 'method' + options.method + 'sk' + this.session_key + 'timestamp' + options.timestamp + 'track' + options.track + this.api_secret; | ||
// console.log("sig = " + sig); | ||
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 api_sig = md5(sig); | ||
// console.log("api sig = " + api_sig); | ||
var post_data = querystring.stringify({ | ||
var post_obj = { | ||
api_key: this.api_key, | ||
@@ -196,4 +197,8 @@ method: options.method, | ||
track: options.track | ||
}); | ||
}; | ||
if(options.tags != null) | ||
post_obj.tags = options.tags; | ||
var post_data = querystring.stringify(post_obj); | ||
// console.log("post_data: ", post_data); | ||
@@ -260,3 +265,3 @@ | ||
this._isTheMethodCaller = true; | ||
this._getInfo($.extend(opt, { | ||
this._getInfo(Object.assign(opt, { | ||
callback: function(result) { | ||
@@ -291,3 +296,3 @@ this._isTheMethodCaller = false; | ||
this._isTheMethodCaller = true; | ||
this._getInfo($.extend(opt, { | ||
this._getInfo(Object.assign(opt, { | ||
callback: function(result) { | ||
@@ -314,3 +319,3 @@ this._isTheMethodCaller = false; | ||
this._isTheMethodCaller = true; | ||
this._getInfo($.extend(opt, { | ||
this._getInfo(Object.assign(opt, { | ||
callback: function(result) { | ||
@@ -346,6 +351,5 @@ this._isTheMethodCaller = false; | ||
this._isTheMethodCaller = true; | ||
this._getInfo($.extend(opt, { | ||
this._getInfo(Object.assign(opt, { | ||
callback: function(result) { | ||
this._isTheMethodCaller = false; | ||
// console.log("result: ", result); | ||
if(typeof the_callback == 'function') { | ||
@@ -425,2 +429,79 @@ if(result['@'].status == 'ok') { | ||
Lastfm.prototype.getTopArtists = function(opt) { | ||
var lastfm = this; | ||
var the_callback = opt.callback; | ||
delete opt.callback; | ||
lastfm.doGet({ | ||
method: 'user.gettopartists', | ||
args: opt, | ||
callback: function(result) { | ||
if(typeof the_callback === 'function') | ||
{ | ||
if (result['@'].status == 'ok') { | ||
the_callback({ | ||
success: true, | ||
topArtists: result.topartists.artist | ||
}); | ||
} else { | ||
the_callback({ | ||
success: false, | ||
error: result.error['#'] | ||
}); | ||
} | ||
} | ||
} | ||
}); | ||
}; | ||
Lastfm.prototype.getSimilarArtists = function(opt) { | ||
var lastfm = this; | ||
var the_callback = opt.callback; | ||
delete opt.callback; | ||
lastfm.doGet({ | ||
method: 'artist.getsimilar', | ||
args: opt, | ||
callback: function(result) { | ||
if(typeof the_callback === 'function') { | ||
if (result['@'].status == 'ok') { | ||
the_callback({ | ||
success: true, | ||
similarArtists: result.similarartists.artist | ||
}); | ||
} else { | ||
the_callback({ | ||
success: false, | ||
error: result.error['#'] | ||
}); | ||
} | ||
} | ||
} | ||
}); | ||
}; | ||
Lastfm.prototype.doGet = function(opt) { | ||
var lastfm = this; | ||
var the_callback = opt.callback; | ||
opt.args.api_key = this.api_key; | ||
opt.args.method = opt.method; | ||
var path = '/2.0/?' + querystring.stringify(opt.args); | ||
http.get({ | ||
host: 'ws.audioscrobbler.com', | ||
port: 80, | ||
path: path | ||
}, function(res) { | ||
var body = ''; | ||
res.on('data', function(chunk) { | ||
body += chunk; | ||
}); | ||
res.on('end', function() { | ||
var parser = new xml2js.Parser(xml2js.defaults["0.1"]); | ||
parser.parseString(body, function(err, result) { | ||
if (typeof the_callback == 'function') { | ||
the_callback(result); | ||
} | ||
}); | ||
}); | ||
}); | ||
}; | ||
function now() { | ||
@@ -427,0 +508,0 @@ return new Date().getTime(); |
{ | ||
"name": "simple-lastfm", | ||
"description": "simple last.fm api for node.js", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"repository": { | ||
@@ -15,3 +15,3 @@ "type": "git", | ||
"engines": { | ||
"node": "*" | ||
"node": ">= 4.8.3" | ||
}, | ||
@@ -21,4 +21,3 @@ "dependencies" : { | ||
"xml2js": ">= 0.1.13", | ||
"querystring": ">= 0.1.0", | ||
"jquery": ">= 1.6.3" | ||
"querystring": ">= 0.1.0" | ||
}, | ||
@@ -25,0 +24,0 @@ "devDependencies": {}, |
@@ -21,3 +21,4 @@ #Simple-Lastfm | ||
username: 'xxx', | ||
password: 'xxx' | ||
password: 'xxx', | ||
authToken: 'xxx' // Optional, you can use this instead of password, where authToken = md5(username + md5(password)) | ||
}); | ||
@@ -101,3 +102,13 @@ | ||
### addTrackTags ( options) | ||
Require parameters: | ||
* `artist` | ||
* `track` | ||
* `tags`: A comma delimited list of user supplied tags to apply to this track. Accepts a maximum of 10 tags. | ||
Optional parameters: | ||
* `callback`: A function which receives a single object, of the form { success: true|false[, error: 'text description of the error']}. | ||
### scrobbleTrack ( options ) | ||
@@ -143,11 +154,13 @@ Required parameters: | ||
### getTrackInfo (options) | ||
### getSimilarArtists (options) | ||
Required parameters: | ||
* `artist` | ||
* `track` | ||
Optional parameters: | ||
* `callback`: A function which receives a single object, of the form { success: true|false[, trackInfo: {}, error: 'text description of the error']}. | ||
* `limit`: The number of results to fetch per page. Defaults to 50. | ||
* `autocorrect`: [0, 1] Transform misspelled artist names into correct artist names, returning the correct version instead. The corrected artist name will be returned in the response. | ||
* `mbid`: The musicbrainz id for the artist. | ||
* `callback`: A function which receives a single object, of the form { success: true|false[, tags: {}, error: 'text description of the error']}. | ||
@@ -164,2 +177,24 @@ ### getTags (options) | ||
### getTopArtists (options) | ||
Required parameters: | ||
* `user` | ||
Optional parameters: | ||
* `period`: overall | 7day | 1month | 3month | 6month | 12month - The time period over which to retrieve top artists for. | ||
* `limit`: The number of results to fetch per page. Defaults to 50. | ||
* `page`: The page number to fetch. Defaults to first page. | ||
* `callback`: A function which receives a single object, of the form { success: true|false[, tags: {}, error: 'text description of the error']}. | ||
### getTrackInfo (options) | ||
Required parameters: | ||
* `artist` | ||
* `track` | ||
Optional parameters: | ||
* `callback`: A function which receives a single object, of the form { success: true|false[, trackInfo: {}, error: 'text description of the error']}. | ||
### getPlays (options) | ||
@@ -166,0 +201,0 @@ Required parameters: |
20365
3
468
206
3
- Removedjquery@>= 1.6.3
- Removedjquery@3.7.1(transitive)