Leave start and end values if we're not modifying them

This commit is contained in:
Scott Wallace 2023-05-12 09:04:31 +01:00
parent cd38fb6b37
commit 45202720ca
Signed by: scott
GPG key ID: AA742FDC5AFE2A72

64
main.py
View file

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