From 3291db820b0990a7cc9e525cdb9447506f2d0369 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Tue, 5 Jul 2016 15:25:08 +0200 Subject: [PATCH] =?UTF-8?q?-=20Caching=20f=C3=BCr=20gImages=20-=209GAG=20m?= =?UTF-8?q?it=20InlineKeyboard=20f=C3=BCr=20Post-URL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- otouto/plugins/9gag.lua | 5 +++-- otouto/plugins/gImages.lua | 43 ++++++++++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/otouto/plugins/9gag.lua b/otouto/plugins/9gag.lua index 15590da..bd6b87e 100644 --- a/otouto/plugins/9gag.lua +++ b/otouto/plugins/9gag.lua @@ -22,19 +22,20 @@ function ninegag:get_9GAG() -- random max json table size local i = math.random(#gag) local link_image = gag[i].src local title = gag[i].title + local post_url = gag[i].url return link_image, title, post_url end function ninegag:action(msg, config) utilities.send_typing(self, msg.chat.id, 'upload_photo') - local url, title = ninegag:get_9GAG() + local url, title, post_url = ninegag:get_9GAG() if not url then utilities.send_reply(self, msg, config.errors.connection) return end local file = download_to_file(url) - utilities.send_photo(self, msg.chat.id, file, title, msg.message_id) + utilities.send_photo(self, msg.chat.id, file, title, msg.message_id, '{"inline_keyboard":[[{"text":"Post aufrufen","url":"'..post_url..'"}]]}') end return ninegag diff --git a/otouto/plugins/gImages.lua b/otouto/plugins/gImages.lua index d63e430..2dc9015 100644 --- a/otouto/plugins/gImages.lua +++ b/otouto/plugins/gImages.lua @@ -6,6 +6,7 @@ local gImages = {} local HTTPS = require('ssl.https') local URL = require('socket.url') local JSON = require('dkjson') +local redis = (loadfile "./otouto/redis.lua")() local utilities = require('otouto.utilities') local bindings = require('otouto.bindings') @@ -63,12 +64,27 @@ function gImages:callback(callback, msg, self, config, input) end function gImages:get_image(input) - local apikey = cred_data.google_apikey_2 -- 100 requests is RIDICULOUS Google! + local hash = 'telegram:cache:gImages' + local results = redis:smembers(hash..':'..string.lower(input)) + if results[1] then + print('getting image from cache') + local i = math.random(#results) + local img_url = results[i] + local mime = redis:hget(hash..':'..img_url, 'mime') + local contextLink = redis:hget(hash..':'..img_url, 'contextLink') + return img_url, mime, contextLink + end + + local apikey = cred_data.google_apikey_2 -- 100 requests is RIDICULOUS, Google! local cseid = cred_data.google_cse_id_2 local BASE_URL = 'https://www.googleapis.com/customsearch/v1' - local url = BASE_URL..'/?searchType=image&alt=json&num=10&key='..apikey..'&cx='..cseid..'&safe=high'..'&q=' .. input .. '&fields=searchInformation(totalResults),queries(request(count)),items(link,mime,image(contextLink))' + local url = BASE_URL..'/?searchType=image&alt=json&num=10&key='..apikey..'&cx='..cseid..'&safe=high'..'&q=' .. input .. '&fields=items(link,mime,image(contextLink))' local jstr, res = HTTPS.request(url) - local jdat = JSON.decode(jstr) + local jdat = JSON.decode(jstr).items + + if not jdat then + return 'NORESULTS' + end if jdat.error then if jdat.error.code == 403 then @@ -78,13 +94,22 @@ function gImages:get_image(input) end end - - if jdat.searchInformation.totalResults == '0' then - return 'NORESULTS' - end + gImages:cache_result(jdat, input) + local i = math.random(#jdat) + return jdat[i].link, jdat[i].mime, jdat[i].image.contextLink +end - local i = math.random(jdat.queries.request[1].count) - return jdat.items[i].link, jdat.items[i].mime, jdat.items[i].image.contextLink +function gImages:cache_result(results, text) + local cache = {} + for v in pairs(results) do + table.insert(cache, results[v].link) + end + for n, link in pairs(cache) do + redis:hset('telegram:cache:gImages:'..link, 'mime', results[n].mime) + redis:hset('telegram:cache:gImages:'..link, 'contextLink', results[n].image.contextLink) + redis:expire('telegram:cache:gImages:'..link, 1209600) + end + cache_data('gImages', string.lower(text), cache, 1209600, 'set') end function gImages:action(msg, config, matches)