Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. #!/usr/bin/python
  2. import imaplib
  3. import datetime
  4. import email
  5. import sys
  6. import argparse
  7.  
  8.  
  9. def _get_arguments():
  10. # Argument Parser
  11. parser = argparse.ArgumentParser(
  12. description="Run daily average stats for an IMAP mailbox.", add_help=False)
  13. parser.add_argument('--server', default=None, help="Mail server to use")
  14. parser.add_argument('--username', '--mailbox',
  15. default=None,
  16. help="Mailbox to run stats on")
  17. parser.add_argument('--password',
  18. default=None,
  19. help="Login password for mailbox")
  20. return parser.parse_args()
  21.  
  22.  
  23. def main():
  24. imap_messages_by_date = {}
  25.  
  26. args = _get_arguments()
  27.  
  28. imap_server = args.server
  29. imap_user = args.username
  30. imap_password = args.password
  31.  
  32. # This section handles interactive obtaining of connection details, if none are provided.
  33. if args.server is None:
  34. imap_server = str(raw_input("Please specify the IMAP server: "))
  35.  
  36. if args.username is None:
  37. imap_user = str(raw_input("Please specify the IMAP username or mailbox to check: "))
  38.  
  39. if args.password is None:
  40. imap_password = str(raw_input("Please enter the password for the IMAP mailbox: "))
  41.  
  42. try:
  43. imap_conn = imaplib.IMAP4_SSL(imap_server, 993)
  44. imap_conn.login(imap_user, imap_password)
  45.  
  46. imap_conn.select("INBOX", True)
  47.  
  48. rv, data = imap_conn.search(None, "ALL")
  49. if rv != 'OK':
  50. print "No Messages!"
  51. sys.exit(0)
  52.  
  53. for num in data[0].split():
  54. rv, data1 = imap_conn.fetch(num, '(RFC822)')
  55. if rv != 'OK':
  56. print "ERROR getting message ", num
  57.  
  58. msg = email.message_from_string(data1[0][1])
  59.  
  60. date_tuple = email.utils.parsedate_tz(msg['Date'])
  61. if date_tuple:
  62. local_date = datetime.datetime.fromtimestamp(email.utils.mktime_tz(date_tuple))
  63.  
  64. datestamp = local_date.strftime('%Y-%m-%d')
  65.  
  66. try:
  67. imap_messages_by_date[datestamp] += 1
  68. except KeyError: # Doesn't exist in system yet
  69. imap_messages_by_date[datestamp] = 1
  70.  
  71. # for key, value in IMAP_MESSAGES_BY_DATE.iteritems():
  72. # print "Date: %s || Count: %s" % (key, value)
  73.  
  74. dates_count = 0.0 # Init this here
  75. messages_total_count = 0.0 # Init this here
  76. for key, value in imap_messages_by_date.iteritems():
  77. dates_count += 1
  78. messages_total_count += value
  79.  
  80. max_emails_per_day = max(imap_messages_by_date.itervalues())
  81. min_emails_per_day = min(imap_messages_by_date.itervalues())
  82.  
  83. rough_daily_average = messages_total_count / dates_count
  84. print "Min Mails Per Day (So Far): %s" % min_emails_per_day
  85. print "Max Mails Per Day (So Far): %s" % max_emails_per_day
  86.  
  87. print "(Rough) Daily Mail Average: ", rough_daily_average
  88. except Exception as error:
  89. print "An error has occurred, and the program has crashed; details:n"
  90. print str(error)
  91. sys.exit(10)
  92.  
  93. if __name__ == "__main__":
  94. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement