2chの対象の板にあるスレッド全部のdatを取得するスクリプト

大量の文章ファイルが欲しかったのです(><)
というわけで2chから文章とって来ようとスクリプトrubyで作った。

#!/usr/local/bin/ruby
require 'net/http'
require 'kconv'

$KCODE='u'

URLS = ['http://yutori.2ch.net/news4vip/']

def make_dir(dir_path)
  full_path = Dir.pwd
  dir_path.split(/\//).each do | path |
    full_path = File.join(full_path, path )
    Dir.mkdir(full_path) unless File.exist?(full_path)
    return false unless File::ftype(full_path) == "directory"
  end
  full_path
end

def get_subject(url)
  bbsname = url.split('://').last
  dir_path = make_dir(bbsname)
  subject_url = "#{url}subject.txt"

  Net::HTTP.get(URI.parse(subject_url)).toutf8.each do | body |
    body_spliter = body.split("<>")
    body_spliter[1] =~ /(.*)\((\d+)\)\n$/
    file_name = File.join(dir_path,"#{body_spliter[0]}.#{$2}.dat") 
    files = Dir.glob("#{dir_path}\/#{body_spliter[0]}*")
    unless files.include?(file_name)
      files.each do | file |
        File.delete(file)
      end
      output_file = File.open(file_name,'w')
      output_file.puts Net::HTTP.get(URI.parse("#{url}dat/#{body_spliter[0]}"))
      output_file.close
      p "#{file_name}/#{$1} get" 
      sleep 5
    end
  end
end

URLS.each do | url |
   get_subject(url)
end

sleep 5は最後の良心。