Init commit.
This commit is contained in:
commit
33d158d766
3 changed files with 50 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
*.vcf
|
||||
*.ics
|
7
README.md
Normal file
7
README.md
Normal 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
41
export.py
Executable 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())
|
||||
|
Loading…
Reference in a new issue