From ea0aab80e29fbdf487ef63dcbfd7424e351b9f8c Mon Sep 17 00:00:00 2001 From: reality Date: Tue, 29 Oct 2013 16:17:22 +0000 Subject: [PATCH] tastiest caching --- modules/lastfm/lastfm.js | 133 +++++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 61 deletions(-) diff --git a/modules/lastfm/lastfm.js b/modules/lastfm/lastfm.js index d1334bb..31783aa 100644 --- a/modules/lastfm/lastfm.js +++ b/modules/lastfm/lastfm.js @@ -228,69 +228,80 @@ var lastfm = function(dbot) { }, '~tastiest': function(event) { - 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 { - 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); - scores[p1.id][p2.id] = score; - } - 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 - }); + 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(); }); - }); - - 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)); - }); + }); + } else { + done(); + } + }, function() { + var output = 'Most musically compatible users: '; + _.each(tastiest, function(pair) { + output += pair.p1.currentNick + ' & ' + + pair.p2.currentNick + ' (' + pair.score + + '%), '; }); - } else { - event.reply('No suitable profiles'); - } - }.bind(this)); + 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 + 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) {