dbot/modules/lastfm/lastfm.js

60 lines
2.4 KiB
JavaScript
Raw Normal View History

2013-10-23 20:15:23 +00:00
/**
* 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) {
2013-10-23 20:25:42 +00:00
if(profile && profile.profile,lastfm != null) {
2013-10-23 20:15:23 +00:00
profile = profile.profile;
request.get(this.ApiRoot, {
'qs': {
'user': profile.lastfm,
2013-10-23 20:25:42 +00:00
'limit': 2,
2013-10-23 20:15:23 +00:00
'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];
if(_.has(track, '@attr') && _.has(track['@attr'], 'nowplaying') && track['@attr'].nowplaying == 'true') {
2013-10-24 09:37:54 +00:00
event.reply(dbot.t('now_listening', {
'user': event.user,
'track': track.name,
'artist': track.artist['#text']
}));
} else {
2013-10-24 09:37:54 +00:00
event.reply(dbot.t('last_listened', {
'user': event.user,
'track': track.name,
'artist': track.artist['#text']
}));
}
2013-10-23 20:15:23 +00:00
} else {
2013-10-24 09:37:54 +00:00
event.reply(dbot.t('no_listen', { 'user': event.user }));
2013-10-23 20:15:23 +00:00
}
});
} else {
event.reply('Set a lastfm username with "~set lastfm username"');
}
}.bind(this));
}
};
};
exports.fetch = function(dbot) {
return new lastfm(dbot);
};