2016-08-18 17:23:55 +02:00
|
|
|
local yandere = {}
|
|
|
|
|
|
|
|
function yandere:init(config)
|
|
|
|
yandere.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('yan', true):t('yandere', true).table
|
|
|
|
yandere.doc = [[*
|
|
|
|
]]..config.cmd_pat..[[yan* _<Tags>_: Sucht auf Yandere mit diesen Tags]]
|
|
|
|
end
|
|
|
|
|
|
|
|
yandere.command = 'yan <Tags>'
|
|
|
|
|
|
|
|
function yandere:get_post(tag)
|
|
|
|
local BASE_URL = 'https://yande.re'
|
|
|
|
local url = BASE_URL..'/post.json?tags='..tag
|
|
|
|
local b,c = https.request(url)
|
|
|
|
if c ~= 200 then return nil end
|
|
|
|
local yan = json.decode(b)
|
|
|
|
if not yan[1] then return nil end
|
|
|
|
|
|
|
|
local i = math.random(#yan)
|
2016-11-29 18:14:26 +01:00
|
|
|
local url = yan[i].file_url
|
|
|
|
local resolution = yan[i].jpeg_width..'x'..yan[i].jpeg_height..'px'
|
|
|
|
local size = ' '..math.ceil(yan[i].file_size/1000)
|
|
|
|
local source_url = '\nhttps://yande.re/post/show/'..yan[i].id
|
|
|
|
local text = resolution..size..'kb'..source_url
|
|
|
|
return url, size, text
|
2016-08-18 17:23:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function yandere:action(msg, config)
|
|
|
|
local input = utilities.input_from_msg(msg)
|
|
|
|
if not input then
|
2016-08-24 17:18:17 +02:00
|
|
|
utilities.send_reply(msg, yandere.doc, true)
|
2016-08-18 17:23:55 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local tag = string.gsub(input, " ", '_' )
|
2016-11-29 18:14:26 +01:00
|
|
|
local tag = string.gsub(input, ":", '%%3A')
|
|
|
|
local tag = string.gsub(input, "+", '%%20')
|
|
|
|
local url, size, text = yandere:get_post(tag)
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2016-08-18 17:23:55 +02:00
|
|
|
if not url then
|
2016-11-29 18:14:26 +01:00
|
|
|
utilities.send_reply(msg, 'Nobody here but us chickens!')
|
2016-08-18 17:23:55 +02:00
|
|
|
return
|
|
|
|
end
|
2016-11-29 18:14:26 +01:00
|
|
|
|
2016-08-24 17:18:17 +02:00
|
|
|
utilities.send_typing(msg.chat.id, 'upload_photo')
|
2016-11-29 18:14:26 +01:00
|
|
|
if string.ends(url, ".gif") then
|
|
|
|
utilities.send_document(msg.chat.id, url, text, msg.message_id)
|
|
|
|
else
|
|
|
|
utilities.send_photo(msg.chat.id, url, text, msg.message_id)
|
|
|
|
end
|
2016-08-18 17:23:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return yandere
|