bvv2cal
changeset 0 c7642a4f51a5
child 1 522bf1c3dd7c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/bvv2cal	Thu Oct 14 18:30:46 2010 +0200
     1.3 @@ -0,0 +1,115 @@
     1.4 +#!/usr/bin/env lua
     1.5 +--
     1.6 +--	$ bvvcal [bezirksname] [startdatum] [enddatum]
     1.7 +--
     1.8 +--	bezirksname : Default "friedrichshain-kreuzberg"
     1.9 +--	startdatum : in deutscher Notation (z.B. 10.10.2010), Default: heute
    1.10 +--	enddatum : Default: startdatum
    1.11 +--
    1.12 +--	Konvertiert den Berliner BVV-Sitzungskalender 
    1.13 +--	ins iCalendar 2.0 (vCal) Format
    1.14 +--
    1.15 +--	Benötigt: 
    1.16 +--	- htmltidy : http://tidy.sourceforge.net/
    1.17 +--	- wget : http://www.gnu.org/software/wget/
    1.18 +--	- Lua 5.1.x : http://www.lua.org/
    1.19 +--	- LuaExpat : http://www.keplerproject.org/luaexpat/
    1.20 +--
    1.21 +
    1.22 +local lxp = require "lxp"
    1.23 +
    1.24 +local district = arg[1] or "friedrichshain-kreuzberg"
    1.25 +local startdate = arg[2] or os.date("%d.%m.%Y")
    1.26 +local enddate = arg[3] or startdate
    1.27 +local uid_uri = district .. ".bvv.berlin.piratenpartei.de"
    1.28 +
    1.29 +----- --- --- -- - -  -   -    -
    1.30 +
    1.31 +local state = "waitcontent"
    1.32 +local record, cell, result = { }, { }, { }
    1.33 +local lnr = 1
    1.34 +local parser = lxp.new {
    1.35 +	StartElement = function(parser, tagname, attr)
    1.36 +		if state == "waitcontent" then
    1.37 +			if tagname == "div" and attr.id == "allrisContent" then
    1.38 +				state = "waittab"
    1.39 +			end
    1.40 +		elseif state == "waittab" then
    1.41 +			if tagname == "table" and attr.class == "tl1" then
    1.42 +				state = "waitrow"
    1.43 +			end
    1.44 +		elseif state == "waitrow" then
    1.45 +			if tagname == "tr" then
    1.46 +				state = "waitcell"
    1.47 +			end
    1.48 +		end
    1.49 +	end,
    1.50 +	EndElement = function(parser, tagname)
    1.51 +		if state == "waitrow" or state == "waittable" then
    1.52 +			if tagname == "table" then
    1.53 +				state = "end"
    1.54 +			end
    1.55 +		elseif state == "waitcell" then
    1.56 +			if tagname == "tr" then
    1.57 +				local day = table.remove(record, 1)
    1.58 +				local sdate = table.remove(record, 1)
    1.59 +				local d, m, y = (sdate or ""):match("^%s*(%d+)%.(%d+)%.(%d+)")
    1.60 +				local time = table.remove(record, 1)
    1.61 +				if d and m and y and time then
    1.62 +					local sH, sM, eH, eM = time:match("^%s*(%d+):(%d+)%s*%-%s*(%d+):(%d+)")
    1.63 +					if not sH then
    1.64 +						sH, sM = time:match("^%s*(%d+):(%d+)")
    1.65 +					end
    1.66 +					if sH then
    1.67 +						local what = table.concat(record, " "):match("^%s*(.-)%s*$")
    1.68 +						table.insert(result, { y .. m .. d, sH .. sM, eH and eH .. eM, what })
    1.69 +					end
    1.70 +				end
    1.71 +				state = "waitrow"
    1.72 +				record = { }
    1.73 +			elseif tagname == "td" then
    1.74 +				table.insert(record, table.concat(cell))
    1.75 +				cell = { }
    1.76 +			end
    1.77 +		end
    1.78 +	end,
    1.79 +	CharacterData = function(parser, s)
    1.80 +		if state == "waitcell" then
    1.81 +			table.insert(cell, s)
    1.82 +		end
    1.83 +	end
    1.84 +}
    1.85 +
    1.86 +parser:setencoding("ISO-8859-1")
    1.87 +
    1.88 +----- --- --- -- - -  -   -    -
    1.89 +
    1.90 +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)
    1.91 +-- print(cmd)
    1.92 +local f = io.popen(cmd)
    1.93 +for line in f:lines() do
    1.94 +-- 	print(lnr .. " : " .. line)
    1.95 +	parser:parse(line)
    1.96 +	lnr = lnr + 1
    1.97 +end
    1.98 +
    1.99 +----- --- --- -- - -  -   -    -
   1.100 +
   1.101 +print "BEGIN:VCALENDAR"
   1.102 +print "PRODID:BVV2CAL"
   1.103 +print "VERSION:2.0"
   1.104 +print ""
   1.105 +for i = 1, #result do
   1.106 +	local r = result[i]
   1.107 +	print "BEGIN:VEVENT"
   1.108 +	local start = r[1] .. "T" .. r[2]
   1.109 +	print("UID:" .. start .. "." .. uid_uri)
   1.110 +	print("SUMMARY:" .. r[4])
   1.111 +	print("DTSTART:" .. start .. "00Z")
   1.112 +	if r[3] then
   1.113 +		print("DTEND:" .. r[1] .. "T" .. r[3] .. "00Z")
   1.114 +	end
   1.115 +	print "END:VEVENT"
   1.116 +	print ""
   1.117 +end
   1.118 +print "END:VCALENDAR"