Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # coding=utf8
- # reconvert vCard format by filling FN: tag in vCard blocks
- # with mail address names if empty
- # also, remove vCard blocks not containing any e-mail addresses
- import os
- import sys
- import re
- # vars
- PARAM = ""
- COUNT_ADD = 0
- COUNT_SUB = 0
- if len(sys.argv) > 1:
- PARAM = sys.argv[1]
- else:
- print "error: imput file missing"
- exit(1)
- print "* start"
- # read .vcf text file
- if not os.path.exists(PARAM):
- print "error: invalid file"
- exit(1)
- FILE = open(PARAM)
- # get lines of text
- TEXT = FILE.readlines()
- L3 = ""
- OUT = ""
- # cycle thorugh every line
- for i in TEXT:
- # get line without ending new line char
- L = i[:-1]
- # add next block
- if L: L3 += L + "\n"
- # does the line start with "END:" tag?
- L2 = L[:4]
- if L2 == "END:":
- if L3:
- # is there any e-mail address in the block?
- if re.findall("^EMAIL.*:.*@", L3, re.M):
- # is there an empty "FN:" tag?
- if re.findall("^FN:\n", L3, re.M):
- # if so, then add the mail name to it
- # get the name part of the e-mail address before "@" char
- PRE = re.findall("^EMAIL.*$", L3, re.M)[0]
- NAME = re.findall("[^:]+$", PRE, re.M)[0]
- # is it not empty?
- L4 = ""
- if NAME:
- L4 = re.sub("FN:\n", "FN:" + NAME + "\n", L3, re.M)
- else:
- L4 = re.sub("FN:\n", "FN:" + "empty" + "\n", L3, re.M)
- else: L4 = L3
- # store output
- OUT += L4 + "\n"
- # count it
- COUNT_ADD += 1
- else:
- COUNT_SUB += 1
- # clear temporary block holder
- L3 = ""
- # convert unicode chars back to utf8 in output
- # 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
- # 123öüóőúéáűí123ÖÜÓŐÚÉÁŰÍ123
- if OUT:
- TEMP = re.sub("=C3=B6", "ö", OUT); OUT = TEMP
- TEMP = re.sub("=C3=BC", "ü", OUT); OUT = TEMP
- TEMP = re.sub("=C3=B3", "ó", OUT); OUT = TEMP
- TEMP = re.sub("=C5=91", "ő", OUT); OUT = TEMP
- TEMP = re.sub("=C3=BA", "ú", OUT); OUT = TEMP
- TEMP = re.sub("=C3=A9", "é", OUT); OUT = TEMP
- TEMP = re.sub("=C3=A1", "á", OUT); OUT = TEMP
- TEMP = re.sub("=C3=A1", "á", OUT); OUT = TEMP
- TEMP = re.sub("=C5=B1", "ű", OUT); OUT = TEMP
- TEMP = re.sub("=C3=AD", "í", OUT); OUT = TEMP
- TEMP = re.sub("=C3=96", "Ö", OUT); OUT = TEMP
- TEMP = re.sub("=C3=9C", "Ü", OUT); OUT = TEMP
- TEMP = re.sub("=C3==0A", "Ó", OUT); OUT = TEMP
- TEMP = re.sub("=93=C5", "Ő", OUT); OUT = TEMP
- TEMP = re.sub("=90=C3", "Ú", OUT); OUT = TEMP
- TEMP = re.sub("=9A=C3", "É", OUT); OUT = TEMP
- TEMP = re.sub("=89=C3", "Á", OUT); OUT = TEMP
- TEMP = re.sub("=81=C5", "Ű", OUT); OUT = TEMP
- TEMP = re.sub("=B0=C3=8D", "Í", OUT); OUT = TEMP
- TEMP = re.sub("==0A", "", OUT); OUT = TEMP
- TEMP = re.sub("=0A", "", OUT); OUT = TEMP
- # file operations
- FILE.close()
- FILE = open(PARAM, "w")
- FILE.writelines(OUT)
- FILE.close()
- # print info
- print "* " + str(COUNT_ADD) + " vCards added, " + str(COUNT_SUB) + " removed"
- print "* exit"
- exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement