Created
May 3, 2019 21:03
-
-
Save KorbinianP/e6c2c2166ee05e2b56659a37306e91d2 to your computer and use it in GitHub Desktop.
A simple example on how to create an ics calendar file in python
This file contains hidden or 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
#!//usr/bin/python3 | |
from icalendar import Calendar, Event | |
from datetime import datetime, timedelta | |
from uuid import uuid4 | |
""" | |
Please install dependencies with pip: | |
- icalendar 4.0.3 | |
- uuid 1.30 | |
Purpose of this script is to create a ics file based on the dates published | |
on www.entsorgungsdaten.de. The date will start at 16:00 on the day before | |
and end on 16:00 at the day itself. The description is hardcoded to trigger | |
an event in OpenHAB. An OpenHAB rule will evaluate this trigger and handle | |
it accordingly. The ics will be imported to Nextcloud Calendar, OpenHAB | |
reads from there with the CalDAV Command Binding. | |
Meaning of this is the need to activate a message and led on the day | |
before the trash can has to be put by the side of the street. On the morning | |
of the day itself it might be too late. | |
Execution of tests: 'python -m doctest -v ics-creator.py' | |
""" | |
# Copy and Paste from https://www.entsorgungsdaten.de/module.php5?mod=entsorgungsdaten&fid=15&gem=18 | |
muelltermine = """Di. 08.01.Di. 22.01.Di. 05.02.Di. 19.02.Di. 05.03.Di. 19.03.Di. 02.04.Mo. 15.04.Di. 30.04.Di. 14.05.Di. 28.05.Mi. 12.06.Di. 25.06.Di. 09.07.Di. 23.07.Di. 06.08.Di. 20.08.Di. 03.09.Di. 17.09.Di. 01.10.Di. 15.10.Di. 29.10.Di. 12.11.Di. 26.11.Di. 10.12.Mo. 23.12.""" | |
papiertermine = """Mo. 07.01.Do. 07.02.Do. 07.03.Do. 04.04.Mi. 08.05.Fr. 07.06.Fr. 05.07.Do. 08.08.Do. 05.09.Fr. 11.10.Fr. 08.11.Fr. 06.12.""" | |
def splitAndCutDates(s, l, c): | |
"""Takes the copied date string from entsorgungsdaten.de and return an | |
array of dates. s is the input string, l is the length of the repeating | |
date string, c is the length of the string to cut from the beginning | |
Example: | |
>>> splitAndCutDates("Di. 08.01.Di. 22.01.Di. 05.02.", 10, 4) | |
['08.01.', '22.01.', '05.02.'] | |
""" | |
return [s[i + c: i + l] for i in range(0, len(s), l)] | |
def getDay(s): | |
"""extract and return the day of a string like '21.04.' as integer | |
Examples: | |
>>> getDay("21.04.") | |
21 | |
>>> getDay("01.12.") | |
1 | |
""" | |
return int(s[0: 2]) | |
def getMonth(s): | |
"""extract and return the month of a string like '21.04.' as integer | |
Examples: | |
>>> getMonth("21.04.") | |
4 | |
>>> getMonth("01.12.") | |
12 | |
""" | |
return int(s[3: 5]) | |
def createIcalEvent(summary, dayAndMonth, year, description): | |
"""Create and return an ical event""" | |
event = Event() | |
event.add('summary', summary) | |
event.add('dtstart', datetime(year, | |
getMonth(dayAndMonth), | |
getDay(dayAndMonth), | |
16, 0, 0, 0)-timedelta(days=1)) | |
event.add('dtend', datetime(year, | |
getMonth(dayAndMonth), | |
getDay(dayAndMonth), | |
16, 0, 0, 0)) | |
event.add('dtstamp', datetime.now()) | |
event.add('description', description) | |
event.add('uid', uuid4()) | |
return event | |
if __name__ == '__main__': | |
# Create array of dates | |
muelltermine = splitAndCutDates(muelltermine, | |
len("Di. 08.01."), | |
len("Di. ")) | |
papiertermine = splitAndCutDates(papiertermine, | |
len("Di. 08.01."), | |
len("Di. ")) | |
# Create calendar | |
cal = Calendar() | |
cal.add('prodid', 'https://icalendar.readthedocs.io') | |
cal.add('version', '2.0') | |
# Create one entry per date | |
for x in muelltermine: | |
cal.add_component( | |
createIcalEvent("Muelltonne", | |
x, | |
2019, | |
"BEGIN:Info_Muelltonne:ON\n" | |
"END:Info_Muelltonne:OFF")) | |
for x in papiertermine: | |
cal.add_component( | |
createIcalEvent("Papiertonne", | |
x, | |
2019, | |
"BEGIN:Info_Papiertonne:ON\n" | |
"END:Info_Papiertonne:OFF")) | |
# Write file | |
f = open('entsorgungsdaten.ics', 'wb') | |
f.write(cal.to_ical()) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment