#!/usr/bin/env python3

import os
import vobject
import csv

def import_vcf_to_csv(vcf_path):
    contacts = []
    with open(vcf_path, 'r') as f:
        for vcard in vobject.readComponents(f):
            name = ''
            if hasattr(vcard, 'fn'):
                name = str(vcard.fn.value)
            elif hasattr(vcard, 'n'):
                name = str(vcard.n.value)
            phone = ''
            if hasattr(vcard, 'tel'):
                phone = str(vcard.tel.value)
            email = ''
            if hasattr(vcard, 'email'):
                email = str(vcard.email.value)
            contacts.append({'name': name, 'phone': phone, 'email': email})
    return contacts

def main():
    vcf_file = 'shenzhen_69.vcf'
    csv_file = 'shenzhen_69.csv'
    contacts = import_vcf_to_csv(vcf_file)
    with open(csv_file, 'w', newline='') as f:
        writer = csv.DictWriter(f, fieldnames=['name', 'phone', 'email'])
        writer.writeheader()
        writer.writerows(contacts)
    print(f"Exported {len(contacts)} contacts to {csv_file}")

if __name__ == '__main__':
    main()