2011年 02月18日(Fri) [長年日記]
_ [アーケード][ruby][プログラム]謎のプログラムコード
(追記)何か検索エンジンから飛んでくる人がいるのでこちらに誘導リンク貼っておきますね。
作成途中だけど、一応動くところまでできたのでとりあえず日記にあげておく。
何か分かった上に、このまま使えるような人は使ってもらっても構わない。いないと思うけど。
IDとパスをプログラム中に書き込む必要アリ。
#coding: utf-8 require 'mechanize' require 'win32ole' def getAbsolutePath filename fso = WIN32OLE.new('Scripting.FileSystemObject') return fso.GetAbsolutePathName(filename) end module WorkSheet def [] y,x cell = self.Cells.Item(y,x) v = cell.Value def v.cell= cell @cell = cell end v.cell = cell def v.method_missing(name,*args) @cell.__send__(name,*args) end return v end def []=(y, x, value) cell = self.Cells.Item y,x if cell.MergeCells cell.MergeArea.Item(1,1).Value = value else cell.Value = value end end end agent = Mechanize.new page = agent.get 'https://project-diva-ac.net/divanet/' form = page.forms[0] form.fields.find {|f| f.name == 'accessCode'}.value = '' form.fields.find {|f| f.name == 'password'}.value = '' page = agent.submit form, form.buttons.first music_list_page = page.link_with(:text=>/全曲一覧/).click music_list = [] loop = true while loop music_list_page.links_with(:href=>/\/divanet\/pv\/info\//){|link| music_list += link } if next_link = music_list_page.link_with(:text=>/次へ\[#\]/) music_list_page = next_link.click else loop = false end end result = [] music_list.each{|music| result << { :title => music.text, :result => { 'EASY' => {:clear => '', :trial => ''}, 'NORMAL' => {:clear => '', :trial => ''}, 'HARD' => {:clear => '', :trial => ''}, 'EXTREME' => {:clear => '', :trial => ''}, } } page = music.click page.search('table tbody').map{|table| if /(EASY|NORMAL|HARD|EXTREME)/ =~ table.xpath('tr[1]/td[1]').inner_text diff = $1 clear = '-' if /\-/ !~ table.xpath('tr[2]/td[3]').inner_text clear = 'P' elsif /\-/ !~ table.xpath('tr[2]/td[2]').inner_text clear = 'G' elsif /\-/ !~ table.xpath('tr[2]/td[1]').inner_text clear = 'C' end trial = '' if /C-TRIAL/ =~ table.xpath('tr[2]/td[4]').inner_text trial = 'C-○' elsif /G-TRIAL/ =~ table.xpath('tr[2]/td[4]').inner_text trial = 'G-○' elsif /COMPLETE/ =~ table.xpath('tr[2]/td[4]').inner_text trial = 'COMP' end result[-1][:result][diff][:clear] = clear result[-1][:result][diff][:trial] = trial end } } filename = getAbsolutePath 'ProjectDiva Arcade ランク計算表 ver2.01.xls'.encode('Windows-31J') xl = WIN32OLE.new 'Excel.Application' begin book = xl.Workbooks.Open filename sheet = xl.Worksheets.Item 'BACKUPDATA' sheet.extend WorkSheet y = 6 result.each{|i| sheet[y, 2] = i[:title].encode('Windows-31J') sheet[y, 3] = i[:result]['EASY'][:clear] sheet[y, 4] = i[:result]['EASY'][:trial].encode('Windows-31J') sheet[y, 5] = i[:result]['NORMAL'][:clear] sheet[y, 6] = i[:result]['NORMAL'][:trial].encode('Windows-31J') sheet[y, 7] = i[:result]['HARD'][:clear] sheet[y, 8] = i[:result]['HARD'][:trial].encode('Windows-31J') sheet[y, 9] = i[:result]['EXTREME'][:clear] sheet[y, 10] = i[:result]['EXTREME'][:trial].encode('Windows-31J') y += 1 } sheet = xl.Worksheets.Item '曲別'.encode('Windows-31J') sheet.extend WorkSheet y = 6 result.each{|i| sheet[y, 12] = i[:title].encode('Windows-31J') y += 1 } book.Save ensure book.Close xl.Quit end