2015-03-31 20:17:14 +02:00
|
|
|
do
|
|
|
|
|
2015-02-04 22:15:49 +01:00
|
|
|
function get_last_id()
|
2015-02-02 23:18:46 +01:00
|
|
|
local res,code = https.request("http://xkcd.com/info.0.json")
|
|
|
|
if code ~= 200 then return "HTTP ERROR" end
|
|
|
|
local data = json:decode(res)
|
2015-02-04 22:15:49 +01:00
|
|
|
return data.num
|
|
|
|
end
|
|
|
|
|
|
|
|
function get_xkcd(id)
|
|
|
|
local res,code = http.request("http://xkcd.com/"..id.."/info.0.json")
|
2015-02-02 23:18:46 +01:00
|
|
|
if code ~= 200 then return "HTTP ERROR" end
|
|
|
|
local data = json:decode(res)
|
|
|
|
local link_image = data.img
|
|
|
|
if link_image:sub(0,2) == '//' then
|
|
|
|
link_image = msg.text:sub(3,-1)
|
|
|
|
end
|
2015-04-08 03:21:56 +02:00
|
|
|
return link_image, data.title, data.alt
|
2015-01-14 21:46:47 +01:00
|
|
|
end
|
|
|
|
|
2015-02-04 22:15:49 +01:00
|
|
|
|
|
|
|
function get_xkcd_random()
|
|
|
|
local last = get_last_id()
|
2015-03-31 20:17:14 +02:00
|
|
|
local i = math.random(1, last)
|
2015-02-04 22:15:49 +01:00
|
|
|
return get_xkcd(i)
|
|
|
|
end
|
|
|
|
|
2015-01-14 21:46:47 +01:00
|
|
|
function send_title(cb_extra, success, result)
|
|
|
|
if success then
|
2015-04-08 03:21:56 +02:00
|
|
|
local message = cb_extra[2] .. "\n" .. cb_extra[3]
|
|
|
|
send_msg(cb_extra[1], message, ok_cb, false)
|
2015-01-14 21:46:47 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function run(msg, matches)
|
|
|
|
local receiver = get_receiver(msg)
|
2015-04-14 20:21:23 +02:00
|
|
|
if matches[1] == "/xkcd" then
|
2015-04-08 03:21:56 +02:00
|
|
|
url, title, alt = get_xkcd_random()
|
2015-02-04 22:15:49 +01:00
|
|
|
else
|
2015-04-08 03:21:56 +02:00
|
|
|
url, title, alt = get_xkcd(matches[1])
|
2015-02-04 22:15:49 +01:00
|
|
|
end
|
2015-01-14 21:46:47 +01:00
|
|
|
file_path = download_to_file(url)
|
2015-04-08 03:21:56 +02:00
|
|
|
send_photo(receiver, file_path, send_title, {receiver, title, alt})
|
2015-01-14 21:46:47 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
2015-04-14 20:21:23 +02:00
|
|
|
description = "Sendet Comics von xkcd",
|
|
|
|
usage = {"/xkcd"},
|
2015-03-31 20:17:14 +02:00
|
|
|
patterns = {
|
2015-04-14 20:21:23 +02:00
|
|
|
"^/xkcd$",
|
|
|
|
"^/xkcd (%d+)",
|
2015-03-31 20:17:14 +02:00
|
|
|
"xkcd.com/(%d+)"
|
2015-04-08 03:21:56 +02:00
|
|
|
},
|
|
|
|
run = run
|
2015-02-11 17:32:09 +01:00
|
|
|
}
|
2015-03-31 20:17:14 +02:00
|
|
|
|
2015-04-08 03:21:56 +02:00
|
|
|
end
|