English

メールを数える

多くの ML に参加していたり, ある いは junk mails の処理に悩まされたりしていると, ふと, 私ってどれだけの メールを送ったり受け取ったりしているのだろうと, 少し気になりました. そ れならば, いっちょ数えてみようかということで, Ruby script を書いてみま した.

メールのデータ処理なら tmail が 簡単. TMail::Mail#load でファイルをそのまま読み込ませて TMail::Mail オブジェクト を作れば, TMail::Mail#date で日付が, TMail::Mail#from で送り主 (From 行) が分かります.

せっかくだから月ごとに送ったメールと受け取ったメールと数え, Gnuplot でグラフを書いてみる. ファイルは, こんな感じ.

#!/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

引数にファイル名を与えて実行すると, 月ごとのメール数を数えてくれま す.

$ 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

このデータを, 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]

送ってるメール数は, 案外少ないですか?



[うさぎ印] おたより, お待ちしています

sato.mshr@gmail.com