Skip to content

Instantly share code, notes, and snippets.

@cato-
Created March 15, 2013 10:44
Show Gist options
  • Save cato-/5168949 to your computer and use it in GitHub Desktop.
Save cato-/5168949 to your computer and use it in GitHub Desktop.
Generate iCal Feed from AHA Hannover website
$HTTP["host"] == "cal.wgchaos.info" {
server.name = "cal.wgchaos.info"
var.dir = "info/wgchaos/cal"
server.document-root = "/var/www/info/wgchaos/cal/html"
accesslog.filename = "/var/log/lighttpd/access-cal.wgchaos.info.log"
$HTTP["host"] != "cal.wgchaos.info" {
$HTTP["host"] =~ "^(.*)$" {
url.redirect = (
"^/(.*)" => "http://cal.wgchaos.info/$1",
)
} # end of $HTTP["host"] =~ "^(.*)$"
} # end of $HTTP["host"] != "cal.wgchaos.info"
$HTTP["url"] == "/aha" {
cgi.assign = (
"" => "",
)
} # end of $HTTP["url"] == "/aha"
} # end of $HTTP["host"] == "cal.wgchaos.info"
#!/usr/bin/env python
from datetime import datetime, timedelta
import locale
import time
import cgi
from lxml.html.soupparser import fromstring
import requests
import vobject
# german locale for dateparsing
locale.setlocale(locale.LC_TIME, ('de_DE', 'UTF8'))
# get args from cgi
args = cgi.FieldStorage()
street = args['streetid'].value # 00732
nr = args['nr'].value # 10b
# Fetch dates from AHA
r=requests.post("http://www.aha-region.de/abfuhrkalender.html", data={'gemeinde': 'Hannover', 'strasse': street, 'hausnr': nr})
# Parse HTML page
result = {}
root = fromstring(r.text)
tbody=root.xpath('//div[@class="tx-vcdrabfallkalender-pi1"]/table/tbody')[0]
for tr in tbody.getchildren():
if tr[0].tag == 'th':
continue # skip table header
if len(tr) < 5:
continue
what = tr[1].text
for dat in tr[3].itertext():
if '###' in dat:
continue # skip comments
dat = dat.strip().strip('*').strip()
dat = datetime.strptime(dat,'%a, %d.%m.%Y')
if not dat in result:
result[dat] = []
if not what in result[dat]:
result[dat].append(what)
# Create Calendar
cal = vobject.iCalendar()
cal.add('method').value = 'PUBLISH' # IE/Outlook needs this
cal.add('X-WR-CALNAME').value = "AHA Muellabfuhrtermine"
cal.add('X-WR-TIMEZONE').value = time.tzname[time.localtime().tm_isdst]
for day, items in result.iteritems():
vevent = cal.add('vevent')
val = ", ".join(items)
vevent.add('summary').value = val
vevent.add('dtstart').value = day.date()
vevent.add('dtend').value = (day+timedelta(days=1)).date()
vevent.add('uid').value = val+str(day.date())
icalstream = cal.serialize()
print "Content-Type: text/calendar"
print ""
print icalstream
#!/bin/bash
. ../venv/bin/activate
python ../generate.py
BeautifulSoup==3.1.0.1
lxml==2.2.8
requests==1.1.0
vobject==0.8.1c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment