ical_offset/main.py

64 lines
1.8 KiB
Python
Raw Normal View History

2022-05-02 13:44:10 +01:00
"""
Take existing calendar and update it slightly
"""
from datetime import datetime, time, timedelta
import requests
from flask import Flask, Response, request
from icalendar import Calendar, Event # type: ignore[import]
from werkzeug.middleware.proxy_fix import ProxyFix
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1) # type: ignore[assignment]
@app.route('/', methods=['get'])
def process() -> Response:
"""
Fetch ICS url and convert from Events to Todos
Returns:
Response: ical response
"""
# Fetch existing calendar
orig = requests.get(request.args['url'])
days = request.args.get('days', '0')
hours = request.args.get('hours', '0')
mins = request.args.get('mins', '0')
orig_cal = Calendar.from_ical(orig.text)
cal = Calendar()
cal.add('version', '2.0')
cal.add(
'prodid',
'-//Scott Wallace//event2task//EN',
)
for component in orig_cal.subcomponents:
if isinstance(component, Event):
entry = Event()
entry.add('description', component['description'])
entry.add('dtstamp', component['dtstamp'])
entry.add(
'dtstart',
datetime.combine(component.decoded('dtstart'), time(0))
+ timedelta(days=int(days), hours=int(hours), minutes=int(mins)),
)
2022-07-07 13:44:24 +01:00
entry.add(
'dtend',
datetime.combine(component.decoded('dtend'), time(0))
+ timedelta(days=int(days), hours=int(hours), minutes=int(mins)),
)
2022-05-02 13:44:10 +01:00
entry.add('summary', component['summary'])
entry.add('uid', component['uid'])
cal.add_component(entry)
return Response(
cal.to_ical().decode().replace('\\r\\n', '\n').strip(),
headers={'content-type': 'text/calendar; charset=UTF-8'},
)