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,157 +9,240 @@ 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.api = { this.internalAPI = {
'getListening': function(user, callback) { 'getLastFM': function(server, nick, callback) {
dbot.api.profile.getProfileByUUID(user.id, function(profile) { dbot.api.profile.getProfile(server, nick, function(err, user, profile) {
if(profile && profile.profile.lastfm != null) { if(user) {
profile = profile.profile; if(profile && _.has(profile.profile, 'lastfm')) {
request.get(this.ApiRoot, { callback(user, profile.profile.lastfm);
'qs': { } else {
'user': profile.lastfm, callback(user, null);
'limit': 2, }
'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) {
callback('no_user', user, null);
} else if(_.has(body, 'recenttracks') && !_.isUndefined(body.recenttracks.track[0])) {
callback(null, user, body.recenttracks.track[0]);
} else {
callback('no_listen', user, null);
}
});
} else { } else {
callback('no_profile', user, null); callback(null, null);
} }
}.bind(this)); });
}
};
this.api = {
'getRandomArtistTrack': function(mbid, callback) {
request.get(this.ApiRoot, {
'qs': {
'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,
'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) {
callback('no_user', null);
} else if(_.has(body, 'recenttracks') && !_.isUndefined(body.recenttracks.track[0])) {
callback(null, body.recenttracks.track[0]);
} else {
callback('no_listen', null);
}
});
}, },
'tasteCompare': function(user, oUser, callback) { 'tasteCompare': function(user, oUser, callback) {
dbot.api.profile.getProfileByUUID(user.id, function(profile) { request.get(this.ApiRoot, {
if(profile && profile.profile.lastfm != null) { 'qs': {
profile = profile.profile; 'type1': 'user',
dbot.api.profile.getProfileByUUID(oUser.id, function(oProfile) { 'type2': 'user',
if(oProfile && oProfile.profile.lastfm != null) { 'value1': user,
oProfile = oProfile.profile; 'value2': oUser,
request.get(this.ApiRoot, { 'method': 'tasteometer.compare',
'qs': { 'api_key': this.config.api_key,
'type1': 'user', 'format': 'json'
'type2': 'user', },
'value1': profile.lastfm, 'json': true
'value2': oProfile.lastfm, }, function(err, res, body) {
'method': 'tasteometer.compare', if(_.has(body, 'error') && body.error == 6 || body.error == 7) {
'api_key': this.config.api_key, callback('no_user', user, null);
'format': 'json' } else if(_.has(body, 'comparison') && _.has(body.comparison, 'result')) {
}, callback(null, body.comparison.result);
'json': true
}, function(err, res, body) {
console.log(body);
if(_.has(body, 'error') && body.error == 6 || body.error == 7) {
callback('no_user', user, null);
} else if(_.has(body, 'comparison') && _.has(body.comparison, 'result')) {
callback(null, body.comparison.result);
} else {
callback('idk', null);
}
});
} else {
callback('no_oprofile', null);
}
}.bind(this));
} else { } else {
callback('no_profile', null); callback('idk', null);
} }
}.bind(this)); });
} }
}; };
this.commands = { this.commands = {
'~listening': function(event) { '~suggestion': function(event) {
var outputListening = function(err, user, track) { if(event.rProfile && _.has(event.rProfile, 'lastfm')) {
if(!err) { this.api.getListening(event.rProfile.lastfm, function(err, track) {
var term = track.name + ' ' + track.artist['#text'], if(!err) {
output = ''; this.api.getSimilarArtists(track.artist.mbid, function(err, similar) {
if(_.has(track, '@attr') && _.has(track['@attr'], 'nowplaying') && track['@attr'].nowplaying == 'true') { if(!err) {
output = dbot.t('now_listening', { var choice = _.random(0, similar.length - 1);
'user': user.currentNick, this.api.getRandomArtistTrack(similar[choice].mbid, function(err, track) {
'track': track.name, if(!err) {
'artist': track.artist['#text'] var output = dbot.t('lfm_suggestion', {
}); 'user': event.user,
} else { 'name': track.name,
output = dbot.t('last_listened', { 'artist': track.artist.name
'user': user.currentNick, });
'track': track.name, var term = track.name + ' ' + track.artist.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];
dbot.api.youtube.search(term, function(body) { link = v.link[0].href.match(dbot.modules.youtube.LinkRegex);
if(_.isObject(body) && _.has(body, 'feed') && _.has(body.feed, 'entry')) { if(link) {
var v = body.feed.entry[0]; output += ' - http://youtu.be/' + link[2];
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));
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 { } else {
event.reply('Unrecognised user.'); 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)); }.bind(this));
} else { } else {
this.api.getListening(event.rUser, outputListening); event.reply(event.user + ': Set a lastfm username with "~set lastfm username"');
}
},
'~listening': function(event) {
var getListening = function(user, lfm) {
if(user && lfm) {
this.api.getListening(lfm, 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 LastFM user.');
} else if(err == 'no_listen') {
event.reply(dbot.t('no_listen', { 'user': user.currentNick }));
}
}
});
} 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]) {
this.internalAPI.getLastFM(event.server, event.params[1], getListening);
} else {
getListening(event.rUser, event.rProfile.lastfm);
} }
}, },
'~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 {
if(err == 'no_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'); if(err == 'no_user') {
event.reply('Unknown Last.FM user.');
} else {
event.reply('Well something went wrong and I don\'t know what it means');
}
} }
});
} else {
if(!u2) {
event.reply('No such user.');
} else {
event.reply(u2.currentNick + ': Set a lastfm username with "~set lastfm username"');
} }
}); }
} else { }.bind(this));
event.reply('Unknown user.'); } else {
} event.reply(event.user + ': Set a lastfm username with "~set lastfm username"');
}.bind(this)); }
} }
}; };
}; };

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);
}; };