Snuggledash icon

gigashit.py

Snuggledash | PRO | 01/28/17 09:33:53 PM UTC | 0 ⭐ | 608 👁️ | Never ⏰ | []
Python |

1.4 KB

|

None

|

0 👍

/

0 👎

#!/usr/bin/python3
 
import sys, re
 
regex = re.compile(r"TEL;TYPE=([0-9A-Za-z-]+).*,pref:(.*)$")
for fn in sys.argv[1:]:
    fnc = fn[:-4] + "_converted.vcf"
    with open(fn, "rt") as f, open(fnc, "wt") as g:
        print("BEGIN:VCARD", file=g)
        print("VERSION:2.1", file=g)
        for line in f:
            line = line.rstrip()
            email_found = False
            if line.startswith("N:"):
                first_semi = line.index(";")
                second_semi = line[first_semi+1:].index(";")
                out = line[:first_semi+1+second_semi]
                print(out, file=g)
            elif line.startswith("BDAY:"):
                print(line, file=g)
            elif line.startswith("TEL;"):
                match = regex.match(line)
                if match:
                    (typ, number) = match.groups()
                    if number.startswith("+49"):
                        number = "0" + number[3:]
                    if number.startswith("+"):
                        number = "00" + number[1:]
                    out = "TEL;" + typ + ":" + number
                    print(out, file=g)
            elif line.startswith("EMAIL;") and not email_found:
                colon_index = line.index(":")
                out = "EMAIL:" + line[colon_index+1:]
                print(out, file=g)
                email_found = True
        print("END:VCARD", file=g)

Comments