Skip to content

Commit 7a48acc

Browse files
handle dates
1 parent bdcf78f commit 7a48acc

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

ical-proxy/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.9-slim
1+
FROM python:3.10-slim
22

33
RUN mkdir /app
44
COPY ./requirements.txt /app/

ical-proxy/app.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
import datetime
12
import os
2-
import requests
3-
import time
4-
import icalendar
53

6-
from datetime import datetime
7-
from flask import Flask, request, abort
4+
import icalendar
5+
import requests
6+
from cachetools import TTLCache, cached
7+
from flask import Flask, abort, request
88
from flask_cors import CORS
99
from requests_file import FileAdapter
10-
from cachetools import TTLCache, cached
1110

1211
CACHE_TTL = os.environ.get("CACHE_TTL", 1800)
1312

@@ -43,7 +42,7 @@ def annotations():
4342

4443
ical_url = request.headers.get(
4544
"X-ICAL-URL",
46-
f"file://{os.path.dirname(os.path.realpath(__file__))}/fixtures/something.ics",
45+
f"file://{os.path.dirname(os.path.realpath(__file__))}/fixtures/calendar.ics",
4746
)
4847

4948
if "X-TAGS" in request.headers:
@@ -75,6 +74,12 @@ def _ical_annotations(url: str, tags: str):
7574
return ical_as_annotations
7675

7776

77+
def _millis_timestamp(dt: datetime.datetime):
78+
if isinstance(dt, datetime.date):
79+
dt = datetime.datetime(dt.year, dt.month, dt.day, tzinfo=datetime.timezone.utc)
80+
return int(dt.timestamp() * 1000)
81+
82+
7883
def _fetch_ical_data(url: str):
7984
s = requests.Session()
8085
s.mount("file://", FileAdapter())
@@ -97,31 +102,29 @@ def _convert_ical_to_annotations(ical_data, tags):
97102
for component in ical_calendar.walk():
98103
if component.name == "VEVENT":
99104
try:
105+
start_time = component.get("dtstart").dt
106+
107+
end_time = component.get("dtstart").dt
108+
if "dtend" in component:
109+
end_time = component.get("dtend").dt
110+
100111
ical_annotation = {
101-
"time": int(
102-
time.mktime(component.get("dtstart").dt.timetuple()) * 1000
103-
),
112+
"time": _millis_timestamp(start_time),
113+
"timeEnd": _millis_timestamp(end_time),
104114
"title": component.get("summary"),
105115
"tags": tags,
106116
"text": component.get("description", ""),
107117
"uid": component.get("uid"),
108118
}
109119

110-
try:
111-
ical_annotation["timeEnd"] = int(
112-
time.mktime(component.get("dtend").dt.timetuple()) * 1000
113-
)
114-
except AttributeError:
115-
pass
116-
117120
ical_annotations.append(ical_annotation)
118121

119-
except AttributeError:
122+
except AttributeError as e:
120123
print(
121-
f'level=ERROR msg="decoding event failed" event_summary="{component.get("summary")}" event_dtstart="{component.get("dtstart")}" event_dtstamp="{component.get("dtstamp")}" event_dtend="{component.get("dtend")}"'
124+
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")}"'
122125
)
123126
return ical_annotations
124127

125128

126129
if __name__ == "__main__":
127-
app.run(host="0.0.0.0", port=5000)
130+
app.run(host="0.0.0.0", port=5000, debug=True)

0 commit comments

Comments
 (0)