From 7f2d813c8cacde8fed1cd2e230eb0524d7fd6c1d Mon Sep 17 00:00:00 2001 From: reality Date: Wed, 23 Oct 2013 20:15:23 +0000 Subject: [PATCH] lastfm module --- modules/lastfm/config.json | 4 ++++ modules/lastfm/lastfm.js | 47 ++++++++++++++++++++++++++++++++++++++ modules/profile/api.js | 16 +++---------- 3 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 modules/lastfm/config.json create mode 100644 modules/lastfm/lastfm.js diff --git a/modules/lastfm/config.json b/modules/lastfm/config.json new file mode 100644 index 0000000..4f737b1 --- /dev/null +++ b/modules/lastfm/config.json @@ -0,0 +1,4 @@ +{ + "dependencies": [ "profile" ], + "api_key": "blah" +} diff --git a/modules/lastfm/lastfm.js b/modules/lastfm/lastfm.js new file mode 100644 index 0000000..c62fbc2 --- /dev/null +++ b/modules/lastfm/lastfm.js @@ -0,0 +1,47 @@ +/** + * Module Name: Last.FM + * Description: Various lastfm functionality. + */ + +var _ = require('underscore')._, + request = require('request'); + +var lastfm = function(dbot) { + this.ApiRoot = 'http://ws.audioscrobbler.com/2.0/'; + + this.commands = { + '~listening': function(event) { + dbot.api.profile.getProfileByUUID(event.rUser.id, function(profile) { + if(profile && _.has(profile.profile, 'lastfm')) { + profile = profile.profile; + request.get(this.ApiRoot, { + 'qs': { + 'user': profile.lastfm, + 'limit': 1, + 'nowplaying': true, + 'method': 'user.getrecenttracks', + 'api_key': this.config.api_key, + 'format': 'json' + }, + 'json': true + }, function(err, res, body) { + if(_.has(body, 'error') && body.error == 6) { + event.reply('Unknown Last.FM user.'); + } else if(_.has(body, 'recenttracks') && !_.isUndefined(body.recenttracks.track[0])) { + var track = body.recenttracks.track[0]; + event.reply(event.user + ' is listening to ' + track.name + ' by ' + track.artist['#text']); + } else { + event.reply(event.user + ' isn\'t listening to anything right now :\'('); + } + }); + } else { + event.reply('Set a lastfm username with "~set lastfm username"'); + } + }.bind(this)); + } + }; +}; + +exports.fetch = function(dbot) { + return new lastfm(dbot); +}; diff --git a/modules/profile/api.js b/modules/profile/api.js index 24059b0..7341cb6 100644 --- a/modules/profile/api.js +++ b/modules/profile/api.js @@ -46,19 +46,9 @@ var api = function(dbot) { }, 'getProfileByUUID': function(uuid, callback){ - if(uuid){ - this.db.read('profiles', uuid, function(err, profile){ - if(!err){ - callback(false, uuid, profile); - } - else{ - callback(true, uuid, null); - } - }); - } - else{ - callback(true, null, null); - } + this.db.read('profiles', uuid, function(err, profile){ + callback(profile); + }); }, 'getAllProfiles': function(callback){