From d271b6672f42b2b2a7a9bd1e403e75da7fd1af60 Mon Sep 17 00:00:00 2001 From: reality Date: Tue, 10 Sep 2013 17:07:34 +0000 Subject: [PATCH] ri cache, replace ri with ssri, move ri to lri --- modules/imgur/config.json | 1 + modules/imgur/imgur.js | 46 ++++++++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/modules/imgur/config.json b/modules/imgur/config.json index c1899c7..b2d3b48 100644 --- a/modules/imgur/config.json +++ b/modules/imgur/config.json @@ -2,6 +2,7 @@ "dbKeys": [ "imgur" ], "dependencies": [ "web", "api", "link" ], "imagelength": 5, + "ricachelength": 7, "nsfwwarn": true, "apikey": "86fd3a8da348b65", "highscore": "ricount", diff --git a/modules/imgur/imgur.js b/modules/imgur/imgur.js index 74a09c9..112ba00 100644 --- a/modules/imgur/imgur.js +++ b/modules/imgur/imgur.js @@ -9,6 +9,7 @@ var _ = require('underscore')._, var imgur = function(dbot) { this.ApiRoot = 'https://api.imgur.com/3/'; + this.riCache = []; this.internalAPI = { 'infoString': function(imgData) { @@ -172,6 +173,31 @@ var imgur = function(dbot) { if(event.params[1]) { local = event.params.splice(1, event.params.length - 1).join(' ').trim(); } + + var postImage = function(link, imgData) { + var info = this.internalAPI.infoString(imgData); + event.reply(local + ': ' + link + ' [' + info + ']'); + }.bind(this); + var newCacheImage = function(link, imgData) { + this.riCache.push([link, imgData]); + }.bind(this); + var callback = postImage; + + if(this.riCache.length > 0) { + var image = this.riCache.pop(); + postImage(image[0], image[1]); + callback = newCacheImage; + } + + this.api.getGoodRandomImage(callback); + }, + + // Legacy RI + '~lri': function(event) { + var local = event.user; + if(event.params[1]) { + local = event.params.splice(1, event.params.length - 1).join(' ').trim(); + } this.api.getRandomImage(function(link, slug) { this.api.getImageInfo(slug, function(imgData) { var info = this.internalAPI.infoString(imgData); @@ -179,19 +205,7 @@ var imgur = function(dbot) { }.bind(this)); }.bind(this)); }, - - // Super Slow RI - '~ssri': function(event) { - var local = event.user; - if(event.params[1]) { - local = event.params.splice(1, event.params.length - 1).join(' ').trim(); - } - this.api.getGoodRandomImage(function(link, imgData) { - var info = this.internalAPI.infoString(imgData); - event.reply(local + ': ' + link + ' [' + info + ']'); - }.bind(this)); - }, - + // Super RI '~sri': function(event) { var local = event.user; @@ -273,6 +287,12 @@ var imgur = function(dbot) { dbot.api.link.addHandler('imgurimage', /https?:\/\/i\.imgur\.com\/([a-zA-Z0-9]+)\.([jpg|png|gif])/, imgurHandler); dbot.api.link.addHandler('imgurimage', /https?:\/\/imgur\.com\/([a-zA-Z0-9]+)/, imgurHandler); + for(var i=0;i