vcard-bday-icalendar/export.py

42 lines
1.1 KiB
Python
Raw Normal View History

2022-11-30 13:19:03 +01:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import sys
import datetime
import vobject
card = vobject.vCard()
cal = vobject.iCalendar()
R_FMT = (
(re.compile('^[0-9]{8}$'), '%Y%m%d'),
(re.compile('^--[0-9]{4}$'), '--%m%d'),
(re.compile('^[0-9]{4}-[0-9]{2}-[0-9]{2}$'), '%Y-%m-%d')
)
def vobj_str2date(content_line):
v = content_line.value
for r, f in R_FMT:
if r.match(v):
return datetime.datetime.strptime(v, f)
raise ValueError(f'cannot parse specified string {v}')
for o in vobject.readComponents(sys.stdin):
if 'bday' not in o.contents:
continue
#print(o.fn, o.bday, type(o.bday), o.bday.value)
name = o.fn.value
date = vobj_str2date(o.bday)
if date.year <= 1900:
date = date.replace(year=datetime.datetime.now().year)
date_end = date + datetime.timedelta(days=1)
cal.add('vevent')
cal.vevent_list[-1].add('summary').value = o.fn.value
cal.vevent_list[-1].add('dtstart').value = date.date()
cal.vevent_list[-1].add('dtend').value = date_end.date()
cal.vevent_list[-1].add('rrule').value = 'FREQ=YEARLY'
print(cal.serialize())