Chapter 7 — Gmail Libraries 133
getMessagesByFolder
The getMessagesByFoldermethod takes the name of the folder, and an
optional True/False flag to indicate selecting every page of that folder’s listing.
(Remember that these libraries interact with Gmail by scraping the pages it
returns, effectively, so you still have to consider the information as it is meant for
the real Gmail interface, not just yours).
Leaving the flag off sets it to the default False. To place the details of the Inbox
into an object called folder, you do the following:
folder= ga.getMessagesByFolder(‘inbox’)
This returns a GmailSearchResultinstance that you can query.
getMessageByLabel
The getMessageByLabelmethod works in exactly the same way as
getMessagesByFolderbut replaces the folder with a label. It returns a
GmailSearchResultinstance, which is examined in two paragraphs’ time.
getMessagesByQuery
The getMessagesByQuerymethod works in exactly the same way as
getMessagesByFolderbut does so with a search query instead of the name of
the mailbox. For example:
messages = ga.getMessagesByQuery(‘ransom note’)
This query will also return a GmailSearchResultinstance.
All this talk of GmailSearchResultinstances begs the question: What exactly is
a GmailSearchResultinstance? A GmailSearchResultinstance is a thread
object. This contains details of the thread, plus one or more msgobjects, corre-
sponding to the messages within. These can be queried like so:
for thread in folder:
print thread.id # the id of the thread
print len(thread) # the number of messages
print thread.subject # the subject of the thread
for msg in thread:
print msg.id # the id of the message
print msg.number # the number within the thread
print msg.subject # the message subject
print msg.source # the raw source of the message