5 function riddim.plugins.xkcd(bot) |
5 function riddim.plugins.xkcd(bot) |
6 require "net.httpclient_listener"; |
6 require "net.httpclient_listener"; |
7 local http = require("net.http"); |
7 local http = require("net.http"); |
8 bot:hook("commands/xkcd", function(command) |
8 bot:hook("commands/xkcd", function(command) |
9 if os.difftime(os.time(), xkcd_list_updated_at) > (3 * 60 * 60) then -- Not refreshed within 3 hours |
9 if os.difftime(os.time(), xkcd_list_updated_at) > (3 * 60 * 60) then -- Not refreshed within 3 hours |
10 http.request('http://xkcd.com/archive/', { |
10 --COMPAT We could have saved 6 bytes here, but Microsoft apparently hates %T, so you got this gigantic comment instead. |
11 headers = { ["If-Modified-Since"] = os.date("!%a, %d %b %Y %T %Z", xkcd_list_updated_at or 0) } |
11 -- http.request('http://xkcd.com/archive/', { headers = { ["If-Modified-Since"] = os.date("!%a, %d %b %Y %T %Z", xkcd_list_updated_at or 0) } }, function (data, code) |
12 }, function (data, code) |
12 http.request('http://xkcd.com/archive/', { headers = { ["If-Modified-Since"] = os.date("!%a, %d %b %Y %H:%M:%S %Z", xkcd_list_updated_at or 0) } }, function (data, code) |
13 if code == 200 then |
13 if code == 200 then |
14 xkcd_list_updated_at = os.time(); |
14 xkcd_list_updated_at = os.time(); |
15 print("debug", "got "..(#data or 0).." bytes of data"); |
15 print("debug", "got "..(#data or 0).." bytes of data"); |
16 parse_xkcd_list(data); |
16 parse_xkcd_list(data); |
17 elseif code == 304 then |
17 elseif code == 304 then |