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

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) {
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) {
if(profiles) {
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)) {
subnext();
} else {
console.log('comparing ' + p1.profile.lastfm + ' and ' + p2.profile.lastfm);
this.api.tasteCompare(p1.profile.lastfm, p2.profile.lastfm, function(err, comp) {
if(!err) {
var score = Math.floor(comp.score * 100);
@ -249,7 +282,6 @@ var lastfm = function(dbot) {
}.bind(this), function() { next(); });
}.bind(this), function(err) {
// Now we better structure the scores for sorting
console.log(scores);
var goodScores = [];
_.each(scores, function(subscores, p1) {
_.each(subscores, function(aScore, p2) {
@ -261,36 +293,15 @@ var lastfm = function(dbot) {
});
});
console.log(goodScores);
var tastiest = _.chain(goodScores)
.sortBy(function(p) { return p.score; })
.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));
});
});
this.tastyCache = goodScores;
this.tastyCacheStamp = new Date().getTime();
sortGoodScores(goodScores);
}.bind(this));
} else {
event.reply('No suitable profiles');
}
}.bind(this));
}
},
'~artists': function(event) {