author | Mikael Berthe <mikael@lilotux.net> |
Sat, 11 Feb 2012 12:45:17 +0100 | |
changeset 58 | 4c3028c21ccb |
parent 57 | 100a831f4f12 |
child 59 | 0a39d217b80a |
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 |
|
58
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
35 |
-- Regex for each day |
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
36 |
local tabregex = '<td id="d_(%d+_%d+_%d%d%d%d)" class="t?o?day"%s*>'.. |
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
37 |
'%s*(.-)%s*</td>' |
36 | 38 |
|
58
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
39 |
-- Regex for parsing one day |
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
40 |
local epregex = '<p>'.. |
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
41 |
'<a href=[^>]+>([^<]+)</a>'.. -- Name |
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
42 |
'<br /><a href=[^>]+>([^<]+)</a>'.. -- Episode |
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
43 |
'</p>' |
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
44 |
|
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
45 |
-- loop over all days |
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
46 |
for day, daytab in string.gmatch(contents, tabregex) do |
36 | 47 |
local d, m, y = day:match("^(%d+)_(%d+)_(%d%d%d%d)$") |
48 |
day = string.format("%04d-%02d-%02d", y, m, d) |
|
49 |
||
58
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
50 |
local shows = {} |
36 | 51 |
|
58
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
52 |
-- Loop over all episodes of a day |
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
53 |
for name, ep in string.gmatch(daytab, epregex) do |
36 | 54 |
name = name:gsub(""", "\""):gsub("&", "&") |
55 |
name = name:gsub("<", "<"):gsub(">", ">") |
|
56 |
name = name:gsub("'", "'") |
|
57 |
||
58 |
local obj = { |
|
59 |
["name"] = name, |
|
60 |
["ep"] = ep |
|
61 |
} |
|
62 |
table.insert(shows, obj) |
|
63 |
end |
|
64 |
tvcaldata[day] = shows |
|
65 |
end |
|
58
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
66 |
|
36 | 67 |
tvcaldata_timestamp = os.date("%F") |
68 |
return true |
|
69 |
end |
|
70 |
||
71 |
local function tvcal_by_date (d) |
|
72 |
if not tvcaldata[d] then |
|
37 | 73 |
return "No episode found for this date ("..d..")" |
36 | 74 |
end |
75 |
||
76 |
local r = "Shows on " .. d .. ":\n" |
|
77 |
for i,j in ipairs(tvcaldata[d]) do |
|
78 |
r = r .. j.name .. " (" ..j.ep .. ")\n" |
|
79 |
end |
|
80 |
r = r:gsub("\n+$", "") |
|
81 |
return r |
|
82 |
end |
|
83 |
||
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
84 |
local function tvcal_by_name (name, plain) |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
85 |
local action = plain and "contain" or "match" |
36 | 86 |
local r = "" |
87 |
name = name:lower() |
|
88 |
||
89 |
for day, obj in pairsByKeys(tvcaldata) do |
|
90 |
for i,j in ipairs(obj) do |
|
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
91 |
if j.name:lower():find(name, 1, plain) then |
36 | 92 |
r = r .. day .. "\t" .. j.name .. " (" ..j.ep .. ")\n" |
93 |
end |
|
94 |
end |
|
95 |
end |
|
96 |
||
97 |
if r == "" then |
|
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
98 |
return "No episode found "..action.."ing this string" |
36 | 99 |
end |
100 |
r = r:gsub("\n+$", "") |
|
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
101 |
return "Episodes "..action.."ing \""..name.."\":\n" .. r |
36 | 102 |
end |
103 |
||
104 |
function tvcal.cmd (arg) |
|
105 |
if not tvcaldata_timestamp or tvcaldata_timestamp ~= os.date("%F") then |
|
106 |
tvcaldata = {} -- Empty the previous list |
|
107 |
local r, err = parse_webpage("http://www.pogdesign.co.uk/cat/") |
|
108 |
if not r then return nil, err end |
|
109 |
-- Fetch next month as well... |
|
110 |
local m = tonumber(os.date("%m"))+1 |
|
111 |
local y = os.date("%Y") |
|
112 |
if m > 12 then m = 1; y = y + 1; end |
|
113 |
parse_webpage("http://www.pogdesign.co.uk/cat/"..m.."-"..y) |
|
114 |
end |
|
115 |
||
116 |
if not arg or arg == "today" then |
|
117 |
arg = os.date("%F") |
|
118 |
elseif arg == "tomorrow" then |
|
119 |
arg = os.date("%F", os.date("%s")+86400) |
|
37 | 120 |
elseif arg:match("^%+%d+$") then |
121 |
local n = tonumber(arg:match("^%+(%d+)$")) |
|
122 |
if n < 60 then |
|
123 |
arg = os.date("%F", os.date("%s")+86400*n) |
|
124 |
end |
|
36 | 125 |
end |
126 |
||
127 |
if arg:match("^%d%d%d%d%-%d%d%-%d%d$") then |
|
128 |
return tvcal_by_date(arg) |
|
129 |
end |
|
130 |
||
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
131 |
-- Is it a pattern? (string surrounded by quotes) |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
132 |
local plain = arg:match('^"(.*)"$') |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
133 |
if plain then |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
134 |
arg = plain |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
135 |
plain = false |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
136 |
else |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
137 |
plain = true |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
138 |
end |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
139 |
return tvcal_by_name(arg, plain) |
36 | 140 |
end |
141 |
||
142 |
mcbot_register_command("tvcal", tvcal) |