This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/miku/plugins/ibSearch.lua

87 lines
2.9 KiB
Lua
Raw Normal View History

2016-10-17 17:09:30 +02:00
local ibSearch = {}
function ibSearch:init(config)
ibSearch.triggers = {
"^/(ibsearch) (.+)$",
"^/(ibnsfw) (.+)$"
}
ibSearch.doc = [[*
]]..config.cmd_pat..[[ibsearch* _<Tags>_: Sucht auf IbSear.ch mit diesen Tags
]]..config.cmd_pat..[[ibnsfw* _<Tags>_: Sucht auf IbSearch.xxx mit diesen Tags (NSFW)
]]
end
ibSearch.command = 'ibsearch <Tags>'
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)
2016-10-17 22:58:32 +02:00
local url = 'https://'..data[i].server..'.ibsear.ch/'..data[i].path
2016-10-17 17:09:30 +02:00
local resolution = data[i].width..'x'..data[i].height..'px'
2016-11-13 23:02:31 +01:00
local size = ' '..math.ceil(data[i].size/1000)
2016-10-17 17:09:30 +02:00
local source_url = '\nhttps://ibsear.ch/images/'..data[i].id
2016-11-13 23:02:31 +01:00
local text = resolution..size..'kb'..source_url
return url, size, text
2016-10-17 17:09:30 +02:00
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)
2016-10-17 22:58:32 +02:00
local url = 'https://'..data[i].server..'.ibsearch.xxx/'..data[i].path
2016-10-17 17:09:30 +02:00
local resolution = data[i].width..'x'..data[i].height..'px'
2016-11-13 23:02:31 +01:00
local size = ' '..math.ceil(data[i].size/1000)
2016-10-17 17:09:30 +02:00
local source_url = '\nhttps://ibsearch.xxx/images/'..data[i].id
2016-11-13 23:02:31 +01:00
local text = resolution..size..'kb'..source_url
return url, size, text
2016-10-17 17:09:30 +02:00
end
function ibSearch:action(msg, config, matches)
local tag = string.gsub(matches[2], " ", '_')
local tag = string.gsub(tag, ":", '%%3A')
2016-10-18 20:38:25 +02:00
local tag = string.gsub(tag, "+", '%%20')
2016-10-17 17:09:30 +02:00
if matches[1] == 'ibsearch' then
2016-11-13 23:02:31 +01:00
url, size, text = ibSearch:get_ibSearch_sfw(tag)
2016-10-17 17:09:30 +02:00
elseif matches[1] == 'ibnsfw' then
2016-11-13 23:02:31 +01:00
url, size, text = ibSearch:get_ibSearch_nsfw(tag)
end
print('URL: '..url)
-- don't send GIFs when they're bigger than 20 MB
-- don't send photos when they're bigger than 5 MB
if string.ends(url, ".gif") then
if tonumber(size) > 19900 then
utilities.send_reply(msg, 'Sorry, die GIF ist zu groß.\n'..text)
return
end
else
if tonumber(size) > 4900 then
utilities.send_reply(msg, 'Sorry, das Bild ist zu groß.\n'..text)
return
end
2016-10-17 17:09:30 +02:00
end
if not url then
utilities.send_reply(msg, 'Nobody here but us chickens!')
return
end
2016-11-13 23:02:31 +01:00
utilities.send_typing(msg.chat.id, 'upload_photo')
2016-10-17 17:09:30 +02:00
if string.ends(url, ".gif") then
2016-11-13 23:02:31 +01:00
utilities.send_document(msg.chat.id, url, text, msg.message_id)
2016-10-17 17:09:30 +02:00
else
2016-11-13 23:02:31 +01:00
utilities.send_photo(msg.chat.id, url, text, msg.message_id)
2016-10-17 17:09:30 +02:00
end
end
return ibSearch