Migrate contacts from C510 to HTC

I order to migrate contacts from a Sony Ericsson C510 to a HTC Desire you first have to export them in an usable format. Wammu recognizes this phone and exports a .vcf file containing all contacts. However there are some major limitations to this process:

  • entries which contains multiple phone numbers are exported separately instead of a single vcf. For an entry named “me” containing a mobile and work phone the exported file contains a: me/M and me/W contact
BEGIN:VCARD
VERSION:2.1
X-GAMMU-LOCATION:1
X-GAMMU-MEMORY:ME
TEL;PREF:012345
N:me/M
END:VCARD

BEGIN:VCARD
VERSION:2.1
X-GAMMU-LOCATION:2
X-GAMMU-MEMORY:ME
TEL;PREF:0123456
N:me/W
END:VCARD
  • entries from the sim card and phone memory are duplicated but in the ones from the sim card the names are truncated even if the phones are identical
BEGIN:VCARD
VERSION:2.1
X-GAMMU-LOCATION:12
X-GAMMU-MEMORY:ME
TEL;PREF:0031234567
N:Antonio Potter/W
END:VCARD

BEGIN:VCARD
VERSION:2.1
X-GAMMU-LOCATION:9
X-GAMMU-MEMORY:SM
TEL;PREF:0031234567
N:Antonio Po/W
END:VCARD

This script I wrote in a hurry to address these issues and unify contacts. It uses the vobject library installable on ubuntu from the python-vobject package. It’s only a 15 minutes work so bear with its deficiencies or extend it otherwise.

#!/usr/bin/env python
# Copyright (C) 2010 Marilen Corciovei
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
 
import os, sys, vobject
 
cards = {}
names = []
 
def clean(fileName):
    vFile = open(fileName, 'r')
    line = vFile.readline()
    vCardData = ''
    while line:
        vCardData += line
        if line.startswith('END'):
            parseVCard(vCardData)
            vCardData = ''
        line = vFile.readline()
 
    cnt = 0
    for k, v in cards.items():
        newcard(k, v)
        cnt += 1
    #print 'Cleaned: %s' % cnt
 
def parseVCard(vCard):        
    card = vobject.readOne(vCard)
    name = str(card.n.value).strip()
    tel = str(card.tel.value).strip()
    type = None
    if name.endswith('/M'):
        name = name[:-2]
        type = 'CELL'
    if name.endswith('/W'):
        name = name[:-2]
        type = 'WORK'
    if name.endswith('/H'):
        name = name[:-2]
        type = 'HOME'
    if name.endswith('/O'):
        name = name[:-2]
        type = 'OFFICE'
 
    duplicated = False
    #based on the fact that simm entries are at the end
    for n in names:
        if len(n) != len(name) and n.find(name) == 0:
            #print 'duplicated %s, %s' %(n, name)
            for i in cards[n]:
                if i[1] == tel:
                    duplicated = True
                    #print 'Duplicated phone'
    if name not in names:
        names.append(name)
 
    if not duplicated: 
        if cards.has_key(name):
            cards[name].append([type, tel])
        else:
            cards[name] = [[type, tel]]
 
    #card.prettyPrint()
 
def newcard(name, tels):
    namesplit = name.split(' ')
    ts = []
    newcard = """BEGIN:VCARD
VERSION:2.1
"""
    n = namesplit[-1] + ';' + ' '.join(namesplit[0:-1]) + ';;;'
    newcard += "N:%s\n" % n        
    newcard += "FN:%s\n" % name
    for tel in tels:
        if tel[1] not in ts:
            newcard += "TEL"
            if tel[0]:
                newcard += ";" + tel[0]
            newcard += ";PREF:" + tel[1] + "\n" 
            ts.append(tel[1])
    newcard += "END:VCARD\n"
    print newcard
 
if __name__=='__main__':
    clean(sys.argv[1])

Once the .vcf file has been cleaned and unified you will end-up with a file which you can copy on the sdcard of the HTC and import it.

Leave a Reply

*