Skip to content

Instantly share code, notes, and snippets.

@larsch
Created September 20, 2010 18:59
Show Gist options
  • Select an option

  • Save larsch/588441 to your computer and use it in GitHub Desktop.

Select an option

Save larsch/588441 to your computer and use it in GitHub Desktop.
Show daily statistic of spam on GMail account
require 'net/imap'
require 'open-uri'
if not File.exist?('cacert.pem')
puts "Downloading CA certificates"
open('http://curl.haxx.se/ca/cacert.pem') do |f|
File.open('cacert.pem', 'wb') { |of| of << f.read }
end
end
cert = File.expand_path('cacert.pem')
# Net::IMAP.debug = true
imap = Net::IMAP.new('imap.gmail.com', 993, true, 'cacert.pem', true)
imap.login(IMAP_LOGIN, IMAP_PASSWORD)
count = imap.status("[Gmail]/Spam", ["MESSAGES"])['MESSAGES']
stats = Hash.new { 0 }
imap.examine("[Gmail]/Spam")
imap.fetch(1..count, "INTERNALDATE").each do |fd|
time = Time.parse(fd.attr["INTERNALDATE"])
date = sprintf "%04d-%02d-%02d", time.year, time.month, time.day
stats[date] += 1
end
sorted = stats.keys.sort.map { |x| [x,stats[x]] }
sorted.each do |date, count|
puts "#{date} #{count}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment