Sun, 17 Jul 2011 02:03:21 +0200
plugins.xkcd: reformat code
57
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 | local parse_xkcd_list; |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 | local xkcd_list_updated_at = 0; |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 | local xkcd_list = { }; |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 | |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 | function riddim.plugins.xkcd(bot) |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 | require "net.httpclient_listener"; |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 | local http = require("net.http"); |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 | bot:hook("commands/xkcd", function(command) |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 | if os.difftime(os.time(), xkcd_list_updated_at) > (3 * 60 * 60) then -- Not refreshed within 3 hours |
77
7e50244d8e42
plugins.xkcd: Microsoft apparently hates %T (crashes on Windows)
God of Dice@mf-nb
parents:
66
diff
changeset
|
10 | --COMPAT We could have saved 6 bytes here, but Microsoft apparently hates %T, so you got this gigantic comment instead. |
7e50244d8e42
plugins.xkcd: Microsoft apparently hates %T (crashes on Windows)
God of Dice@mf-nb
parents:
66
diff
changeset
|
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) |
7e50244d8e42
plugins.xkcd: Microsoft apparently hates %T (crashes on Windows)
God of Dice@mf-nb
parents:
66
diff
changeset
|
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) |
57
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 | if code == 200 then |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 | xkcd_list_updated_at = os.time(); |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 | print("debug", "got "..(#data or 0).." bytes of data"); |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 | parse_xkcd_list(data); |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 | elseif code == 304 then |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 | xkcd_list_updated_at = os.time(); |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 | else |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 | if code > 0 then |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 | command:reply("Received HTTP "..code.." error trying to fetch the XKCD archive"); |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 | else |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 | command:reply("Unable to fetch the XKCD archive from xkcd.com: "..data:gsub("%-", " ")); |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 | end |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 | return; |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 | end |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 | command:reply(handle_xkcd_command(command)); |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 | end); |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 | else |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 | return handle_xkcd_command(command); |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 | end |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 | end); |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 | end |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 | |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 | function handle_xkcd_command(command) |
78 | 36 | local xkcdnum = command.param; |
37 | if not xkcdnum then return "Please supply an XKCD number or a search string :)"; end | |
38 | if not tonumber(xkcdnum) then -- Search for an xkcd | |
39 | xkcdnum = xkcdnum:lower() | |
40 | local xkcdpat = xkcdnum:gsub("[()]", function(s) return "%" .. s end) | |
41 | :gsub("[%[]",function(s) return "%" .. s end) | |
42 | :gsub("%%(%b[])",function(s) return (#s > 2 and "" or "%") .. s end); | |
43 | local results = {}; | |
44 | for x, xkcd in pairs(xkcd_list) do | |
45 | name = xkcd:lower() | |
46 | if name == xkcdnum then -- exact match | |
47 | return xkcd..", http://xkcd.org/"..x.."/"; | |
48 | elseif name:match(xkcdpat) then | |
49 | table.insert(results, x); | |
50 | --return commands.xkcd(msg, x); | |
51 | end | |
52 | end | |
53 | if #results == 0 then | |
54 | return "Sorry, I couldn't find a match"; | |
55 | elseif #results == 1 then | |
56 | command.param = results[1]; | |
57 | return handle_xkcd_command(command); | |
58 | else | |
59 | -- We have more than one match | |
60 | local ret = "Multiple matches:"; | |
61 | for _, x in ipairs(results) do | |
62 | local xkcdnum = tostring(tonumber(x)); | |
63 | local xkcd = xkcd_list[tostring(x)]; | |
64 | ret = string.format("%s %s%s", ret, xkcd, ((_ < #results) and ",") or ""); | |
65 | if _ > 5 then ret = ret .. " " .. (#results - 5) .. " more"; break; end | |
66 | end | |
67 | return ret; | |
68 | end | |
69 | end | |
70 | -- Check that xkcdnum is a valid number | |
71 | xkcdnum = tostring(tonumber(xkcdnum)); | |
72 | if not xkcdnum then return "What XKCD strip number? Or enter a search string."; end | |
73 | xkcd = xkcd_list[xkcdnum]; | |
74 | if not xkcd then return "Sorry, I don't think there is a XKCD #"..xkcdnum; end | |
75 | return xkcd..", http://xkcd.org/"..xkcdnum.."/"; | |
57
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 | end |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 | |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 | function parse_xkcd_list(t) |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 | if not t then return nil; end |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 | for number, name in string.gmatch(t,"<a [^>]*href=\"/(%d+)/\"[^>]*>([^<]+)") do |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 | xkcd_list[number] = name; |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 | end |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 | return true; |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 | end |