Today I figured out how to use this Gmail Python Library to fetch Gmail data for my ITP listserv visualization. First up, I installed the Gmail Python library by cloning the GitHub repo to a folder on my local computer.
|
git clone git://github.com/charlierguo/gmail.git |
In Terminal, I changed directories so I was in the directory where I saved the library.
|
cd /Users/michellechandra/rwet/gmail |
I then installed the GMail Python library:
And opened the Python Interpreter in Terminal:
and ran these commands to log-in to my Gmail Account:
|
g = gmail.login('username'. 'password') |
This command accesses messages labeled “ITP” in my Inbox:
I saved these messages in a variable called messages and loaded all of them at once using prefetch=True. This took a minute (given the # of messages!)
|
messages = g.mailbox('ITP').mail(prefetch=True) |
I then printed to Terminal the subject line, sender and thread ID number of each message:
|
for message in messages: print message.subject + ", " + message.fr + ", " + message.thread_id |
Messages that are in the same thread have the same thread id #! Example:
Re: high end gym pass, Allison Burtch <XXXXXXX@XXXXXXX.net>, 1462863563962904214
Re: high end gym pass, jon Wasserman <XXXXXXXXX@gmail.com>, 1462863563962904214
I am also interested in visualizing who I have emailed at ITP during the same timeframe as a comparison data visualization to the conversations happening on the list-serv. First up, I added the datetime Python module:
To access my sent messages delivered after September 3, 2013, I ran this command:
|
messages = g.sent_mail().mail(after=datetime.date(2013, 9, 3), prefetch=True) |
And printed out to Terminal who I have emailed using this loop:
|
for message in messages: print message.to |
However, I was not able to access my NYU emails using this library, this is probably a login authentication issue with the library given Gmail hosts NYU emails.
All in all though, I am really excited that it has been relatively easy to get my Gmail email data using this library, and highly recommend using it!