3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-27 14:29:29 +01:00

fancy ass too complicated suggestion thing + lastfm module rewrite w/ proper api

This commit is contained in:
reality 2013-10-24 20:31:49 +00:00
parent 6d81b21b43
commit 96926ab5fa
4 changed files with 224 additions and 129 deletions

View File

@ -9,14 +9,66 @@ var _ = require('underscore')._,
var lastfm = function(dbot) { var lastfm = function(dbot) {
this.ApiRoot = 'http://ws.audioscrobbler.com/2.0/'; this.ApiRoot = 'http://ws.audioscrobbler.com/2.0/';
this.internalAPI = {
'getLastFM': function(server, nick, callback) {
dbot.api.profile.getProfile(server, nick, function(err, user, profile) {
if(user) {
if(profile && _.has(profile.profile, 'lastfm')) {
callback(user, profile.profile.lastfm);
} else {
callback(user, null);
}
} else {
callback(null, null);
}
});
}
};
this.api = { this.api = {
'getListening': function(user, callback) { 'getRandomArtistTrack': function(mbid, callback) {
dbot.api.profile.getProfileByUUID(user.id, function(profile) {
if(profile && profile.profile.lastfm != null) {
profile = profile.profile;
request.get(this.ApiRoot, { request.get(this.ApiRoot, {
'qs': { 'qs': {
'user': profile.lastfm, 'method': 'artist.gettoptracks',
'mbid': mbid,
'api_key': this.config.api_key,
'format': 'json'
},
'json': true
}, function(err, res, body) {
if(_.has(body, 'toptracks') && _.has(body.toptracks, 'track')) {
var tracks = body.toptracks.track;
choice = _.random(0, tracks.length - 1),
track = tracks[choice];
callback(null, track);
} else {
callback('idk', body);
}
});
},
'getSimilarArtists': function(mbid, callback) {
request.get(this.ApiRoot, {
'qs': {
'method': 'artist.getsimilar',
'mbid': mbid,
'api_key': this.config.api_key,
'format': 'json'
},
'json': true
}, function(err, res, body) {
if(_.has(body, 'similarartists') && _.has(body.similarartists, 'artist')) {
callback(null, body.similarartists.artist);
} else {
callback('idk', body);
}
});
},
'getListening': function(username, callback) {
request.get(this.ApiRoot, {
'qs': {
'user': username,
'limit': 2, 'limit': 2,
'nowplaying': true, 'nowplaying': true,
'method': 'user.getrecenttracks', 'method': 'user.getrecenttracks',
@ -26,40 +78,28 @@ var lastfm = function(dbot) {
'json': true 'json': true
}, function(err, res, body) { }, function(err, res, body) {
if(_.has(body, 'error') && body.error == 6) { if(_.has(body, 'error') && body.error == 6) {
callback('no_user', user, null); callback('no_user', null);
} else if(_.has(body, 'recenttracks') && !_.isUndefined(body.recenttracks.track[0])) { } else if(_.has(body, 'recenttracks') && !_.isUndefined(body.recenttracks.track[0])) {
callback(null, user, body.recenttracks.track[0]); callback(null, body.recenttracks.track[0]);
} else { } else {
callback('no_listen', user, null); callback('no_listen', null);
} }
}); });
} else {
callback('no_profile', user, null);
}
}.bind(this));
}, },
'tasteCompare': function(user, oUser, callback) { 'tasteCompare': function(user, oUser, callback) {
dbot.api.profile.getProfileByUUID(user.id, function(profile) {
if(profile && profile.profile.lastfm != null) {
profile = profile.profile;
dbot.api.profile.getProfileByUUID(oUser.id, function(oProfile) {
if(oProfile && oProfile.profile.lastfm != null) {
oProfile = oProfile.profile;
request.get(this.ApiRoot, { request.get(this.ApiRoot, {
'qs': { 'qs': {
'type1': 'user', 'type1': 'user',
'type2': 'user', 'type2': 'user',
'value1': profile.lastfm, 'value1': user,
'value2': oProfile.lastfm, 'value2': oUser,
'method': 'tasteometer.compare', 'method': 'tasteometer.compare',
'api_key': this.config.api_key, 'api_key': this.config.api_key,
'format': 'json' 'format': 'json'
}, },
'json': true 'json': true
}, function(err, res, body) { }, function(err, res, body) {
console.log(body);
if(_.has(body, 'error') && body.error == 6 || body.error == 7) { if(_.has(body, 'error') && body.error == 6 || body.error == 7) {
callback('no_user', user, null); callback('no_user', user, null);
} else if(_.has(body, 'comparison') && _.has(body.comparison, 'result')) { } else if(_.has(body, 'comparison') && _.has(body.comparison, 'result')) {
@ -68,20 +108,60 @@ var lastfm = function(dbot) {
callback('idk', null); callback('idk', null);
} }
}); });
} else {
callback('no_oprofile', null);
}
}.bind(this));
} else {
callback('no_profile', null);
}
}.bind(this));
} }
}; };
this.commands = { this.commands = {
'~suggestion': function(event) {
if(event.rProfile && _.has(event.rProfile, 'lastfm')) {
this.api.getListening(event.rProfile.lastfm, function(err, track) {
if(!err) {
this.api.getSimilarArtists(track.artist.mbid, function(err, similar) {
if(!err) {
var choice = _.random(0, similar.length - 1);
this.api.getRandomArtistTrack(similar[choice].mbid, function(err, track) {
if(!err) {
var output = dbot.t('lfm_suggestion', {
'user': event.user,
'name': track.name,
'artist': track.artist.name
});
var term = track.name + ' ' + track.artist.name;
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 {
event.reply('something broke');
}
});
} else {
event.reply('something broke');
}
}.bind(this));
} else {
if(err == 'no_user') {
event.reply('Unknown Last.FM user.');
} else if(err == 'no_listen') {
event.reply(dbot.t('no_listen', { 'user': event.user }));
}
}
}.bind(this));
} else {
event.reply(event.user + ': Set a lastfm username with "~set lastfm username"');
}
},
'~listening': function(event) { '~listening': function(event) {
var outputListening = function(err, user, track) { var getListening = function(user, lfm) {
if(user && lfm) {
this.api.getListening(lfm, function(err, track) {
if(!err) { if(!err) {
var term = track.name + ' ' + track.artist['#text'], var term = track.name + ' ' + track.artist['#text'],
output = ''; output = '';
@ -110,56 +190,59 @@ var lastfm = function(dbot) {
}); });
} else { } else {
if(err == 'no_user') { if(err == 'no_user') {
event.reply('Unknown Last.FM user.'); event.reply('Unknown LastFM user.');
} else if(err == 'no_listen') { } else if(err == 'no_listen') {
event.reply(dbot.t('no_listen', { 'user': user.currentNick })); event.reply(dbot.t('no_listen', { 'user': user.currentNick }));
} else if(err == 'no_profile') {
event.reply('Set a lastfm username with "~set lastfm username"');
} }
} }
}; });
} else {
if(!user) {
event.reply('Unknown user.');
} else {
event.reply(user.currentNick + ': Set a lastfm username with "~set lastfm username"');
}
}
}.bind(this);
if(event.params[1]) { if(event.params[1]) {
dbot.api.users.resolveUser(event.server, event.params[1], function(user) { this.internalAPI.getLastFM(event.server, event.params[1], getListening);
if(user) {
this.api.getListening(user, outputListening);
} else { } else {
event.reply('Unrecognised user.'); getListening(event.rUser, event.rProfile.lastfm);
}
}.bind(this));
} else {
this.api.getListening(event.rUser, outputListening);
} }
}, },
'~taste': function(event) { '~taste': function(event) {
var oNick = event.params[1]; if(event.rProfile && _.has(event.rProfile, 'lastfm')) {
dbot.api.users.resolveUser(event.server, oNick, function(oUser) { this.internalAPI.getLastFM(event.server, event.params[1], function(u2, lfm2) {
if(oUser) { if(u2 && lfm2) {
this.api.tasteCompare(event.rUser, oUser, function(err, comp) { this.api.tasteCompare(event.rProfile.lastfm, lfm2, function(err, comp) {
if(!err) { if(!err) {
var score = Math.floor(comp.score * 100); var score = Math.floor(comp.score * 100);
event.reply(dbot.t('taste_compat', { event.reply(dbot.t('taste_compat', {
'user1': event.user, 'user1': event.user,
'user2': oUser.currentNick, 'user2': u2.currentNick,
'score': score 'score': score
})); }));
} else { } else {
if(err == 'no_user') { if(err == 'no_user') {
event.reply('Unknown Last.FM user.'); event.reply('Unknown Last.FM user.');
} else if(err == 'no_profile') {
event.reply(event.user + ': Set a lastfm username with "~set lastfm username"');
} else if(err == 'no_oprofile') {
event.reply(oUser.currentNick + ': Set a lastfm username with "~set lastfm username"');
} else { } else {
event.reply('Well something went wrong and I don\'t know what it means'); event.reply('Well something went wrong and I don\'t know what it means');
} }
} }
}); });
} else { } else {
event.reply('Unknown user.'); if(!u2) {
event.reply('No such user.');
} else {
event.reply(u2.currentNick + ': Set a lastfm username with "~set lastfm username"');
}
} }
}.bind(this)); }.bind(this));
} else {
event.reply(event.user + ': Set a lastfm username with "~set lastfm username"');
}
} }
}; };
}; };

View File

@ -10,5 +10,8 @@
}, },
"taste_compat": { "taste_compat": {
"en": "{user1} and {user2} are {score}% musically compatible!" "en": "{user1} and {user2} are {score}% musically compatible!"
},
"lfm_suggestion": {
"en": "{user}: Try listening to {name} by {artist}"
} }
} }

View File

@ -33,8 +33,7 @@ var api = function(dbot) {
this.db.read('profiles', user.id, function(err, profile){ this.db.read('profiles', user.id, function(err, profile){
if(!err){ if(!err){
callback(false, user, profile); callback(false, user, profile);
} } else {
else{
callback(true, user, null); callback(true, user, null);
} }
}); });

View File

@ -22,6 +22,16 @@ var profile = function(dbot) {
// Add API Hooks // Add API Hooks
dbot.api.event.addHook('new_user', this.api.createProfile); dbot.api.event.addHook('new_user', this.api.createProfile);
dbot.instance.addPreEmitHook(function(event, callback) {
if(!event.rUser) return callback();
this.api.getProfileByUUID(event.rUser.id, function(uProfile) {
if(uProfile) {
event.rProfile = uProfile.profile;
callback();
}
}.bind(this));
}.bind(this));
//TODO(@samstudio8) Profile Merging //TODO(@samstudio8) Profile Merging
//dbot.api.command.addHook('~mergeusers', this.api.mergeProfile); //dbot.api.command.addHook('~mergeusers', this.api.mergeProfile);
}; };