cal-proxy/app.py
2016-09-17 00:12:32 +02:00

54 lines
1.1 KiB
Python

from flask import Flask
import os
import urllib.request
from icalendar.cal import Calendar, Component
app = Flask(__name__)
@app.route("/")
def hello():
url = os.environ.get('URL', None)
if url is None:
return ""
cal_str = urllib.request.urlopen(url).read()
FILTERED_EVENTS = [
'MGL',
'SYD',
'SQE',
'COM',
'RSM',
'ANG1',
'DLG1',
'SPO1',
'SPO2',
'STA1',
'PST',
'SEC',
'DIA'
]
cal = Component.from_ical(cal_str)
other = Calendar()
# Copy all properties of calendar
for k, v in cal.items():
other.add(k, v)
# Copy the VTIMEZONE component
other.add_component(cal.walk('VTIMEZONE')[0])
# Filter and copy VEVENTs
for ev in cal.walk('VEVENT'):
course_code = ev['SUMMARY'].split('-')[1].split('/')[0]
if course_code not in FILTERED_EVENTS:
other.add_component(ev)
return other.to_ical().replace(b'\r\n', b'\n').strip()
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
app.run('0.0.0.0', port)