Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. import mailbox
  2. from email.utils import parsedate
  3. from dateutil.parser import parse
  4. import itertools
  5. import plotly.plotly as py
  6. from plotly.graph_objs import *
  7. import plotly.tools as tls
  8. import csv
  9. from itertools import izip
  10.  
  11. path = 'mail.mbox'
  12. mbox = mailbox.mbox(path)
  13.  
  14. def extract_date(email):
  15. date = email.get('Date')
  16. return parsedate(date)
  17.  
  18. #sort the email by a given date
  19. sorted_mails = sorted(mbox, key=extract_date)
  20. mbox.update(enumerate(sorted_mails))
  21. mbox.flush()
  22.  
  23. #it finds all the dates within the MBOX and split
  24. all_dates = []
  25. mbox = mailbox.mbox(path)
  26. for message in mbox:
  27. all_dates.append( str( parse( message['date'] ) ).split(' ')[0] )
  28.  
  29. #counts the number of emails per given date
  30. email_count = [(g[0], len(list(g[1]))) for g in itertools.groupby(all_dates)]
  31. email_count[0]
  32.  
  33. #makes a list of (x,y)
  34. x = []
  35. y = []
  36. for date, count in email_count:
  37. x.append(date)
  38. y.append(count)
  39.  
  40. #produce a CSV file of X and Y, for plotting
  41. with open('data.csv', 'wb') as f:
  42. writer = csv.writer(f)
  43. writer.writerows(izip(x, y))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement