From 60925fa6ddeb36b551d91c184e2b46765b8a07dc Mon Sep 17 00:00:00 2001 From: Akamaru Date: Sat, 5 Dec 2015 21:36:56 +0100 Subject: [PATCH] Upstream # 97c3e6be von Brawlbot --- bot/bot.lua | 2 +- bot/utils.lua | 13 +++++++++++++ plugins/bitly.lua | 13 ++++++++++--- plugins/gender.lua | 23 ++++++++++++++++------- 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/bot/bot.lua b/bot/bot.lua index 8bac362..63fe075 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -2,7 +2,7 @@ package.path = './.luarocks/share/lua/5.2/?.lua;./.luarocks/share/lua/5.2/?/init require("luarocks.loader") require("./bot/utils") -VERSION = '20151112' +VERSION = '20151205' -- This function is called when tg receive a msg function on_msg_receive (msg) diff --git a/bot/utils.lua b/bot/utils.lua index 51c6198..0b8c37e 100644 --- a/bot/utils.lua +++ b/bot/utils.lua @@ -640,4 +640,17 @@ function get_location(user_id) else return set_location end +end + +function cache_data(plugin, query, data, timeout, typ) + -- How to: cache_data(pluginname, query_name, data_to_cache, expire_in_seconds) + if not timeout then timeout = 86400 end + local hash = 'telegram:cache:'..plugin..':'..query + print('Caching "'..query..'" from plugin '..plugin..' (expires in '..timeout..' seconds)') + if typ == 'key' then + redis:set(hash, data) + else + redis:hmset(hash, data) + end + redis:expire(hash, timeout) end \ No newline at end of file diff --git a/plugins/bitly.lua b/plugins/bitly.lua index 404701e..c50b253 100644 --- a/plugins/bitly.lua +++ b/plugins/bitly.lua @@ -10,13 +10,20 @@ local function expand_bitly_link (shorturl) local url = BASE_URL..'?access_token='..access_token..'&shortUrl=https://bit.ly/'..shorturl local res,code = https.request(url) if code ~= 200 then return "HTTP-FEHLER" end - local data = json:decode(res).data - return data.expand[1].long_url + local data = json:decode(res).data.expand[1] + cache_data('bitly', shorturl, data, 5184000) + return data.long_url end local function run(msg, matches) local shorturl = matches[1] - return expand_bitly_link(shorturl) + local hash = 'telegram:cache:bitly:'..shorturl + if redis:exists(hash) == false then + return expand_bitly_link(shorturl) + else + local data = redis:hgetall(hash) + return data.long_url + end end return { diff --git a/plugins/gender.lua b/plugins/gender.lua index cb58c1f..61b3329 100644 --- a/plugins/gender.lua +++ b/plugins/gender.lua @@ -5,16 +5,17 @@ do local BASE_URL = 'https://gender-api.com/get' -function get_gender_data (name) +local function get_gender_data (name) local apikey = cred_data.gender_apikey local url = BASE_URL..'?name='..name..'&key='..apikey local res,code = https.request(url) if code ~= 200 then return "HTTP-FEHLER" end local data = json:decode(res) + cache_data('gender', string.lower(name), data, 345600) return data end -function send_gender_data(data, receiver) +local function send_gender_data(data, receiver) if data.gender == "female" then gender = 'weiblich' end @@ -29,18 +30,26 @@ function send_gender_data(data, receiver) send_msg(receiver, text, ok_cb, false) end -function run(msg, matches) +local function run(msg, matches) name = matches[1] - local data = get_gender_data(name) local receiver = get_receiver(msg) + local hash = 'telegram:cache:gender:'..string.lower(name) + if redis:exists(hash) == false then + data = get_gender_data(name) + else + data = redis:hgetall(hash) + end send_gender_data(data, receiver) end return { description = "Sendet Geschlecht", - usage = {"/geschlecht [Name]","/gender [Name]"}, - patterns = {"^/geschlecht (.*)$","^/gender (.*)$"}, + usage = "/geschlecht [Name]: Sendet Geschlecht", + patterns = { + "^/geschlecht (.*)$", + "^/gender (.*)$" + }, run = run } -end +end \ No newline at end of file