Advertisement
Snuggledash

gigashit.py

Jan 28th, 2017
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import sys, re
  4.  
  5. regex = re.compile(r"TEL;TYPE=([0-9A-Za-z-]+).*,pref:(.*)$")
  6. for fn in sys.argv[1:]:
  7.     fnc = fn[:-4] + "_converted.vcf"
  8.     with open(fn, "rt") as f, open(fnc, "wt") as g:
  9.         print("BEGIN:VCARD", file=g)
  10.         print("VERSION:2.1", file=g)
  11.         for line in f:
  12.             line = line.rstrip()
  13.             email_found = False
  14.             if line.startswith("N:"):
  15.                 first_semi = line.index(";")
  16.                 second_semi = line[first_semi+1:].index(";")
  17.                 out = line[:first_semi+1+second_semi]
  18.                 print(out, file=g)
  19.             elif line.startswith("BDAY:"):
  20.                 print(line, file=g)
  21.             elif line.startswith("TEL;"):
  22.                 match = regex.match(line)
  23.                 if match:
  24.                     (typ, number) = match.groups()
  25.                     if number.startswith("+49"):
  26.                         number = "0" + number[3:]
  27.                     if number.startswith("+"):
  28.                         number = "00" + number[1:]
  29.                     out = "TEL;" + typ + ":" + number
  30.                     print(out, file=g)
  31.             elif line.startswith("EMAIL;") and not email_found:
  32.                 colon_index = line.index(":")
  33.                 out = "EMAIL:" + line[colon_index+1:]
  34.                 print(out, file=g)
  35.                 email_found = True
  36.         print("END:VCARD", file=g)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement