Browse Source

Gmail message search

Shendan JIN 8 years ago
parent
commit
a389871081
2 changed files with 262 additions and 0 deletions
  1. 102 0
      gmail.py
  2. 160 0
      gmail_msg.py

+ 102 - 0
gmail.py

@@ -0,0 +1,102 @@
+
+from __future__ import print_function
+import httplib2
+import os
+
+from apiclient import discovery
+import oauth2client
+from oauth2client import client
+from oauth2client import tools
+from gmail_msg import *
+try:
+    import argparse
+    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
+except ImportError:
+    flags = None
+
+# If modifying these scopes, delete your previously saved credentials
+# at ~/.credentials/gmail-python-quickstart.json
+SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
+CLIENT_SECRET_FILE = 'client_secret.json'
+APPLICATION_NAME = 'Gmail API Python Quickstart111'
+
+
+def get_credentials():
+    """Gets valid user credentials from storage.
+
+    If nothing has been stored, or if the stored credentials are invalid,
+    the OAuth2 flow is completed to obtain the new credentials.
+
+    Returns:
+        Credentials, the obtained credential.
+    """
+    home_dir = os.path.expanduser('~')
+    credential_dir = os.path.join(home_dir, '.credentials')
+    if not os.path.exists(credential_dir):
+        os.makedirs(credential_dir)
+    credential_path = os.path.join(credential_dir,
+                                   'gmail-python-quickstart.json')
+
+    store = oauth2client.file.Storage(credential_path)
+    credentials = store.get()
+    if not credentials or credentials.invalid:
+        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
+        flow.user_agent = APPLICATION_NAME
+        if flags:
+            credentials = tools.run_flow(flow, store, flags)
+        else: # Needed only for compatibility with Python 2.6
+            credentials = tools.run(flow, store)
+        print('Storing credentials to ' + credential_path)
+    return credentials
+
+def search(service, labels):
+    while True:
+        keyword = raw_input('Please enter a keyword to start searching the related messages in the mailbox(ctrl+c for quit) : ').lower()
+
+        for i in range(3):
+            print()
+        
+        #print('Labels:')
+        for label in labels:
+            if (label['name']=='INBOX'):
+                print(label['name'])        
+                list_msg = ListMessagesMatchingQuery(service,"me", query=keyword)
+                #list_msg = ListMessagesWithLabels(service, "me", label['id'])
+                #print(list_msg)
+                i = 0
+                for msg in list_msg:
+                    i = i + 1
+                    print()
+                    print('Message #%d:'% i)
+                    print('----')
+                    msg = GetMessage(service,"me",msg['id'])
+                    get_message_header(msg)
+                    
+                    mime_msg = GetMimeMessage(service,"me",msg['id'])
+                    msg_body = get_message_body(mime_msg)
+                    print('Body:')
+                    print('%s' % msg_body)
+                  
+                    #print(GetMessage(service, "me", msg['id']))
+                    #GetMimeMessage(service,"me",msg['id'])
+        print()
+        
+def main():
+    """
+    Creates a Gmail API service object and outputs a list of messages which contain the keyword in the user's Gmail account.
+   
+    """
+    
+    credentials = get_credentials()
+    http = credentials.authorize(httplib2.Http())
+    service = discovery.build('gmail', 'v1', http=http)
+    results = service.users().labels().list(userId='me').execute()
+    labels = results.get('labels', [])
+    if not labels:
+        print('No labels found.')
+    else:
+        search(service, labels)
+
+if __name__ == '__main__':
+    main()
+

+ 160 - 0
gmail_msg.py

@@ -0,0 +1,160 @@
+"""Get a list of Messages from the user's mailbox.
+"""
+
+from apiclient import errors
+
+
+def ListMessagesMatchingQuery(service, user_id, query=''):
+  """List all Messages of the user's mailbox matching the query.
+
+  Args:
+    service: Authorized Gmail API service instance.
+    user_id: User's email address. The special value "me"
+    can be used to indicate the authenticated user.
+    query: String used to filter messages returned.
+    Eg.- 'from:user@some_domain.com' for Messages from a particular sender.
+
+  Returns:
+    List of Messages that match the criteria of the query. Note that the
+    returned list contains Message IDs, you must use get with the
+    appropriate ID to get the details of a Message.
+  """
+  try:
+    response = service.users().messages().list(userId=user_id,
+                                               q=query).execute()
+    messages = []
+    if 'messages' in response:
+      messages.extend(response['messages'])
+
+    while 'nextPageToken' in response:
+      page_token = response['nextPageToken']
+      response = service.users().messages().list(userId=user_id, q=query,
+                                         pageToken=page_token).execute()
+      messages.extend(response['messages'])
+
+    return messages
+  except errors.HttpError, error:
+    print 'An error occurred: %s' % error
+
+
+def ListMessagesWithLabels(service, user_id, label_ids=[]):
+  """List all Messages of the user's mailbox with label_ids applied.
+
+  Args:
+    service: Authorized Gmail API service instance.
+    user_id: User's email address. The special value "me"
+    can be used to indicate the authenticated user.
+    label_ids: Only return Messages with these labelIds applied.
+
+  Returns:
+    List of Messages that have all required Labels applied. Note that the
+    returned list contains Message IDs, you must use get with the
+    appropriate id to get the details of a Message.
+  """
+  try:
+    response = service.users().messages().list(userId=user_id,
+                                               labelIds=label_ids).execute()
+    messages = []
+    if 'messages' in response:
+      messages.extend(response['messages'])
+
+    while 'nextPageToken' in response:
+      page_token = response['nextPageToken']
+      response = service.users().messages().list(userId=user_id,
+                                                 labelIds=label_ids,
+                                                 pageToken=page_token).execute()
+      messages.extend(response['messages'])
+
+    return messages
+  except errors.HttpError, error:
+    print 'An error occurred: %s' % error
+
+
+
+"""Get Message with given ID.
+"""
+
+import base64
+import email
+from apiclient import errors
+
+def GetMessage(service, user_id, msg_id):
+    """Get a Message with given ID.
+
+    Args:
+        service: Authorized Gmail API service instance.
+        user_id: User's email address. The special value "me"
+        can be used to indicate the authenticated user.
+        msg_id: The ID of the Message required.
+
+    Returns:
+        A Message.
+    """
+    try:
+        message = service.users().messages().get(userId=user_id, id=msg_id).execute()
+      
+            
+        return message
+    except errors.HttpError, error:
+        print 'An error occurred: %s' % error
+
+def get_message_header(message):
+   
+    #print 'Message snippet: %s' % message['snippet']
+    headers = message['payload']['headers']
+    for e in headers:
+        if (e['name']=='From'):
+            print 'Receive from: %s' % e['value']
+        if (e['name']=='Subject'):
+            print 'Subject: %s' % e['value']
+        if (e['name']=='Date'):
+            print 'Date: %s' % e['value']
+
+
+def GetMimeMessage(service, user_id, msg_id):
+    """Get a Message and use it to create a MIME Message.
+
+    Args:
+        service: Authorized Gmail API service instance.
+        user_id: User's email address. The special value "me"
+        can be used to indicate the authenticated user.
+        msg_id: The ID of the Message required.
+
+        Returns:
+            A MIME Message, consisting of data from Message.
+    """
+    try:
+        message = service.users().messages().get(userId=user_id, id=msg_id,
+                                             format='raw').execute()
+
+        #print 'Message snippet: %s' % message['snippet']
+
+        msg_str = base64.urlsafe_b64decode(message['raw'].encode('ASCII'))
+
+        mime_msg = email.message_from_string(msg_str)
+
+        return mime_msg
+    except errors.HttpError, error:
+        print 'An error occurred: %s' % error
+
+
+def get_message_body(mime_msg):
+    body = ""
+
+    if mime_msg.is_multipart():
+        for part in mime_msg.walk():
+            ctype = part.get_content_type()
+            cdispo = str(part.get('Content-Disposition'))
+
+            # skip any text/plain (txt) attachments
+            if ctype == 'text/plain' and 'attachment' not in cdispo:
+                body = part.get_payload(decode=True)  # decode
+                break
+    # not multipart - i.e. plain text, no attachments, keeping fingers crossed
+    else:
+        body = mime_msg.get_payload(decode=True)
+
+    #print 'Message body: %s' % body
+    return body
+
+