First version and vCard test files
parent
d7569fae20
commit
990dabde1c
|
@ -0,0 +1 @@
|
||||||
|
out/
|
|
@ -0,0 +1,74 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import vobject
|
||||||
|
import os
|
||||||
|
import quopri
|
||||||
|
import re
|
||||||
|
|
||||||
|
def input():
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('vcard',
|
||||||
|
type=argparse.FileType('r'),
|
||||||
|
help='Vcard file to parse')
|
||||||
|
args = parser.parse_args()
|
||||||
|
return args
|
||||||
|
|
||||||
|
# Remove FN
|
||||||
|
# Add N;CHARSET=ISO-8859-1;ENCODING=QUOTED-PRINTABLE:<familyName>;<Name>
|
||||||
|
def legacy_encoding_filter(given_name, family_name, serialized_vcard):
|
||||||
|
# Ugly hack to get charset latin-1 + quoted printable syntax
|
||||||
|
gname = quopri.encodestring(unicode(quopri.decodestring(given_name), "utf-8").encode('latin-1'))
|
||||||
|
fname = quopri.encodestring(unicode(quopri.decodestring(family_name), "utf-8").encode('latin-1'))
|
||||||
|
|
||||||
|
print "given : %s ; Family : %s" % (gname, fname)
|
||||||
|
|
||||||
|
ret = re.sub(r'^FN:.*\n',
|
||||||
|
r'',
|
||||||
|
serialized_vcard,
|
||||||
|
flags=re.MULTILINE)
|
||||||
|
# Do not forget \r !
|
||||||
|
ret = re.sub(r'^N:(.*)',
|
||||||
|
r'N;CHARSET=ISO-8859-1;ENCODING=QUOTED-PRINTABLE:%s;%s\r'
|
||||||
|
% (fname, gname),
|
||||||
|
ret,
|
||||||
|
flags=re.MULTILINE)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
args = input()
|
||||||
|
print("*** Parsing Vcard : ***")
|
||||||
|
|
||||||
|
vlist = vobject.readComponents(args.vcard)
|
||||||
|
|
||||||
|
print("Creating output directory :")
|
||||||
|
try:
|
||||||
|
os.mkdir("out/")
|
||||||
|
except OSError:
|
||||||
|
print "Output directory already exist"
|
||||||
|
|
||||||
|
for i, vcard in enumerate(vlist):
|
||||||
|
try:
|
||||||
|
print "*** VCARD number %d ***" % i
|
||||||
|
try:
|
||||||
|
if vcard.tel:
|
||||||
|
print "Creating single vcard file"
|
||||||
|
filename = quopri.decodestring(str(vcard.n.value).strip().replace(" ", "_"))
|
||||||
|
print "filename : '%s'" % filename
|
||||||
|
# Save to file
|
||||||
|
with open("out/%s.vcf" % filename, 'w') as f:
|
||||||
|
f.write(legacy_encoding_filter(vcard.n.value.given,
|
||||||
|
vcard.n.value.family,
|
||||||
|
vcard.serialize()))
|
||||||
|
except (AttributeError) as e:
|
||||||
|
# print "No vcard.tel for vcard number %d" % i
|
||||||
|
continue
|
||||||
|
except:
|
||||||
|
print "Uncatched exception"
|
||||||
|
except (vobject.base.ValidateError) as e:
|
||||||
|
# print "VCARD not valid : %s" % e
|
||||||
|
continue
|
||||||
|
except:
|
||||||
|
print "Uncatched exception"
|
||||||
|
args.vcard.close()
|
|
@ -0,0 +1,5 @@
|
||||||
|
BEGIN:VCARD
|
||||||
|
VERSION:2.1
|
||||||
|
N;CHARSET=ISO-8859-1;ENCODING=QUOTED-PRINTABLE:Test;=E9=E7=E0
|
||||||
|
TEL;PREF;VOICE:+33600000000
|
||||||
|
END:VCARD
|
|
@ -0,0 +1,14 @@
|
||||||
|
begin:vcard
|
||||||
|
fn:test 1
|
||||||
|
n:1;test
|
||||||
|
tel;cell:+33 6 00 00 00 00
|
||||||
|
version:2.1
|
||||||
|
end:vcard
|
||||||
|
|
||||||
|
begin:vcard
|
||||||
|
fn;quoted-printable:test =C3=A9=C3=A0=C3=A9=C3=A7 2
|
||||||
|
n;quoted-printable:2;test =C3=A9=C3=A7=C3=A0
|
||||||
|
tel;cell:+33 7 00 00 00 00
|
||||||
|
version:2.1
|
||||||
|
end:vcard
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
begin:vcard
|
||||||
|
fn;quoted-printable:test =C3=A9=C3=A0=C3=A9=C3=A7 2
|
||||||
|
n;quoted-printable:2;test =C3=A9=C3=A7=C3=A0
|
||||||
|
tel;cell:+33 7 00 00 00 00
|
||||||
|
version:2.1
|
||||||
|
end:vcard
|
||||||
|
|
Loading…
Reference in New Issue