tmueller@0
|
1 |
#!/usr/bin/env lua
|
tmueller@0
|
2 |
--
|
tmueller@0
|
3 |
-- $ bvvcal [bezirksname] [startdatum] [enddatum]
|
tmueller@0
|
4 |
--
|
tmueller@0
|
5 |
-- bezirksname : Default "friedrichshain-kreuzberg"
|
tmueller@0
|
6 |
-- startdatum : in deutscher Notation (z.B. 10.10.2010), Default: heute
|
tmueller@0
|
7 |
-- enddatum : Default: startdatum
|
tmueller@0
|
8 |
--
|
tmueller@0
|
9 |
-- Konvertiert den Berliner BVV-Sitzungskalender
|
tmueller@0
|
10 |
-- ins iCalendar 2.0 (vCal) Format
|
tmueller@0
|
11 |
--
|
tmueller@0
|
12 |
-- Benötigt:
|
tmueller@0
|
13 |
-- - htmltidy : http://tidy.sourceforge.net/
|
tmueller@0
|
14 |
-- - wget : http://www.gnu.org/software/wget/
|
tmueller@0
|
15 |
-- - Lua 5.1.x : http://www.lua.org/
|
tmueller@0
|
16 |
-- - LuaExpat : http://www.keplerproject.org/luaexpat/
|
tmueller@0
|
17 |
--
|
tmueller@0
|
18 |
|
tmueller@0
|
19 |
local lxp = require "lxp"
|
tmueller@0
|
20 |
|
tmueller@0
|
21 |
local district = arg[1] or "friedrichshain-kreuzberg"
|
tmueller@0
|
22 |
local startdate = arg[2] or os.date("%d.%m.%Y")
|
tmueller@0
|
23 |
local enddate = arg[3] or startdate
|
tmueller@0
|
24 |
local uid_uri = district .. ".bvv.berlin.piratenpartei.de"
|
tmueller@0
|
25 |
|
tmueller@0
|
26 |
----- --- --- -- - - - - -
|
tmueller@0
|
27 |
|
tmueller@0
|
28 |
local state = "waitcontent"
|
tmueller@0
|
29 |
local record, cell, result = { }, { }, { }
|
tmueller@0
|
30 |
local lnr = 1
|
tmueller@0
|
31 |
local parser = lxp.new {
|
tmueller@0
|
32 |
StartElement = function(parser, tagname, attr)
|
tmueller@0
|
33 |
if state == "waitcontent" then
|
tmueller@0
|
34 |
if tagname == "div" and attr.id == "allrisContent" then
|
tmueller@0
|
35 |
state = "waittab"
|
tmueller@0
|
36 |
end
|
tmueller@0
|
37 |
elseif state == "waittab" then
|
tmueller@0
|
38 |
if tagname == "table" and attr.class == "tl1" then
|
tmueller@0
|
39 |
state = "waitrow"
|
tmueller@0
|
40 |
end
|
tmueller@0
|
41 |
elseif state == "waitrow" then
|
tmueller@0
|
42 |
if tagname == "tr" then
|
tmueller@0
|
43 |
state = "waitcell"
|
tmueller@0
|
44 |
end
|
tmueller@0
|
45 |
end
|
tmueller@0
|
46 |
end,
|
tmueller@0
|
47 |
EndElement = function(parser, tagname)
|
tmueller@0
|
48 |
if state == "waitrow" or state == "waittable" then
|
tmueller@0
|
49 |
if tagname == "table" then
|
tmueller@0
|
50 |
state = "end"
|
tmueller@0
|
51 |
end
|
tmueller@0
|
52 |
elseif state == "waitcell" then
|
tmueller@0
|
53 |
if tagname == "tr" then
|
tmueller@0
|
54 |
local day = table.remove(record, 1)
|
tmueller@0
|
55 |
local sdate = table.remove(record, 1)
|
tmueller@0
|
56 |
local d, m, y = (sdate or ""):match("^%s*(%d+)%.(%d+)%.(%d+)")
|
tmueller@0
|
57 |
local time = table.remove(record, 1)
|
tmueller@0
|
58 |
if d and m and y and time then
|
tmueller@0
|
59 |
local sH, sM, eH, eM = time:match("^%s*(%d+):(%d+)%s*%-%s*(%d+):(%d+)")
|
tmueller@0
|
60 |
if not sH then
|
tmueller@0
|
61 |
sH, sM = time:match("^%s*(%d+):(%d+)")
|
tmueller@0
|
62 |
end
|
tmueller@0
|
63 |
if sH then
|
tmueller@0
|
64 |
local what = table.concat(record, " "):match("^%s*(.-)%s*$")
|
tmueller@0
|
65 |
table.insert(result, { y .. m .. d, sH .. sM, eH and eH .. eM, what })
|
tmueller@0
|
66 |
end
|
tmueller@0
|
67 |
end
|
tmueller@0
|
68 |
state = "waitrow"
|
tmueller@0
|
69 |
record = { }
|
tmueller@0
|
70 |
elseif tagname == "td" then
|
tmueller@0
|
71 |
table.insert(record, table.concat(cell))
|
tmueller@0
|
72 |
cell = { }
|
tmueller@0
|
73 |
end
|
tmueller@0
|
74 |
end
|
tmueller@0
|
75 |
end,
|
tmueller@0
|
76 |
CharacterData = function(parser, s)
|
tmueller@0
|
77 |
if state == "waitcell" then
|
tmueller@0
|
78 |
table.insert(cell, s)
|
tmueller@0
|
79 |
end
|
tmueller@0
|
80 |
end
|
tmueller@0
|
81 |
}
|
tmueller@0
|
82 |
|
tmueller@0
|
83 |
parser:setencoding("ISO-8859-1")
|
tmueller@0
|
84 |
|
tmueller@0
|
85 |
----- --- --- -- - - - - -
|
tmueller@0
|
86 |
|
tmueller@0
|
87 |
local cmd = ("wget --quiet --post-data 'kaldatvon=%s&kaldatbis=%s' 'http://www.berlin.de/ba-%s/bvv-online/si010.asp' -O - | tidy -latin1 -asxml -i -w 0 2>/dev/null -q"):format(startdate, enddate, district)
|
tmueller@0
|
88 |
-- print(cmd)
|
tmueller@0
|
89 |
local f = io.popen(cmd)
|
tmueller@0
|
90 |
for line in f:lines() do
|
tmueller@0
|
91 |
-- print(lnr .. " : " .. line)
|
tmueller@0
|
92 |
parser:parse(line)
|
tmueller@0
|
93 |
lnr = lnr + 1
|
tmueller@0
|
94 |
end
|
tmueller@0
|
95 |
|
tmueller@0
|
96 |
----- --- --- -- - - - - -
|
tmueller@0
|
97 |
|
tmueller@0
|
98 |
print "BEGIN:VCALENDAR"
|
tmueller@0
|
99 |
print "PRODID:BVV2CAL"
|
tmueller@0
|
100 |
print "VERSION:2.0"
|
tmueller@0
|
101 |
print ""
|
tmueller@0
|
102 |
for i = 1, #result do
|
tmueller@0
|
103 |
local r = result[i]
|
tmueller@0
|
104 |
print "BEGIN:VEVENT"
|
tmueller@0
|
105 |
local start = r[1] .. "T" .. r[2]
|
tmueller@0
|
106 |
print("UID:" .. start .. "." .. uid_uri)
|
tmueller@0
|
107 |
print("SUMMARY:" .. r[4])
|
tmueller@0
|
108 |
print("DTSTART:" .. start .. "00Z")
|
tmueller@0
|
109 |
if r[3] then
|
tmueller@0
|
110 |
print("DTEND:" .. r[1] .. "T" .. r[3] .. "00Z")
|
tmueller@0
|
111 |
end
|
tmueller@0
|
112 |
print "END:VEVENT"
|
tmueller@0
|
113 |
print ""
|
tmueller@0
|
114 |
end
|
tmueller@0
|
115 |
print "END:VCALENDAR"
|