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