In UMA, we have used Java ME and Java to manipulate the contacts in Symbian and Android phones. However, today we wanted to prototype some app, and we thought Python was the best way to go. (some people see vested interests in me doing this :D)
Looks like it is non-trivial to access contacts in Python. I'll quickly share what I learnt
ContactsDB is the database of contacts. This is obtained by calling open() function from contacts module.
>>> import contacts
>>> cdb = contacts.open()
By not giving any argument to open() we open the default contacts list (the one you want to manipulate most likely)
Now, to access contacts from the cdb, you can do the following
>>> ckeys = cdb.keys()
>>> for i in ckeys:
>>> c = ckeys[k]
Contacts are stored in ContactsDB like a dictionary of type
Now, comes the question of accessing the fields inside a contact. For this, we need to understand how a Contact is constructed - It is constructed as a list of ContactField type. To access say first_name, do:
>>> names = c.find('first_name') #names is a list now
>>> name = names[0] # There is only one first name :D
>>> print name.value # This would print the first name
Make sure you check if len(names) > 0, because sometimes this field may not be present in the contact
find() returns the list of ContactField type. This is because, you can access mobile number by
>>> mobile_nos = c.find('mobile_number')
>>> for m in mobile_nos: print m.value
This would print the list of all mobile numbers for Contact - c. Just that the same interface is used for first name and last name.
More info on:
1. ContactsDB
2. Contact
3. ContactField
Btw, do you guys use the Bluetooth console from PC onto the Python on the phone - It is quite cool. Probably, I'll write a short note on how to get that going in my next post.
Thx for this interesting article :)
ReplyDeleteseShadri