ContactRescue.py 4.8 KB

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