Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

adressimport 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/python
  2. # adr_conv.py
  3. #
  4. # Converts a vCard address book into abook addressbook format
  5. #
  6. # Author: Gavin Costello (gavcos@gmail.com)
  7. # Date: 19.02.2009
  8. import optparse
  9. desc="""This program converts a gmail vCard address book into a format readable by abook
  10. (http://abook.sourceforge.net).
  11. To export your gmail contacts in vCard format, select Contacts -> Export and choose the 3rd export format (vCard).
  12. The file will be saved by default as contacts.vcf
  13. """
  14. parser = optparse.OptionParser(description=desc, version='%prog version 1.0')
  15. parser.add_option('-i', '--inputfile', \
  16. dest='inputfile', action='store', default='contacts.vcf', \
  17. help='The name of the vCard file containing the contacts to convert (default=%default)')
  18. parser.add_option('-o', '--outputfile', \
  19. dest='outputfile', action='store', default='addressbook', \
  20. help='The name of the output file to be generated by this script (default=%default)')
  21. parser.add_option('-d', '--debug', \
  22. dest='debug', action='store_true', default=False,
  23. help='Print debug statements (default=%default)')
  24. (opts, args) = parser.parse_args()
  25. name = ''; # Contact name
  26. phone = ''; # Contact phone
  27. email = ''; # Contact email
  28. nick = ''; # Contact nickname
  29. org = ''; # Contact organisation
  30. note = ''; # Notes
  31. emails = [] # Email list
  32. count = 0; # Contacts count
  33. # Open input file, read-only
  34. try:
  35. cfile = open(opts.inputfile, 'r')
  36. except IOError:
  37. print('Unable to open input file: %s' % opts.inputfile)
  38. raise
  39. # Open output file for writing
  40. try:
  41. ofile = open(opts.outputfile, 'w+')
  42. except IOError:
  43. print('Unable to open output file: %s' % opts.outputfile)
  44. raise
  45. ofile.write('\n')
  46. ofile.write('[format]\n')
  47. ofile.write('program=abook\n')
  48. ofile.write('\n')
  49. # Loop through input file
  50. for line in cfile.readlines():
  51. # Name field
  52. if (line.startswith('FN')):
  53. name = line.split(':')[1].strip()
  54. #name = unicode(name, 'utf-8')
  55. ofile.write('\n')
  56. ofile.write('[%d]\n' % count)
  57. count += 1
  58. if opts.debug:
  59. print(name, end="")
  60. ofile.write('name=%s\n' % name)
  61. elif (line.startswith('EMAIL')):
  62. email = line.split(':')[1].strip()
  63. emails.append(email)
  64. elif (line.startswith('TEL')):
  65. # Verify if it's a fax number
  66. parts = line.split(';')
  67. if (len(parts) > 2): # Fax number
  68. phone = parts[2].split(':')[1]
  69. ofile.write('fax=%s' % phone)
  70. continue
  71. # Normal telephone number
  72. try:
  73. tel = parts[1]
  74. except IndexError: # Phone type not defined (other in gmail)
  75. phone = line.split(':')[1]
  76. ofile.write('mobile=%s' % phone)
  77. continue
  78. fulltype = tel.split('=')[1]
  79. type = fulltype.split(':')[0]
  80. if opts.debug:
  81. print('type=%s' % type,)
  82. phone = line.split(':')[1]
  83. if (type == 'HOME'):
  84. ofile.write('phone=%s' % phone)
  85. if (type == 'CELL'):
  86. ofile.write('mobile=%s' % phone)
  87. if (type == 'WORK'):
  88. ofile.write('workphone=%s' % phone)
  89. elif (line.startswith('NICKNAME')):
  90. # Notes / nicknames
  91. try:
  92. nick = line.split(':')[2].strip()
  93. except IndexError:
  94. nick = line.split(':')[1].strip()
  95. ofile.write('nick=%s\n' % nick,)
  96. elif (line.startswith('ORG')):
  97. org = line.split(':')[1].strip()
  98. if ((name == "") & (org != "")):
  99. ofile.write('name=%s' % org,)
  100. ofile.write('custom1=%s' % org,)
  101. elif (line.startswith('NOTE')):
  102. note = line.split(':')[1].strip()
  103. ofile.write('note=%s\n' % note,)
  104. elif (line.startswith('ADR')):
  105. # Address
  106. adr_parts = line.split(';')[2:]
  107. for i, part in enumerate(adr_parts):
  108. if (part):
  109. if i == 0:
  110. ofile.write('address_lines=%s\n' % part)
  111. if i == 1:
  112. ofile.write('address_lines=%s\n' % part)
  113. if i == 2:
  114. ofile.write('city=%s\n' % part)
  115. if i == 3:
  116. ofile.write('state=%s\n' % part)
  117. if i == 4:
  118. ofile.write('zip=%s\n' % part)
  119. if i == 5:
  120. ofile.write('country=%s\n' % part)
  121. elif (line.startswith('END')):
  122. if (len(emails) > 0):
  123. ofile.write('email=%s\n' % ", ".join(emails),)
  124. emails = []
  125. else:
  126. continue;
  127. print('\n%s contacts processed' % count)
  128. try:
  129. cfile.close()
  130. except IOError:
  131. print('Unable to close input file: %s' % opts.inputfile)
  132. raise
  133. try:
  134. ofile.close()
  135. except IOError:
  136. print('Unable to close output file: %s' % opts.outputfile)
  137. raise
  138. exit(0)