51 lines
1.0 KiB
Python
51 lines
1.0 KiB
Python
|
#!/usr/bin/env python3
|
||
|
|
||
|
import os
|
||
|
import sys
|
||
|
import re
|
||
|
import sqlite3
|
||
|
import vobject
|
||
|
|
||
|
db_path = '~/contacts.db'
|
||
|
def_nat_pref = "33"
|
||
|
|
||
|
db_path = os.path.expanduser(db_path)
|
||
|
directory = os.path.dirname(db_path)
|
||
|
|
||
|
if not os.path.exists(directory):
|
||
|
os.makedirs(directory)
|
||
|
|
||
|
conn = sqlite3.connect(db_path)
|
||
|
c = conn.cursor()
|
||
|
|
||
|
c.execute("CREATE TABLE IF NOT EXISTS contacts (uid text primary key not null, name text not null, nick text, number text)")
|
||
|
|
||
|
vcf_file = open(sys.argv[1])
|
||
|
vcard = vobject.readOne(vcf_file)
|
||
|
vcf_file.close()
|
||
|
|
||
|
uid = vcard.uid.value
|
||
|
name = vcard.fn.value
|
||
|
nick = ''
|
||
|
try:
|
||
|
nick = vcard.nickname.value
|
||
|
except AttributeError:
|
||
|
pass
|
||
|
num = []
|
||
|
try:
|
||
|
for i in vcard.tel_list:
|
||
|
if i.type_param.lower() == "cell":
|
||
|
n = re.sub('[^0-9]','',i.value)
|
||
|
n = re.sub('^00','',n)
|
||
|
n = re.sub('^0',def_nat_pref,n)
|
||
|
num += [n]
|
||
|
except AttributeError:
|
||
|
pass
|
||
|
|
||
|
row = (uid,name,nick,','.join(num))
|
||
|
c.execute("INSERT OR REPLACE INTO contacts (uid, name, nick, number) VALUES (?,?,?,?)", row)
|
||
|
|
||
|
|
||
|
conn.commit()
|
||
|
conn.close()
|