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_nsfw.lua
Andreas Bielawski 8f161217ad Vergessen...
2016-09-04 13:25:05 +02:00

49 lines
1.5 KiB
Lua

local derpibooru_nsfw = {}
function derpibooru_nsfw:init(config)
if not cred_data.derpibooru_apikey then
print('Fehlender Key: derpibooru_apikey.')
print('derpibooru_nsfw.lua wird nicht aktiviert.')
return
end
derpibooru_nsfw.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('dpnsfw', true):t('derpiboorunsfw', true).table
derpibooru_nsfw.doc = [[*
]]..config.cmd_pat..[[dpnsfw* _<Tags>_: Sucht auf Derpibooru mit diesen Tags (NSFW)]]
end
derpibooru_nsfw.command = 'dpnsfw <Tags>'
function derpibooru_nsfw:get_dp(tag)
local BASE_URL = 'https://derpiboo.ru/'
local apikey = cred_data.derpibooru_apikey
local url = BASE_URL..'/search.json?key='..apikey..'&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_nsfw:action(msg, config)
local input = utilities.input_from_msg(msg)
if not input then
utilities.send_reply(msg, derpibooru_nsfw.doc, true)
return
end
local tag = string.gsub(input, " ", '+' )
local url, source = derpibooru_nsfw:get_dp(tag)
if not url then
utilities.send_reply(msg, config.errors.results)
return
end
utilities.send_typing(msg.chat.id, 'upload_photo')
local file = download_to_file(url)
utilities.send_photo(msg.chat.id, file, source, msg.message_id)
end
return derpibooru_nsfw