tastiest caching

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

View File

@ -228,69 +228,80 @@ var lastfm = function(dbot) {
}, },
'~tastiest': function(event) { '~tastiest': function(event) {
dbot.api.profile.getAllProfilesWith('lastfm', function(profiles) { var sortGoodScores = function(goodScores) {
if(profiles) { var tastiest = _.chain(goodScores)
var scores = {}; // Using this structure first for easier testing in the async .sortBy(function(p) { return p.score; })
async.eachSeries(profiles, function(p1, next) { .reverse()
scores[p1.id] = {}; .first(10)
async.eachSeries(profiles, function(p2, subnext) { .value();
if(p1.id == p2.id || p1.profile.lastfm == p2.profile.lastfm || _.has(scores, p2.id) && _.has(scores[p2.id], p1.id)) {
subnext(); async.each(tastiest, function(pair, done) {
} else { if(!_.isObject(pair.p1)) { // fix this
console.log('comparing ' + p1.profile.lastfm + ' and ' + p2.profile.lastfm); dbot.api.users.getUser(pair.p1, function(user) {
this.api.tasteCompare(p1.profile.lastfm, p2.profile.lastfm, function(err, comp) { pair.p1 = user;
if(!err) { dbot.api.users.getUser(pair.p2, function(user) {
var score = Math.floor(comp.score * 100); pair.p2 = user;
scores[p1.id][p2.id] = score; done();
}
subnext();
});
}
}.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) {
goodScores.push({
'p1': p1,
'p2': p2,
'score': aScore
});
}); });
}); });
} else {
console.log(goodScores); done();
}
var tastiest = _.chain(goodScores) }, function() {
.sortBy(function(p) { return p.score; }) var output = 'Most musically compatible users: ';
.reverse() _.each(tastiest, function(pair) {
.first(10) output += pair.p1.currentNick + ' & ' +
.value(); pair.p2.currentNick + ' (' + pair.score +
'%), ';
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 { event.reply(output.slice(0, -2));
event.reply('No suitable profiles'); });
} };
}.bind(this));
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
async.eachSeries(profiles, function(p1, next) {
scores[p1.id] = {};
async.eachSeries(profiles, function(p2, subnext) {
if(p1.id == p2.id || p1.profile.lastfm == p2.profile.lastfm || _.has(scores, p2.id) && _.has(scores[p2.id], p1.id)) {
subnext();
} else {
this.api.tasteCompare(p1.profile.lastfm, p2.profile.lastfm, function(err, comp) {
if(!err) {
var score = Math.floor(comp.score * 100);
scores[p1.id][p2.id] = score;
}
subnext();
});
}
}.bind(this), function() { next(); });
}.bind(this), function(err) {
// Now we better structure the scores for sorting
var goodScores = [];
_.each(scores, function(subscores, p1) {
_.each(subscores, function(aScore, p2) {
goodScores.push({
'p1': p1,
'p2': p2,
'score': aScore
});
});
});
this.tastyCache = goodScores;
this.tastyCacheStamp = new Date().getTime();
sortGoodScores(goodScores);
}.bind(this));
} else {
event.reply('No suitable profiles');
}
}.bind(this));
}
}, },
'~artists': function(event) { '~artists': function(event) {