42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
|
#!/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())
|
||
|
|