ContactRescue.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. SQLADRCur = SQLconn.cursor()
  27. for ADRrow in SQLADRCur.execute('SELECT * FROM Addresses JOIN Details on Details.detailId = Addresses.detailId where Addresses.contactId = ' + str(contactID)):
  28. adr = vcf.add('ADR').value = vobject.vcard.Address(street=ADRrow[2], city=ADRrow[5], region=ADRrow[4], code=ADRrow[6],country=ADRrow[7])
  29. ## Abfragen Organisation
  30. SQLORGCur = SQLconn.cursor()
  31. for ORGrow in SQLORGCur.execute('SELECT * from Organizations where contactId = ' + str(contactID)):
  32. org = vcf.add('ORG').value = [str(ORGrow[2]), str(ORGrow[6])]
  33. if ORGrow[4] is not None:
  34. title = vcf.add('TITLE').value = str(ORGrow[4])
  35. if ORGrow[3] is not None:
  36. role = vcf.add('ROLE').value = str(ORGrow[3])
  37. # Also parameters are possible. Could be read out
  38. # | columnID | column |
  39. # ----------------------------
  40. # | 0 | detailId |
  41. # | 1 | contactId |
  42. # | 2 | name |
  43. # | 3 | role |
  44. # | 4 | title |
  45. # | 5 | location |
  46. # | 6 | department |
  47. # | 7 | logoUrl |
  48. # | 8 | assistantName |
  49. ## Abfragen E-Mail-Adressen
  50. SQLEmailCur = SQLconn.cursor()
  51. for Emailrow in SQLEmailCur.execute('SELECT * from EmailAddresses JOIN Details on Details.detailId= EmailAddresses.detailId where EmailAddresses.contactId = ' + str(contactID)):
  52. # debug ausgabe
  53. print("...debug: " + str(Emailrow[2]) + " at " + str(Emailrow[9]))
  54. email = vcf.add('email')
  55. email.value = str(Emailrow[2])
  56. # nur den Typ einpflegen, wenn das hier nicht none ist
  57. if Emailrow[9] != None:
  58. email.type_param = str(Emailrow[9])
  59. SQLPhoneCur = SQLconn.cursor()
  60. ## Abfragen Telefonnummer, Fax, SMS - Nummern kommen aus der gleichen Tabelle
  61. for Phonerow in SQLPhoneCur.execute('SELECT * from PhoneNumbers JOIN Details on Details.detailId = PhoneNumbers.detailId where PhoneNumbers.contactId = ' + str(contactID)):
  62. # wir müssen die SubTypen unterscheiden
  63. #Null voice
  64. #1 cell
  65. #2 fax
  66. #3 pager
  67. #6 video
  68. #10 Assistent
  69. # debug ausgabe
  70. print("...debug: " + str(Phonerow[2]) + " at " + str(Phonerow[10]) + " subtype=" + str(Phonerow[3]))
  71. # None is a normal phone Number
  72. if Phonerow[3] == "1":
  73. phcat='cell'
  74. elif Phonerow[3] == "2":
  75. phcat='fax'
  76. elif Phonerow[3] == "3":
  77. phcat='pager'
  78. elif Phonerow[3] == "6":
  79. phcat='video'
  80. elif Phonerow[3] == "10":
  81. phcat='assistent'
  82. elif Phonerow[3] is None:
  83. phcat='voice'
  84. print("...debug: " + phcat)
  85. phone = vcf.add(phcat).value = str(Phonerow[2])
  86. # nur den Typ einpflegen, wenn das hier nicht none ist
  87. if Phonerow[10] != None:
  88. try:
  89. phone.type_param = str(Phonerow[10])
  90. except AttributeError:
  91. continue
  92. # Ausgabe
  93. print(vcf.serialize())
  94. # hier brauchen wir einige eception handles -> wie bekommen wir die einzelnen exceptions heruas ?
  95. #except:
  96. #print("Error in executing SQL")
  97. except AttributeError:
  98. print("Datatype mismatch")
  99. raise
  100. # das generöse Except am Ende
  101. except:
  102. print("unhandled error")
  103. raise