Skip to content

Instantly share code, notes, and snippets.

@tmalsburg
Last active August 13, 2023 00:02
Show Gist options
  • Save tmalsburg/9747104 to your computer and use it in GitHub Desktop.
Save tmalsburg/9747104 to your computer and use it in GitHub Desktop.

Revisions

  1. tmalsburg revised this gist Feb 2, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion vcard2org-contacts.py
    Original file line number Diff line number Diff line change
    @@ -31,8 +31,9 @@
    UTF8Writer = codecs.getwriter('utf8')
    sys.stdout = UTF8Writer(sys.stdout)

    template = " :%s: %s%s"
    template = " :%s: %s%s"
    prefix = "*"
    indentation = " "

    def flatten(l):
    '''Flatten a arbitrarily nested lists and return the result as a single list.
  2. tmalsburg revised this gist Mar 29, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions vcard2org-contacts.py
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,11 @@
    # org-contacts. There is one mandatory argument: the name of the
    # vCard file. The result is printed to standard out.

    # Usage:
    #
    # python vcard2org-contacts.py contacts.vcf > contacts.org
    #

    # Issues:
    #
    # The LABEL property in the Forrest Gump example (from Wikipedia) is
  3. tmalsburg revised this gist Mar 28, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions vcard2org-contacts.py
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,9 @@

    # Written by Titus von der Malsburg <[email protected]>, March 2014.

    # This is a simple script for converting vcard files to
    # This is a simple script for converting vCard files to
    # org-contacts. There is one mandatory argument: the name of the
    # vcard file. The result is printed to standard out.
    # vCard file. The result is printed to standard out.

    # Issues:
    #
    @@ -43,7 +43,7 @@ def flatten(l):
    if __name__=="__main__":

    if len(sys.argv) != 2:
    raise ValueError("Please specify exactly one vcard file.")
    raise ValueError("Please specify exactly one vCard file.")

    fname = sys.argv[1]
    stream = io.open(fname, "r", encoding="utf-8")
  4. tmalsburg revised this gist Mar 25, 2014. 1 changed file with 0 additions and 0 deletions.
    Empty file modified vcard2org-contacts.py
    100644 → 100755
    Empty file.
  5. tmalsburg revised this gist Mar 25, 2014. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions vcard2org-contacts.py
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,16 @@
    # org-contacts. There is one mandatory argument: the name of the
    # vcard file. The result is printed to standard out.

    # Issues:
    #
    # The LABEL property in the Forrest Gump example (from Wikipedia) is
    # not handled correctly:
    #
    # LABEL;TYPE=WORK:100 Waters Edge\nBaytown, LA 30314\nUnited States of America
    #
    # The part after the comma is missing. That seems to be due to a bug
    # in vobject.

    import sys
    import io
    import dateutil.parser
    @@ -16,7 +26,7 @@
    UTF8Writer = codecs.getwriter('utf8')
    sys.stdout = UTF8Writer(sys.stdout)

    indentation = " "
    template = " :%s: %s%s"
    prefix = "*"

    def flatten(l):
    @@ -39,9 +49,6 @@ def flatten(l):
    stream = io.open(fname, "r", encoding="utf-8")
    vcards = vobject.readComponents(stream)

    template = indentation + ":%s: %s%s"


    for vcard in vcards:

    note = ""
    @@ -88,7 +95,6 @@ def flatten(l):
    if p.name == "TEL":
    name = "PHONE"


    # Collect type attributes:
    attribs = ", ".join(p.params.get("TYPE", []))
    if attribs:
  6. tmalsburg revised this gist Mar 25, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion vcard2org-contacts.py
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,7 @@

    import sys
    import io
    import dateutil.parser
    import vobject
    import codecs

    @@ -81,7 +82,8 @@ def flatten(l):
    name = "ADDRESS"

    if p.name == "REV":
    value = "[%s]" % p.value.split("T")[0]
    value = dateutil.parser.parse(p.value)
    value = value.strftime("[%Y-%m-%d %a %H:%M]")

    if p.name == "TEL":
    name = "PHONE"
  7. tmalsburg revised this gist Mar 25, 2014. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions vcard2org-contacts.py
    Original file line number Diff line number Diff line change
    @@ -78,15 +78,14 @@ def flatten(l):
    value = (p.value.street, p.value.code + " " + p.value.city,
    p.value.region, p.value.country, p.value.extended, p.value.box)
    value = ", ".join([x for x in value if x!=''])
    name = "ADDRESS"

    if p.name == "REV":
    value = "[%s]" % p.value.split("T")[0]

    if p.name == "TEL":
    name = "PHONE"

    if p.name == "ADR":
    name = "ADDRESS"

    # Collect type attributes:
    attribs = ", ".join(p.params.get("TYPE", []))
  8. tmalsburg created this gist Mar 24, 2014.
    105 changes: 105 additions & 0 deletions vcard2org-contacts.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,105 @@
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    # Written by Titus von der Malsburg <[email protected]>, March 2014.

    # This is a simple script for converting vcard files to
    # org-contacts. There is one mandatory argument: the name of the
    # vcard file. The result is printed to standard out.

    import sys
    import io
    import vobject
    import codecs

    UTF8Writer = codecs.getwriter('utf8')
    sys.stdout = UTF8Writer(sys.stdout)

    indentation = " "
    prefix = "*"

    def flatten(l):
    '''Flatten a arbitrarily nested lists and return the result as a single list.
    '''
    ret = []
    for i in l:
    if isinstance(i, list) or isinstance(i, tuple):
    ret.extend(flatten(i))
    else:
    ret.append(i)
    return ret

    if __name__=="__main__":

    if len(sys.argv) != 2:
    raise ValueError("Please specify exactly one vcard file.")

    fname = sys.argv[1]
    stream = io.open(fname, "r", encoding="utf-8")
    vcards = vobject.readComponents(stream)

    template = indentation + ":%s: %s%s"


    for vcard in vcards:

    note = ""

    print "%s %s" % (prefix, vcard.fn.value)
    print indentation + ":PROPERTIES:"

    for p in vcard.getChildren():

    if p.name in ("VERSION", "PRODID", "FN") or p.name.startswith("X-"):
    continue

    if p.name == "NOTE":
    note = p.value
    continue

    name = p.name
    value = p.value

    # Special treatment for some fields:

    if p.name == "ORG":
    try:
    value = ", ".join(flatten(p.value))
    except:
    print p.value

    if p.name == "N":
    value = "%s;%s;%s;%s;%s" % (p.value.family, p.value.given,
    p.value.additional, p.value.prefix,
    p.value.suffix)

    if p.name == "ADR":
    # TODO Make the formatting sensitive to X-ABADR:
    value = (p.value.street, p.value.code + " " + p.value.city,
    p.value.region, p.value.country, p.value.extended, p.value.box)
    value = ", ".join([x for x in value if x!=''])

    if p.name == "REV":
    value = "[%s]" % p.value.split("T")[0]

    if p.name == "TEL":
    name = "PHONE"

    if p.name == "ADR":
    name = "ADDRESS"

    # Collect type attributes:
    attribs = ", ".join(p.params.get("TYPE", []))
    if attribs:
    attribs = " (%s)" % attribs

    # Make sure that there are no newline chars left as that would
    # break org's property format:
    value = value.replace("\n", ", ")

    print template % (name, value, attribs)

    print indentation + ":END:"

    if note:
    print note