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/rule34.lua

81 lines
2.6 KiB
Lua
Raw Normal View History

local rule34 = {}
function rule34:init(config)
rule34.triggers = {
"^/r34 (.+)$",
"^/rule34 (.+)$",
"https?://(rule34.xxx)/index.php%?page=post&s=view&id=(%d+)"
}
rule34.doc = [[*
]]..config.cmd_pat..[[r34* _<Tags>_: Sucht auf Rule34 mit diesen Tags (NSFW)]]
end
rule34.command = 'r34 <Tags>'
function rule34:get_r34_info(tag)
local limit = 100 -- number of posts to return (higher = more choices for random, but longer load times, hard limit of 100 posts)
local BASE_URL = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%27http%3A%2F%2Frule34.xxx%2Findex.php%3Fpage%3Ddapi%26s%3Dpost%26q%3Dindex%26limit%3D'..limit..'%26tags%3D'
local END_URL = '%27&format=json'
local url = BASE_URL..tag..END_URL
local res, code = https.request(url)
if code ~= 200 then return nil end
local r34 = json.decode(res).query.results.posts.post
if not r34[1] then return nil end
local i = math.random(#r34)
local url = r34[i].file_url
local source_url = 'http://rule34.xxx/index.php?page=post&s=view&id='..r34[i].id
return url, source_url
end
function rule34:get_r34_post(id)
local url = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%27http%3A%2F%2Frule34.xxx%2Findex.php%3Fpage%3Ddapi%26s%3Dpost%26q%3Dindex%26id%3D'..id..'%27&format=json'
local res ,code = https.request(url)
if code ~= 200 then return nil end
local r34 = json.decode(res).query.results.posts.post
if not r34 then return nil end
local img_url = r34.file_url
return img_url
end
function rule34:action(msg, config, matches)
if matches[1] == 'rule34.xxx' and matches[2] then
local id = matches[2]
local img_url = rule34:get_r34_post(id)
if not img_url then
utilities.send_reply(self, msg, config.errors.results)
return
end
utilities.send_typing(self, msg.chat.id, 'upload_photo')
local file = download_to_file(img_url)
if string.ends(img_url, ".gif") then
utilities.send_document(self, msg.chat.id, file, nil, msg.message_id)
else
utilities.send_photo(self, msg.chat.id, file, nil, msg.message_id)
end
else
local tag = string.gsub(matches[1], " ", '_')
local tag = string.gsub(tag, ":", '%%3A')
local tag = string.gsub(tag, "+", '%%2B')
local url, id = rule34:get_r34_info(tag)
if not url then
utilities.send_reply(self, msg, config.errors.results)
return
end
utilities.send_typing(self, msg.chat.id, 'upload_photo')
local file = download_to_file(url)
if string.ends(url, ".gif") then
utilities.send_document(self, msg.chat.id, file, id, msg.message_id)
else
utilities.send_photo(self, msg.chat.id, file, id, msg.message_id)
end
end
end
return rule34