plugins/xkcd.lua

Sun, 23 Mar 2014 15:02:39 +0100

author
Kim Alvefur <zash@zash.se>
date
Sun, 23 Mar 2014 15:02:39 +0100
changeset 107
441ec8dcdfde
parent 98
1f365028aae1
child 108
70db447e3669
permissions
-rw-r--r--

plugins.xkcd: Forward-declare handle_xkcd_command()

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 = { };
107
441ec8dcdfde plugins.xkcd: Forward-declare handle_xkcd_command()
Kim Alvefur <zash@zash.se>
parents: 98
diff changeset
4 local handle_xkcd_command;
57
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 function riddim.plugins.xkcd(bot)
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 require "net.httpclient_listener";
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local http = require("net.http");
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 bot:hook("commands/xkcd", function(command)
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 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
11 --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
12 -- 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
13 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
14 if code == 200 then
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 xkcd_list_updated_at = os.time();
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 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
17 parse_xkcd_list(data);
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 elseif code == 304 then
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 xkcd_list_updated_at = os.time();
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 else
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 if code > 0 then
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 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
23 else
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 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
25 end
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 return;
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 end
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 command:reply(handle_xkcd_command(command));
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 end);
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 else
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 return handle_xkcd_command(command);
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 end
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 function handle_xkcd_command(command)
78
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
37 local xkcdnum = command.param;
97
a532667d596e plugins.xkcd: If no argument is given, return the latest strip
Kim Alvefur <zash@zash.se>
parents: 78
diff changeset
38 if not xkcdnum then
a532667d596e plugins.xkcd: If no argument is given, return the latest strip
Kim Alvefur <zash@zash.se>
parents: 78
diff changeset
39 xkcdnum = #xkcd_list;
a532667d596e plugins.xkcd: If no argument is given, return the latest strip
Kim Alvefur <zash@zash.se>
parents: 78
diff changeset
40 end
78
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
41 if not tonumber(xkcdnum) then -- Search for an xkcd
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
42 xkcdnum = xkcdnum:lower()
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
43 local xkcdpat = xkcdnum:gsub("[()]", function(s) return "%" .. s end)
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
44 :gsub("[%[]",function(s) return "%" .. s end)
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
45 :gsub("%%(%b[])",function(s) return (#s > 2 and "" or "%") .. s end);
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
46 local results = {};
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
47 for x, xkcd in pairs(xkcd_list) do
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
48 name = xkcd:lower()
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
49 if name == xkcdnum then -- exact match
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
50 return xkcd..", http://xkcd.org/"..x.."/";
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
51 elseif name:match(xkcdpat) then
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
52 table.insert(results, x);
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
53 --return commands.xkcd(msg, x);
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
54 end
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
55 end
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
56 if #results == 0 then
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
57 return "Sorry, I couldn't find a match";
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
58 elseif #results == 1 then
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
59 command.param = results[1];
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
60 return handle_xkcd_command(command);
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
61 else
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
62 -- We have more than one match
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
63 local ret = "Multiple matches:";
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
64 for _, x in ipairs(results) do
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
65 local xkcdnum = tostring(tonumber(x));
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
66 local xkcd = xkcd_list[tostring(x)];
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
67 ret = string.format("%s %s%s", ret, xkcd, ((_ < #results) and ",") or "");
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
68 if _ > 5 then ret = ret .. " " .. (#results - 5) .. " more"; break; end
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
69 end
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
70 return ret;
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
71 end
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
72 end
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
73 -- Check that xkcdnum is a valid number
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
74 xkcdnum = tostring(tonumber(xkcdnum));
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
75 if not xkcdnum then return "What XKCD strip number? Or enter a search string."; end
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
76 xkcd = xkcd_list[xkcdnum];
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
77 if not xkcd then return "Sorry, I don't think there is a XKCD #"..xkcdnum; end
00ad6fe8975e plugins.xkcd: reformat code
God of Dice@mf-nb
parents: 77
diff changeset
78 return xkcd..", http://xkcd.org/"..xkcdnum.."/";
57
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 end
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 function parse_xkcd_list(t)
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 if not t then return nil; end
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 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
84 xkcd_list[number] = name;
98
1f365028aae1 plugins.xkcd: Make really sure that strip numbers are numbers
Kim Alvefur <zash@zash.se>
parents: 97
diff changeset
85 local number = tonumber(number);
1f365028aae1 plugins.xkcd: Make really sure that strip numbers are numbers
Kim Alvefur <zash@zash.se>
parents: 97
diff changeset
86 if number then
1f365028aae1 plugins.xkcd: Make really sure that strip numbers are numbers
Kim Alvefur <zash@zash.se>
parents: 97
diff changeset
87 xkcd_list[number] = name;
1f365028aae1 plugins.xkcd: Make really sure that strip numbers are numbers
Kim Alvefur <zash@zash.se>
parents: 97
diff changeset
88 end
57
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 end
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 return true;
766f4225110b plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 end

mercurial