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/danbooru.lua
Andreas Bielawski d9a6836dd5 - Portiere get_data
- Ergänze Antworten da, wo ich es vergessen habe...
2016-08-18 17:38:20 +02:00

112 lines
3.2 KiB
Lua

local danbooru = {}
function danbooru:init(config)
danbooru.triggers = {
"^/dan$",
"^/dan (d)$",
"^/dan (w)$",
"^/dan (m)$",
"^/(dan) (.+)$",
"(danbooru.donmai.us)/posts/([0-9]+)"
}
danbooru.doc = [[*
]]..config.cmd_pat..[[dan*: Zufälliges Bild von Danbooru
*]]..config.cmd_pat..[[dan* _<d/w/m>_: Zufälliges Bild von Danbooru (populär heute/in der Woche/im Monat)
*]]..config.cmd_pat..[[dan* _<Tags>_: Sucht auf Danbooru mit diesen Tags
]]
end
danbooru.command = 'dan <Tags>'
local BASE_URL = "http://danbooru.donmai.us"
local URL_NEW = "/posts.json"
local URL_POP = "/explore/posts/popular.json"
local scale_day = "?scale=day"
local scale_week = "?scale=week"
local scale_month = "?scale=month"
function danbooru:get_post(url)
local res, code = http.request(url)
if code ~= 200 then return nil end
local posts = json.decode(res)
if not posts[1] then return nil end
return posts[math.random(#posts)]
end
function danbooru:get_specific_post(id)
local url = BASE_URL..'/posts/'..id..'.json'
local res, code = http.request(url)
if code ~= 200 then return nil end
local db = json.decode(res)
local link_image = BASE_URL..db.file_url
return link_image
end
function danbooru:action(msg, config, matches)
if matches[1] == 'danbooru.donmai.us' and matches[2] then
local url = danbooru:get_specific_post(matches[2])
if not url then utilities.send_reply(self, msg, config.errors.connection) return end
utilities.send_typing(self, msg.chat.id, 'upload_photo')
local file = download_to_file(url)
if string.ends(url, ".gif") or string.ends(url, ".zip") or string.ends(url, ".webm") 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
return
end
if matches[1] == "d" or matches[1] == "w" or matches[1] == "m" then
url = BASE_URL..URL_POP
else
url = BASE_URL..URL_NEW
end
if matches[1] == "d" then
url = url..scale_day
elseif matches[1] == "w" then
url = url..scale_week
elseif matches[1] == "m" then
url = url..scale_month
elseif matches[1] == "dan" and matches[2] then
local tag = string.gsub(matches[2], " ", '_' )
local tag = string.gsub(tag, ":", '%%3A' )
url = url..'?tags='..tag
end
local post = danbooru:get_post(url)
if post then
local download_url = BASE_URL..post.large_file_url
utilities.send_typing(self, msg.chat.id, 'upload_photo')
local img = download_to_file(download_url)
if string.ends(download_url, ".gif") or string.ends(download_url, ".zip") or string.ends(download_url, ".webm") then
utilities.send_document(self, msg.chat.id, img, nil, msg.message_id)
else
utilities.send_photo(self, msg.chat.id, img, nil, msg.message_id)
end
local txt = ''
if post.tag_string_artist ~= '' then
txt = 'Artist: ' .. post.tag_string_artist .. '\n'
end
if post.tag_string_character ~= '' then
txt = txt .. 'Charakter: ' .. post.tag_string_character .. '\n'
end
if post.file_size ~= '' then
txt = txt .. '[' .. math.ceil(post.file_size/1000) .. 'kb] ' .. BASE_URL .. post.file_url
end
if txt ~= '' then
utilities.send_reply(self, msg, txt)
end
return
else
utilities.send_reply(self, msg, config.errors.results)
return
end
end
return danbooru