Nihongo (Japanese)

Count your e-mails

I join several MLs. In addition, everyday I recieved many junk mails. Unintentionally, I am interested in how many e-mails I have sent and recieved. To count e-mails, I write a Ruby script.

tmail is a great Ruby library for processing e-mail data. When you read an e-mail file using TMail::Mail#load and create a TMail::Mail object, you can easily get the date with TMail::Mail#date, and a sender described in From line with TMail::Mail#from.

And more, I'd like to count e-mails I sent and recieved in each month, and draw a graph using Gnuplot. First of all, the Ruby script for e-mail count is as follows.

#!/usr/bin/ruby
# mail_count.rb ; check date of mail file
#
# ruby mail_count.rb 

require 'tmail'

send = {};
recieve = {};
my_addr = 'm@sa.to'

while f = ARGV.shift
  begin
    mail = TMail::Mail.load(f);
    date_str = mail.date.strftime("%Y-%m");

    if mail.from.include?(my_addr)  # mail.from is Array
      if send.has_key?(date_str)
	send[date_str] += 1
      else
	send[date_str] = 1
      end
    else
      if recieve.has_key?(date_str)
	recieve[date_str] += 1
      else
	recieve[date_str] = 1
      end
    end

  rescue
    printf "%s is not found\n", f
  end
end

(send.keys | recieve.keys).sort.each do |key|
  printf "%s %d %d\n", key, send[key].to_i, recieve[key].to_i
  # Year-Month Send Recieve
end

Specifying files of e-mails as arguments, you can get the number of e-mails in each month.

$ ruby mail_count.rb ~/Mail/inbox/* > mail_count.dat
$ cat mail_count.dat
2003-03 7 54
2003-04 15 133
2003-05 16 173
2003-06 27 212
2003-07 21 131
2003-08 19 146
2003-09 16 128
2003-10 8 371
2003-11 25 211
2003-12 6 136
2004-01 13 120
2004-02 3 48

Using the data above, let's draw a graph using Gnuplot.

$ cat mail_count.gp
#!/usr/bin/gnuplot
# mail_count.gp
# 2004/02/11 13:22:21 msato

set xdata time
set timefmt "%Y-%m"
set format x "%Y-%m"
set xtics ("2003-03", "2003-05", "2003-07", "2003-09", "2003-11", \
    "2004-01", "2004-03")
set xrange ["2003-2" : "2004-3"]

set grid
set data style linespoints
set pointsize 3

set terminal png small color
set output "mail_count.png"

plot "mail_count.dat" using 1:2 title "Sent", \
     "" using 1:3 title "Recieved"

$ gnuplot mail_count.gp
$ display mail_count.png

[Mail Count Graph]

Are e-mails I sent less than what you thought?



[Rabbit Mark] Feel free to mail me

sato.mshr@gmail.com