Sun, 24 Jul 2011 19:16:40 +0700
plugins/version improvements
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 |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 | http.request('http://xkcd.com/archive/', { |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 | headers = { ["If-Modified-Since"] = os.date("!%a, %d %b %Y %T %Z", xkcd_list_updated_at or 0) } |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 | }, function (data, code) |
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) |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 | local xkcdnum = command.param; |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 | if not xkcdnum then return "Please supply an XKCD number or a search string :)"; end |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 | if not tonumber(xkcdnum) then -- Search for an xkcd |
66
bdaf81ab55ad
plugins.xkcd: Do exact match comparison with original, but lowercased, parameter
Kim Alvefur <zash@zash.se>
parents:
65
diff
changeset
|
39 | xkcdnum = xkcdnum:lower() |
bdaf81ab55ad
plugins.xkcd: Do exact match comparison with original, but lowercased, parameter
Kim Alvefur <zash@zash.se>
parents:
65
diff
changeset
|
40 | local xkcdpat = xkcdnum:gsub("[()]", function(s) return "%" .. s end) |
63
8f9675d0101d
plugins.xkcd: Escape non-balanced non-empty square brackets in search queries
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
41 | :gsub("[%[]",function(s) return "%" .. s end) |
8f9675d0101d
plugins.xkcd: Escape non-balanced non-empty square brackets in search queries
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
42 | :gsub("%%(%b[])",function(s) return (#s > 2 and "" or "%") .. s end); |
57
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 | local results = {}; |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 | for x, xkcd in pairs(xkcd_list) do |
64
a317a5d4340c
plugins.xkcd: Don't pad with spaces
Kim Alvefur <zash@zash.se>
parents:
63
diff
changeset
|
45 | name = xkcd:lower() |
65
25f4445447b2
plugins.xkcd: Exact matches takes priority over others
Kim Alvefur <zash@zash.se>
parents:
64
diff
changeset
|
46 | if name == xkcdnum then -- exact match |
25f4445447b2
plugins.xkcd: Exact matches takes priority over others
Kim Alvefur <zash@zash.se>
parents:
64
diff
changeset
|
47 | return xkcd..", http://xkcd.org/"..x.."/"; |
66
bdaf81ab55ad
plugins.xkcd: Do exact match comparison with original, but lowercased, parameter
Kim Alvefur <zash@zash.se>
parents:
65
diff
changeset
|
48 | elseif name:match(xkcdpat) then |
57
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 | table.insert(results, x); |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 | --return commands.xkcd(msg, x); |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 | end |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 | end |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 | if #results == 0 then |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 | return "Sorry, I couldn't find a match"; |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 | elseif #results == 1 then |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 | command.param = results[1]; |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 | return handle_xkcd_command(command); |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 | else |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 | -- We have more than one match |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 | local ret = "Multiple matches:"; |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 | for _, x in ipairs(results) do |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 | local xkcdnum = tostring(tonumber(x)); |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 | local xkcd = xkcd_list[tostring(x)]; |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 | ret = string.format("%s %s%s", ret, xkcd, ((_ < #results) and ",") or ""); |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 | if _ > 5 then ret = ret .. " " .. (#results - 5) .. " more"; break; end |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 | end |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 | return ret; |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 | end |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 | end |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 | -- Check that xkcdnum is a valid number |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 | xkcdnum = tostring(tonumber(xkcdnum)); |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 | if not xkcdnum then return "What XKCD strip number? Or enter a search string."; end |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 | xkcd = xkcd_list[xkcdnum]; |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 | if not xkcd then return "Sorry, I don't think there is a XKCD #"..xkcdnum; end |
766f4225110b
plugins.xkcd: New plugin, guess what it does ;)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 | return xkcd..", http://xkcd.org/"..xkcdnum.."/"; |
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 |