Created
March 28, 2017 19:24
-
-
Save jay0lee/2bfa451e1f79354f450ff5f1a757e743 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
__author__ = 'Jay Lee' | |
import argparse | |
import sys | |
import gdata.gauth | |
import gdata.contacts.client | |
import gdata.contacts.data | |
import oauth2client.service_account | |
import httplib2 | |
SOURCE_APP_NAME = 'List Contact Emails Python Script - %s' % __author__ | |
def SetupOptionParser(argv): | |
parser = argparse.ArgumentParser(add_help=True) | |
parser.add_argument('--email', | |
dest='email', | |
help='Full email address of user') | |
parser.add_argument('--debug', | |
action='store_true', | |
dest='debug', | |
help='Turn on verbose debugging and connection information (troubleshooting)') | |
parser.add_argument('--doit', | |
action='store_true', | |
dest='doit', | |
help='Actually perform contact cleanup. Default just says what we would do') | |
return parser.parse_args(argv) | |
class TokenFromOAuth2Creds: | |
def __init__(self, creds): | |
self.creds = creds | |
def modify_request(self, req): | |
if self.creds.access_token_expired or not self.creds.access_token: | |
self.creds.refresh(httplib2.Http()) | |
self.creds.apply(req.headers) | |
def ListContacts(client): | |
start_index = 1 | |
get_more = True | |
batch_uri = client.GetFeedUri(projection='full/batch') | |
batch_size = 0 | |
batch_request = gdata.contacts.data.ContactsFeed() | |
while get_more: | |
query = gdata.contacts.client.ContactsQuery() | |
query.max_results = 500 | |
query.start_index = start_index | |
feed = client.GetContacts(q=query) | |
num_contacts = len(feed.entry) | |
print '%s Contacts' % num_contacts | |
contact_num = 1 | |
for entry in feed.entry: | |
for email in entry.email: | |
print email.address | |
contact_num += 1 | |
if feed.get_next_link() != None: | |
start_index += 500 | |
else: | |
get_more = False | |
def getSACreds(): | |
credentials = oauth2client.service_account.ServiceAccountCredentials.from_json_keyfile_name('oauth2service.json', 'http://www.google.com/m8/feeds/') | |
credentials = credentials.create_delegated(options.email) | |
credentials.user_agent = SOURCE_APP_NAME | |
return credentials | |
def main(argv): | |
global options | |
options = SetupOptionParser(argv) | |
if not options.email: | |
print u'ERROR: --email <email> is a required argument' | |
sys.exit(3) | |
credentials = getSACreds() | |
contacts_client = gdata.contacts.client.ContactsClient(source=SOURCE_APP_NAME) | |
contacts_client.ssl = True | |
contacts_client.http_client.debug = options.debug | |
contacts_client.auth_token = TokenFromOAuth2Creds(credentials) | |
ListContacts(contacts_client) | |
if __name__ == '__main__': | |
try: | |
main(sys.argv[1:]) | |
except KeyboardInterrupt: | |
sys.exit(4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment