#! ruby -KS require "open-uri" $KCODE = "SJIS" # 京成バスのデータをJNTrain/NextTrain用に整形する(処理部) # 動作確認 # Windows XP SP3 # ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32] # 手動で設定するデータは"keiseibus_setting.rb"から読み込む # 引数を与えた場合は、そのファイルから読み込む # 引数は一つしか対応していない # ただし、ファイル名は"keiseibus_settings-○○.rb"の形式のみ対応 # 引数には上記の○○の部分のみ与える # (例 ruby keiseibus_conv.rb chi010506) # 読み込む変数は、$station, $url_list, $notes_list, $exception_route p ARGV if $DEBUG if ARGV.empty? $setting_file = "keiseibus_settings.rb" else $setting_file = "keiseibus_settings-#{ARGV[0]}.rb" end if File.exist?($setting_file) load $setting_file else print "設定ファイルがありませんでした。\n終了します。" exit end print "#{$setting_file}\n" if $DEBUG # 処理日 $date = Time.now.strftime("%Y年%m月%d日") # 時刻表データ格納用 $weekday = [[]] * 28 # 月〜金の時刻表データ $saturday = [[]] * 28 # 土の時刻表データ $holiday = [[]] * 28 # 日の時刻表データ $all = [] # 上記のデータを全て入れる # 出力しないデータの正規表現オブジェクトを作成 if $exception_route.size != 0 #|| defined?($exception_route) $exception_route_reg = Regexp.new($exception_route.join("|")) end # $notes_listからJNTrain用備考を作成する $notes = [] $notes_list.each do |each_notes| each_notes.each_value do |value| (next if value =~ $exception_route_reg) if defined?($exception_route_reg) $notes.push(value.split(/・/)) end end $notes = $notes.flatten.uniq.sort p $notes if $DEBUG $routes = [] $notes.each do |elem| if elem =~ /(.+)系統/ $routes.push($1) end end $routes = $routes.uniq.sort $symbol_list = {} $jntrain_notes = "" $notes.each_with_index do |elem, i| # 97.chrで"a"を得る $symbol_list[elem] = (i + 97).chr $jntrain_notes += "#{(i + 97).chr}:#{elem}\n" end p $symbol_list if $DEBUG print "#{$jntrain_notes}\n" if $DEBUG # データの入力から変換まで $url_list.each_with_index do |url, i| open(url) do |f| hour = 99 timetable = [] counter = 1 f.each do |line| case line # 乗車するバス停名 # 複数路線処理する場合も乗車するバス停は同じと仮定し、最後に処理した時刻表サイトのデータが保存される when /(.+)<\/span>/ $station = $1 # 処理フラグ・時刻 # 出てくる順は月〜金、土、日の順で3種類(counterで判断) # 時刻をインデックスに使用(ただし、0〜3時は+24する) when /([0-9]+)<\/td>/ hour_prev = hour hour = $1.to_i if counter % 3 == 1 status = "weekday" # この直前に処理していたのは"holiday"(hour_prevが99を除く) if hour_prev < 4 $holiday[hour_prev + 24] += timetable else $holiday[hour_prev] += timetable if hour_prev != 99 end elsif counter % 3 == 2 status = "saturday" # この直前に処理していたのは"weekday" if hour < 4 $weekday[hour + 24] += timetable else $weekday[hour] += timetable end elsif counter % 3 == 0 status = "holiday" # この直前に処理していたのは"saturday" if hour < 4 $saturday[hour + 24] += timetable else $saturday[hour] += timetable end end print "count #{counter}: " if $DEBUG print "#{hour}時(#{status})(前の処理対象: #{hour_prev}時)\n" if $DEBUG counter = counter + 1 timetable = [] # 行き先(系統/備考)・分 when /(.+)<\/sub>
(.+)<\/td>/ print " #{$1} #{$2} | " if $DEBUG destination = $1 minute = $2.to_i print "#{minute} #{destination}" if $DEBUG notes = $notes_list[i][destination] print " #{notes}\n" if $DEBUG timetable.push([minute, destination, notes]) # 最後の「行き先・分」データはpushするところがないのでここで追加する when // if hour < 4 $holiday[hour + 24] = timetable else $holiday[hour] = timetable end end end end end if $DEBUG print "\n--- Weekday ---\n" p $weekday print "\n--- Saturday ---\n" p $saturday print "\n--- Holiday ---\n" p $holiday print "\n" end # データの出力 # スクリプトと同じディレクトリに出力する output_file = open("(京成バス)#{$station} #{$routes.join("・")}系統.tbl" , "w") output_file.print "; 京成バス #{$station} #{$routes.join("・")}系統 [#{$date}現在]\n\n" output_file.print $jntrain_notes $all = [$weekday, $saturday, $holiday] $all.each_with_index do |day_of_the_week, i| # 曜日ごとのヘッダー if i == 0 output_file.print "\n[MON][TUE][WED][THU][FRI]\n" output_file.print "# #{$station} #{$routes.join("・")}系統(平日)" elsif i == 1 output_file.print "\n[SAT]\n" output_file.print "# #{$station} #{$routes.join("・")}系統(土)" elsif i == 2 output_file.print "\n[SUN][HOL]\n" output_file.print "# #{$station} #{$routes.join("・")}系統(休日)" end day_of_the_week.each_with_index do |each_time, j| # 複数の時刻表データが処理した順でそのまま入っているので「分」でソートし直す each_time.sort!{|a, b| a[0] <=> b[0]} next if each_time == nil || each_time.size == 0 if j < 24 output_file.print "\n#{j}:" else output_file.print "\n#{j - 24}:" end each_time.each do |minutes_list| # 出力する必要のないデータはここで除外する (next if minutes_list[2] =~ $exception_route_reg) if defined?($exception_route_reg) minute = minutes_list[0] symbol_reg_str = $symbol_list.keys.join("|") symbol = minutes_list[2].gsub(/#{symbol_reg_str}/o){|m| $symbol_list[m]}.gsub(/・/, "") output_file.print " #{symbol}#{minute}" end end output_file.print "\n" end output_file.close