Created
August 11, 2019 19:32
-
-
Save dargmuesli/2d5a7a121a280f7a07fdb09072a7e20b to your computer and use it in GitHub Desktop.
Converts a VCards with photo urls to VCards with base64 images.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from io import BytesIO | |
from itertools import tee | |
import base64 | |
import requests | |
import vobject | |
# change this variable! | |
filename = 'contacts' | |
out = str() | |
gen1, gen2 = tee(vobject.readComponents(str(open(filename + '.vcf', 'r').read())), 2) | |
gen_sum = sum(1 for i in gen1) | |
index = 0 | |
for v in gen2: | |
index += 1 | |
status = 'Processing ' + str(index) + '/' + str(gen_sum) + ' (' + str(int(index / gen_sum * 100)) + '%) ' | |
if hasattr(v, 'n'): | |
status += str(v.n.value) | |
else: | |
status += 'N/A' | |
print(status) | |
if hasattr(v, 'photo'): | |
v.photo.value = 'data:image/jpeg;base64,' + base64.b64encode(BytesIO(requests.get(v.photo.value).content).getbuffer()).decode("utf-8") | |
out += v.serialize() | |
text_file = open(filename + '_out.vcf', 'w') | |
text_file.write(out) | |
text_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment