author | Mikael Berthe <mikael@lilotux.net> |
Sun, 25 Nov 2012 19:29:32 +0100 | |
changeset 65 | 2cefbe9f3ac6 |
parent 63 | 31f967ba9e1f |
child 66 | d9c00a9fe9d5 |
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("'", "'") |
|
63
31f967ba9e1f
tvcal: Remove newlines in episode names
Mikael Berthe <mikael@lilotux.net>
parents:
61
diff
changeset
|
60 |
name = name:gsub("%s*\n%s*", " ") |
36 | 61 |
|
62 |
local obj = { |
|
63 |
["name"] = name, |
|
64 |
["ep"] = ep |
|
65 |
} |
|
66 |
table.insert(shows, obj) |
|
67 |
end |
|
68 |
tvcaldata[day] = shows |
|
69 |
end |
|
58
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
70 |
|
36 | 71 |
tvcaldata_timestamp = os.date("%F") |
72 |
return true |
|
73 |
end |
|
74 |
||
75 |
local function tvcal_by_date (d) |
|
76 |
if not tvcaldata[d] then |
|
37 | 77 |
return "No episode found for this date ("..d..")" |
36 | 78 |
end |
79 |
||
80 |
local r = "Shows on " .. d .. ":\n" |
|
81 |
for i,j in ipairs(tvcaldata[d]) do |
|
82 |
r = r .. j.name .. " (" ..j.ep .. ")\n" |
|
83 |
end |
|
84 |
r = r:gsub("\n+$", "") |
|
85 |
return r |
|
86 |
end |
|
87 |
||
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
88 |
local function tvcal_by_name (name, plain) |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
89 |
local action = plain and "contain" or "match" |
36 | 90 |
local r = "" |
91 |
name = name:lower() |
|
92 |
||
93 |
for day, obj in pairsByKeys(tvcaldata) do |
|
94 |
for i,j in ipairs(obj) do |
|
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
95 |
if j.name:lower():find(name, 1, plain) then |
36 | 96 |
r = r .. day .. "\t" .. j.name .. " (" ..j.ep .. ")\n" |
97 |
end |
|
98 |
end |
|
99 |
end |
|
100 |
||
101 |
if r == "" then |
|
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
102 |
return "No episode found "..action.."ing this string" |
36 | 103 |
end |
104 |
r = r:gsub("\n+$", "") |
|
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
105 |
return "Episodes "..action.."ing \""..name.."\":\n" .. r |
36 | 106 |
end |
107 |
||
108 |
function tvcal.cmd (arg) |
|
109 |
if not tvcaldata_timestamp or tvcaldata_timestamp ~= os.date("%F") then |
|
110 |
tvcaldata = {} -- Empty the previous list |
|
111 |
local r, err = parse_webpage("http://www.pogdesign.co.uk/cat/") |
|
112 |
if not r then return nil, err end |
|
113 |
-- Fetch next month as well... |
|
114 |
local m = tonumber(os.date("%m"))+1 |
|
115 |
local y = os.date("%Y") |
|
116 |
if m > 12 then m = 1; y = y + 1; end |
|
117 |
parse_webpage("http://www.pogdesign.co.uk/cat/"..m.."-"..y) |
|
118 |
end |
|
119 |
||
120 |
if not arg or arg == "today" then |
|
121 |
arg = os.date("%F") |
|
122 |
elseif arg == "tomorrow" then |
|
123 |
arg = os.date("%F", os.date("%s")+86400) |
|
37 | 124 |
elseif arg:match("^%+%d+$") then |
125 |
local n = tonumber(arg:match("^%+(%d+)$")) |
|
126 |
if n < 60 then |
|
127 |
arg = os.date("%F", os.date("%s")+86400*n) |
|
128 |
end |
|
36 | 129 |
end |
130 |
||
131 |
if arg:match("^%d%d%d%d%-%d%d%-%d%d$") then |
|
132 |
return tvcal_by_date(arg) |
|
133 |
end |
|
134 |
||
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
135 |
-- Is it a pattern? (string surrounded by quotes) |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
136 |
local plain = arg:match('^"(.*)"$') |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
137 |
if plain then |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
138 |
arg = plain |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
139 |
plain = false |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
140 |
else |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
141 |
plain = true |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
142 |
end |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
143 |
return tvcal_by_name(arg, plain) |
36 | 144 |
end |
145 |
||
146 |
mcbot_register_command("tvcal", tvcal) |