|
@@ -1,73 +1,68 @@
|
|
|
-"""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
|
|
|
+ """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:
|
|
|
+ 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,
|
|
|
+ """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'])
|
|
|
+ messages.extend(response['messages'])
|
|
|
|
|
|
- return messages
|
|
|
- except errors.HttpError, error:
|
|
|
- print 'An error occurred: %s' % error
|
|
|
+ return messages
|
|
|
+ except errors.HttpError:
|
|
|
+ print('An error occurred: %s' % error)
|
|
|
|
|
|
|
|
|
|
|
@@ -95,8 +90,8 @@ def GetMessage(service, user_id, msg_id):
|
|
|
|
|
|
|
|
|
return message
|
|
|
- except errors.HttpError, error:
|
|
|
- print 'An error occurred: %s' % error
|
|
|
+ except errors.HttpError:
|
|
|
+ print('An error occurred: %s' % error)
|
|
|
|
|
|
def get_message_header(message):
|
|
|
|
|
@@ -134,11 +129,13 @@ def GetMimeMessage(service, user_id, msg_id):
|
|
|
|
|
|
msg_str = base64.urlsafe_b64decode(message['raw'].encode('ASCII'))
|
|
|
|
|
|
- mime_msg = email.message_from_string(msg_str)
|
|
|
-
|
|
|
+ print()
|
|
|
+ print(type(msg_str))
|
|
|
+ mime_msg = email.message_from_string(msg_str.decode())
|
|
|
+
|
|
|
return mime_msg
|
|
|
- except errors.HttpError, error:
|
|
|
- print 'An error occurred: %s' % error
|
|
|
+ except errors.HttpError:
|
|
|
+ print('An error occurred: %s' % error)
|
|
|
|
|
|
|
|
|
def get_message_body(mime_msg):
|