Created
February 24, 2012 07:34
-
-
Save takuya/1898767 to your computer and use it in GitHub Desktop.
ゆうちょ銀行のログインとか取得か ref: http://qiita.com/items/2781
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'kconv' | |
require 'openssl' | |
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE | |
require 'mechanize' | |
class YuchoBank | |
attr_accessor :password, :passphrase_list, :id, :password,:m | |
START_URL ="https://direct.jp-bank.japanpost.jp/direct_login.html?link_id=ycDctLgn" | |
def initialize() | |
self.password = "" | |
self.passphrase_list = {} #合言葉の質問と回答ペア | |
self.id = "" | |
self.password = "" | |
@is_login | |
end | |
def login() | |
self.start | |
self.send_id | |
self.send_aikotoba1 # first_time | |
self.send_aikotoba2 # second_time | |
self.send_password # パスワードを送る。 | |
@is_login = true | |
end | |
def start() @m = Mechanize.new end | |
def send_id | |
id= self.id.split('-') | |
@m.get START_URL | |
#お客様番号を入れる。 | |
@m.page.forms[0].fields_with({:name=>"okyakusamaBangou1"}).first.value = id[0] | |
@m.page.forms[0].fields_with({:name=>"okyakusamaBangou2"}).first.value = id[1] | |
@m.page.forms[0].fields_with({:name=>"okyakusamaBangou3"}).first.value = id[2] | |
self.send_request("次") | |
end | |
alias send_aikotoba1 send_aikotoba | |
alias send_aikotoba2 send_aikotoba | |
def send_aikotoba() | |
return :skip unless @m.page.title =~ /合言葉/ | |
q_text = @m.page.search('div.loginbox table td span.bold').first.text #合言葉の質問を探す。 | |
#合言葉の回答入力エリア | |
e = @m.page.forms[0].field_with(:name=>"aikotoba") | |
#合言葉の入力 | |
e.value = passphrase_list[q_text] | |
#合言葉の送信 | |
self.send_request("次") | |
end | |
def send_password() | |
#パスワードを入力 | |
e = @m.page.forms[0].field_with(:type=>"password") | |
e.value = password | |
self.send_request("ログイン") | |
end | |
def send_request(button_name) | |
#JavaScriptのsend_requestをFakeする。 | |
button = @m.page.search('//input[contains(@value,"'+button_name+'")]').first | |
#ボタンを押した時に遷移する。URLを指定する。 | |
action = button.attr('onclick').split(',')[1].gsub(%r"'", "") #=> ex "https://direct3.jp-bank.japanpost.jp/tp1web/U010701BLC.do" | |
@m.page.forms[0].action = action | |
# event のフィールドに値を入れる。 | |
event_value = button.attr('onclick').match(/new Array\('([^']+)'\),false/)[1] | |
@m.page.forms[0].fields.select{|e| e.name == "event"}.first.value = event_value | |
@m.page.forms[0].submit | |
end | |
####ログイン後に使う。 | |
#ホーム | |
def go_home() | |
self.send_request("ホームヘ") if @is_login | |
end | |
#残高照会 | |
def zandaka_shokai() | |
self.send_request("現在高照会") if @is_login | |
end | |
def go_page(button_name) | |
self.send_request(button_name) if @is_login | |
end | |
def current_page | |
@m.page | |
end | |
def current_page_body | |
@m.page.body.toutf8 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment