RSS

(root)/misc-scripts : /mutt-gcontact.py (revision 75)

Line Revision Contents
1 54
#!/usr/local/bin/python -tt
2
3 55
""" mutt-gcontacts.py (c) 2008 Matthew John Ernisse <mernisse@ub3rgeek.net>
4 54
5
License:
6
        If you find this useful, I'd love to hear from you.
7
8
Redistribution and use in source and binary forms, 
9
with or without modification, are permitted provided 
10
that the following conditions are met:
11
 
12
    * Redistributions of source code must retain the 
13
      above copyright notice, this list of conditions 
14
      and the following disclaimer.
15
    * Redistributions in binary form must reproduce 
16
      the above copyright notice, this list of conditions 
17
      and the following disclaimer in the documentation 
18
      and/or other materials provided with the distribution.
19
20
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
21
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
22
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
23
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
24
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
25
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
26
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 
27
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
28
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
29
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
30
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32
To Use:
33
	add the following to your .muttrc and then you will be able to query
34
	your Gmail contact list from mutt.
35
36 55
	set query_command = "mutt-gcontacts.py -u you@gmail.com -p pass -s '%s'"
37 54
38
Requires:
39
	gdata python bindings with contacts support
40
	atom python module
41
42
	Google Account
43
44 55
$Id: mutt-gcontact.py,v 1.2 2008/12/14 20:07:24 mernisse Exp $
45 54
"""
46
47
import gdata.contacts
48
import gdata.contacts.service
49
import getopt
50
import re
51
import sys
52
53
54
def Usage():
55
	print """Usage: %s -u <username> -p <password> -s <search term>""" % (
56
		sys.argv[0]
57
	)
58
	return 2
59
60
def main():
61
62
	try:
63
		opts, args = getopt.getopt(sys.argv[1:], "u:p:s:")
64
	except getopt.error, e:
65
		print str(e)
66
		return Usage()
67
68
	username = ""
69
	password = ""
70
	search = ""
71
72
	for o, a in opts:
73
		if o == "-u":
74
			username = a
75
		elif o == "-p":
76
			password = a
77
		elif o == "-s":
78
			search = a
79
80
	if not username or \
81
		not password or \
82
		not search:
83
		print "Insufficient Arguments"
84
		return Usage()
85
86
	g = gdata.contacts.service.ContactsService()
87
	g.email = username
88
	g.password = password
89
	g.source = "mernisse@ub3rgeek.net"
90
91
	try:
92
		g.ProgrammaticLogin()
93
	except Exception, e:
94
		print "cannot login to Google Contact Service %s" % (str(e))
95
		return Usage()
96
97
	# fetch the user's contacts.
98
	contacts = []
99
	offset = 1
100
	while True:
101
		q = gdata.contacts.service.ContactsQuery()
102
		q.max_results = 20
103
		q.start_index = offset
104
		c = 0
105
		feed = g.GetContactsFeed(q.ToUri())
106
		for contact in feed.entry:
107
			for email in contact.email:
108 71
				contacts.append(
109
					{ contact.title.text : email.address }
110
				)
111 54
				  
112
			c += 1
113
114
		if c == 20:
115
			offset += 20
116
		else:
117
			break
118
119
	found = None
120
	for contact in contacts:
121
		c = "<%s>	%s" % (contact.values()[0], contact.keys()[0])
122
		if re.search(r'%s' % (search), c, re.I):
123 55
			if not found:
124
				print "Gmail Query Results"
125
				found = True
126
127 54
			print c
128
	
129
	if not found:
130 55
		print "No Contacts Found"
131 54
		return 1
132
133
	return 0
134
135
if __name__ == "__main__":
136
	sys.exit(main())

Loggerhead 1.17 is a web-based interface for Bazaar branches