Last active
October 2, 2023 09:41
-
-
Save Amir-P/d7017ce65da1bd40f4372dfbd2b68ec2 to your computer and use it in GitHub Desktop.
Convert JSON exported contacts from Telegram to vCard (.vcf)
This file contains 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
import sys | |
import json | |
input_file = sys.argv[1] | |
output_file = sys.argv[2] | |
output_file = open(output_file, 'w') | |
def to_vcf_string(first_name, last_name, phone): | |
vcf_lines = [] | |
vcf_lines.append('BEGIN:VCARD') | |
vcf_lines.append('VERSION:4.0') | |
vcf_lines.append('N:%s' % (last_name + ';' + first_name)) | |
vcf_lines.append('FN:%s' % (first_name + ('' if len(last_name) == 0 else ' ') + last_name)) | |
vcf_lines.append('TEL:%s' % phone) | |
vcf_lines.append('END:VCARD') | |
vcf_string = '\n'.join(vcf_lines) + '\n' | |
return vcf_string | |
with open(input_file) as json_file: | |
data = json.load(json_file) | |
contacts_list = data['contacts']['list'] | |
for contact in contacts_list: | |
first_name = contact['first_name'] | |
last_name = contact['last_name'] | |
if len(first_name) == 0 and len(last_name) == 0: | |
continue | |
phone = contact['phone_number'] | |
output_file.write(to_vcf_string(first_name, last_name, phone).encode('utf-8')) | |
output_file.close() |
Hello
how give him json file?
Hi. Just run the script passing json file path and desired output path as input like this: python converter.py json_file_path output_file_path
. @MrWesturn
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
try this:
import sys
import json
def to_vcf_string(first_name, last_name, phone):
vcf_lines = [
'BEGIN:VCARD',
'VERSION:4.0',
f'N:{last_name};{first_name}',
f'FN:{first_name}{" " if last_name else ""}{last_name}',
f'TEL:{phone}',
'END:VCARD'
]
return '\n'.join(vcf_lines) + '\n'
def main(input_file_path, output_file_path):
with open(input_file_path) as json_file:
data = json.load(json_file)
contacts_list = data['contacts']['list']
if name == "main":
if len(sys.argv) < 3:
print("Usage: script_name input_file output_file")
sys.exit(1)