ContactRescue.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/bin/python3
  2. # coding=utf-8
  3. # Doc
  4. # Script to extract Contacts from Sailfish Contact SQLite DB located at
  5. #
  6. #
  7. # Links
  8. # FileFormatdescription: https://docs.fileformat.com/email/vcf/#vcf-30-example
  9. # Pytho vobject: http://eventable.github.io/vobject/
  10. # Version
  11. version=0.2
  12. # ChangeLog
  13. # 2021-08-03 - 0.1 - multiple E-Mails with different types are working correctly
  14. # 2021-08-09 - 0.2 - Phonenumbers with parameters, Addresses with parameters, E-Mail-Addresses with marameters
  15. import sqlite3
  16. import vobject
  17. import uuid
  18. import argparse
  19. def DEBUG(debug,msg):
  20. if debug is not None:
  21. print("..DEBUG: " + msg)
  22. parser = argparse.ArgumentParser(description='Restore SailfishOS 3 Contacts', epilog='This script was written to restore SailfishOS 3 contacts as VCF files. To see additional information, visit: https://wiki.siningsoft.de/doku.php?id=sailfishos:projects:sailfish_contacts_rescue' )
  23. parser.add_argument('--db','-d', required=True, help="Sqlite3 Database file usually /home/{nemo,defaultuser)/.local/share/system/Contacts/qtcontacts-sqlite/contacts.db")
  24. parser.add_argument('--output','-o',required=True, help="Output directory for vcf files")
  25. parser.add_argument('--debug',action="store_true",help="debugging output to identify problems")
  26. parser.add_argument('--version', action='version', version='%(prog)s ' + str(version))
  27. args = parser.parse_args()
  28. SQLconn = sqlite3.connect(args.db)
  29. try:
  30. SQLContCur = SQLconn.cursor()
  31. for row in SQLContCur.execute('SELECT * FROM Contacts'):
  32. # contactID abfragen
  33. contactID=row[0]
  34. # wir erstellen das Objekt
  35. vcf = vobject.vCard()
  36. vcf.add('uid').value = str(uuid.uuid4())
  37. #vcf.add('uid').value = "Testdaten"
  38. vcf.add('n').value = vobject.vcard.Name( family=row[6], given=row[4] )
  39. vcf.add('fn').value =row[1]
  40. DEBUG(args.debug,"Contact " + row[1])
  41. # abfrage der Adressdaten
  42. SQLADRCur = SQLconn.cursor()
  43. for ADRrow in SQLADRCur.execute('SELECT * FROM Addresses JOIN Details on Details.detailId = Addresses.detailId where Addresses.contactId = ' + str(contactID)):
  44. if ADRrow[2] is not None:
  45. ADRstr=str(ADRrow[2])
  46. else:
  47. ADRstr=""
  48. if ADRrow[5] is not None:
  49. ADRcit=str(ADRrow[5])
  50. else:
  51. ADRcit=""
  52. if ADRrow[4] is not None:
  53. ADRreg=str(ADRrow[4])
  54. else:
  55. ADRreg=""
  56. if ADRrow[6] is not None:
  57. ADRcod=str(ADRrow[6])
  58. else:
  59. ADRcod=""
  60. if ADRrow[7] is not None:
  61. ADRcou=str(ADRrow[7])
  62. else:
  63. ADRcou=""
  64. DEBUG(args.debug,"Addressdata: street=" + ADRstr + " city=" + ADRcit + " region=" + ADRreg + " code=" + ADRcod + " country=" + ADRcou)
  65. adr = vcf.add('ADR').value = vobject.vcard.Address(street=ADRstr, city=ADRcit, region=ADRreg, code=ADRcod,country=ADRcou)
  66. ## Abfragen Organisation
  67. SQLORGCur = SQLconn.cursor()
  68. for ORGrow in SQLORGCur.execute('SELECT * from Organizations where contactId = ' + str(contactID)):
  69. org = vcf.add('ORG').value = [str(ORGrow[2]), str(ORGrow[6])]
  70. if ORGrow[4] is not None:
  71. title = vcf.add('TITLE').value = str(ORGrow[4])
  72. if ORGrow[3] is not None:
  73. role = vcf.add('ROLE').value = str(ORGrow[3])
  74. # Also parameters are possible. Could be read out
  75. # | columnID | column |
  76. # ----------------------------
  77. # | 0 | detailId |
  78. # | 1 | contactId |
  79. # | 2 | name |
  80. # | 3 | role |
  81. # | 4 | title |
  82. # | 5 | location |
  83. # | 6 | department |
  84. # | 7 | logoUrl |
  85. # | 8 | assistantName |
  86. ## Abfragen E-Mail-Adressen
  87. SQLEmailCur = SQLconn.cursor()
  88. for Emailrow in SQLEmailCur.execute('SELECT * from EmailAddresses JOIN Details on Details.detailId= EmailAddresses.detailId where EmailAddresses.contactId = ' + str(contactID)):
  89. # debug ausgabe
  90. DEBUG(args.debug,str(Emailrow[2]) + " at " + str(Emailrow[9]))
  91. email = vcf.add('email')
  92. email.value = str(Emailrow[2])
  93. # nur den Typ einpflegen, wenn das hier nicht none ist
  94. if Emailrow[9] != None:
  95. email.type_param = str(Emailrow[9])
  96. SQLPhoneCur = SQLconn.cursor()
  97. ## Abfragen Telefonnummer, Fax, SMS - Nummern kommen aus der gleichen Tabelle
  98. for Phonerow in SQLPhoneCur.execute('SELECT * from PhoneNumbers JOIN Details on Details.detailId = PhoneNumbers.detailId where PhoneNumbers.contactId = ' + str(contactID)):
  99. # wir müssen die SubTypen unterscheiden
  100. #Null voice
  101. #1 cell
  102. #2 fax
  103. #3 pager
  104. #6 video
  105. #10 Assistent
  106. # debug ausgabe
  107. DEBUG(args.debug,str(Phonerow[2]) + " at " + str(Phonerow[10]) + " subtype=" + str(Phonerow[3]))
  108. # None is a normal phone Number
  109. if Phonerow[3] == "1":
  110. phcat='cell'
  111. elif Phonerow[3] == "2":
  112. phcat='fax'
  113. elif Phonerow[3] == "3":
  114. phcat='pager'
  115. elif Phonerow[3] == "6":
  116. phcat='video'
  117. elif Phonerow[3] == "10":
  118. phcat='assistent'
  119. elif Phonerow[3] is None:
  120. phcat='voice'
  121. DEBUG(args.debug,phcat)
  122. phone = vcf.add(phcat).value = str(Phonerow[2])
  123. # nur den Typ einpflegen, wenn das hier nicht none ist
  124. if Phonerow[10] != None:
  125. try:
  126. phone.type_param = str(Phonerow[10])
  127. except AttributeError:
  128. continue
  129. # Ausgabe
  130. print(vcf.serialize())
  131. # hier brauchen wir einige eception handles -> wie bekommen wir die einzelnen exceptions heruas ?
  132. #except:
  133. #print("Error in executing SQL")
  134. except AttributeError:
  135. print("Datatype mismatch")
  136. raise
  137. # das generöse Except am Ende
  138. except:
  139. print("unhandled error")
  140. raise