Finally, one Contact List to rule them all
July 29th, 2007 admin
I have a Yahoo email account. I have a Google email account. I have a few more that I do not use much. Each email application or service provider typically has an Address Book or Contacts List, which we can use to list the names, email addresses and other information about people. For a while, I have been thinking about getting the contact lists organized. There were several layers to the word “organized” and I was apprehensive about starting to peel those layers. The first layer of the problem was figuring out which mail server I wanted to stick to. The next thought was to create a superset of all the contact lists, currently scattered across applications and mail servers, at one place. The next issue was to find a way to update the contact list quickly, rather than clicking around a web-based Address Book or Contact List applications such as the one provided by Yahoo and Google Mail. Then there was the hope that I could keep a copy of the contact list locally on my personal computer, in case, at some point, I did not have internet access to get to the Yahoo Address Book.
This list of requirements seemed formidable in itself, yet, what made me skeptical of a final solution, was one last requirement I had. I had maintained a list of birthdays and anniversaries in a text file separate from the contact lists in the mail servers I mentioned. It was a simple text file and a simple Perl script I wrote could go through this text file everyday and send me an email if it found any upcoming event. I wanted to retain the ability to do such scripting and not have to maintain a separate text file version of the contact list, just for the purposes of being able to run such a reminder script.
After collecting and formulating these thoughts over a long time, I finally spent a few minutes last week looking for a solution to the multi-layered problem. Searching on the internet revealed that there WAS a relatively easy solution that fixes ALL the above problems, including giving me the ability to run a simple script to extract birthday and anniversary information! Here is the solution. Yahoo and Google Address Books allow the existing contacts-list to be exported as a CSV (Comma Separated Variable) file, or a CSV file to be imported to populate the Contact List or Address Book application. A CSV file, as the name suggests, is just a regular text file, with many fields belonging to a record typed across a single line, with the comma symbol (”,”) separating the fields. A new record starts in a new line. The file can be opened with a regular text-editor such as Notepad, Wordpad or Textpad in Windows and vi, pico and emacs in Unix. The file may also be opened using Microsoft Excel spread sheet and the fields show up in separate column and the lines show up in separate rows. This solves the problem of easily modifying the contact list in bulk and storing the contact list as a local file on your personal computer. The CSV file is compatible across Yahoo and Google, and probably across many other applications like Microsoft Outlook and Orkut (web-based networking application). The CSV file can then be imported into Yahoo Mail, Google Mail or other such applications. Problem solved. Single contact list. Storable and updateable locally. Uploadable to multiple web-based servers.
The CSV file based common contact list also allowed me to enter the anniversary and birthday in appropriate columns. I wrote a script called contact.py in the Python scripting language to read the contact list file as a simple text file (in the CSV format) and search for upcoming events. This allowed me to get rid of the earlier text file I had my Perl script read. The CSV file, I called it contactlist.csv, was truly the one file I needed to retain for all my address-book related needs. Whenever I want to add a new contact or update information about an existing contact, I update the local copy of the contact list, contactlist.csv, and then import it into Yahoo Mail and Google Mail to keep them up to date. I have noticed that before I import the latest contactlist.csv file into Yahoo or Google, I need to delete all the existing contacts from Yahoo and Google, respectively. Once, we have an empty contact list on the mail server, the importing of contactlist.csv recreates the complete list. Not starting with an empty contact list on the mail servers, creates duplicates, probably because the “import” function is not smart enough to recognize duplicates.
Here is an example of what a few rows from the CSV file contactlist.csv looks like. It gives us idea of what the fields are. The example also shows that all the fields in a CSV file need not be filled. A field can be left empty if we do not know the information relating to that field for a given contact. Also, I use xxxx for the year field of a date (such as a birthday or an anniversary date), in case I do not know the year. This is OK because the script that parses this CSV file, called contact.py, and which is shown later, does not use the year field to determine if an anniversary is approaching. It only uses the day and month parts of the field.
First,Middle,Last,Nickname,Email,Messenger ID,Home,Work,Pager,Fax,Mobile,Other,Yahoo! Phone,Alternate Email 1,Alternate Email 2,Personal Website,Business Website,Title,Company,Work Address,Work City,Work State,Work ZIP,Work Country,Home Address,Home City,Home State,Home ZIP,Home Country,Birthday,Anniversary,Custom 1,Custom 2,Custom 3,Custom 4,Comments,Messenger ID1,Messenger ID2,Messenger ID3,Messenger ID4,Messenger ID5,Messenger ID6,Messenger ID7,Messenger ID8,Messenger ID9,Skype ID,IRC ID,ICQ ID,Google ID,MSN ID,AIM ID,QQ ID
Shahrukh,Mayur,Khan,srk,srk@bollywood.com,,,,,,,,,,,,,,,,,,,,,,,,,1/2/xxxx,,,,,,,,,,,,,,,,,,,,,,
Here is the contact.py Python script which then works on the CSV file called contactlist.csv with contents as shown above, and sends email to your email account. You might have to appropriately fix some of the fields in the script to get it to work. I present it here just as a hint.
import csv, datetime, re
from string import split
filename = “contactlist.csv”
warnZone = 8 #number of days before which email reminder should be sent
daysInMonth = [’31′,’28′,’31′,’30′,’31′,’30′,’31′,’31′,’30′,’31′,’30′,’31′];
def dayOfYear(month, day):
#print “%s %s” %(month, day)
doy = 0
for n in range(0,int(month)):
if(n == int(month)-1):
doy = doy+int(day)
return doy
else:
doy = doy+int(daysInMonth[n])
now = datetime.datetime.now()
today_month = now.strftime(”%m”)
today_day = now.strftime(”%d”)
today_doy = dayOfYear(today_month, today_day)
#print “%s %s %s” %(today_month, today_day, today_doy)
reader = csv.reader(open(filename))
content = “”
for row in reader:
firstname = (row[0])
middlename = (row[1])
lastname = (row[2])
anniversary = (row[30])
birthday = (row[29])
anni_split = anniversary.split(’/')
bday_split = birthday.split(’/')
#print “len anni_split %s” %(len(anni_split))
#print “len bday_split %s” %(len(bday_split))
if(len(anni_split)>1): #keeps “a/b/c, gets rid of “A”, as in 1st row
anni_month = anni_split[0]
anni_day = anni_split[1]
anni_doy = dayOfYear(anni_month, anni_day)
diff = anni_doy - today_doy
if((anni_doy >= today_doy and anni_doy <= today_doy + warnZone) or (anni_doy <= today_doy + warnZone - 365)):
# print “%s %s %s’s anniversary is on %s/%s” %(firstname, middlename, lastname, anni_month, anni_day)
content += firstname+” “+middlename+” “+lastname+”\’s anniversary is on “+anni_month+” “+anni_day+”\n”
if(len(bday_split)>1): #keeps “a/b/c, gets rid of “A”, as in 1st row
bday_month = bday_split[0]
bday_day = bday_split[1]
bday_doy = dayOfYear(bday_month, bday_day)
diff = bday_doy - today_doy
if((bday_doy >= today_doy and bday_doy <= today_doy + warnZone) or (bday_doy <= today_doy + warnZone - 365)):
# print “%s %s %s’s birthday is on %s/%s” %(firstname, middlename, lastname, anni_month, anni_day)
content += firstname+” “+middlename+” “+lastname+”\’s birthday is on “+bday_month+” “+bday_day+”\n”
#print “%s” %(content)
import smtplib
smtpserver = ‘mailserver.department.company.com’
AUTHREQUIRED = 0
RECIPIENTS = [’gol345die@gmail.com’]
SENDER = [’con789vey@po.doc.com’]
session = smtplib.SMTP(smtpserver)
smtpresult = session.sendmail(SENDER, RECIPIENTS, content)
if smtpresult:
errstr = “”
for recip in smtpresult.keys():
errstr = “”"Could not deliver mail to : %s Server said: %s %s %s”"” % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
raise smtplib.SMTPException, errstr
Posted in Information, Tutorials | 2 Comments »