email_stats.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/python
  2. #
  3. # email_stats.py
  4. #
  5. # Time vs date plot for an email mbox file.
  6. #
  7. # Copyright (C) 2015 George C. Privon
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. import mailbox
  22. import datetime
  23. import matplotlib.pyplot as plt
  24. import matplotlib.dates as mdates
  25. import argparse
  26. parser = argparse.ArgumentParser()
  27. parser.add_argument('mbox', help='Mailbox to analyze.')
  28. parser.add_argument('--plotfile', '-p', default=False, action='store',
  29. help='Name of output plotting file.')
  30. parser.add_argument('--title', '-t', default='Email Send Times',
  31. action='store', help='Plot title.')
  32. args = parser.parse_args()
  33. plt.figure()
  34. plt.ylim([0, 24])
  35. plt.ylabel('Hour')
  36. plt.xlabel('Date')
  37. plt.minorticks_on()
  38. plt.title(args.title)
  39. a = mailbox.mbox(args.mbox)
  40. for msg in a:
  41. if msg['date'] is not None:
  42. try:
  43. z = datetime.datetime.strptime(msg['date'], '%a, %d %b %Y %H:%M:%S %z')
  44. except ValueError:
  45. try:
  46. z = datetime.datetime.strptime(msg['date'], '%a, %d %b %Y %H:%M:%S %Z')
  47. except ValueError:
  48. print("Skipping message from " + msg['date'])
  49. continue
  50. plt.plot_date(z.date(),
  51. z.hour + z.minute/60.,
  52. fmt='r.',
  53. # tz=z.tzname(),
  54. xdate=True)
  55. if args.plotfile:
  56. plt.savefig(args.plotfile)
  57. else:
  58. plt.savefig('email_times.png')