cal-proxy/app.py

40 lines
961 B
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 = os.environ.get('COURSES', '').split(',')
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)