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/plugins/danbooru.lua

73 lines
1.7 KiB
Lua
Raw Normal View History

2015-04-28 17:46:04 +02:00
do
local 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"
2015-05-28 16:47:30 +02:00
local function get_post(url)
2015-04-28 17:46:04 +02:00
local b, c, h = http.request(url)
2015-05-28 16:47:30 +02:00
if c ~= 200 then return nil end
2015-04-28 17:46:04 +02:00
local posts = json:decode(b)
return posts[math.random(#posts)]
end
2015-05-28 16:47:30 +02:00
local function run(msg, matches)
2015-04-28 17:46:04 +02:00
local url = URL
if matches[1] == "/dan" then
url = url .. URL_NEW
else
url = url .. URL_POP
2015-05-28 16:47:30 +02:00
if matches[1] == "d" then
2015-04-28 17:46:04 +02:00
url = url .. scale_day
2015-05-28 16:47:30 +02:00
elseif matches[1] == "w" then
2015-04-28 17:46:04 +02:00
url = url .. scale_week
2015-05-28 16:47:30 +02:00
elseif matches[1] == "m" then
2015-04-28 17:46:04 +02:00
url = url .. scale_month
end
end
local post = get_post(url)
2015-05-28 16:47:30 +02:00
if post then
2015-05-28 21:35:56 +02:00
--vardump(post)
2015-05-28 16:47:30 +02:00
local img = URL .. post.large_file_url
send_photo_from_url(get_receiver(msg), img)
2015-04-28 17:46:04 +02:00
2015-05-28 16:47:30 +02:00
local txt = ''
if post.tag_string_artist ~= '' then
txt = 'Artist: ' .. post.tag_string_artist .. '\n'
end
if post.tag_string_character ~= '' then
txt = txt .. 'Character: ' .. post.tag_string_character .. '\n'
end
if post.file_size ~= '' then
txt = txt .. '[' .. math.ceil(post.file_size/1000) .. 'kb] ' .. URL .. post.file_url
end
return txt
end
2015-04-28 17:46:04 +02:00
end
return {
description = "Gets a random fresh or popular image from Danbooru",
usage = {
"/dan - gets a random fresh image from Danbooru 🔞",
2015-05-28 16:47:30 +02:00
"/dan d - random daily popular image 🔞",
"/dan w - random weekly popular image 🔞",
"/dan m - random monthly popular image 🔞"},
2015-04-28 17:46:04 +02:00
patterns = {
"^/dan$",
2015-05-28 16:47:30 +02:00
"^/dan ?(d)$",
"^/dan ?(w)$",
"^/dan ?(m)$"},
2015-04-28 17:46:04 +02:00
run = run
}
end