-
-
Notifications
You must be signed in to change notification settings - Fork 709
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from mampfes/icalevents
Icalevents
- Loading branch information
Showing
4 changed files
with
101 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
custom_components/waste_collection_schedule/waste_collection_schedule/service/ICS_v1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import datetime | ||
import logging | ||
import re | ||
|
||
import icalendar | ||
import recurring_ical_events | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class ICS_v1: | ||
def __init__(self, offset=None, regex=None, split_at=None): | ||
self._offset = offset | ||
self._regex = None | ||
if regex is not None: | ||
self._regex = re.compile(regex) | ||
self._split_at = split_at | ||
|
||
def convert(self, ics_data): | ||
# parse ics file | ||
try: | ||
calendar = icalendar.Calendar.from_ical(ics_data) | ||
except Exception as err: | ||
_LOGGER.error(f"Parsing ics data failed:{str(err)}") | ||
_LOGGER.debug(ics_data) | ||
return [] | ||
|
||
# calculate start- and end-date for recurring events | ||
start_date = datetime.datetime.now().replace( | ||
hour=0, minute=0, second=0, microsecond=0 | ||
) | ||
if self._offset is not None: | ||
start_date -= datetime.timedelta(days=self._offset) | ||
end_date = start_date.replace(year=start_date.year + 1) | ||
|
||
events = recurring_ical_events.of(calendar).between(start_date, end_date) | ||
|
||
entries = [] | ||
for e in events: | ||
if e.name == "VEVENT": | ||
# calculate date | ||
dtstart = None | ||
if type(e.get("dtstart").dt) == datetime.date: | ||
dtstart = e.get("dtstart").dt | ||
elif type(e.get("dtstart").dt) == datetime.datetime: | ||
dtstart = e.get("dtstart").dt.date() | ||
if self._offset is not None: | ||
dtstart += datetime.timedelta(days=self._offset) | ||
|
||
# calculate waste type | ||
summary = str(e.get("summary")) | ||
if self._regex is not None: | ||
match = self._regex.match(summary) | ||
if match: | ||
summary = match.group(1) | ||
|
||
if self._split_at is not None: | ||
summary = re.split(self._split_at, summary) | ||
for t in summary: | ||
entries.append((dtstart, t.strip().title())) | ||
else: | ||
entries.append((dtstart, summary)) | ||
|
||
return entries |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters