2016-08-25 00:30:25 +02:00
|
|
|
local ponyfaces = {}
|
|
|
|
|
|
|
|
function ponyfaces:init(config)
|
|
|
|
ponyfaces.triggers = {
|
|
|
|
"^/ponyfaces? (.+)$",
|
|
|
|
"^/pf (.+)$",
|
|
|
|
"ponyfac.es/(%d+)"
|
|
|
|
}
|
|
|
|
ponyfaces.doc = [[*
|
|
|
|
]]..config.cmd_pat..[[pf* _<Tags>_: Sucht auf Ponyfac.es mit diesen Tags]]
|
|
|
|
end
|
|
|
|
|
|
|
|
ponyfaces.command = 'pf <Tags>'
|
|
|
|
|
|
|
|
local BASE_URL = 'http://ponyfac.es/api.json'
|
|
|
|
|
|
|
|
function ponyfaces:get_ponyface_by_id(id)
|
|
|
|
local url = BASE_URL..'/id:'..id
|
|
|
|
local res, code = http.request(url)
|
|
|
|
if code ~= 200 then return nil end
|
|
|
|
local data = json.decode(res)
|
|
|
|
if not data then return nil end
|
2016-12-15 19:24:08 +01:00
|
|
|
local link = 'http://ponyfac.es/'..data.faces[1].id
|
|
|
|
local pic = data.faces[1].image
|
|
|
|
return link, pic
|
2016-08-25 00:30:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function ponyfaces:get_ponyface(tag)
|
|
|
|
local url = BASE_URL..'/tag:'..tag
|
|
|
|
local res, code = http.request(url)
|
|
|
|
if code ~= 200 then return nil end
|
2016-12-15 19:24:08 +01:00
|
|
|
local data = json.decode(res).faces
|
2016-08-25 00:30:25 +02:00
|
|
|
-- truly randomize
|
|
|
|
math.randomseed(os.time())
|
|
|
|
-- random max json table size
|
2016-12-15 19:24:08 +01:00
|
|
|
local i = math.random(#data)
|
|
|
|
local link = 'http://ponyfac.es/'..data[i].id
|
|
|
|
local pic = data[i].image
|
|
|
|
return link, pic
|
2016-08-25 00:30:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function ponyfaces:action(msg, config, matches)
|
|
|
|
if tonumber(matches[1]) ~= nil then
|
|
|
|
local id = matches[1]
|
2016-12-15 19:24:08 +01:00
|
|
|
link, pic = ponyfaces:get_ponyface_by_id(id)
|
2016-08-25 00:30:25 +02:00
|
|
|
else
|
|
|
|
local tag = matches[1]
|
2016-12-15 19:24:08 +01:00
|
|
|
link, pic = ponyfaces:get_ponyface(tag)
|
2016-08-25 00:30:25 +02:00
|
|
|
end
|
2016-12-15 19:24:08 +01:00
|
|
|
if not pic then
|
2016-08-25 00:30:25 +02:00
|
|
|
utilities.send_reply(msg, config.errors.results)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
utilities.send_typing(msg.chat.id, 'upload_photo')
|
2016-12-15 19:24:08 +01:00
|
|
|
utilities.send_photo(msg.chat.id, pic, link, msg.message_id)
|
2016-08-25 00:30:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return ponyfaces
|