atelier:mitsuba

i love UI/UX, Blend, XAML, Behavior, P5, oF, Web, Tangible Bits and Physical computing. なにかあればお気軽にご連絡ください。atelier@c-mitsuba.com

IRCのトピックログをcsvに吐き出すRuby

かなり強引だけど、Limechatのログを参考に、IRCのトピックログをcsvに吐き出すスクリプトかいてみた。
久々のRubyすぎて、全然すまーとじゃない。。。

# -*- encoding: utf-8 -*-
Encoding.default_external = 'UTF-8'

output =""

Dir::entries("./log/").each{|fn|
  if fn != "." && fn != ".."
    f = open("./log/"+fn)
      date =  fn[0..3]+"-"+fn[4..5]+"-"+fn[6..7]+","
      f.each {|line|
        if /New topic*/ =~ line
          time = line[0..7]+","
          if line[22..23] == "by"
            apply = ""
            (25..40).each{|i|
              line[i] != ":" ? apply += line[i] : break
            }
            apply+=","
            
            if line.match(/<.*?>/)
               name = line.match(/<.*?>/).to_s
               name = name[1..name.length-2]
               topic = line.match(/<.*/)
            elsif line.match(/\(.*?\)/)
               name = line.match(/\(.*?\)/).to_s
               name = name[1..name.length-2]
               topic = line.match(/\(.*/)
            end
            topic = ',"' + topic.to_s + '"'
            if topic != ',""'
              prkey = date[0..3]+date[5..6]+date[8..9]+time[0..1]+time[3..4]+time[6..7]+","
              output += prkey + date + time + apply + name.to_s + topic + "\n"
            end
          end
        end
      }
    f.close
  end
  
  puts output
}