#!/usr/bin/env python

import jppy
import sys

if len(sys.argv) < 2:
    print "No query string. Please try again with a query string!"
    sys.exit(10)
    
term = sys.argv[1]

ab = jppy.addressBook()

#email = ab.getPhoneLabels().index('E-mail')
email = 4

results = []
for contact in ab.records(search=term):
    for phone, label in contact['phones_with_labels']:
        if label == email and phone:
            results.append("%s\t%s %s\t%s" % (phone,
                                              contact['lastname'],
                                              contact['firstname'],
                                              contact['company']))
                           
print "Found %d records." % len(results)
print "\n".join(results)

    
