XKCD url sussport
This commit is contained in:
parent
0f49b69133
commit
bd7264f496
@ -1,12 +1,12 @@
|
|||||||
|
function get_last_id()
|
||||||
function get_xkcd()
|
|
||||||
-- Get the latest num
|
|
||||||
local res,code = https.request("http://xkcd.com/info.0.json")
|
local res,code = https.request("http://xkcd.com/info.0.json")
|
||||||
if code ~= 200 then return "HTTP ERROR" end
|
if code ~= 200 then return "HTTP ERROR" end
|
||||||
local data = json:decode(res)
|
local data = json:decode(res)
|
||||||
math.randomseed(os.time())
|
return data.num
|
||||||
i = math.random(1, data.num) -- Latest
|
end
|
||||||
local res,code = http.request("http://xkcd.com/"..i.."/info.0.json")
|
|
||||||
|
function get_xkcd(id)
|
||||||
|
local res,code = http.request("http://xkcd.com/"..id.."/info.0.json")
|
||||||
if code ~= 200 then return "HTTP ERROR" end
|
if code ~= 200 then return "HTTP ERROR" end
|
||||||
local data = json:decode(res)
|
local data = json:decode(res)
|
||||||
local link_image = data.img
|
local link_image = data.img
|
||||||
@ -16,6 +16,14 @@ function get_xkcd()
|
|||||||
return link_image, data.title
|
return link_image, data.title
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function get_xkcd_random()
|
||||||
|
local last = get_last_id()
|
||||||
|
math.randomseed(os.time())
|
||||||
|
i = math.random(1, data.num)
|
||||||
|
return get_xkcd(i)
|
||||||
|
end
|
||||||
|
|
||||||
function send_title(cb_extra, success, result)
|
function send_title(cb_extra, success, result)
|
||||||
if success then
|
if success then
|
||||||
send_msg(cb_extra[1], cb_extra[2], ok_cb, false)
|
send_msg(cb_extra[1], cb_extra[2], ok_cb, false)
|
||||||
@ -24,15 +32,23 @@ end
|
|||||||
|
|
||||||
function run(msg, matches)
|
function run(msg, matches)
|
||||||
local receiver = get_receiver(msg)
|
local receiver = get_receiver(msg)
|
||||||
url, title = get_xkcd()
|
if matches[1] == "!xkcd" then
|
||||||
|
url, title = get_xkcd_random()
|
||||||
|
else
|
||||||
|
url, title = get_xkcd(matches[1])
|
||||||
|
end
|
||||||
file_path = download_to_file(url)
|
file_path = download_to_file(url)
|
||||||
send_photo(receiver, file_path, send_title, {receiver, title})
|
send_photo(receiver, file_path, send_title, {receiver, title})
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
description = "Send random comic image from xkcd",
|
description = "Send comic images from xkcd",
|
||||||
usage = "!xkcd",
|
usage = {"!xkcd (id): Send an xkcd image and tigle. If not id, send a random one"},
|
||||||
patterns = {"^!xkcd$"},
|
patterns = {
|
||||||
|
"^!xkcd$",
|
||||||
|
"^!xkcd (%d+)",
|
||||||
|
"xkcd.com/(%d+)"
|
||||||
|
},
|
||||||
run = run
|
run = run
|
||||||
}
|
}
|
Reference in New Issue
Block a user