Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. # This groups emails into senders and then calculates some basic stats for the
  4. # times of those emails - the earliest, latest and average
  5. #
  6. # Note that there is some oddity relating to my specific needs around timezone
  7. # handling - this is in the code below the comment starting "Normalise"
  8.  
  9. # These two are for debugging
  10. #require 'awesome_print'
  11. #require 'byebug'
  12. require 'net/imap'
  13. require 'progressbar'
  14. require 'active_support/all'
  15. require 'yaml'
  16. require 'yaml/store'
  17.  
  18. begin
  19. # Make sure we have everything we need to connect
  20. fail IOError unless File.exist?('settings.yml')
  21. settings = YAML.load_file('settings.yml')
  22. password = ENV['ES_PASSWORD']
  23. fail ArgumentError unless password
  24. fail NameError unless settings['server'] && settings['email']
  25.  
  26. # Connect and login to the server
  27. imap = Net::IMAP.new(settings['server'], 993, true)
  28. imap.login(settings['email'], password)
  29. imap.select('INBOX.Prices')
  30.  
  31. # Get all mail in the folder
  32. mail_ids = imap.search(['ALL'])
  33.  
  34. puts "Found #{mail_ids.count} emails. Fetching mail headers..."
  35.  
  36. # Create a new progressbar
  37. bar = ProgressBar.new 'progress', mail_ids.count
  38.  
  39. time_regex = /\d{2}:\d{2}:\d{2}\s([+-]\d{4}|UTC)/
  40.  
  41. # Fetch mail details
  42. stats = imap.fetch(mail_ids, 'ENVELOPE').map do |mail|
  43. bar.inc
  44.  
  45. sender = mail.attr['ENVELOPE'].sender.first
  46. time = Time.parse(mail.attr.first.second.date.match(time_regex).to_s).utc
  47.  
  48. # Normalise the days for my use case
  49. time += 1.day if time.hour < 6 && time.day == Date.current.day ||
  50. time.day == Date.yesterday.day
  51.  
  52. ["#{sender.mailbox}@#{sender.host}", time.to_i]
  53. end
  54.  
  55. # Calculate the stats for the emails
  56. stats = stats.group_by { |sender, _| sender }
  57. parsed_stats = Hash[stats.map do |group, details|
  58. times = details.map(&:last).sort
  59. [group, {
  60. earliest: Time.at(times.first),
  61. latest: Time.at(times.last),
  62. average: Time.at(times.sum.to_f / times.count)
  63. }]
  64. end]
  65.  
  66. # Output results
  67. bar.finish
  68. puts ''
  69.  
  70. parsed_stats.map do |address, numbers|
  71. puts address
  72. puts '-----------------------------'
  73. puts numbers[:earliest].strftime('%H:%M')
  74. puts numbers[:latest].strftime('%H:%M')
  75. puts numbers[:average].strftime('%H:%M')
  76. puts ''
  77. end
  78. rescue IOError
  79. # The setting file doesn't exist, so create it
  80. store = YAML::Store.new('settings.yml')
  81. store.transaction do
  82. store['server'] = nil
  83. store['email'] = nil
  84. end
  85. puts 'Created configuration file - please update it and try again.'
  86. rescue ArgumentError
  87. # Probably no password supplied
  88. puts 'Password not supplied - please supply a value in the ES_PASSWORD ' \
  89. 'environment variable'
  90. rescue NameError
  91. # Probably invalid settings
  92. puts 'Settings are invalid. Please check the settings file for server and ' \
  93. 'email options'
  94. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement