email_destinations.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/python
  2. #
  3. # email_destinations.py
  4. #
  5. # Emails sent to specified domains as a function of time.
  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. import re
  27. import numpy as np
  28. import cubehelix
  29. parser = argparse.ArgumentParser()
  30. parser.add_argument('mbox', help='Mailbox to analyze.')
  31. parser.add_argument('--plotfile', '-p', default=False, action='store',
  32. help='Name of output plotting file.')
  33. parser.add_argument('--title', '-t', default='Email Destinations',
  34. action='store', help='Plot title.')
  35. args = parser.parse_args()
  36. plt.figure()
  37. plt.ylabel('Emails sent to Domain')
  38. plt.xlabel('Year-Month')
  39. plt.minorticks_on()
  40. plt.title(args.title)
  41. a = mailbox.mbox(args.mbox)
  42. scolor = cubehelix.cmap(startHue=240, endHue=-300,
  43. minSat=1, maxSat=2.5,
  44. minLight=.3, maxLight=.8,
  45. gamma=.9)
  46. domains = ['gmail',
  47. 'hotmail',
  48. 'aol']
  49. senders = {}
  50. for msg in a:
  51. cid = None
  52. label = None
  53. if msg['date'] is not None:
  54. try:
  55. z = datetime.datetime.strptime(msg['date'], '%a, %d %b %Y %H:%M:%S %z')
  56. except ValueError:
  57. try:
  58. z = datetime.datetime.strptime(msg['date'], '%a, %d %b %Y %H:%M:%S %Z')
  59. except ValueError:
  60. print("Skipping message from " + msg['date'])
  61. continue
  62. dateID = "{0:1d}-{1:2d}".format(z.year, z.month)
  63. if not(dateID in senders.keys()):
  64. senders[dateID] = np.zeros(len(domains) + 1)
  65. dmatch = False
  66. for i, domain in enumerate(domains):
  67. if not(msg['To'] is None) and re.search(domain, msg['To']):
  68. senders[dateID][i+1] += 1
  69. dmatch = True
  70. if not(dmatch):
  71. senders[dateID][0] += 1
  72. months = list(senders.keys())
  73. months.sort()
  74. for j in range(len(domains) + 1):
  75. domainlist = []
  76. if j == 0:
  77. label = 'Other'
  78. else:
  79. label = domains[j - 1]
  80. for month in months:
  81. domainlist.append(senders[month][j])
  82. plt.plot(np.arange(len(months)),
  83. domainlist,
  84. label=label)
  85. plt.legend(frameon=False, loc='best')
  86. plt.xticks(np.arange(len(months)), months, rotation=90)
  87. plt.setp(plt.axes().get_xticklabels(), visible=False)
  88. plt.setp(plt.axes().get_xticklabels()[::6], visible=True)
  89. if args.plotfile:
  90. plt.savefig(args.plotfile, bbox_inches='tight')
  91. else:
  92. plt.savefig('email_destinations.png', bbox_inches='tight')