From 45202720ca411455abc967f4a98b9df300262efe Mon Sep 17 00:00:00 2001 From: Scott Wallace Date: Fri, 12 May 2023 09:04:31 +0100 Subject: [PATCH] Leave start and end values if we're not modifying them --- main.py | 64 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/main.py b/main.py index 24db799..8e5cbcb 100644 --- a/main.py +++ b/main.py @@ -2,18 +2,18 @@ Take existing calendar and update it slightly """ -from datetime import datetime, time, timedelta +from datetime import date, datetime, time, timedelta import requests 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 app = Flask(__name__) app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1) # type: ignore[assignment] -@app.route('/', methods=['get']) +@app.route("/", methods=["get"]) def process() -> Response: """ Fetch ICS url and convert from Events to Todos @@ -23,41 +23,53 @@ def process() -> 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 = requests.get(request.args["url"], timeout=30) + days = int(request.args.get("days", "0")) + hours = int(request.args.get("hours", "0")) + mins = int(request.args.get("mins", "0")) orig_cal = Calendar.from_ical(orig.text) cal = Calendar() - cal.add('version', '2.0') + cal.add("version", "2.0") cal.add( - 'prodid', - '-//Scott Wallace//event2task//EN', + "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)), - ) - entry.add( - 'dtend', - datetime.combine(component.decoded('dtend'), time(0)) - + timedelta(days=int(days), hours=int(hours), minutes=int(mins)), - ) - entry.add('summary', component['summary']) - entry.add('uid', component['uid']) + + entry.add("description", component["description"]) + entry.add("dtstamp", component["dtstamp"]) + if days or hours or mins: + start = component.decoded("dtstart") + end = component.decoded("dtend") + if isinstance(start, date): + entry.add( + "dtstart", + datetime.combine(start, time(0)) + + timedelta(days=days, hours=hours, minutes=mins), + ) + if isinstance(end, date): + 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) return Response( - cal.to_ical().decode().replace('\\r\\n', '\n').strip(), - headers={'content-type': 'text/calendar; charset=UTF-8'}, + cal.to_ical().decode().replace("\\r\\n", "\n").strip(), + headers={"content-type": "text/calendar; charset=UTF-8"}, )