Python Analyzing your Gmail with Matplotlib

jieforest發表於2012-07-09
Lately, I read this post about using Mathematica to analyze a Gmail account. I found it very interesting and I worked a with imaplib and matplotlib to create two of the graph they showed:

•        A diurnal plot, which shows the date and time each email was sent (or received), with years running along the x axis and times of day on the y axis.

•        And a daily distribution histogram, which represents the distribution of emails sent by time of day.

In order to plot those graphs I created three functions. The first one, retrieve the headers of the emails we want to analyze:

CODE:

from imaplib import IMAP4_SSL
from datetime import date,timedelta,datetime
from time import mktime
from email.utils import parsedate
from pylab import plot_date,show,xticks,date2num
from pylab import figure,hist,num2date
from matplotlib.dates import DateFormatter

def getHeaders(address,password,folder,d):
""" retrieve the headers of the emails
     from d days ago until now """
# imap connection
mail = IMAP4_SSL('imap.gmail.com')
mail.login(address,password)
mail.select(folder)
# retrieving the uids
interval = (date.today() - timedelta(d)).strftime("%d-%b-%Y")
result, data = mail.uid('search', None,
                      '(SENTSINCE {date})'.format(date=interval))
# retrieving the headers
result, data = mail.uid('fetch', data[0].replace(' ',','),
                         '(BODY[HEADER.FIELDS (DATE)])')
mail.close()
mail.logout()
return data

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-734953/,如需轉載,請註明出處,否則將追究法律責任。

相關文章