2016-08-18 17:23:55 +02:00
|
|
|
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])
|
2016-08-24 17:18:17 +02:00
|
|
|
if not url then utilities.send_reply(msg, config.errors.connection) return end
|
|
|
|
utilities.send_typing(msg.chat.id, 'upload_photo')
|
2016-08-18 17:23:55 +02:00
|
|
|
local file = download_to_file(url)
|
|
|
|
if string.ends(url, ".gif") or string.ends(url, ".zip") or string.ends(url, ".webm") then
|
2016-08-24 17:18:17 +02:00
|
|
|
utilities.send_document(msg.chat.id, file, nil, msg.message_id)
|
2016-08-18 17:23:55 +02:00
|
|
|
else
|
2016-08-24 17:18:17 +02:00
|
|
|
utilities.send_photo(msg.chat.id, file, nil, msg.message_id)
|
2016-08-18 17:23:55 +02:00
|
|
|
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
|
2016-08-24 17:18:17 +02:00
|
|
|
utilities.send_typing(msg.chat.id, 'upload_photo')
|
2016-08-18 17:23:55 +02:00
|
|
|
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
|
2016-08-24 17:18:17 +02:00
|
|
|
utilities.send_document(msg.chat.id, img, nil, msg.message_id)
|
2016-08-18 17:23:55 +02:00
|
|
|
else
|
2016-08-24 17:18:17 +02:00
|
|
|
utilities.send_photo(msg.chat.id, img, nil, msg.message_id)
|
2016-08-18 17:23:55 +02:00
|
|
|
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
|
2016-08-24 17:18:17 +02:00
|
|
|
utilities.send_reply(msg, txt)
|
2016-08-18 17:23:55 +02:00
|
|
|
end
|
|
|
|
return
|
|
|
|
else
|
2016-08-24 17:18:17 +02:00
|
|
|
utilities.send_reply(msg, config.errors.results)
|
2016-08-18 17:23:55 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return danbooru
|