Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

gcontacts 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/python2
  2. #
  3. # pbrisbin 2014 - access Google Contacts for mutt's query_command.
  4. #
  5. # Originally script by Jim Karsten <jimkarsten@gmail.com>. Modified by Pat
  6. # Brisbin <pbrisbin@gmail.com> to do a lot less and handle arguments
  7. # differently.
  8. #
  9. # Usage:
  10. #
  11. # gcontacts <account> <password-command> <keyword>, ...
  12. #
  13. # Example Mutt Usage:
  14. #
  15. # set query_command = "echo; gcontacts foo@gmail.com 'echo SuperSecret' '%s'"
  16. #
  17. ###
  18. import re
  19. import sys
  20. import subprocess
  21. import gdata.contacts
  22. import gdata.contacts.service
  23. P_REL = re.compile(r'^http://schemas.google.com/g/2005#(.*)$')
  24. class Contacts():
  25. def __init__(self, gd_client):
  26. self.gd_client = gd_client
  27. self.query = gdata.contacts.service.ContactsQuery()
  28. self.query.max_results = 999999
  29. self.feed = gd_client.GetContactsFeed(self.query.ToUri())
  30. self.contacts = []
  31. self.filtered_contacts = []
  32. def get(self):
  33. for entry in self.feed.entry:
  34. contact = Contact(entry)
  35. self.contacts.append(contact)
  36. def filter(self, keyword):
  37. for contact in self.contacts:
  38. if contact.is_match(keyword=keyword):
  39. self.filtered_contacts.append(contact)
  40. def print_contacts(self):
  41. printable_item = []
  42. for contact in self.filtered_contacts:
  43. emails = contact.emails
  44. if not emails:
  45. emails = [{'address': 'n/a', 'rel':'n/a'}]
  46. for email_addr in emails:
  47. sortable_name = contact.name or contact.organization or ''
  48. c = {'email': email_addr['address'], 'name': sortable_name,
  49. 'other_info': email_addr['rel'], 'contact': contact}
  50. printable_item.append(c)
  51. cmp_fn = lambda x, y: cmp(x['name'].lower(), y['name'].lower())
  52. for count, printable in enumerate(sorted(printable_item, cmp=cmp_fn)):
  53. contact = printable['contact']
  54. print "{email}\t{name}\t{info}".format(
  55. email=printable['email'], name=contact.fullname,
  56. info=printable['other_info'])
  57. class Contact():
  58. def __init__(self, entry):
  59. self.entry = entry
  60. self.name = self.entry.title.text or None
  61. self.organization = None
  62. if self.entry.organization and self.entry.organization.org_name:
  63. self.organization = self.entry.organization.org_name.text or None
  64. self.parse_emails()
  65. self.set_fullname()
  66. def is_match(self, keyword):
  67. match = False
  68. for email_addr in self.emails:
  69. if re.search(keyword, email_addr['address'], re.IGNORECASE):
  70. match = True
  71. if self.name:
  72. if re.search(keyword, self.name, re.IGNORECASE):
  73. match = True
  74. if self.organization:
  75. if re.search(keyword, self.organization, re.IGNORECASE):
  76. match = True
  77. return match
  78. def parse_emails(self):
  79. self.emails = []
  80. for email_addr in self.entry.email:
  81. rel = 'other'
  82. if email_addr.rel:
  83. match = P_REL.match(email_addr.rel)
  84. if match:
  85. rel = match.group(1)
  86. self.emails.append({'address': email_addr.address, 'rel': rel})
  87. def set_fullname(self):
  88. if self.name and self.organization:
  89. self.fullname = "{name} ({org})".format(name=self.name,
  90. org=self.organization)
  91. elif self.organization:
  92. self.fullname = "{org}".format(org=self.organization)
  93. elif self.name:
  94. self.fullname = "{name}".format(name=self.name)
  95. else:
  96. self.fullname = 'n/a'
  97. if __name__ == '__main__':
  98. if len(sys.argv) < 4:
  99. print >> sys.stderr, "usage: gcontacts <email> <password-command> <keyword>, ..."
  100. quit(64)
  101. email, command = sys.argv[1:3]
  102. keyword = ' '.join(sys.argv[3:])
  103. password = subprocess.check_output(command.split(' '))
  104. gd_client = gdata.contacts.service.ContactsService()
  105. gd_client.email = email
  106. gd_client.password = password
  107. gd_client.source = 'dm-contacts-1'
  108. gd_client.ProgrammaticLogin()
  109. contact_set = Contacts(gd_client)
  110. contact_set.get()
  111. contact_set.filter(keyword)
  112. contact_set.print_contacts()