Init commit.

This commit is contained in:
redxef 2022-11-30 13:19:03 +01:00
commit 33d158d766
Signed by: redxef
GPG key ID: 7DAC3AA211CBD921
3 changed files with 50 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*.vcf
*.ics

7
README.md Normal file
View file

@ -0,0 +1,7 @@
# vCard to iCalendar Birthday export
Export birthdays from contacts to a iCalendar.
## Usage
`./export.py < contacts.vcf > birthdays.ics`

41
export.py Executable file
View file

@ -0,0 +1,41 @@
#!/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())