ical-birthday-generator/parse.py

64 lines
No EOL
1.4 KiB
Python

import pytz
from datetime import datetime, timedelta, date
# copied from a spreadsheet (so it's tab separated)
bdays = """
lastname firstname 07-May
lastname firstname 07-May
lastname firstname 07-May
lastname firstname 07-May
lastname firstname 07-May
lastname firstname 07-May
""".strip().split('\n')
months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
]
months = {m : i for i, m in enumerate(months)}
bdays = [[cell.strip() for cell in line.split("\t")] for line in bdays]
tzname = "Europe/Warsaw"
tz = pytz.timezone(tzname)
cal = """
BEGIN:VCALENDAR
VERSION:2.0
PRODID:Birthdays autogenerated
""".strip()
for sname, fname, bday_date in bdays:
event = "BEGIN:VEVENT"
day, month = bday_date.split('-')
print(f'{sname:<16}|{fname:<16}|{day:>02}.{months[month]+1:>02}')
event += "\nUID:" + str((fname+sname+bday_date).__hash__())
event += "\nDTSTAMP;TZID=" + tzname + ":" + datetime.now(tz=tz).strftime("%Y%m%dT%H%M%S")
event += "\nDTSTART;VALUE=DATE:" + f'{datetime.now().year}{months[month]+1:>02}{day:>02}'
# event += "\n"
event += "\nSUMMARY:" + f'🎂 {fname} {sname} birthday!'
event += "\nRRULE:FREQ=YEARLY"
event += "\nEND:VEVENT"
print(event)
cal += "\n" + event
cal += "\nEND:VCALENDAR"
with open("birthday_calendar.ical", 'w', encoding='utf-8') as f:
f.write(cal)