Skip to content

Instantly share code, notes, and snippets.

@misterwell
Created July 10, 2014 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misterwell/533e02bf520e68465008 to your computer and use it in GitHub Desktop.
Save misterwell/533e02bf520e68465008 to your computer and use it in GitHub Desktop.
Pulling convos with Kate from Gmail
#!/usr/bin/python
import httplib2
from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run
# Path to the client_secret.json file downloaded from the Developer Console
CLIENT_SECRET_FILE = 'client_secret.json'
# Check https://developers.google.com/gmail/api/auth/scopes for all available scopes
OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.readonly'
# Location of the credentials storage file
STORAGE = Storage('gmail.storage')
# Start the OAuth flow to retrieve credentials
flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=OAUTH_SCOPE)
http = httplib2.Http()
# Try to retrieve credentials from storage or run the flow to generate them
credentials = STORAGE.get()
if credentials is None or credentials.invalid:
credentials = run(flow, STORAGE, http=http)
# Authorize the httplib2.Http object with our credentials
http = credentials.authorize(http)
# Build the Gmail service from discovery
gmail_service = build('gmail', 'v1', http=http)
# Retrieve a page of threads
response = gmail_service.users().threads().list(userId='me', q='from:(kate.longcrier@gmail.com) to:(misterwell@gmail.com) label:chats').execute()
threads = []
# Print ID for each thread
if response['threads']:
threads.extend(response['threads'])
print 'threads.len = %s' % threads.__len__()
# print 'Thread ID: %s' % (message['id'])
while response.has_key('nextPageToken'):
print 'Next pageToken = %s' % (response['nextPageToken'])
response = gmail_service.users().threads().list(userId='me', q='from:(kate.longcrier@gmail.com) to:(misterwell@gmail.com) label:chats', pageToken=response['nextPageToken']).execute()
if response['threads']:
print '# in page: %s' % response['threads'].__len__()
threads.extend(response['threads'])
print 'threads.len = %s' % threads.__len__()
print 'threads = %s' % threads.__len__()
idx = 0
for i in range(10):
thread = gmail_service.users().threads().get(userId='me', id=threads[i]['id']).execute()
print 'thread {0} = {1}'.format(i, thread['snippet'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment