Advertisement
Guest User

vcard_reformat.py

a guest
Sep 27th, 2011
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.99 KB | None | 0 0
  1. #!/usr/bin/python
  2. # coding=utf8
  3.  
  4. # reconvert vCard format by filling FN: tag in vCard blocks
  5. # with mail address names if empty
  6. # also, remove vCard blocks not containing any e-mail addresses
  7.  
  8. import os
  9. import sys
  10. import re
  11.  
  12. # vars
  13. PARAM = ""
  14. COUNT_ADD = 0
  15. COUNT_SUB = 0
  16.  
  17. if len(sys.argv) > 1:
  18.     PARAM = sys.argv[1]
  19. else:
  20.     print "error: imput file missing"
  21.     exit(1)
  22.  
  23. print "* start"
  24.  
  25. # read .vcf text file
  26. if not os.path.exists(PARAM):
  27.     print "error: invalid file"
  28.     exit(1)
  29. FILE = open(PARAM)
  30. # get lines of text
  31. TEXT = FILE.readlines()
  32.  
  33. L3 = ""
  34. OUT = ""
  35.  
  36. # cycle thorugh every line
  37. for i in TEXT:
  38.     # get line without ending new line char
  39.     L = i[:-1]
  40.  
  41.     # add next block
  42.     if L: L3 += L + "\n"
  43.  
  44.     # does the line start with "END:" tag?
  45.     L2 = L[:4]
  46.     if L2 == "END:":
  47.         if L3:
  48.             # is there any e-mail address in the block?
  49.             if re.findall("^EMAIL.*:.*@", L3, re.M):
  50.  
  51.                 # is there an empty "FN:" tag?
  52.                 if re.findall("^FN:\n", L3, re.M):
  53.                     # if so, then add the mail name to it
  54.                     # get the name part of the e-mail address before "@" char
  55.                     PRE  = re.findall("^EMAIL.*$", L3, re.M)[0]
  56.                     NAME = re.findall("[^:]+$", PRE, re.M)[0]
  57.                     # is it not empty?
  58.                     L4 = ""
  59.                     if NAME:
  60.                         L4 = re.sub("FN:\n", "FN:" + NAME + "\n", L3, re.M)
  61.                     else:
  62.                         L4 = re.sub("FN:\n", "FN:" + "empty" + "\n", L3, re.M)
  63.  
  64.                 else: L4 = L3
  65.                
  66.                 # store output
  67.                 OUT += L4 + "\n"
  68.                
  69.                 # count it
  70.                 COUNT_ADD += 1
  71.  
  72.             else:
  73.                 COUNT_SUB += 1
  74.  
  75.         # clear temporary block holder
  76.         L3 = ""
  77.  
  78.  
  79. # convert unicode chars back to utf8 in output
  80. # 123=C3=B6=C3=BC=C3=B3=C5=91=C3=BA=C3=A9=C3=A1=C5=B1=C3=AD123=C3=96=C3=9C=C3==0A=93=C5=90=C3=9A=C3=89=C3=81=C5=B0=C3=8D123=0A
  81. # 123öüóőúéáűí123ÖÜÓŐÚÉÁŰÍ123
  82. if OUT:
  83.     TEMP = re.sub("=C3=B6", "ö", OUT); OUT = TEMP
  84.     TEMP = re.sub("=C3=BC", "ü", OUT); OUT = TEMP
  85.     TEMP = re.sub("=C3=B3", "ó", OUT); OUT = TEMP
  86.     TEMP = re.sub("=C5=91", "ő", OUT); OUT = TEMP
  87.     TEMP = re.sub("=C3=BA", "ú", OUT); OUT = TEMP
  88.     TEMP = re.sub("=C3=A9", "é", OUT); OUT = TEMP
  89.     TEMP = re.sub("=C3=A1", "á", OUT); OUT = TEMP
  90.     TEMP = re.sub("=C3=A1", "á", OUT); OUT = TEMP
  91.     TEMP = re.sub("=C5=B1", "ű", OUT); OUT = TEMP
  92.     TEMP = re.sub("=C3=AD", "í", OUT); OUT = TEMP
  93.  
  94.     TEMP = re.sub("=C3=96", "Ö", OUT); OUT = TEMP
  95.     TEMP = re.sub("=C3=9C", "Ü", OUT); OUT = TEMP
  96.     TEMP = re.sub("=C3==0A", "Ó", OUT);    OUT = TEMP
  97.     TEMP = re.sub("=93=C5", "Ő", OUT); OUT = TEMP
  98.     TEMP = re.sub("=90=C3", "Ú", OUT); OUT = TEMP
  99.     TEMP = re.sub("=9A=C3", "É", OUT); OUT = TEMP
  100.     TEMP = re.sub("=89=C3", "Á", OUT); OUT = TEMP
  101.     TEMP = re.sub("=81=C5", "Ű", OUT); OUT = TEMP
  102.     TEMP = re.sub("=B0=C3=8D", "Í", OUT);  OUT = TEMP
  103.  
  104.     TEMP = re.sub("==0A", "", OUT);     OUT = TEMP
  105.     TEMP = re.sub("=0A", "", OUT);      OUT = TEMP
  106.  
  107.  
  108. # file operations
  109. FILE.close()
  110. FILE = open(PARAM, "w")
  111. FILE.writelines(OUT)
  112. FILE.close()
  113.  
  114. # print info
  115. print "* " + str(COUNT_ADD) + " vCards added, " + str(COUNT_SUB) + " removed"
  116. print "* exit"
  117.  
  118. exit(0)
  119.  
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement