author | Mikael Berthe <mikael@lilotux.net> |
Mon, 19 Apr 2010 10:20:28 +0200 | |
changeset 38 | a51ddbce247b |
parent 37 | 9d7f19e4e4fe |
child 39 | 237af42156a1 |
permissions | -rwxr-xr-x |
36 | 1 |
#! /usr/bin/env lua |
2 |
||
3 |
-- Data extractor for http://www.pogdesign.co.uk/cat/ |
|
4 |
-- Mikael BERTHE, 2010-04-18 |
|
5 |
||
6 |
require "libs.shcmd" |
|
7 |
||
8 |
local tvcal = { ["desc"] = "Display TV calendar" } |
|
9 |
||
10 |
local tvcaldata = {} |
|
11 |
local tvcaldata_timestamp |
|
12 |
||
13 |
-- Function from PIL |
|
14 |
local function pairsByKeys (t, f) |
|
15 |
local a = {} |
|
16 |
for n in pairs(t) do table.insert(a, n) end |
|
17 |
table.sort(a, f) |
|
18 |
local i = 0 -- iterator variable |
|
19 |
local iter = function () -- iterator function |
|
20 |
i = i + 1 |
|
21 |
if a[i] == nil then return nil |
|
22 |
else return a[i], t[a[i]] |
|
23 |
end |
|
24 |
end |
|
25 |
return iter |
|
26 |
end |
|
27 |
||
28 |
local function parse_webpage (url) |
|
29 |
local contents = shcmd("curl "..url) |
|
30 |
||
31 |
local tabregex = '<td id="d_(%d+_%d+_%d%d%d%d)" class="t?o?day"%s*>%s*\n%s*'.. |
|
32 |
'<table>(.-)</table>' |
|
33 |
||
34 |
for day, tab in string.gmatch(contents, tabregex) do |
|
35 |
local epregex |
|
36 |
local d, m, y = day:match("^(%d+)_(%d+)_(%d%d%d%d)$") |
|
37 |
day = string.format("%04d-%02d-%02d", y, m, d) |
|
38 |
||
39 |
epregex = '<td><a id="[^"]+" href="[^"]+" class="eplink%s*"%s*>([^<]+)</a>' |
|
40 |
epregex = epregex .. '<span class="seasep"%s*></span><br />' |
|
41 |
epregex = epregex .. '<span class="seasep"%s*>([^<]+)</span>[%s\n]*</td>' |
|
42 |
||
43 |
local shows = {} |
|
44 |
for name, ep in string.gmatch(tab, epregex) do |
|
45 |
name = name:gsub(""", "\""):gsub("&", "&") |
|
46 |
name = name:gsub("<", "<"):gsub(">", ">") |
|
47 |
name = name:gsub("'", "'") |
|
48 |
||
49 |
local obj = { |
|
50 |
["name"] = name, |
|
51 |
["ep"] = ep |
|
52 |
} |
|
53 |
table.insert(shows, obj) |
|
54 |
end |
|
55 |
tvcaldata[day] = shows |
|
56 |
end |
|
57 |
tvcaldata_timestamp = os.date("%F") |
|
58 |
return true |
|
59 |
end |
|
60 |
||
61 |
local function tvcal_by_date (d) |
|
62 |
if not tvcaldata[d] then |
|
37 | 63 |
return "No episode found for this date ("..d..")" |
36 | 64 |
end |
65 |
||
66 |
local r = "Shows on " .. d .. ":\n" |
|
67 |
for i,j in ipairs(tvcaldata[d]) do |
|
68 |
r = r .. j.name .. " (" ..j.ep .. ")\n" |
|
69 |
end |
|
70 |
r = r:gsub("\n+$", "") |
|
71 |
return r |
|
72 |
end |
|
73 |
||
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
74 |
local function tvcal_by_name (name, plain) |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
75 |
local action = plain and "contain" or "match" |
36 | 76 |
local r = "" |
77 |
name = name:lower() |
|
78 |
||
79 |
for day, obj in pairsByKeys(tvcaldata) do |
|
80 |
for i,j in ipairs(obj) do |
|
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
81 |
if j.name:lower():find(name, 1, plain) then |
36 | 82 |
r = r .. day .. "\t" .. j.name .. " (" ..j.ep .. ")\n" |
83 |
end |
|
84 |
end |
|
85 |
end |
|
86 |
||
87 |
if r == "" then |
|
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
88 |
return "No episode found "..action.."ing this string" |
36 | 89 |
end |
90 |
r = r:gsub("\n+$", "") |
|
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
91 |
return "Episodes "..action.."ing \""..name.."\":\n" .. r |
36 | 92 |
end |
93 |
||
94 |
function tvcal.cmd (arg) |
|
95 |
if not tvcaldata_timestamp or tvcaldata_timestamp ~= os.date("%F") then |
|
96 |
tvcaldata = {} -- Empty the previous list |
|
97 |
local r, err = parse_webpage("http://www.pogdesign.co.uk/cat/") |
|
98 |
if not r then return nil, err end |
|
99 |
-- Fetch next month as well... |
|
100 |
local m = tonumber(os.date("%m"))+1 |
|
101 |
local y = os.date("%Y") |
|
102 |
if m > 12 then m = 1; y = y + 1; end |
|
103 |
parse_webpage("http://www.pogdesign.co.uk/cat/"..m.."-"..y) |
|
104 |
end |
|
105 |
||
106 |
if not arg or arg == "today" then |
|
107 |
arg = os.date("%F") |
|
108 |
elseif arg == "tomorrow" then |
|
109 |
arg = os.date("%F", os.date("%s")+86400) |
|
37 | 110 |
elseif arg:match("^%+%d+$") then |
111 |
local n = tonumber(arg:match("^%+(%d+)$")) |
|
112 |
if n < 60 then |
|
113 |
arg = os.date("%F", os.date("%s")+86400*n) |
|
114 |
end |
|
36 | 115 |
end |
116 |
||
117 |
if arg:match("^%d%d%d%d%-%d%d%-%d%d$") then |
|
118 |
return tvcal_by_date(arg) |
|
119 |
end |
|
120 |
||
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
121 |
-- Is it a pattern? (string surrounded by quotes) |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
122 |
local plain = arg:match('^"(.*)"$') |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
123 |
if plain then |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
124 |
arg = plain |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
125 |
plain = false |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
126 |
else |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
127 |
plain = true |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
128 |
end |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
129 |
return tvcal_by_name(arg, plain) |
36 | 130 |
end |
131 |
||
132 |
mcbot_register_command("tvcal", tvcal) |