Last active
June 12, 2022 15:40
-
-
Save archey347/463e221cd8c008224f862182c095f8e6 to your computer and use it in GitHub Desktop.
Converts Google Contact to a VCard format supported by BT Voice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# BTVoiceContactConverter | |
# | |
# Converts from Google Contacts to BT Voice contact format | |
import vobject | |
import sys, getopt | |
from os.path import exists | |
import_file = "" | |
export_file = "" | |
try: | |
opts, args = getopt.getopt(sys.argv[1:],"hi:o:",["ifile=","ofile="]) | |
except getopt.GetoptError: | |
print('.\BTVoiceContactConverter.py -i <inputfile> -o <outputfile>') | |
sys.exit(2) | |
for opt, arg in opts: | |
if opt == '-h': | |
print('.\BTVoiceContactConverter.py -i <inputfile> -o <outputfile>') | |
sys.exit() | |
elif opt in ("-i", "--ifile"): | |
import_file = arg | |
elif opt in ("-o", "--ofile"): | |
export_file = arg | |
if not exists(import_file): | |
print("Invalid import filename") | |
print('.\BTVoiceContactConverter.py -i <inputfile> -o <outputfile>') | |
sys.exit(2) | |
if export_file.strip() == "": | |
print("Invalid output filename") | |
print('convert.py -i <inputfile> -o <outputfile>') | |
sys.exit(2) | |
# Read import file contents | |
file = open(import_file, "r") | |
data = file.read() | |
file.close() | |
data = vobject.readComponents(data) | |
out = open(export_file, "w") | |
while True: | |
contact = next(data, None) | |
if contact is None: | |
break | |
buffer = "" | |
buffer += ("BEGIN:VCARD\n") | |
buffer += ("VERSION:4.0\n") | |
# Output contact here | |
if 'fn' in contact.contents: | |
buffer += ("FN:" + contact.contents['fn'][0].value.replace("-", " ") + "\n") | |
#if contact.contents['fn'][0].value == "Dave Barrell": | |
# name = contact.contents['n'][0].value[0] | |
elif 'org' in contact.contents: | |
# Set name of contact as organisation name | |
buffer += ("FN:" + contact.contents['org'][0].value[0].replace("-", " ") + "\n") | |
if 'n' in contact.contents: | |
family = contact.contents['n'][0].value.family.replace("-", " ") | |
given = contact.contents['n'][0].value.given.replace("-", " ") | |
# Input on phone restricts names to 16 characters | |
if len(family) > 16: | |
print("Warning: '" + family + "' is too long. It has been truncated.") | |
family = family[0:16] | |
if len(given) > 16: | |
print("Warning: '" + given + "' is too long. It has been truncated.") | |
given = given[0:16] | |
buffer += ("N:" + family + ";" + given + ";;;\n") | |
elif 'org' in contact.contents: | |
buffer += ("N:" + contact.contents['org'][0].value[0].replace("-", " ") + ";;;;\n") | |
if 'tel' in contact.contents: | |
# It doesn't like contacts with no phone number | |
if len(contact.contents['tel']) == 0: | |
print("Warning: '" + contact.contents['fn'][0].value + "' has no phone number. It will be ignored.") | |
continue | |
for tel in contact.contents['tel']: | |
buffer += ("TEL;VALUE=uri;") | |
t = "" | |
try: | |
t = tel.params['TYPE'][0] | |
except: | |
t = "WORK" | |
t = t.lower() | |
buffer += ("TYPE=" + t + ":") | |
buffer += ("tel:" + tel.value.replace(" ", "")) # Caller ID doesn't work if there's spaces in the phone number | |
buffer += ("\n") | |
else: | |
print("Warning: '" + contact.contents['fn'][0].value + "' has no phone number. It will be ignored.") | |
continue | |
buffer += ("MELODY:0\n") | |
buffer += ("ACCOUNTID:0\n") | |
buffer += ("DFTNUM:0\n") | |
buffer += ("OWN:0\n") | |
buffer += ("END:VCARD\n") | |
out.write(buffer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment