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

43 lines
1.3 KiB
Lua

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
utilities.send_reply(self, msg, derpibooru.doc, true)
return
end
local tag = string.gsub(input, " ", '+' )
local url, source = derpibooru:get_dp(tag)
if not url then
utilities.send_reply(self, msg, config.errors.results)
return
end
utilities.send_typing(self, msg.chat.id, 'upload_photo')
local file = download_to_file(url)
utilities.send_photo(self, msg.chat.id, file, source, msg.message_id)
end
return derpibooru