tastiest caching

This commit is contained in:
reality 2013-10-29 16:17:22 +00:00
parent ba8a603e26
commit ea0aab80e2

View File

@ -228,6 +228,40 @@ var lastfm = function(dbot) {
}, },
'~tastiest': function(event) { '~tastiest': function(event) {
var sortGoodScores = function(goodScores) {
var tastiest = _.chain(goodScores)
.sortBy(function(p) { return p.score; })
.reverse()
.first(10)
.value();
async.each(tastiest, function(pair, done) {
if(!_.isObject(pair.p1)) { // fix this
dbot.api.users.getUser(pair.p1, function(user) {
pair.p1 = user;
dbot.api.users.getUser(pair.p2, function(user) {
pair.p2 = user;
done();
});
});
} else {
done();
}
}, function() {
var output = 'Most musically compatible users: ';
_.each(tastiest, function(pair) {
output += pair.p1.currentNick + ' & ' +
pair.p2.currentNick + ' (' + pair.score +
'%), ';
});
event.reply(output.slice(0, -2));
});
};
if(this.tastyCache && Date.now() - this.tastyCacheStamp <= 1800000) {
sortGoodScores(this.tastyCache);
} else {
event.reply('Updating tasty cache... Hold onto your coconuts...');
dbot.api.profile.getAllProfilesWith('lastfm', function(profiles) { dbot.api.profile.getAllProfilesWith('lastfm', function(profiles) {
if(profiles) { if(profiles) {
var scores = {}; // Using this structure first for easier testing in the async var scores = {}; // Using this structure first for easier testing in the async
@ -237,7 +271,6 @@ var lastfm = function(dbot) {
if(p1.id == p2.id || p1.profile.lastfm == p2.profile.lastfm || _.has(scores, p2.id) && _.has(scores[p2.id], p1.id)) { if(p1.id == p2.id || p1.profile.lastfm == p2.profile.lastfm || _.has(scores, p2.id) && _.has(scores[p2.id], p1.id)) {
subnext(); subnext();
} else { } else {
console.log('comparing ' + p1.profile.lastfm + ' and ' + p2.profile.lastfm);
this.api.tasteCompare(p1.profile.lastfm, p2.profile.lastfm, function(err, comp) { this.api.tasteCompare(p1.profile.lastfm, p2.profile.lastfm, function(err, comp) {
if(!err) { if(!err) {
var score = Math.floor(comp.score * 100); var score = Math.floor(comp.score * 100);
@ -249,7 +282,6 @@ var lastfm = function(dbot) {
}.bind(this), function() { next(); }); }.bind(this), function() { next(); });
}.bind(this), function(err) { }.bind(this), function(err) {
// Now we better structure the scores for sorting // Now we better structure the scores for sorting
console.log(scores);
var goodScores = []; var goodScores = [];
_.each(scores, function(subscores, p1) { _.each(scores, function(subscores, p1) {
_.each(subscores, function(aScore, p2) { _.each(subscores, function(aScore, p2) {
@ -261,36 +293,15 @@ var lastfm = function(dbot) {
}); });
}); });
console.log(goodScores); this.tastyCache = goodScores;
this.tastyCacheStamp = new Date().getTime();
var tastiest = _.chain(goodScores) sortGoodScores(goodScores);
.sortBy(function(p) { return p.score; }) }.bind(this));
.reverse()
.first(10)
.value();
async.each(tastiest, function(pair, done) {
dbot.api.users.getUser(pair.p1, function(user) {
pair.p1 = user;
dbot.api.users.getUser(pair.p2, function(user) {
pair.p2 = user;
done();
});
});
}, function() {
var output = 'Most musically compatible users: ';
_.each(tastiest, function(pair) {
output += pair.p1.currentNick + ' & ' +
pair.p2.currentNick + ' (' + pair.score +
'%), ';
});
event.reply(output.slice(0, -2));
});
});
} else { } else {
event.reply('No suitable profiles'); event.reply('No suitable profiles');
} }
}.bind(this)); }.bind(this));
}
}, },
'~artists': function(event) { '~artists': function(event) {