diff --git a/lib/index.js b/lib/index.js index 5f523c8..d30b2c0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) { @@ -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 = { @@ -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; @@ -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; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..4e7e751 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,92 @@ +{ + "name": "simple-lastfm", + "version": "1.1.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "simple-lastfm", + "version": "1.1.0", + "dependencies": { + "crypto": ">= 0.0.3", + "querystring": ">= 0.1.0", + "xml2js": ">= 0.1.13" + }, + "devDependencies": {}, + "engines": { + "node": ">= 4.8.3" + } + }, + "node_modules/crypto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/crypto/-/crypto-1.0.1.tgz", + "integrity": "sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==", + "deprecated": "This package is no longer supported. It's now a built-in Node module. If you've depended on crypto, you should switch to the one that's built-in." + }, + "node_modules/querystring": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.1.tgz", + "integrity": "sha512-wkvS7mL/JMugcup3/rMitHmd9ecIGd2lhFhK9N3UUQ450h66d1r3Y9nvXzQAW1Lq+wyx61k/1pfKS5KuKiyEbg==", + "deprecated": "The querystring API is considered Legacy. new code should use the URLSearchParams API instead.", + "engines": { + "node": ">=0.4.x" + } + }, + "node_modules/sax": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", + "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==" + }, + "node_modules/xml2js": { + "version": "0.4.23", + "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.23.tgz", + "integrity": "sha512-ySPiMjM0+pLDftHgXY4By0uswI3SPKLDw/i3UXbnO8M/p28zqexCUoPmQFrYD+/1BzhGJSs2i1ERWKJAtiLrug==", + "dependencies": { + "sax": ">=0.6.0", + "xmlbuilder": "~11.0.0" + }, + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/xmlbuilder": { + "version": "11.0.1", + "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-11.0.1.tgz", + "integrity": "sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==", + "engines": { + "node": ">=4.0" + } + } + }, + "dependencies": { + "crypto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/crypto/-/crypto-1.0.1.tgz", + "integrity": "sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==" + }, + "querystring": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.1.tgz", + "integrity": "sha512-wkvS7mL/JMugcup3/rMitHmd9ecIGd2lhFhK9N3UUQ450h66d1r3Y9nvXzQAW1Lq+wyx61k/1pfKS5KuKiyEbg==" + }, + "sax": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", + "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==" + }, + "xml2js": { + "version": "0.4.23", + "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.23.tgz", + "integrity": "sha512-ySPiMjM0+pLDftHgXY4By0uswI3SPKLDw/i3UXbnO8M/p28zqexCUoPmQFrYD+/1BzhGJSs2i1ERWKJAtiLrug==", + "requires": { + "sax": ">=0.6.0", + "xmlbuilder": "~11.0.0" + } + }, + "xmlbuilder": { + "version": "11.0.1", + "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-11.0.1.tgz", + "integrity": "sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==" + } + } +} diff --git a/package.json b/package.json index 2645f84..c2b000a 100644 --- a/package.json +++ b/package.json @@ -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"