[calendar.ical] Fixed timezone/datetime parsing issues.

Closes: #405
This commit is contained in:
Fabio Manganiello 2024-06-06 02:22:23 +02:00
parent 4f19b45975
commit e52f5e06f4
2 changed files with 20 additions and 12 deletions

View file

@ -1,5 +1,9 @@
# Changelog # Changelog
## [Unreleased]
- [#405] Fixed timezone/timestamp rendering issues for `calendar.ical` events.
## [1.0.7] - 2024-06-02 ## [1.0.7] - 2024-06-02
- [#384] Added `assistant.openai` and `tts.openai` plugins. - [#384] Added `assistant.openai` and `tts.openai` plugins.

View file

@ -1,7 +1,9 @@
import datetime import datetime
import requests
from typing import Optional from typing import Optional
import requests
from dateutil.tz import gettz
from platypush.plugins import Plugin, action from platypush.plugins import Plugin, action
from platypush.plugins.calendar import CalendarInterface from platypush.plugins.calendar import CalendarInterface
from platypush.utils import utcnow from platypush.utils import utcnow
@ -21,19 +23,20 @@ class CalendarIcalPlugin(Plugin, CalendarInterface):
self.url = url self.url = url
@staticmethod @staticmethod
def _convert_timestamp(event, attribute: str) -> Optional[str]: def _convert_timestamp(event: dict, attribute: str) -> Optional[str]:
t = event.get(attribute) t = event.get(attribute)
if not t: if not t:
return return None
if isinstance(t.dt, datetime.date): if isinstance(t.dt, datetime.date) and not isinstance(t.dt, datetime.datetime):
return datetime.datetime( return datetime.datetime(
t.dt.year, t.dt.month, t.dt.day, tzinfo=datetime.timezone.utc t.dt.year, t.dt.month, t.dt.day, tzinfo=gettz()
).isoformat() ).isoformat()
return ( return (
datetime.datetime.utcfromtimestamp(t.dt.timestamp()) datetime.datetime.fromtimestamp(t.dt.timestamp())
.replace(tzinfo=datetime.timezone.utc) .replace(tzinfo=t.dt.tzinfo or gettz())
.astimezone(datetime.timezone.utc)
.isoformat() .isoformat()
) )
@ -82,10 +85,10 @@ class CalendarIcalPlugin(Plugin, CalendarInterface):
from icalendar import Calendar from icalendar import Calendar
events = [] events = []
response = requests.get(self.url) response = requests.get(self.url, timeout=20)
assert response.ok, "HTTP error while getting events from {}: {}".format( assert (
self.url, response.text response.ok
) ), f"HTTP error while getting events from {self.url}: {response.text}"
calendar = Calendar.from_ical(response.text) calendar = Calendar.from_ical(response.text)
for event in calendar.walk(): for event in calendar.walk():
@ -97,7 +100,8 @@ class CalendarIcalPlugin(Plugin, CalendarInterface):
if ( if (
event['status'] != 'cancelled' event['status'] != 'cancelled'
and event['end'].get('dateTime') and event['end'].get('dateTime')
and event['end']['dateTime'] >= utcnow().isoformat() and datetime.datetime.fromisoformat(event['end']['dateTime'])
>= utcnow()
and ( and (
( (
only_participating only_participating