require 'net/http' require 'uri' require 'kconv' require 'pathname' $KCODE = 'S' def fetch_result(url) uri = URI.parse(url) html = "" begin Net::HTTP.start(uri.host, uri.port) { |http| response = http.get(uri.path) html = response.body if(html.isutf8) then html = html.kconv(Kconv::SJIS, Kconv::UTF8) else html = html.kconv(Kconv::SJIS, Kconv::AUTO) end } rescue => err p err return nil end return html end def split_result(html, date, output_dir) ofh = File.open(Pathname.new(output_dir + "/smsbs_" + date + ".res.txt").cleanpath, 'w') #pattern = %r{([A-Z]{2}[0-9]{4}[MF])(.*)(.*)(回収|帰還)\[(\d\d):(\d\d)\](\d+)(\d+)(-?\d+)(.*)\((.*)\)(\d+)} pattern = %r{([A-Z]{2}[0-9]{4}[MF])(.*)(.*)(回収|帰還)(\d+)(\d+)(\d+)(-?\d+)(.*)((.*))(\d+)} html.each_line { |line| match_result = pattern.match(line) if (match_result) then arr = match_result.to_a arr.shift write_str = arr.join("\t") + "\n" #write_str += match_result[1] + "\t" #id #write_str += match_result[2] + "\t" #name #write_str += match_result[3] + "\t" #team #write_str += match_result[4] + "\t" #survive #write_str += String(match_result[5].to_i * 60 + match_result[6].to_i) + "\t" #time = hour * 60 + min #write_str += match_result[7] + "\t" #score #write_str += match_result[8] + "\t" #exp #write_str += match_result[9] + "\t" #msp #write_str += match_result[10] + "\t" #ms #write_str += match_result[11] + "\t" #color #write_str += match_result[12] + "\n" #cost ofh.print(write_str) end } ofh.close end def main() mode = "-u" input = "http://mb2223f.hp.infoseek.co.jp/sMSBS_LOG/Res.html" output_dir = "result/" result_date = Time.now.strftime("%y%m%d") #ARGVの処理 while(ARGV.length != 0) opt = ARGV.shift if(opt == "-f" && ARGV.length > 0) then mode = opt input = ARGV.shift elsif(opt == "-u" && ARGV.length > 0) then mode = opt input = ARGV.shift elsif(opt == "-d" && ARGV.length > 0) then result_date = ARGV.shift elsif(opt == "-ud" && ARGV.length > 0) then mode = "-u" result_date = ARGV.shift input = "http://mb2223f.hp.infoseek.co.jp/sMSBS_LOG/#{result_date}.html" elsif(opt == "-o" && ARGV.length > 0) then output_dir = ARGV.shift else printf("bad ARGV\n") exit(1) end end if (mode == "-u") then split_result(fetch_result(input), result_date, output_dir) else ifh = File.open(input,'r') split_result(ifh.read.kconv(Kconv::SJIS, Kconv::AUTO), result_date, output_dir) ifh.close end end main()