#!/usr/bin/env ruby -Ks # 攻城戦ログ取得 # Web上から各ワールドごとにダウンロードして、ディレクトリに格納します。 # 入力:日付 (YYYYMMDD) # Lydia GvG log http://ro.ayatan.org/lydgvg/ # Freya GvG LOG 置き場 http://www.geocities.jp/freya_gvg/ # Sara鯖GvGギルド過去落城ログ http://link.sara-g.com/ajito/log.php require 'parsedate' require 'nkf' require 'net/http' require 'gvglib/date.rb' log_date = ARGV[0] # 日付 (YYYYMMDD) world = ARGV[1] # ワールド名(鯖名) if log_date == '0' or log_date == nil file_date = Date.today.wday_before(0) else file_date = Date.parse(log_date) # 日付取得(YMD) end now_ymd = file_date.ymd # YYYYMMDD now_y_m_d = file_date.ymd_under # YYYY_MM_DD option = "-Esx" # ESC to SJIS, 半角カナはそのままにする # 設定 worldlist = ['lydia', 'freya'] # 実行ワールドリスト log = { # 各ワールド別の設定 'lydia' => { # ワールド 'server' => 'ro.ayatan.org', # httpサーバー 'dir' => '/lydgvg/log', # ログファイルのあるディレクトリ 'remotefile' => [ # 取得するログファイルリスト now_y_m_d + '.i.txt', # timetable now_y_m_d + '.l.txt' # lydia ], 'localfile' => [ # 保存ログのファイル名 'timetable_' + now_ymd + '.txt', 'lydia_' + now_ymd + '.txt' ] }, 'freya' => { 'server' => 'www.geocities.jp', 'dir' => '/freya_gvg', 'remotefile' => [ now_ymd + '_GvG_Log.txt', now_ymd + '_GvG_LogCut.txt', now_ymd + '_GvG_Point.txt', now_ymd + '_GvG_Point8week.txt', now_ymd + '_GvG_Toride.txt' ], 'localfile' => [ 'ropp_' + now_ymd + '.txt', ] }, 'sara' => { 'server' => 'link.sara-g.com', 'dir' => '/sara_gvglog', 'remotefile' => [ # ChatSave_Sara_YYYY_MM_DD.txt 'Saragvg' + now_ymd + '.txt', ], 'localfile' => [ 'ropp_' + now_ymd + '.txt', ] =begin # sara旧設定 'server' => 'imo-gvg.hp.infoseek.co.jp', 'dir' => '/cgi-bin', 'remotefile' => [ # ChatSave_Sara_YYYY_MM_DD.txt 'ChatSave_Sara_' + now_y_m_d + '.txt', ], 'localfile' => [ 'sara_' + now_ymd + '.txt', ] =end } } # ダウンロードしたデータをファイルに保存する # 特殊改行"\r\n"が入っていたら改行"\n"に置き換える。 def makelogfile(path, data, resp, option) print 'Put LogFile: ', path, "\n" file = File.open(path, "w") if option == "-Esx" data.each{ |i| file.puts NKF.nkf(option, i) } else data.each{ |i| file.puts i.gsub(/\r\n$/, "\n") } end file.close setfiletime(path, resp) # ファイル時間修正 end # ディレクトリチェック # ディレクトリが存在しなければディレクトリを作成する。 # ディレクトリと同名のファイルがある場合は警告して終了する。 def check_dir(dir) if FileTest.exist?(dir) == true unless FileTest.directory?(dir) == true print dir, " はディレクトリではありません。\n" print "以上の名前のファイルを削除してから再実行して下さい。\n" exit end else Dir.mkdir(dir) print "ディレクトリ ", dir, " を作成しました。\n" end end # ファイル時間を元のものに修正 def setfiletime(filename, resp) access = modtime = "" resp.each{ |key, val| if key == 'date' year, month, day, hour, min, sec = ParseDate.parsedate(val) access = Time.gm(year, month, day, hour, min, sec) end if key == 'last-modified' year, month, day, hour, min, sec = ParseDate.parsedate(val) modtime = Time.gm(year, month, day, hour, min, sec) end # printf("%-14s =%-40.40s\n", key, val) } if modtime == "" # ファイル名がないとき print "更新時刻を取得できません(ファイルが存在しません)。\n" modtime = access # 更新時刻をアクセス時刻に設定 end File.utime(access, modtime, filename) end # ダウンロード処理 def getlist(log, world) getfile = resp = data = '' # 初期化で処理が早くなる check_dir("./" + world) # ワールドディレクトリチェック check_dir("./" + world + "/tmp") # ログディレクトリチェック h = Net::HTTP.new(log[world]['server'], 80) count = 0 worldtmp = './'+world+'/tmp/' # ファイル取得して保存 log[world]['remotefile'].each { |getfile| print 'Get LogFile: http://', log[world]['server'] print log[world]['dir'], '/', getfile, "\n" resp, data = h.get(log[world]['dir'] + "/" + getfile, nil) if world == 'lydia' # Lydia Log のみ EUC to SJIS に変換 if getfile =~ /.i.txt$/ || getfile =~ /.l.txt$/ makelogfile(worldtmp+getfile, data, resp, "") makelogfile(worldtmp+log[world]['localfile'][count], data, resp, "-Esx") count += 1 end elsif world == 'freya' # if getfile =~ /_GvG_Log.txt$/ if getfile =~ /_GvG_LogCut.txt$/ makelogfile(worldtmp+getfile, data, resp, "") makelogfile(worldtmp+log[world]['localfile'][0], data, resp, "") else makelogfile(worldtmp+getfile, data, resp, "") end elsif world == 'sara' makelogfile(worldtmp+getfile, data, resp, "") makelogfile(worldtmp+log[world]['localfile'][0], data, resp, "") else makelogfile(worldtmp+getfile, data, resp, "") end } end # ワールド指定がなければ全ワールドを指定 if world == 0 or world == nil # 各ワールドごとにログを取得 worldlist.each { |world| getlist(log, world) } else # 指定ワールドのログのみを取得 getlist(log, world) end exit