author | Mikael Berthe <mikael@lilotux.net> |
Thu, 30 Sep 2010 20:44:08 +0200 | |
changeset 43 | 7f9ecc483f73 |
parent 39 | 237af42156a1 |
child 57 | 100a831f4f12 |
permissions | -rw-r--r-- |
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 |
||
39
237af42156a1
Add checks after shcmd() calls
Mikael Berthe <mikael@lilotux.net>
parents:
38
diff
changeset
|
31 |
if not contents then |
237af42156a1
Add checks after shcmd() calls
Mikael Berthe <mikael@lilotux.net>
parents:
38
diff
changeset
|
32 |
return nil, "Could not fetch calendar, please try again later!" |
237af42156a1
Add checks after shcmd() calls
Mikael Berthe <mikael@lilotux.net>
parents:
38
diff
changeset
|
33 |
end |
237af42156a1
Add checks after shcmd() calls
Mikael Berthe <mikael@lilotux.net>
parents:
38
diff
changeset
|
34 |
|
36 | 35 |
local tabregex = '<td id="d_(%d+_%d+_%d%d%d%d)" class="t?o?day"%s*>%s*\n%s*'.. |
36 |
'<table>(.-)</table>' |
|
37 |
||
38 |
for day, tab in string.gmatch(contents, tabregex) do |
|
39 |
local epregex |
|
40 |
local d, m, y = day:match("^(%d+)_(%d+)_(%d%d%d%d)$") |
|
41 |
day = string.format("%04d-%02d-%02d", y, m, d) |
|
42 |
||
43 | 43 |
epregex = '<td><a id="[^"]+" href="[^"]+" class="eplink%s*%w*"%s*>([^<]+)</a>' |
44 |
epregex = epregex .. '<span class="seasep%s*%w*"%s*></span><br />' |
|
45 |
epregex = epregex .. '<span class="seasep%s*%w*"%s*>([^<]+)</span>[%s\n]*</td>' |
|
36 | 46 |
|
47 |
local shows = {} |
|
48 |
for name, ep in string.gmatch(tab, epregex) do |
|
49 |
name = name:gsub(""", "\""):gsub("&", "&") |
|
50 |
name = name:gsub("<", "<"):gsub(">", ">") |
|
51 |
name = name:gsub("'", "'") |
|
52 |
||
53 |
local obj = { |
|
54 |
["name"] = name, |
|
55 |
["ep"] = ep |
|
56 |
} |
|
57 |
table.insert(shows, obj) |
|
58 |
end |
|
59 |
tvcaldata[day] = shows |
|
60 |
end |
|
61 |
tvcaldata_timestamp = os.date("%F") |
|
62 |
return true |
|
63 |
end |
|
64 |
||
65 |
local function tvcal_by_date (d) |
|
66 |
if not tvcaldata[d] then |
|
37 | 67 |
return "No episode found for this date ("..d..")" |
36 | 68 |
end |
69 |
||
70 |
local r = "Shows on " .. d .. ":\n" |
|
71 |
for i,j in ipairs(tvcaldata[d]) do |
|
72 |
r = r .. j.name .. " (" ..j.ep .. ")\n" |
|
73 |
end |
|
74 |
r = r:gsub("\n+$", "") |
|
75 |
return r |
|
76 |
end |
|
77 |
||
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
78 |
local function tvcal_by_name (name, plain) |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
79 |
local action = plain and "contain" or "match" |
36 | 80 |
local r = "" |
81 |
name = name:lower() |
|
82 |
||
83 |
for day, obj in pairsByKeys(tvcaldata) do |
|
84 |
for i,j in ipairs(obj) do |
|
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
85 |
if j.name:lower():find(name, 1, plain) then |
36 | 86 |
r = r .. day .. "\t" .. j.name .. " (" ..j.ep .. ")\n" |
87 |
end |
|
88 |
end |
|
89 |
end |
|
90 |
||
91 |
if r == "" then |
|
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
92 |
return "No episode found "..action.."ing this string" |
36 | 93 |
end |
94 |
r = r:gsub("\n+$", "") |
|
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
95 |
return "Episodes "..action.."ing \""..name.."\":\n" .. r |
36 | 96 |
end |
97 |
||
98 |
function tvcal.cmd (arg) |
|
99 |
if not tvcaldata_timestamp or tvcaldata_timestamp ~= os.date("%F") then |
|
100 |
tvcaldata = {} -- Empty the previous list |
|
101 |
local r, err = parse_webpage("http://www.pogdesign.co.uk/cat/") |
|
102 |
if not r then return nil, err end |
|
103 |
-- Fetch next month as well... |
|
104 |
local m = tonumber(os.date("%m"))+1 |
|
105 |
local y = os.date("%Y") |
|
106 |
if m > 12 then m = 1; y = y + 1; end |
|
107 |
parse_webpage("http://www.pogdesign.co.uk/cat/"..m.."-"..y) |
|
108 |
end |
|
109 |
||
110 |
if not arg or arg == "today" then |
|
111 |
arg = os.date("%F") |
|
112 |
elseif arg == "tomorrow" then |
|
113 |
arg = os.date("%F", os.date("%s")+86400) |
|
37 | 114 |
elseif arg:match("^%+%d+$") then |
115 |
local n = tonumber(arg:match("^%+(%d+)$")) |
|
116 |
if n < 60 then |
|
117 |
arg = os.date("%F", os.date("%s")+86400*n) |
|
118 |
end |
|
36 | 119 |
end |
120 |
||
121 |
if arg:match("^%d%d%d%d%-%d%d%-%d%d$") then |
|
122 |
return tvcal_by_date(arg) |
|
123 |
end |
|
124 |
||
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
125 |
-- Is it a pattern? (string surrounded by quotes) |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
126 |
local plain = arg:match('^"(.*)"$') |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
127 |
if plain then |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
128 |
arg = plain |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
129 |
plain = false |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
130 |
else |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
131 |
plain = true |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
132 |
end |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
133 |
return tvcal_by_name(arg, plain) |
36 | 134 |
end |
135 |
||
136 |
mcbot_register_command("tvcal", tvcal) |