Skip to content

Instantly share code, notes, and snippets.

@tsenger
Created December 20, 2018 08:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tsenger/ca4480235b5b29431965ee0b919ff6ad to your computer and use it in GitHub Desktop.
Save tsenger/ca4480235b5b29431965ee0b919ff6ad to your computer and use it in GitHub Desktop.
Converter for CardDav phonebook to Grandstream DP720 / DP750 XML phonebook
#Convert CardDav contacts to GrandStream DP720 / DP750 XML phonebook
import vobject
vcf_path = "./contacts.vcf"
xml_path = './xml_contacts.xml'
# phone number: CardDav to Grandstream
map_number_types = {
"work": "Work",
"home": "Home",
"main": "Home",
"cell": "Mobile",
"other": "Home",
"voice": "Home",
"mobile": "Mobile"
}
input_counter = 0
output_counter = 0
with open(vcf_path, 'r') as f:
vcards = vobject.readComponents(f.read())
out_file = open(xml_path,'w')
out_file.write('<?xml version="1.0" encoding="UTF-8"?>')
out_file.write('<AddressBook><version>1</version>')
for vcard in vcards:
input_counter += 1
numbers = []
for p in vcard.getChildren():
if p.name == "N":
givenname = p.value.given
familyname = p.value.family
if p.name == "TEL":
itype = p.type_param.lower()
if not itype in map_number_types:
if not itype in ["pref"]:
print("Warning: Unknown type: '%s'" % (itype))
continue
ntype = map_number_types[itype]
t = ntype, p.value
numbers.append(t)
if len(numbers)>0:
output_counter += 1
out_file.write('<Contact>')
out_file.write('<FirstName>'+givenname+'</FirstName>')
out_file.write('<LastName>'+familyname+'</LastName>')
out_file.write('<Ringtone>0</Ringtone>')
for n in numbers:
ntype, value = n
out_file.write('<Phone type="'+ntype+'">')
out_file.write('<phonenumber>'+value+'</phonenumber>')
out_file.write('</Phone>')
out_file.write('</Contact>')
out_file.write('</AddressBook>')
out_file.close
print ("DONE!")
print ("Processed %s input contacts. %s contacts contained phone numbers and where exported into XML file: %s" % (input_counter, output_counter, out_file.name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment