リンク: [ホーム] [自己紹介] [リンク集] [アルバム] [ソフトウェア] [発表文献] [その他]

まさおのChangeLogメモ / 2005-06-07

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

2005-06-07 Tue

* received.rb

junk フォルダにあるファイルの届いた月ごとに集計するのに使ったスク
リプト:

#!/usr/local/bin/ruby

#require 'mailread'
require 'date'

class Mail
   def initialize(file)
      @header = Hash.new([])
      open(file) do |f|
         lines = f.readlines
         while (not lines.empty?) do
            line = lines.shift
            break if line =~ /^$/
            line << lines.shift while lines[0] =~ /^\s/
            if line =~ /\A(\S+):\s*(.*)\Z/m
               @header[$1.downcase] << $2
            end
         end
      end
   end
   def [](key)
      @header[key.downcase]
   end
end

if $0 == __FILE__
   counter = {}
   ARGV.each do |dir|
      Dir.new(dir).entries.grep(/^\d+$/).sort{|a,b| a.to_i <=> b.to_i}.each do |f|
         path = dir + "/" + f
         #STDERR.puts path
         m = Mail.new(path)
         next if m["Received"].empty? or m["Received"].nil? or m["Received"][0].empty?
         date = nil
         while date.nil?
            begin
               date = DateTime.parse(m["Received"].shift)
            rescue
               break if m["Received"].empty? or m["Received"].nil?
            end
         end
         next if date.nil?
         counter[date.strftime("%Y-%m-%d")] ||= 0
         counter[date.strftime("%Y-%m-%d")] += 1
# puts "#{f}\t#{date}"
      end
   end

   puts
   counter.keys.sort.each do |d|
      puts "#{d}\t#{counter[d]}"
   end
end

全部はひろえてないけど。。。