email_stats.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/python
  2. import mailbox
  3. import datetime
  4. import matplotlib.pyplot as plt
  5. import matplotlib.dates as mdates
  6. import argparse
  7. parser = argparse.ArgumentParser()
  8. parser.add_argument('mbox', help='Mailbox to analyze.')
  9. parser.add_argument('--plotfile', '-p', default=False, action='store',
  10. help='Name of output plotting file.')
  11. parser.add_argument('--title', '-t', default='Email Send Times',
  12. action='store', help='Plot title.')
  13. args = parser.parse_args()
  14. plt.figure()
  15. plt.ylim([0, 24])
  16. plt.ylabel('Hour')
  17. plt.xlabel('Date')
  18. plt.minorticks_on()
  19. plt.title(args.title)
  20. a = mailbox.mbox(args.mbox)
  21. for msg in a:
  22. if msg['date'] is not None:
  23. try:
  24. z = datetime.datetime.strptime(msg['date'], '%a, %d %b %Y %H:%M:%S %z')
  25. except ValueError:
  26. try:
  27. z = datetime.datetime.strptime(msg['date'], '%a, %d %b %Y %H:%M:%S %Z')
  28. except ValueError:
  29. print("Skipping message from " + msg['date'])
  30. continue
  31. plt.plot_date(z.date(),
  32. z.hour + z.minute/60.,
  33. fmt='r.',
  34. # tz=z.tzname(),
  35. xdate=True)
  36. if args.plotfile:
  37. plt.savefig(args.plotfile)
  38. else:
  39. plt.savefig('email_times.png')