Python Analyzing your Gmail with Matplotlib
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:
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
• 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_SSLfrom 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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Restrictions on Analyzing TablesREST
- python包:matplotlibPython
- Analyzing Strings with sscanf
- Python進階 — matplotlibPython
- 【莫煩】Python MatplotlibPython
- Python進階 -- matplotlibPython
- python繪圖之matplotlibPython繪圖
- Python之matplotlib基礎Python
- Gmail程式資訊轉儲分析工具pdgmailAI
- python——Matplotlib學習筆記Python筆記
- 繪圖: Python matplotlib簡介繪圖Python
- python--matplotlib庫使用3Python
- Python中Numpy及Matplotlib使用Python
- Python的Matplotlib庫簡述Python
- Python - Matplotlib and Numpy on Debian/UbuntuPythonUbuntu
- 用gmail.google.com/gmail不能正常訪問AIGo
- Gmail走出BetaAI
- Python matplotlib繪製散點圖Python
- Python 繪相簿 Matplotlib 入門教程Python
- windows下python配置numpy、matplotlib、scipyWindowsPython
- python使用matplotlib繪圖詳解Python繪圖
- Your Prediction Gets As Good As Your DataGo
- Keep Your Hook Script in Source Control: PythonHookPython
- Python資料視覺化matplotlib庫Python視覺化
- Python畫圖——matplotlib(普通折線圖)Python
- 高效使用 Python 視覺化工具 MatplotlibPython視覺化
- Python Matplotlib繪製氣溫圖表Python
- python matplotlib畫圖是設定線寬Python
- [python][科學計算][matplotlib]使用指南Python
- python中Matplotlib是什麼?怎麼用?Python
- Python 利用pandas和matplotlib繪製餅圖Python
- 使用python matplotlib實現動圖繪製Python
- python: matplotlib-繪製精美的圖表Python
- python2.7 + numpy-1.6.1 + matplotlib-1.1.0Python
- Analyzing and Reproducing the EOS Out-of-Bound Write Vulnerability in nodeos
- Your title
- matplotlib
- python實踐系列之(一)安裝 python/pip/numpy/matplotlibPython