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