From 8e791ed581ea36286b37c138c9ad7feb8a579920 Mon Sep 17 00:00:00 2001 From: reality Date: Thu, 24 Oct 2013 12:30:53 +0000 Subject: [PATCH] look at what others are listening to, fix output if user doesnt have lastfm proeprty set --- modules/lastfm/lastfm.js | 97 ++++++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 33 deletions(-) diff --git a/modules/lastfm/lastfm.js b/modules/lastfm/lastfm.js index 952c41f..0da4c28 100644 --- a/modules/lastfm/lastfm.js +++ b/modules/lastfm/lastfm.js @@ -9,10 +9,10 @@ var _ = require('underscore')._, 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 && profile.profile,lastfm != null) { + this.api = { + 'getListening': function(user, callback) { + dbot.api.profile.getProfileByUUID(user.id, function(profile) { + if(profile && profile.profile.lastfm != null) { profile = profile.profile; request.get(this.ApiRoot, { 'qs': { @@ -26,42 +26,73 @@ var lastfm = function(dbot) { 'json': true }, function(err, res, body) { if(_.has(body, 'error') && body.error == 6) { - event.reply('Unknown Last.FM user.'); + callback('no_user', null); } else if(_.has(body, 'recenttracks') && !_.isUndefined(body.recenttracks.track[0])) { - var track = body.recenttracks.track[0]; - term = track.name + ' ' + track.artist['#text'], - output = ''; - if(_.has(track, '@attr') && _.has(track['@attr'], 'nowplaying') && track['@attr'].nowplaying == 'true') { - output = dbot.t('now_listening', { - 'user': event.user, - 'track': track.name, - 'artist': track.artist['#text'] - }); - } else { - output = dbot.t('last_listened', { - 'user': event.user, - 'track': track.name, - 'artist': track.artist['#text'] - }); - } - dbot.api.youtube.search(term, function(body) { - if(_.isObject(body) && _.has(body, 'feed') && _.has(body.feed, 'entry')) { - var v = body.feed.entry[0]; - link = v.link[0].href.match(dbot.modules.youtube.LinkRegex); - if(link) { - output += ' - http://youtu.be/' + link[2]; - } - } - event.reply(output); - }); + callback(null, body.recenttracks.track[0]); } else { - event.reply(dbot.t('no_listen', { 'user': event.user })); + callback('no_listen', null); } }); } else { - event.reply('Set a lastfm username with "~set lastfm username"'); + callback('no_profile', null); } }.bind(this)); + + } + }; + + this.commands = { + '~listening': function(event) { + var outputListening = function(err, track) { + if(!err) { + var term = track.name + ' ' + track.artist['#text'], + output = ''; + if(_.has(track, '@attr') && _.has(track['@attr'], 'nowplaying') && track['@attr'].nowplaying == 'true') { + output = dbot.t('now_listening', { + 'user': user.currentNick, + 'track': track.name, + 'artist': track.artist['#text'] + }); + } else { + output = dbot.t('last_listened', { + 'user': user.currentNick, + 'track': track.name, + 'artist': track.artist['#text'] + }); + } + dbot.api.youtube.search(term, function(body) { + if(_.isObject(body) && _.has(body, 'feed') && _.has(body.feed, 'entry')) { + var v = body.feed.entry[0]; + link = v.link[0].href.match(dbot.modules.youtube.LinkRegex); + if(link) { + output += ' - http://youtu.be/' + link[2]; + } + } + event.reply(output); + }); + } else { + if(err == 'no_user') { + event.reply('Unknown Last.FM user.'); + } else if(err == 'no_listen') { + event.reply(dbot.t('no_listen', { 'user': user.currentNick })); + } else if(err == 'no_profile') { + event.reply('Set a lastfm username with "~set lastfm username"'); + } + } + }; + + if(event.params[1]) { + dbot.api.users.resolveUser(event.server, event.params[1], function(user) { + if(user) { + this.api.getListening(user, outputListening); + } else { + event.reply('Unrecognised user.'); + } + }.bind(this)); + } else { + user = event.rUser; + this.api.getListening(event.rUser, outputListening); + } } }; };