# HG changeset patch # User Timm S. Mueller # Date 1287073846 -7200 # Node ID c7642a4f51a569f0e7c0875e4f9fa43f59473692 added diff -r 000000000000 -r c7642a4f51a5 bvv2cal --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bvv2cal Thu Oct 14 18:30:46 2010 +0200 @@ -0,0 +1,115 @@ +#!/usr/bin/env lua +-- +-- $ bvvcal [bezirksname] [startdatum] [enddatum] +-- +-- bezirksname : Default "friedrichshain-kreuzberg" +-- startdatum : in deutscher Notation (z.B. 10.10.2010), Default: heute +-- enddatum : Default: startdatum +-- +-- Konvertiert den Berliner BVV-Sitzungskalender +-- ins iCalendar 2.0 (vCal) Format +-- +-- Benötigt: +-- - htmltidy : http://tidy.sourceforge.net/ +-- - wget : http://www.gnu.org/software/wget/ +-- - Lua 5.1.x : http://www.lua.org/ +-- - LuaExpat : http://www.keplerproject.org/luaexpat/ +-- + +local lxp = require "lxp" + +local district = arg[1] or "friedrichshain-kreuzberg" +local startdate = arg[2] or os.date("%d.%m.%Y") +local enddate = arg[3] or startdate +local uid_uri = district .. ".bvv.berlin.piratenpartei.de" + +----- --- --- -- - - - - - + +local state = "waitcontent" +local record, cell, result = { }, { }, { } +local lnr = 1 +local parser = lxp.new { + StartElement = function(parser, tagname, attr) + if state == "waitcontent" then + if tagname == "div" and attr.id == "allrisContent" then + state = "waittab" + end + elseif state == "waittab" then + if tagname == "table" and attr.class == "tl1" then + state = "waitrow" + end + elseif state == "waitrow" then + if tagname == "tr" then + state = "waitcell" + end + end + end, + EndElement = function(parser, tagname) + if state == "waitrow" or state == "waittable" then + if tagname == "table" then + state = "end" + end + elseif state == "waitcell" then + if tagname == "tr" then + local day = table.remove(record, 1) + local sdate = table.remove(record, 1) + local d, m, y = (sdate or ""):match("^%s*(%d+)%.(%d+)%.(%d+)") + local time = table.remove(record, 1) + if d and m and y and time then + local sH, sM, eH, eM = time:match("^%s*(%d+):(%d+)%s*%-%s*(%d+):(%d+)") + if not sH then + sH, sM = time:match("^%s*(%d+):(%d+)") + end + if sH then + local what = table.concat(record, " "):match("^%s*(.-)%s*$") + table.insert(result, { y .. m .. d, sH .. sM, eH and eH .. eM, what }) + end + end + state = "waitrow" + record = { } + elseif tagname == "td" then + table.insert(record, table.concat(cell)) + cell = { } + end + end + end, + CharacterData = function(parser, s) + if state == "waitcell" then + table.insert(cell, s) + end + end +} + +parser:setencoding("ISO-8859-1") + +----- --- --- -- - - - - - + +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) +-- print(cmd) +local f = io.popen(cmd) +for line in f:lines() do +-- print(lnr .. " : " .. line) + parser:parse(line) + lnr = lnr + 1 +end + +----- --- --- -- - - - - - + +print "BEGIN:VCALENDAR" +print "PRODID:BVV2CAL" +print "VERSION:2.0" +print "" +for i = 1, #result do + local r = result[i] + print "BEGIN:VEVENT" + local start = r[1] .. "T" .. r[2] + print("UID:" .. start .. "." .. uid_uri) + print("SUMMARY:" .. r[4]) + print("DTSTART:" .. start .. "00Z") + if r[3] then + print("DTEND:" .. r[1] .. "T" .. r[3] .. "00Z") + end + print "END:VEVENT" + print "" +end +print "END:VCALENDAR"