From 1808a007f4b26beaefdfbd190fc1a1265ba33a8b Mon Sep 17 00:00:00 2001 From: Akamaru Date: Mon, 17 Oct 2016 17:09:30 +0200 Subject: [PATCH] Neues Plugin: ibSearch.lua --- miku/plugins/ibSearch.lua | 72 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 miku/plugins/ibSearch.lua diff --git a/miku/plugins/ibSearch.lua b/miku/plugins/ibSearch.lua new file mode 100644 index 0000000..bf436ba --- /dev/null +++ b/miku/plugins/ibSearch.lua @@ -0,0 +1,72 @@ +local ibSearch = {} + +function ibSearch:init(config) + ibSearch.triggers = { + "^/(ibsearch) (.+)$", + "^/(ibnsfw) (.+)$" + } + ibSearch.doc = [[* +]]..config.cmd_pat..[[ibsearch* __: Sucht auf IbSear.ch mit diesen Tags +]]..config.cmd_pat..[[ibnsfw* __: Sucht auf IbSearch.xxx mit diesen Tags (NSFW) +]] +end + +ibSearch.command = 'ibsearch ' + +local limit = 100 -- number of posts to return (higher = more choices for random, but longer load times, hard limit of 100 posts) + +function ibSearch:get_ibSearch_sfw(tag) + local url = 'https://ibsear.ch/api/v1/images.json?q='..tag..'&limit='..limit..'&shuffle=20' + local res, code = https.request(url) + if code ~= 200 then return end + local data = json.decode(res) + if not data then return end + + local i = math.random(#data) + local url = 'https://im1.ibsear.ch/'..data[i].path + local resolution = data[i].width..'x'..data[i].height..'px' + local size = ' '..data[i].size..'kb' + local source_url = '\nhttps://ibsear.ch/images/'..data[i].id + local text = resolution..size..source_url + return url, text +end +function ibSearch:get_ibSearch_nsfw(tag) + local url = 'https://ibsearch.xxx/api/v1/images.json?q='..tag..'&limit='..limit..'&shuffle=20' + local res, code = https.request(url) + if code ~= 200 then return end + local data = json.decode(res) + if not data then return end + + local i = math.random(#data) + local url = 'https://im1.ibsearch.xxx/'..data[i].path + local resolution = data[i].width..'x'..data[i].height..'px' + local size = ' '..data[i].size..'kb' + local source_url = '\nhttps://ibsearch.xxx/images/'..data[i].id + local text = resolution..size..source_url + return url, text +end + +function ibSearch:action(msg, config, matches) + local tag = string.gsub(matches[2], " ", '_') + local tag = string.gsub(tag, ":", '%%3A') + local tag = string.gsub(tag, "+", '%%2B') + if matches[1] == 'ibsearch' then + url, id = ibSearch:get_ibSearch_sfw(tag) + elseif matches[1] == 'ibnsfw' then + url, id = ibSearch:get_ibSearch_nsfw(tag) + end + + if not url then + utilities.send_reply(msg, 'Nobody here but us chickens!') + return + end + utilities.send_typing(msg.chat.id, 'upload_photo') + local file = download_to_file(url) + if string.ends(url, ".gif") then + utilities.send_document(msg.chat.id, file, id, msg.message_id) + else + utilities.send_photo(msg.chat.id, file, id, msg.message_id) + end +end + +return ibSearch \ No newline at end of file