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
Akamaru cddb7e03ab Fixes & Verbesserungen
-download_to_file in paar Plugins entfernt
-Antworte auf Nachrichten
-Stabilitätsverbesserungen
2016-12-15 19:24:08 +01:00

80 lines
2.8 KiB
Lua

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 data = json.decode(res).query
if not data.results or data.results.posts.count == '0' then return end
local r34 = data.results.posts.post
local i = math.random(#r34)
local img_url = 'http:'..r34[i].file_url
local source_url = 'http://rule34.xxx/index.php?page=post&s=view&id='..r34[i].id
return img_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 data = json.decode(res).query
if not data.results or data.results.posts.count == '0' then return end
local r34 = data.results.posts.post
local img_url = 'http:'..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(msg, 'Nobody here but us chickens!')
return
end
if string.ends(img_url, ".gif") then
utilities.send_typing(msg.chat.id, 'upload_document')
utilities.send_document(msg.chat.id, img_url, nil, msg.message_id)
else
utilities.send_typing(msg.chat.id, 'upload_photo')
utilities.send_photo(msg.chat.id, img_url, 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 img_url, source_url = rule34:get_r34_info(tag)
if not img_url then
utilities.send_reply(msg, 'Nobody here but us chickens!')
return
end
if string.ends(img_url, ".gif") then
utilities.send_typing(msg.chat.id, 'upload_document')
utilities.send_document(msg.chat.id, img_url, source_url, msg.message_id)
else
utilities.send_typing(msg.chat.id, 'upload_photo')
utilities.send_photo(msg.chat.id, img_url, source_url, msg.message_id)
end
end
end
return rule34