diff --git a/.gitignore b/.gitignore index 7bbc71c..6d2408f 100644 --- a/.gitignore +++ b/.gitignore @@ -99,3 +99,6 @@ ENV/ # mypy .mypy_cache/ +contacts2.db +*.vcf +.idea diff --git a/export.py b/export.py new file mode 100644 index 0000000..637c556 --- /dev/null +++ b/export.py @@ -0,0 +1,38 @@ +import sqlite3 + +contactDBName = 'contacts2.db' +outputFileName = 'contacts.vcf' + +outputFormat = """ +BEGIN:VCARD +VERSION:3.0 +N:;{name};;; +FN:{name} +{content}END:VCARD""" + +if __name__ == '__main__': + sqlCon = sqlite3.connect(contactDBName) + sqlCur = sqlCon.cursor() + contactList = list(sqlCur.execute('SELECT _id,name_raw_contact_id FROM contacts')) + with open(outputFileName, 'w+', encoding='UTF-8') as fp: + for row in contactList: + data = { + 'name': '', + 'content': '', + } + for dataRec in sqlCur.execute( + 'select _id,mimetype_id,data1,raw_contact_id from data where raw_contact_id=%s' % (row[0],) + ): + if (dataRec[1] == 7): + data['name'] = dataRec[2] + if (dataRec[1] == 5): + data['content'] += 'TEL;TYPE=cell:%s\n' % ( + str(dataRec[2]).replace(' ', '').replace('-', ''), + ) + if (dataRec[1] == 1): + data['content'] += 'EMAIL;TYPE=WORK:%s\n' % (dataRec[2],) + resText = outputFormat.format(**data) + # if not data['name']: + # print(data) + if data['content'] and data['name']: + fp.write(resText)