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

43 lines
1.2 KiB
Lua
Raw Normal View History

local derpibooru = {}
function derpibooru:init(config)
derpibooru.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('dp', true):t('derpibooru', true).table
derpibooru.doc = [[*
]]..config.cmd_pat..[[dp* _<Tags>_: Sucht auf Derpibooru mit diesen Tags]]
end
derpibooru.command = 'dp <Tags>'
function derpibooru:get_dp(tag)
local BASE_URL = 'https://derpiboo.ru/'
local url = BASE_URL..'/search.json?q='..tag
local res, code = https.request(url)
if code ~= 200 then return nil end
local dp = json.decode(res).search
if not dp[1] then return nil end
local i = math.random(#dp)
local link_image = 'https:'..dp[i].image
local source = 'https://derpiboo.ru/'..dp[i].id
return link_image, source
end
function derpibooru:action(msg, config)
local input = utilities.input_from_msg(msg)
if not input then
2016-08-24 17:18:17 +02:00
utilities.send_reply(msg, derpibooru.doc, true)
return
end
local tag = string.gsub(input, " ", '+' )
local url, source = derpibooru:get_dp(tag)
if not url then
2016-08-24 17:18:17 +02:00
utilities.send_reply(msg, config.errors.results)
return
end
2016-08-24 17:18:17 +02:00
utilities.send_typing(msg.chat.id, 'upload_photo')
local file = download_to_file(url)
2016-08-24 17:18:17 +02:00
utilities.send_photo(msg.chat.id, file, source, msg.message_id)
end
return derpibooru