123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #!/usr/bin/python2
- #
- # pbrisbin 2014 - access Google Contacts for mutt's query_command.
- #
- # Originally script by Jim Karsten <jimkarsten@gmail.com>. Modified by Pat
- # Brisbin <pbrisbin@gmail.com> to do a lot less and handle arguments
- # differently.
- #
- # Usage:
- #
- # gcontacts <account> <password-command> <keyword>, ...
- #
- # Example Mutt Usage:
- #
- # set query_command = "echo; gcontacts foo@gmail.com 'echo SuperSecret' '%s'"
- #
- ###
- import re
- import sys
- import subprocess
-
- import gdata.contacts
- import gdata.contacts.service
-
- P_REL = re.compile(r'^http://schemas.google.com/g/2005#(.*)$')
-
- class Contacts():
- def __init__(self, gd_client):
- self.gd_client = gd_client
- self.query = gdata.contacts.service.ContactsQuery()
- self.query.max_results = 999999
- self.feed = gd_client.GetContactsFeed(self.query.ToUri())
- self.contacts = []
- self.filtered_contacts = []
-
- def get(self):
- for entry in self.feed.entry:
- contact = Contact(entry)
- self.contacts.append(contact)
-
- def filter(self, keyword):
- for contact in self.contacts:
- if contact.is_match(keyword=keyword):
- self.filtered_contacts.append(contact)
-
- def print_contacts(self):
- printable_item = []
-
- for contact in self.filtered_contacts:
- emails = contact.emails
-
- if not emails:
- emails = [{'address': 'n/a', 'rel':'n/a'}]
- for email_addr in emails:
- sortable_name = contact.name or contact.organization or ''
- c = {'email': email_addr['address'], 'name': sortable_name,
- 'other_info': email_addr['rel'], 'contact': contact}
- printable_item.append(c)
-
- cmp_fn = lambda x, y: cmp(x['name'].lower(), y['name'].lower())
-
- for count, printable in enumerate(sorted(printable_item, cmp=cmp_fn)):
- contact = printable['contact']
-
- print "{email}\t{name}\t{info}".format(
- email=printable['email'], name=contact.fullname,
- info=printable['other_info'])
-
- class Contact():
- def __init__(self, entry):
- self.entry = entry
- self.name = self.entry.title.text or None
- self.organization = None
-
- if self.entry.organization and self.entry.organization.org_name:
- self.organization = self.entry.organization.org_name.text or None
-
- self.parse_emails()
- self.set_fullname()
-
- def is_match(self, keyword):
- match = False
-
- for email_addr in self.emails:
- if re.search(keyword, email_addr['address'], re.IGNORECASE):
- match = True
- if self.name:
- if re.search(keyword, self.name, re.IGNORECASE):
- match = True
- if self.organization:
- if re.search(keyword, self.organization, re.IGNORECASE):
- match = True
-
- return match
-
- def parse_emails(self):
- self.emails = []
-
- for email_addr in self.entry.email:
- rel = 'other'
- if email_addr.rel:
- match = P_REL.match(email_addr.rel)
- if match:
- rel = match.group(1)
- self.emails.append({'address': email_addr.address, 'rel': rel})
-
- def set_fullname(self):
- if self.name and self.organization:
- self.fullname = "{name} ({org})".format(name=self.name,
- org=self.organization)
- elif self.organization:
- self.fullname = "{org}".format(org=self.organization)
- elif self.name:
- self.fullname = "{name}".format(name=self.name)
- else:
- self.fullname = 'n/a'
-
- if __name__ == '__main__':
- if len(sys.argv) < 4:
- print >> sys.stderr, "usage: gcontacts <email> <password-command> <keyword>, ..."
- quit(64)
-
- email, command = sys.argv[1:3]
- keyword = ' '.join(sys.argv[3:])
- password = subprocess.check_output(command.split(' '))
-
- gd_client = gdata.contacts.service.ContactsService()
- gd_client.email = email
- gd_client.password = password
- gd_client.source = 'dm-contacts-1'
- gd_client.ProgrammaticLogin()
-
- contact_set = Contacts(gd_client)
- contact_set.get()
- contact_set.filter(keyword)
- contact_set.print_contacts()
|