author | Mikael Berthe <mikael@lilotux.net> |
Tue, 27 Nov 2012 16:26:04 +0100 | |
changeset 66 | d9c00a9fe9d5 |
parent 63 | 31f967ba9e1f |
permissions | -rw-r--r-- |
36 | 1 |
#! /usr/bin/env lua |
2 |
||
66
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
63
diff
changeset
|
3 |
-- This module is part of the McBot / mcabbot project |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
63
diff
changeset
|
4 |
-- Data extractor for http://www.pogdesign.co.uk/cat/ |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
63
diff
changeset
|
5 |
-- |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
63
diff
changeset
|
6 |
-- Copyright (C) 2010-2012 Mikael Berthe <mikael@lilotux.net> |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
63
diff
changeset
|
7 |
-- |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
63
diff
changeset
|
8 |
-- This program is free software; you can redistribute it and/or modify |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
63
diff
changeset
|
9 |
-- it under the terms of the GNU General Public License as published by |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
63
diff
changeset
|
10 |
-- the Free Software Foundation; either version 2 of the License, or (at |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
63
diff
changeset
|
11 |
-- your option) any later version. |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
63
diff
changeset
|
12 |
-- |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
63
diff
changeset
|
13 |
-- Please check the license in the COPYING file at the root of the tree. |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
63
diff
changeset
|
14 |
-- |
36 | 15 |
|
16 |
require "libs.shcmd" |
|
17 |
||
18 |
local tvcal = { ["desc"] = "Display TV calendar" } |
|
19 |
||
20 |
local tvcaldata = {} |
|
21 |
local tvcaldata_timestamp |
|
22 |
||
23 |
-- Function from PIL |
|
24 |
local function pairsByKeys (t, f) |
|
25 |
local a = {} |
|
26 |
for n in pairs(t) do table.insert(a, n) end |
|
27 |
table.sort(a, f) |
|
28 |
local i = 0 -- iterator variable |
|
29 |
local iter = function () -- iterator function |
|
30 |
i = i + 1 |
|
31 |
if a[i] == nil then return nil |
|
32 |
else return a[i], t[a[i]] |
|
33 |
end |
|
34 |
end |
|
35 |
return iter |
|
36 |
end |
|
37 |
||
38 |
local function parse_webpage (url) |
|
39 |
local contents = shcmd("curl "..url) |
|
40 |
||
39
237af42156a1
Add checks after shcmd() calls
Mikael Berthe <mikael@lilotux.net>
parents:
38
diff
changeset
|
41 |
if not contents then |
237af42156a1
Add checks after shcmd() calls
Mikael Berthe <mikael@lilotux.net>
parents:
38
diff
changeset
|
42 |
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
|
43 |
end |
237af42156a1
Add checks after shcmd() calls
Mikael Berthe <mikael@lilotux.net>
parents:
38
diff
changeset
|
44 |
|
61
5d807892b439
tvcal: Work around buggy 0x0d at end of lines
Mikael Berthe <mikael@lilotux.net>
parents:
59
diff
changeset
|
45 |
-- 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
|
46 |
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
|
47 |
|
58
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
48 |
-- Regex for each day |
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
49 |
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
|
50 |
'%s*(.-)%s*</td>' |
36 | 51 |
|
58
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
52 |
-- Regex for parsing one day |
59 | 53 |
local epregex = '<p[^>]*>'.. |
61
5d807892b439
tvcal: Work around buggy 0x0d at end of lines
Mikael Berthe <mikael@lilotux.net>
parents:
59
diff
changeset
|
54 |
'<a href=[^>]+>([^<]+)</a>'..eol.. -- Name |
5d807892b439
tvcal: Work around buggy 0x0d at end of lines
Mikael Berthe <mikael@lilotux.net>
parents:
59
diff
changeset
|
55 |
'<br /><a href=[^>]+>([^<]+)</a>'..eol.. -- Episode |
58
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
56 |
'</p>' |
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
57 |
|
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
58 |
-- loop over all days |
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
59 |
for day, daytab in string.gmatch(contents, tabregex) do |
36 | 60 |
local d, m, y = day:match("^(%d+)_(%d+)_(%d%d%d%d)$") |
61 |
day = string.format("%04d-%02d-%02d", y, m, d) |
|
62 |
||
58
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
63 |
local shows = {} |
36 | 64 |
|
58
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
65 |
-- Loop over all episodes of a day |
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
66 |
for name, ep in string.gmatch(daytab, epregex) do |
36 | 67 |
name = name:gsub(""", "\""):gsub("&", "&") |
68 |
name = name:gsub("<", "<"):gsub(">", ">") |
|
69 |
name = name:gsub("'", "'") |
|
63
31f967ba9e1f
tvcal: Remove newlines in episode names
Mikael Berthe <mikael@lilotux.net>
parents:
61
diff
changeset
|
70 |
name = name:gsub("%s*\n%s*", " ") |
36 | 71 |
|
72 |
local obj = { |
|
73 |
["name"] = name, |
|
74 |
["ep"] = ep |
|
75 |
} |
|
76 |
table.insert(shows, obj) |
|
77 |
end |
|
78 |
tvcaldata[day] = shows |
|
79 |
end |
|
58
4c3028c21ccb
Sync tvcal module (another website change...)
Mikael Berthe <mikael@lilotux.net>
parents:
57
diff
changeset
|
80 |
|
36 | 81 |
tvcaldata_timestamp = os.date("%F") |
82 |
return true |
|
83 |
end |
|
84 |
||
85 |
local function tvcal_by_date (d) |
|
86 |
if not tvcaldata[d] then |
|
37 | 87 |
return "No episode found for this date ("..d..")" |
36 | 88 |
end |
89 |
||
90 |
local r = "Shows on " .. d .. ":\n" |
|
91 |
for i,j in ipairs(tvcaldata[d]) do |
|
92 |
r = r .. j.name .. " (" ..j.ep .. ")\n" |
|
93 |
end |
|
94 |
r = r:gsub("\n+$", "") |
|
95 |
return r |
|
96 |
end |
|
97 |
||
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
98 |
local function tvcal_by_name (name, plain) |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
99 |
local action = plain and "contain" or "match" |
36 | 100 |
local r = "" |
101 |
name = name:lower() |
|
102 |
||
103 |
for day, obj in pairsByKeys(tvcaldata) do |
|
104 |
for i,j in ipairs(obj) do |
|
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
105 |
if j.name:lower():find(name, 1, plain) then |
36 | 106 |
r = r .. day .. "\t" .. j.name .. " (" ..j.ep .. ")\n" |
107 |
end |
|
108 |
end |
|
109 |
end |
|
110 |
||
111 |
if r == "" then |
|
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
112 |
return "No episode found "..action.."ing this string" |
36 | 113 |
end |
114 |
r = r:gsub("\n+$", "") |
|
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
115 |
return "Episodes "..action.."ing \""..name.."\":\n" .. r |
36 | 116 |
end |
117 |
||
118 |
function tvcal.cmd (arg) |
|
119 |
if not tvcaldata_timestamp or tvcaldata_timestamp ~= os.date("%F") then |
|
120 |
tvcaldata = {} -- Empty the previous list |
|
121 |
local r, err = parse_webpage("http://www.pogdesign.co.uk/cat/") |
|
122 |
if not r then return nil, err end |
|
123 |
-- Fetch next month as well... |
|
124 |
local m = tonumber(os.date("%m"))+1 |
|
125 |
local y = os.date("%Y") |
|
126 |
if m > 12 then m = 1; y = y + 1; end |
|
127 |
parse_webpage("http://www.pogdesign.co.uk/cat/"..m.."-"..y) |
|
128 |
end |
|
129 |
||
130 |
if not arg or arg == "today" then |
|
131 |
arg = os.date("%F") |
|
132 |
elseif arg == "tomorrow" then |
|
133 |
arg = os.date("%F", os.date("%s")+86400) |
|
37 | 134 |
elseif arg:match("^%+%d+$") then |
135 |
local n = tonumber(arg:match("^%+(%d+)$")) |
|
136 |
if n < 60 then |
|
137 |
arg = os.date("%F", os.date("%s")+86400*n) |
|
138 |
end |
|
36 | 139 |
end |
140 |
||
141 |
if arg:match("^%d%d%d%d%-%d%d%-%d%d$") then |
|
142 |
return tvcal_by_date(arg) |
|
143 |
end |
|
144 |
||
38
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
145 |
-- Is it a pattern? (string surrounded by quotes) |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
146 |
local plain = arg:match('^"(.*)"$') |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
147 |
if plain then |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
148 |
arg = plain |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
149 |
plain = false |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
150 |
else |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
151 |
plain = true |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
152 |
end |
a51ddbce247b
Add regex support to tvcal
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
153 |
return tvcal_by_name(arg, plain) |
36 | 154 |
end |
155 |
||
156 |
mcbot_register_command("tvcal", tvcal) |