Skip to content

Commit

Permalink
enable recurrent events in ical-proxy
Browse files Browse the repository at this point in the history
portswigger-tim committed Oct 17, 2023
1 parent 4bdba8d commit 1d7780e
Showing 3 changed files with 61 additions and 24 deletions.
57 changes: 33 additions & 24 deletions ical-proxy/app.py
Original file line number Diff line number Diff line change
@@ -2,8 +2,10 @@
import os

import icalendar
import recurring_ical_events
import requests
from cachetools import TTLCache, cached
from dateutil.relativedelta import relativedelta
from flask import Flask, abort, request
from flask_cors import CORS
from requests_file import FileAdapter
@@ -12,6 +14,8 @@

CACHE = TTLCache(maxsize=1000, ttl=CACHE_TTL)

DATE_MIN = (1970, 1, 1)

app = Flask(__name__)

cors = CORS(app)
@@ -100,30 +104,35 @@ def _convert_ical_to_annotations(ical_data, tags):
ical_annotations = []
ical_calendar = icalendar.Calendar.from_ical(ical_data)

for component in ical_calendar.walk():
if component.name == "VEVENT":
try:
start_time = component.get("dtstart").dt

end_time = component.get("dtstart").dt
if "dtend" in component:
end_time = component.get("dtend").dt

ical_annotation = {
"time": _millis_timestamp(start_time),
"timeEnd": _millis_timestamp(end_time),
"title": component.get("summary"),
"tags": tags,
"text": component.get("description", ""),
"uid": component.get("uid"),
}

ical_annotations.append(ical_annotation)

except AttributeError as e:
print(
f'level=ERROR {e} msg="decoding event failed" event_summary="{component.get("summary")}" event_dtstart="{component.get("dtstart")}" event_dtstamp="{component.get("dtstamp")}" event_dtend="{component.get("dtend")}"'
)
date_max = datetime.date.today() + relativedelta(years=1)

ical_events = recurring_ical_events.of(
ical_calendar, components=["VEVENT"]
).between(DATE_MIN, date_max)

for component in ical_events:
try:
start_time = component.get("dtstart").dt

end_time = component.get("dtstart").dt
if "dtend" in component:
end_time = component.get("dtend").dt

ical_annotation = {
"time": _millis_timestamp(start_time),
"timeEnd": _millis_timestamp(end_time),
"title": component.get("summary"),
"tags": tags,
"text": component.get("description", ""),
"uid": component.get("uid"),
}

ical_annotations.append(ical_annotation)

except AttributeError as e:
print(
f'level=ERROR {e} msg="decoding event failed" event_summary="{component.get("summary")}" event_dtstart="{component.get("dtstart")}" event_dtstamp="{component.get("dtstamp")}" event_dtend="{component.get("dtend")}"'
)
return ical_annotations


26 changes: 26 additions & 0 deletions ical-proxy/fixtures/calendar.ics
Original file line number Diff line number Diff line change
@@ -36,4 +36,30 @@ STATUS:CONFIRMED
SUMMARY:Test event
TRANSP:TRANSPARENT
END:VEVENT
BEGIN:VEVENT
DTSTART;VALUE=DATE:20230925
DTEND;VALUE=DATE:20230930
RRULE:FREQ=WEEKLY;WKST=SU;INTERVAL=3;BYDAY=MO
DTSTAMP:20231003T134624Z
UID:[email protected]
CREATED:20230911T092330Z
LAST-MODIFIED:20230925T092701Z
SEQUENCE:1
STATUS:CONFIRMED
SUMMARY:[Platform] Tim
TRANSP:TRANSPARENT
END:VEVENT
BEGIN:VEVENT
DTSTART;VALUE=DATE:20230918
DTEND;VALUE=DATE:20230923
RRULE:FREQ=WEEKLY;WKST=SU;INTERVAL=3;BYDAY=MO
DTSTAMP:20231003T134624Z
UID:[email protected]
CREATED:20230911T091947Z
LAST-MODIFIED:20230925T092728Z
SEQUENCE:1
STATUS:CONFIRMED
SUMMARY:[Platform] Ben
TRANSP:TRANSPARENT
END:VEVENT
END:VCALENDAR
2 changes: 2 additions & 0 deletions ical-proxy/requirements.txt
Original file line number Diff line number Diff line change
@@ -5,3 +5,5 @@ requests-file==1.5.1
uWSGI==2.0.20
icalendar==4.1.0
cachetools==5.2.0
recurring-ical-events==2.1.0
python-dateutil==2.8.2

0 comments on commit 1d7780e

Please sign in to comment.