| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- #!/usr/bin/python
- #
- # email_stats.py
- #
- # Time vs date plot for an email mbox file.
- #
- # Copyright (C) 2015 George C. Privon
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- import mailbox
- import datetime
- import matplotlib.pyplot as plt
- import matplotlib.dates as mdates
- import argparse
- parser = argparse.ArgumentParser()
- parser.add_argument('mbox', help='Mailbox to analyze.')
- parser.add_argument('--plotfile', '-p', default=False, action='store',
- help='Name of output plotting file.')
- parser.add_argument('--title', '-t', default='Email Send Times',
- action='store', help='Plot title.')
- args = parser.parse_args()
- plt.figure()
- plt.ylim([0, 24])
- plt.ylabel('Hour')
- plt.xlabel('Date')
- plt.minorticks_on()
- plt.title(args.title)
- a = mailbox.mbox(args.mbox)
- for msg in a:
- if msg['date'] is not None:
- try:
- z = datetime.datetime.strptime(msg['date'], '%a, %d %b %Y %H:%M:%S %z')
- except ValueError:
- try:
- z = datetime.datetime.strptime(msg['date'], '%a, %d %b %Y %H:%M:%S %Z')
- except ValueError:
- print("Skipping message from " + msg['date'])
- continue
- plt.plot_date(z.date(),
- z.hour + z.minute/60.,
- fmt='r.',
- # tz=z.tzname(),
- xdate=True)
- if args.plotfile:
- plt.savefig(args.plotfile)
- else:
- plt.savefig('email_times.png')
|