Returns HTTP Error if any in XKCD plugin

This commit is contained in:
yago 2015-02-02 23:18:46 +01:00
parent da91a550fc
commit 4305c7b87e

View File

@ -1,18 +1,19 @@
function get_xkcd() function get_xkcd()
first_url = http.request("http://xkcd.com/info.0.json") -- Get the latest num
local xcomicinfo = json:decode(first_url) local res,code = https.request("http://xkcd.com/info.0.json")
if code ~= 200 then return "HTTP ERROR" end
local data = json:decode(res)
math.randomseed(os.time()) math.randomseed(os.time())
i = math.random(1,xcomicinfo.num) i = math.random(1, data.num) -- Latest
b = http.request("http://xkcd.com/" .. i .. "/info.0.json") local res,code = http.request("http://xkcd.com/"..i.."/info.0.json")
local comicjson = json:decode(b) if code ~= 200 then return "HTTP ERROR" end
local link_image = comicjson.img local data = json:decode(res)
c = http.request(link_image) local link_image = data.img
local title = comicjson.title
if link_image:sub(0,2) == '//' then if link_image:sub(0,2) == '//' then
link_image = msg.text:sub(3,-1) link_image = msg.text:sub(3,-1)
end end
return link_image, title return link_image, data.title
end end
function send_title(cb_extra, success, result) function send_title(cb_extra, success, result)
@ -30,9 +31,8 @@ function run(msg, matches)
end end
return { return {
description = "send random comic image from xkcd", description = "Send random comic image from xkcd",
usage = "!xkcd", usage = "!xkcd",
patterns = {"^!xkcd$"}, patterns = {"^!xkcd$"},
run = run run = run
} }