cal-proxy/app.py

40 lines
961 B
Python
Raw Normal View History

2016-09-16 23:58:26 +02:00
from flask import Flask
2016-09-17 00:02:27 +02:00
import os
2016-09-16 23:58:26 +02:00
import urllib.request
from icalendar.cal import Calendar, Component
app = Flask(__name__)
@app.route("/")
def hello():
2016-09-17 00:12:32 +02:00
url = os.environ.get('URL', None)
if url is None:
return ""
2016-09-17 00:12:32 +02:00
cal_str = urllib.request.urlopen(url).read()
2016-09-16 23:58:26 +02:00
FILTERED_EVENTS = os.environ.get('COURSES', '').split(',')
2016-09-16 23:58:26 +02:00
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__":
2016-09-17 00:06:06 +02:00
port = int(os.environ.get('PORT', 5000))
app.run('0.0.0.0', port)