From 5cf4735ddff86923f7952103c54e96e7f64d7f63 Mon Sep 17 00:00:00 2001 From: ChrisW-B Date: Wed, 23 Mar 2016 11:24:55 -0400 Subject: [PATCH 1/5] Added getTopTracks --- lib/index.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/index.js b/lib/index.js index 449525f..d3f1f9f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -438,6 +438,32 @@ Lastfm.prototype.getTopArtists = function(opt) { }); }; +Lastfm.prototype.getTopTracks = function(opt) { + var lastfm = this; + var the_callback = opt.callback; + delete opt.callback; + lastfm.doGet({ + method: 'user.gettoptracks', + args: opt, + callback: function(result) { + if(typeof the_callback === 'function') + { + if (result['@'].status == 'ok') { + the_callback({ + success: true, + topTracks: result.toptracks.track + }); + } else { + the_callback({ + success: false, + error: result.error['#'] + }); + } + } + } + }); +}; + Lastfm.prototype.getSimilarArtists = function(opt) { var lastfm = this; var the_callback = opt.callback; From a4d64e9b3122494486de558860e52a648395c704 Mon Sep 17 00:00:00 2001 From: ChrisW-B Date: Wed, 23 Mar 2016 11:41:33 -0400 Subject: [PATCH 2/5] Updated jquery version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ab69017..a337a32 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "crypto": ">= 0.0.3", "xml2js": ">= 0.1.13", "querystring": ">= 0.1.0", - "jquery": "= 1.8.3" + "jquery": "*" }, "devDependencies": {}, "optionalDependencies": {} From 474909b0d9a75e2c481f6a953d60c4dfa0d026c4 Mon Sep 17 00:00:00 2001 From: Christopher Barry Date: Wed, 6 Apr 2016 14:46:50 -0400 Subject: [PATCH 3/5] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a337a32..be57779 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "crypto": ">= 0.0.3", "xml2js": ">= 0.1.13", "querystring": ">= 0.1.0", - "jquery": "*" + "jquery": ">= 1.8.3" }, "devDependencies": {}, "optionalDependencies": {} From 27d9c4e5c9c37e0bbbaf07652c146fb08372c8ce Mon Sep 17 00:00:00 2001 From: Christopher Barry Date: Sun, 11 Sep 2016 18:59:00 -0400 Subject: [PATCH 4/5] Added recent tracks --- lib/index.js | 422 +++++++++++++++++++++++++-------------------------- 1 file changed, 203 insertions(+), 219 deletions(-) diff --git a/lib/index.js b/lib/index.js index d3f1f9f..48d250c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,7 +3,6 @@ var crypto = require('crypto'); var xml2js = require('xml2js'); var querystring = require('querystring'); var $ = require('jquery'); - var Lastfm = function(options) { options = options || {}; var api_key; @@ -11,30 +10,26 @@ var Lastfm = function(options) { var username; var password; var session_key; - var self = this; var _isTheMethodCaller = false; - this.debug = options.debug || false; - this.api_key = options.api_key; - if(options.api_secret != undefined && options.api_secret != '') - 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.session_key != undefined && options.session_key != '') - this.session_key = options.session_key; - + if (options.api_secret != undefined && options.api_secret != '') 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.session_key != undefined && options.session_key != '') this.session_key = options.session_key; // Privileged method - available to public methods but not to the instance itself. self._getInfo = function(opt) { - if(!self._isTheMethodCaller) throw new Error('Security exception.'); + if (!self._isTheMethodCaller) throw new Error('Security exception.'); try { - if(opt.artist == undefined || opt.artist == '' && typeof opt.callback == 'function') { + if (opt.artist == undefined || opt.artist == '' && typeof opt.callback == 'function') { opt.callback({ - '@': {status: 'error'}, - error: {'#': 'Artist not specified.'} + '@': { + status: 'error' + }, + error: { + '#': 'Artist not specified.' + } }); } else { http.get({ @@ -56,26 +51,19 @@ var Lastfm = function(options) { }); }); } - } catch(e) { - if(this.debug) - console.log("Exception getting track info: ", e); + } catch (e) { + if (this.debug) console.log("Exception getting track info: ", e); } }; }; - // Left for backwards compatibility Lastfm.prototype.init = function(options) { this.api_key = options.api_key; - if(options.api_secret != undefined && options.api_secret != '') - 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.session_key != undefined && options.session_key != '') - this.session_key = options.session_key; + if (options.api_secret != undefined && options.api_secret != '') 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.session_key != undefined && options.session_key != '') this.session_key = options.session_key; }; - Lastfm.prototype.getSessionKey = function(callback) { var authToken = md5(this.username + md5(this.password)); var sig = 'api_key' + this.api_key + 'authToken' + authToken + 'methodauth.getMobileSessionusername' + this.username + this.api_secret; @@ -84,11 +72,7 @@ Lastfm.prototype.getSessionKey = function(callback) { http.get({ host: 'ws.audioscrobbler.com', port: 80, - path: '/2.0/?method=auth.getMobileSession&' + - 'username=' + this.username + '&' + - 'authToken=' + authToken + '&' + - 'api_key=' + this.api_key + '&' + - 'api_sig=' + api_sig + path: '/2.0/?method=auth.getMobileSession&' + 'username=' + this.username + '&' + 'authToken=' + authToken + '&' + 'api_key=' + this.api_key + '&' + 'api_sig=' + api_sig }, function(res) { var body = ''; res.on('data', function(chunk) { @@ -101,82 +85,78 @@ Lastfm.prototype.getSessionKey = function(callback) { var ret = { success: result['@'].status == 'ok' }; - if(ret.success) { + if (ret.success) { ret.session_key = result.session.key; lastfmObj.session_key = result.session.key; - } else - ret.error = result.error['#']; - if(typeof callback == 'function') { + } else ret.error = result.error['#']; + if (typeof callback == 'function') { callback(ret); } }); - } catch(e) { - if(lastfmObj.debug) - console.log("Exception: ", e); + } catch (e) { + if (lastfmObj.debug) console.log("Exception: ", e); } }); }); }; - Lastfm.prototype.scrobbleTrack = function(opt) { - var options = $.extend(opt || {}, {method: 'track.scrobble'}); + var options = $.extend(opt || {}, { + method: 'track.scrobble' + }); this.doScrobble(options); }; - Lastfm.prototype.loveTrack = function(opt) { - var options = $.extend(opt || {}, {method: 'track.love'}); + var options = $.extend(opt || {}, { + method: 'track.love' + }); this.doScrobble(options); }; - Lastfm.prototype.unloveTrack = function(opt) { - var options = $.extend(opt || {}, {method: 'track.unlove'}); + var options = $.extend(opt || {}, { + method: 'track.unlove' + }); this.doScrobble(options); }; - Lastfm.prototype.scrobbleNowPlayingTrack = function(opt) { - var options = $.extend(opt || {}, {method: 'track.updateNowPlaying'}); + var options = $.extend(opt || {}, { + method: 'track.updateNowPlaying' + }); this.doScrobble(options); }; - Lastfm.prototype.addTrackTags = function(opt) { - var options = $.extend(opt || {}, {method: 'track.addTags'}); + var options = $.extend(opt || {}, { + method: 'track.addTags' + }); this.doScrobble(options); }; - Lastfm.prototype.doScrobble = function(options) { - if(this.debug) - console.log("Starting scrobbleTrack: ", options); + if (this.debug) console.log("Starting scrobbleTrack: ", options); options = options || {}; - if((this.api_secret == undefined || this.api_secret == '') && typeof options.callback == 'function') { + if ((this.api_secret == undefined || this.api_secret == '') && typeof options.callback == 'function') { options.callback({ success: false, error: 'API Secret not specified.' }); } - if((this.username == undefined || this.username == '') && typeof options.callback == 'function') { + if ((this.username == undefined || this.username == '') && typeof options.callback == 'function') { options.callback({ success: false, error: 'Username not specified.' }); } - if((this.password == undefined || this.password == '') && typeof options.callback == 'function') { + if ((this.password == undefined || this.password == '') && typeof options.callback == 'function') { options.callback({ success: false, error: 'Password not specified.' }); } - -// session.scrobbled = true; - options.timestamp = options.timestamp != undefined ? Math.floor(options.timestamp) : Math.floor(now() / 1000); - + // 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"); + if (this.debug) console.log("Using session key: " + this.session_key + "\n\n"); var 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); - var post_obj = { api_key: this.api_key, method: options.method, @@ -186,49 +166,43 @@ Lastfm.prototype.doScrobble = function(options) { artist: options.artist, track: options.track }; - - if(options.tags != null) - post_obj.tags = options.tags; + if (options.tags != null) post_obj.tags = options.tags; var post_data = querystring.stringify(post_obj); - -// console.log("post_data: ", post_data); - + // console.log("post_data: ", post_data); var post_options = { host: 'ws.audioscrobbler.com', - port: '80', - path: '/2.0/', - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - 'Content-Length': post_data.length - } + port: '80', + path: '/2.0/', + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + 'Content-Length': post_data.length + } }; - var post_req = http.request(post_options, function(res) { res.setEncoding('utf8'); res.on('data', function(chunk) { -// console.log('Response: ' + chunk); + // console.log('Response: ' + chunk); var parser = new xml2js.Parser(xml2js.defaults["0.1"]); parser.parseString(chunk, function(err, result) { try { if (result['@'].status == 'ok') { -// console.log("Track scrobbled (" + options.method + " )"); - if(typeof options.callback == 'function') { + // console.log("Track scrobbled (" + options.method + " )"); + if (typeof options.callback == 'function') { options.callback({ success: true }); } } else { - if(typeof options.callback == 'function') { + if (typeof options.callback == 'function') { options.callback({ success: false, error: result.error['#'] }); } } - } catch(e) { - if(this.debug) - console.log("Exception parsing scrobble result: ", e); + } catch (e) { + if (this.debug) console.log("Exception parsing scrobble result: ", e); } }); }); @@ -236,26 +210,25 @@ Lastfm.prototype.doScrobble = function(options) { post_req.write(post_data); post_req.end(); }; - Lastfm.prototype.getTrackInfo = function(opt) { opt = opt || {}; - if(opt.artist == undefined || opt.artist == '' && typeof opt.callback == 'function') { + if (opt.artist == undefined || opt.artist == '' && typeof opt.callback == 'function') { opt.callback({ success: false, error: 'Artist not specified.' }); - } else if(opt.track == undefined || opt.track == '' && typeof opt.callback == 'function') { + } else if (opt.track == undefined || opt.track == '' && typeof opt.callback == 'function') { opt.callback({ success: false, error: 'Track not specified.' }); - } else if(typeof opt.callback == 'function') { + } else if (typeof opt.callback == 'function') { var the_callback = opt.callback; this._isTheMethodCaller = true; this._getInfo($.extend(opt, { callback: function(result) { this._isTheMethodCaller = false; - if(result['@'].status == 'ok') { + if (result['@'].status == 'ok') { the_callback({ success: true, trackInfo: result.track @@ -270,22 +243,21 @@ Lastfm.prototype.getTrackInfo = function(opt) { })); } }; - Lastfm.prototype.getArtistInfo = function(opt) { opt = opt || {}; opt.track = ''; - if(opt.artist == undefined || opt.artist == '' && typeof opt.callback == 'function') { + if (opt.artist == undefined || opt.artist == '' && typeof opt.callback == 'function') { opt.callback({ success: false, error: 'Artist not specified.' }); - } else if(typeof opt.callback == 'function') { + } else if (typeof opt.callback == 'function') { var the_callback = opt.callback; this._isTheMethodCaller = true; this._getInfo($.extend(opt, { callback: function(result) { this._isTheMethodCaller = false; - if(result['@'].status == 'ok') { + if (result['@'].status == 'ok') { the_callback({ success: true, artistInfo: result.artist @@ -300,26 +272,23 @@ Lastfm.prototype.getArtistInfo = function(opt) { })); } }; - Lastfm.prototype.getTags = function(opt) { var the_callback = opt.callback; this._isTheMethodCaller = true; this._getInfo($.extend(opt, { callback: function(result) { this._isTheMethodCaller = false; -// console.log("result: ", result); - if(typeof the_callback == 'function') { - if(result['@'].status == 'ok') { + // console.log("result: ", result); + if (typeof the_callback == 'function') { + if (result['@'].status == 'ok') { var tags = opt.track != undefined && opt.track != '' ? result.track.toptags.tag : result.artist.tags.tag; - if(typeof tags == 'object' && !tags.length) - tags = [tags]; + if (typeof tags == 'object' && !tags.length) tags = [tags]; var args = { - success: true, - tags: tags || [], - artist: opt.track != undefined && opt.track != '' ? result.track.artist.name : result.artist.name - }; - if(opt.track != undefined && opt.track != '') - args.track = result.track.name; + success: true, + tags: tags || [], + artist: opt.track != undefined && opt.track != '' ? result.track.artist.name : result.artist.name + }; + if (opt.track != undefined && opt.track != '') args.track = result.track.name; the_callback(args); } else { the_callback({ @@ -331,24 +300,21 @@ Lastfm.prototype.getTags = function(opt) { } })); }; - Lastfm.prototype.getPlays = function(opt) { var the_callback = opt.callback; this._isTheMethodCaller = true; this._getInfo($.extend(opt, { callback: function(result) { this._isTheMethodCaller = false; - if(typeof the_callback == 'function') { - if(result['@'].status == 'ok') { + if (typeof the_callback == 'function') { + if (result['@'].status == 'ok') { var ret = { success: true, plays: opt.track != undefined && opt.track != '' ? result.track.userplaycount : result.artist.stats.userplaycount, artist: opt.track != undefined && opt.track != '' ? result.track.artist.name : result.artist.name }; - if(ret.plays == undefined) - ret.plays = 0; - if(opt.track != undefined && opt.track != '') - ret.track = result.track.name; + if (ret.plays == undefined) ret.plays = 0; + if (opt.track != undefined && opt.track != '') ret.track = result.track.name; the_callback(ret); } else { the_callback({ @@ -360,9 +326,8 @@ Lastfm.prototype.getPlays = function(opt) { } })); }; - Lastfm.prototype.getTracks = function(opt) { -// var the_callback = opt.callback; + // var the_callback = opt.callback; var page = opt.page ? opt.page : 1; http.get({ host: 'ws.audioscrobbler.com', @@ -383,136 +348,156 @@ Lastfm.prototype.getTracks = function(opt) { }); }); }; - Lastfm.prototype.getAllTracks = function(opt) { var lastfm = this; var the_callback = opt.callback; var tracks = []; opt.callback = function(result) { - if(result['@'].status == 'failed') { + if (result['@'].status == 'failed') { the_callback({ success: false, reason: result.error['#'] }); } else { var numPages = result.artisttracks['@'].totalPages; - for(var i=0;i Date: Sun, 11 Sep 2016 19:00:50 -0400 Subject: [PATCH 5/5] Fixed typo --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 48d250c..a34bf2c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -438,7 +438,7 @@ Lastfm.prototype.getRecentTracks = function(opt) { if (result['@'].status == 'ok') { the_callback({ success: true, - recentTracks: result.recentTracks.track + recentTracks: result.recenttracks.track }); } else { the_callback({