Import contacts

Alexander Lehner lehner at edv-buero-lehner.de
Fri Jul 18 15:01:32 CEST 2008


I've attached a script that I use to convert tab-separated data (as 
generated from mysql) into a vcard file.

Alex.

On Fri, 18 Jul 2008, Al Johnson wrote:

> On Friday 18 July 2008, Markus Schlichting wrote:
>> Am Freitag, 18. Juli 2008 07:14:13 schrieb Curtis Vaughan:
>>> I use Thunderbird for Email. I want to export my contacts from the
>>> Address Book and import them to FR. I don't see any instructions on how
>>> to do that. In fact, the only solutions seem to be for me to import my
>>> contacts into Evolution or (god forbid) Outlook and then export them from
>>> there.
>>> Is there no other solution?
>>
>> Is it possible to export Thunderbird Contacts as VCF files ? I don't use
>> thunderbird regulary, but I think it is, so you can use
>> http://http//wiki.openmoko.org/wiki/Import_Vcf_Contacts to import your
>> contacts. Used it to copy my contact from kontact :-)
>
> Kontact is nice like that, but it helps that it uses vCard natively.
> Thunderbird doesn't - you can export to LDIF, CSV or tab-delimited. At
> present the solutions seem to be to use a roundabout route or write some
> code.
>
> _______________________________________________
> Openmoko community mailing list
> community at lists.openmoko.org
> http://lists.openmoko.org/mailman/listinfo/community
>
>
-------------- next part --------------
######################################################################
# Takes a (tab-) seperated ascii file and writes out a file of
# VCards (stdin -> stdout).
# Input is a textfile with separated fields and a header. example:
# matchcode	vorname	land	ort	telefon	telefon2	fax	nr_gruppe	mark	gruppen	name	info	plz	strasse	handy	gebdat	memo	picture	
# FA-ERDING	Erding		Erding	123/456	NULL		0	F		Finanzamt	StNr. 123/456/78900	85422	Postf. 1262		NULL			
#
# Or using mysql:
# echo 'select * from my_addresstable'|mysql  -pmy_password -hlocalhost my_database > ~/tmp/contacts.vcf
# 
# No "..." around fields, tab separated (configurable, see below).
# You shoud run this script on the linux host, since ASCII<->UTF-8
# conversion seems not very well supported on the moko phone.
# Copy the resulting output file via scp onto the phone and
# Use the manage-contacts.py load < contacts.vcf
# There.

import sys,time

####################
# user configuration

# adapt here the real field names of the table

sql_surename_field='name'
sql_prename_field='vorname'
sql_phone_field='telefon'
sql_phone2_field='telefon2'
sql_mobile_field='handy'

# It is possible to convert only a subset of all addresses.
# In my special case, I have a groups field which contains
# an 'O' character if it shoud be exported to some phone.
# To switch off the filter, replace the 'sql_filter_search'
# content with '' (instead of the 'O').

sql_filter_field='gruppen'
sql_filter_search='O'

# tab by default
sql_delimiter='\t'

# set this to your own country phone prefix, e.g. +1 for USA
# it will be prepended automatically
country_code='+49'

# end configuration options
####################



# convert a phone number of the form '089/123456' into
# '+4989123456' so that moko phone recognizes the sender
# id correctly.
# This is probably germany phone number specific...

def normalize(nr):    
    result=nr.replace( "/", "" )
    if result.startswith('0'):
        result = '+49' + result[1:]
    return result



lines = sys.stdin.readlines ()

name_idx=-1
vorname_idx=-1
phonenr_idx = -1
phonenr2_idx = -1
mobile_idx = -1
filter_idx = -1

linenr=0

for line in lines:
    columns = line.split(sql_delimiter)
    if linenr == 0:
        # print columns
        try:
            name_idx=columns.index(sql_surename_field)
            vorname_idx=columns.index(sql_prename_field)
            phonenr_idx=columns.index(sql_phone_field)
            phonenr2_idx=columns.index(sql_phone2_field)
            mobile_idx=columns.index(sql_mobile_field)
            filter_idx=columns.index(sql_filter_field)
        except:
            # print "some fields not found: " + str(name_idx) + " " + str(vorname_idx ) + " " + str( phonenr_idx ) + " " + str( phonenr2_idx ) + " " + str( mobile_idx ) + " " + str( filter_idx )
            pass

    else:

        # convert all fields into unicode.
        # This seems not to be supported well on the moko,
        # so it is better to let the linux host do the unicode
        # conversion (run this script locally)

        for i in range( 0,len(columns)-1 ):

            # convert the read-in string from ASCII into unicode
            try:
                columns[i] = columns[i].decode( "iso-8859-1" )
            except:
                print "cannot convert to unicode: " + columns[i]

            # and convert it back again into UTF-8 what is expected
            # from the VCF format.
            try:
                columns[i] = columns[i].encode( 'utf-8', "replace" )
            except:
                print "cannot convert to utf-8: " + columns[i]

        # only export those entries, that match the filter condition
        # (see above in the field description)
        
        if columns[filter_idx].find(sql_filter_search) >= 0:
            phone1=normalize( columns[phonenr_idx] )
            phone2=normalize( columns[phonenr2_idx] )
            mobile=normalize( columns[mobile_idx] )
            print "BEGIN:VCARD"
            print "VERSION:3.0"
            print "REV:" + time.strftime ('%Y-%m-%dT%H:%M:%SZ', time.gmtime())
            print "UID:sql-" + str(linenr)
            print "FN:" + columns[name_idx] + " " + columns[vorname_idx]
            print "TEL;TYPE=HOME;TYPE=pref:" + phone1
            if phone2 != '' and phone2 != 'NULL':
                print "TEL;TYPE=WORK:" + phone2
            if mobile != '' and mobile != 'NULL':
                print "TEL;TYPE=CELL:" + mobile
            print "END:VCARD"

    linenr = linenr + 1


            
        


More information about the community mailing list