forked from FoxP/VCF-to-ICS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcf_to_ics.py
152 lines (129 loc) · 5.41 KB
/
vcf_to_ics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# Name :
# VCF to ICS
# Author :
# ▄▄▄▄▄▄▄ ▄ ▄▄ ▄▄▄▄▄▄▄
# █ ▄▄▄ █ ██ ▀▄ █ ▄▄▄ █
# █ ███ █ ▄▀ ▀▄ █ ███ █
# █▄▄▄▄▄█ █ ▄▀█ █▄▄▄▄▄█
# ▄▄ ▄ ▄▄▀██▀▀ ▄▄▄ ▄▄
# ▀█▄█▄▄▄█▀▀ ▄▄▀█ █▄▀█
# █ █▀▄▄▄▀██▀▄ █▄▄█ ▀█
# ▄▄▄▄▄▄▄ █▄█▀ ▄ ██ ▄█
# █ ▄▄▄ █ █▀█▀ ▄▀▀ ▄▀
# █ ███ █ ▀▄ ▄▀▀▄▄▀█▀█
# █▄▄▄▄▄█ ███▀▄▀ ▀██ ▄
# DEPENDENCIES
from string import ascii_letters
from string import digits
import argparse
import logging
import quopri
import random
import time
import sys
import os
import re
import datetime
# CONFIGURATION
PROGRAM_NAME = "VCF to ICS"
PROGRAM_VERSION = "1.0"
# Command-line interface
argParser = argparse.ArgumentParser(description=PROGRAM_NAME + " " + PROGRAM_VERSION)
argParser.add_argument('-i', '--input', metavar='PATH', help='Input .vcf file path', required=True)
argParser.add_argument('-o', '--output', metavar='PATH', help='Output .ics file path', required=True)
argParser.add_argument('-n', '--name', metavar='NAME', help='Desired calendar name', required=True)
args = vars(argParser.parse_args())
sInputPath = args['input']
sOutputPath = args['output']
sCalendarName = args['name']
# LOGGING
# Default logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# Log to console
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.DEBUG)
logger.addHandler(stream_handler)
# MAIN PROGRAM
if os.path.isfile(sInputPath):
logger.info("Input file path : " + sInputPath)
else:
logger.error("Invalid input file path : " + sInputPath)
sys.exit(1)
# Read VCF file content
fileInput = open(sInputPath, 'r', encoding='utf-8')
sFileContent = fileInput.read()
fileInput.close()
# Separate VCards
sVCards = sFileContent.split("END:VCARD")
iVCardNbr = 0
# Write ICS calendar header
try:
fileOutput = open(sOutputPath, 'w', encoding='utf-8')
except:
logger.error("Invalid output file path : " + sOutputPath)
sys.exit(1)
else:
logger.info("Output file path : " + sOutputPath)
fileOutput.write("BEGIN:VCALENDAR\nPRODID:-//" + PROGRAM_NAME + "//NONSGML " + sCalendarName + " V1.0//EN\nX-WR-CALNAME:" + sCalendarName + "\nVERSION:2.0\n")
# Parse VCards
for sVCard in sVCards:
# "BDAY:--12-01" --> ["BDAY:--12-01", "-", "12", "01"]
# "BDAY:2018-12-01" --> ["BDAY:2018-12-01", "2018", "12", "01"]
matchBirthday = re.search("BDAY;VALUE=date:(\-|\d+)-(\d+)-(\d+)[\s\S]*?", sVCard)
# "FN:John Doe" --> ["FN:John Doe", "John Doe"]
# "FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=4A=6F=68=6E=20=44=6F=65" --> ["FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=4A=6F=68=6E=20=44=6F=65", "=4A=6F=68=6E=20=44=6F=65"]
matchName = re.search("FN(?:\:|;.*:)(.*)[\s\S]*?", sVCard)
if (matchBirthday is not None) and (matchName is not None):
# Contact birthday
if matchBirthday.group(1) == "-":
# Replace "-" by current year
# ["BDAY:--12-01", "-", "12", "01"] --> 20181201
sBirthday = time.strftime("%Y") + matchBirthday.group(2) + matchBirthday.group(3)
else:
# ["BDAY:2018-12-01", "2018", "12", "01"] --> 20181201
sBirthday = matchBirthday.group(1) + matchBirthday.group(2) + matchBirthday.group(3)
# Contact name
try:
# Try to decode Quoted-Printable
# ["FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=4A=6F=68=6E=20=44=6F=65", "=4A=6F=68=6E=20=44=6F=65"] --> "John Doe"
sName = quopri.decodestring(matchName.group(1)).decode('utf-8')
except:
# ["FN:John Doe", "John Doe"] --> "John Doe"
sName = matchName.group(1)
logger.info(str(sName) + " : " + str(sBirthday))
iVCardNbr = iVCardNbr + 1
# Unique ID
sUID = sBirthday + "-" + ''.join([random.choice(list(ascii_letters + digits)) for _ in range(16)]) + "@VCFtoICS.com"
# Next day after birthday to make the event correctly show as "takes a whole day" https://stackoverflow.com/questions/6871016/adding-days-to-a-date-in-python
date_1 = datetime.datetime.strptime(sBirthday, "%Y%m%d")
next_day = date_1 + datetime.timedelta(days=1)
sNextDayAfterBirthday = next_day.strftime("%Y%m%d")
# Write ICS event
fileOutput.write("BEGIN:VEVENT\nDTSTART;VALUE=DATE:" + sBirthday + "\nDTEND;VALUE=DATE:" + sNextDayAfterBirthday + "\nSUMMARY:" + sName + "\nRRULE:FREQ=YEARLY\nUID:" + sUID + "\nEND:VEVENT\n")
if (iVCardNbr > 0):
logger.info(str(iVCardNbr) + " VCards found")
else:
logger.info("No VCard found")
# Write ICS calendar footer
fileOutput.write("END:VCALENDAR")
fileOutput.close