From c19c30933d819c744cf949f06eb228bdda3e606e Mon Sep 17 00:00:00 2001 From: yago Date: Sun, 5 Apr 2015 16:26:30 +0200 Subject: [PATCH] Locals on 9gag --- plugins/9gag.lua | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/plugins/9gag.lua b/plugins/9gag.lua index f2a7d70..a370ade 100644 --- a/plugins/9gag.lua +++ b/plugins/9gag.lua @@ -1,9 +1,12 @@ do -function get_9GAG() - local b = http.request("http://api-9gag.herokuapp.com/") +local function get_9GAG() + local url = "http://api-9gag.herokuapp.com/" + local b,c = http.request(url) + if c ~= 200 then return nil end local gag = json:decode(b) - local i = math.random(#gag) -- random max json table size (# is an operator o.O) + -- random max json table size + local i = math.random(#gag) local link_image = gag[i].src local title = gag[i].title if link_image:sub(0,2) == '//' then @@ -12,16 +15,16 @@ function get_9GAG() return link_image, title end -function send_title(cb_extra, success, result) +local 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 function run(msg, matches) local receiver = get_receiver(msg) - url, title = get_9GAG() - file_path = download_to_file(url) + local url, title = get_9GAG() + local file_path = download_to_file(url) _send_photo(receiver, file_path, send_title, {receiver, title}) return false end