From 81802ccdce0290f5b688dea795925f53c59ace87 Mon Sep 17 00:00:00 2001 From: Nitesh A Jain Date: Thu, 15 Jan 2015 02:16:47 +0530 Subject: [PATCH 1/2] added xkcd plugin --- plugins/xkcd.lua | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 plugins/xkcd.lua diff --git a/plugins/xkcd.lua b/plugins/xkcd.lua new file mode 100644 index 0000000..a6069c0 --- /dev/null +++ b/plugins/xkcd.lua @@ -0,0 +1,38 @@ + +function get_xkcd() + first_url = http.request("http://xkcd.com/info.0.json") + local xcomicinfo = json:decode(first_url) + math.randomseed(os.time()) + i = math.random(1,xcomicinfo.num) + b = http.request("http://xkcd.com/" .. i .. "/info.0.json") + local comicjson = json:decode(b) + local link_image = comicjson.img + c = http.request("link_image") + local title = comicjson.title + if link_image:sub(0,2) == '//' then + link_image = msg.text:sub(3,-1) + end + return link_image, title +end + +function send_title(cb_extra, success, result) + if success then + send_msg(cb_extra[1], cb_extra[2], ok_cb, false) + end +end + +function run(msg, matches) + local receiver = get_receiver(msg) + url, title = get_xkcd() + file_path = download_to_file(url) + send_photo(receiver, file_path, send_title, {receiver, title}) + return false +end + +return { + description = "send random comic image from xkcd", + usage = "!xkcd", + patterns = {"^!xkcd$"}, + run = run +} + From 9a343040c7b317f9a0a0a8b3d4497f2893a45bce Mon Sep 17 00:00:00 2001 From: Nitesh A Jain Date: Thu, 15 Jan 2015 20:52:24 +0530 Subject: [PATCH 2/2] Oops!! Removed quotes around variable --- plugins/xkcd.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/xkcd.lua b/plugins/xkcd.lua index a6069c0..94abd30 100644 --- a/plugins/xkcd.lua +++ b/plugins/xkcd.lua @@ -7,7 +7,7 @@ function get_xkcd() b = http.request("http://xkcd.com/" .. i .. "/info.0.json") local comicjson = json:decode(b) local link_image = comicjson.img - c = http.request("link_image") + c = http.request(link_image) local title = comicjson.title if link_image:sub(0,2) == '//' then link_image = msg.text:sub(3,-1)