大学のIT技術講演会で株式会社はてなの人に触発され、株式会社はてなの回し者である友人 @y_uuki に招待されるがまま
ブログを始めました。
はてな記法モードというものでソースコードを綺麗に表示出来るとのことなのでとりあえず
rubyのMechanizeライブラリを用いたとあるテニスコートのコート予約自動化スクリプト
(の一部、テニスコートの抽選予約に選ばれたものに対して「予約します」という意思確認を行う処理の自動化)のソースコードを公開します。
因にputs("Usage:intentioncheck.rb input/id.txt output/filename_out")となっていますが、読み込むファイルid.txtは
"ID,パスワード,名前(改行)"を80人分書いてあります。
かなり汚いコードなので書き方の添削をしていただけるとありがたいです。
# encoding: utf-8 require 'rubygems' require 'mechanize' require 'kconv' def check_unit(id,password,name,date,range_time,out_file) agent = Mechanize.new() #if(agent.get('http://www.hyogo-park.or.jp/yoyaku/intention/auth.asp?ch=0'))then #if(agent.get('http://www.hyogo-park.or.jp/yoyaku/intention/auth.asp?ch=0'))then # $stderr.print("Error:Term is over\n") # exit #end agent.get('http://www.hyogo-park.or.jp/yoyaku/intention/auth.asp?ch=0') agent.page.form_with(:name => 'form1'){|f| f.field_with(:name => 'mem_number').value =id f.field_with(:name => 'mem_password').value = password f.click_button(f.button_with(:value => ' 次 へ ')) } #http://www.hyogo-park.or.jp/yoyaku/intention/intention_check.asp agent.page.search('tr/td/table[@width="400"]')[0..50].each do |p| if(/(予約は承認されました。)/ =~ p.inner_text)then if(/([0-9]*年[0-9]*月[0-9]*日)/ =~ p.inner_text) then print($1,(',')) #puts p.inner_text out_file.print($1,(',')) end if(/([0-9]*時〜[0-9]*時)/ =~ p.inner_text) then print($1,(',')) #puts p.inner_text out_file.print($1,(','),id,(','),password,(','),name,(',')) end puts("予約済み\n") out_file.print("予約済み\n") end end approvals = agent.page.links_with(:text => '予約承認確認') approvals.each do |apr| apr.click s_date = 'init' agent.page.search('div')[0..50].each do |p| if(/([0-9]*年[0-9]*月[0-9]*日)/ =~ p.inner_text) then print($1,(',')) s_date = $1 #$&.clone #puts p.inner_text out_file.print($1,(',')) end if(/時〜/ =~ p.inner_text) then puts p.inner_text search_time = p.inner_text if(date == s_date && range_time == search_time)then puts('match so resist') agent.page.form_with(:name => 'form1'){|f| f.submit(f.button_with(:name => 'submi2')) } puts agent.page.uri puts('resitered') end out_file.print(p.inner_text,',',id,',',password,',',name,("\n")) end end end end def check_only(filename_id,filename_out) if(filename_id && filename_out) then else puts("Usage:intentioncheck.rb input/id.txt output/filename_out") return end id_file = open(filename_id) finish_file = open(filename_out,"w") while(id_text = id_file.gets) do id_array = id_text.split(/,/) puts id_array[2] id_array[2].chomp! id = id_array[0] password = id_array[1] name = id_array[2] begin timeout(30){ check_unit(id,password,name,'tekitou','tekitou2',finish_file) } rescue Timeout::Error retry end end id_file.close finish_file.close end def check_and_regist(filename_id,filename_out) id_file = open(filename_id) finish_file = open(filename_out,"w") while(id_text = id_file.gets) do if(/----/=~id_text)then print(id_text.chomp," next day----\n") next end id_array = id_text.split(/,/) date = id_array[0] range_time = id_array[1] id = id_array[2] password = id_array[3] name = id_array[4].chomp print(date,range_time,name,"\n") check_unit(id,password,name,date,range_time,finish_file) end id_file.close finish_file.close end #if(ARGV[2])then # check_and_regist(ARGV[0],ARGV[1]) #end #if(ARGV[1])then # check_only(ARGV[0],ARGV[1]) #else # puts("Usage:intentioncheck.rb input/id.txt output/filename_out") # puts("Usage:intentioncheck.rb input/int_file output/filename_out -resist") #end