vcfExport_PhoneNumbers.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Doc
  2. # Script to extract Contacts from Sailfish Contact SQLite DB located at
  3. #
  4. #
  5. # Links
  6. # FileFormatdescription: https://docs.fileformat.com/email/vcf/#vcf-30-example
  7. # Pytho vobject: http://eventable.github.io/vobject/
  8. # ChangeLog
  9. # 2021-08-03 - multiple E-Mails with different types are working correctly
  10. import sqlite3
  11. import vobject
  12. import uuid
  13. SQLconn = sqlite3.connect('../Testdata/contacts_with-Phone-Mobile-Fax-Pager-Assistent.db')
  14. try:
  15. SQLContCur = SQLconn.cursor()
  16. for row in SQLContCur.execute('SELECT * FROM Contacts'):
  17. # contactID abfragen
  18. contactID=row[0]
  19. # wir erstellen das Objekt
  20. vcf = vobject.vCard()
  21. vcf.add('uid').value = str(uuid.uuid4())
  22. #vcf.add('uid').value = "Testdaten"
  23. vcf.add('n').value = vobject.vcard.Name( family=row[6], given=row[4] )
  24. vcf.add('fn').value =row[1]
  25. print("debug: Contact " + row[1])
  26. SQLEmailCur = SQLconn.cursor()
  27. ## Abfragen E-Mail-Adressen
  28. for Emailrow in SQLEmailCur.execute('SELECT * from EmailAddresses JOIN Details on Details.detailId= EmailAddresses.detailId where EmailAddresses.contactId = ' + str(contactID)):
  29. # debug ausgabe
  30. print("...debug: " + str(Emailrow[2]) + " at " + str(Emailrow[9]))
  31. email = vcf.add('email')
  32. email.value = str(Emailrow[2])
  33. # nur den Typ einpflegen, wenn das hier nicht none ist
  34. if Emailrow[9] != None:
  35. email.type_param = str(Emailrow[9])
  36. SQLPhoneCur = SQLconn.cursor()
  37. ## Abfragen Telefonnummer, Fax, SMS - Nummern kommen aus der gleichen Tabelle
  38. for Phonerow in SQLPhoneCur.execute('SELECT * from PhoneNumbers JOIN Details on Details.detailId = PhoneNumbers.detailId where PhoneNumbers.contactId = ' + str(contactID)):
  39. # wir müssen die SubTypen unterscheiden
  40. #Null voice
  41. #1 cell
  42. #2 fax
  43. #3 pager
  44. #6 video
  45. #10 Assistent
  46. # debug ausgabe
  47. print("...debug: " + str(Phonerow[2]) + " at " + str(Phonerow[10]) + " subtype=" + str(Phonerow[3]))
  48. # None is a normal phone Number
  49. if Phonerow[3] == "1":
  50. phcat='cell'
  51. elif Phonerow[3] == "2":
  52. phcat='fax'
  53. elif Phonerow[3] == "3":
  54. phcat='pager'
  55. elif Phonerow[3] == "6":
  56. phcat='video'
  57. elif Phonerow[3] == "10":
  58. phcat='assistent'
  59. elif Phonerow[3] is None:
  60. phcat='voice'
  61. print("...debug: " + phcat)
  62. phone = vcf.add(phcat).value = str(Phonerow[2])
  63. # nur den Typ einpflegen, wenn das hier nicht none ist
  64. if Phonerow[10] != None:
  65. try:
  66. phone.type_param = str(Phonerow[10])
  67. except AttributeError:
  68. continue
  69. # Ausgabe
  70. print(vcf.serialize())
  71. # hier brauchen wir einige eception handles -> wie bekommen wir die einzelnen exceptions heruas ?
  72. #except:
  73. #print("Error in executing SQL")
  74. except AttributeError:
  75. print("Datatype mismatch")
  76. raise
  77. # das generöse Except am Ende
  78. except:
  79. print("unhandled error")
  80. raise