Skip to content

Instantly share code, notes, and snippets.

@sevkin
Last active August 14, 2021 11:59
Show Gist options
  • Save sevkin/12b6b21fe6f00e9f7ed084e23e3d8b5d to your computer and use it in GitHub Desktop.
Save sevkin/12b6b21fe6f00e9f7ed084e23e3d8b5d to your computer and use it in GitHub Desktop.
конвертер контактов из яндекс почты в список для "рассылки"
import vobject # pip install vobject
import argparse
import csv
import sys
def notify_skip(card: vobject.vCard) -> None:
print(sys.stderr, 'skipped:', card,
file=sys.stderr)
pass
def convert(src, dst) -> None:
# TODO check if src == sys.stdin and it`s empty
writer = csv.writer(dst)
writer.writerow(['name', 'email'])
for card in vobject.readComponents(src):
if not hasattr(card, 'fn'):
notify_skip(card)
continue
if not hasattr(card, 'email_list'):
notify_skip(card)
continue
writer.writerow([
card.fn.value.strip().split(' ')[0],
card.email_list[0].value.strip()
])
def new_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description='Extract name and email from vcf')
parser.add_argument('src', nargs='?',
type=argparse.FileType('r'), default=sys.stdin,
help='source file in vcf format (default stdin)')
parser.add_argument('dst', nargs='?',
type=argparse.FileType('w'), default=sys.stdout,
help='result file in csv format (default stdout)')
return parser
if '__main__' == __name__:
args = new_parser().parse_args()
convert(args.src, args.dst)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment