MySoftBankのWeb明細をダウンロードする

前回に引き続き、今回はMySoftBankのサイトからiPhone使用料のWeb明細を自動ダウンロードする。
この電話料金内訳明細書PDFの保持期間は過去6ヶ月分まで。
PDFのファイル名にはダウンロード時のタイムスタンプがついているだけで何月のものかわからないのでプレフィックスに年月を追加した。


mysoftbank.rb

require 'mechanize'
require 'kconv'

class MySoftbankClient

  def initialize
    @agent = Mechanize.new
  end

  def top
    @agent.get('https://my.softbank.jp/msb/d/top')
  end

  def login
    top()
    @agent.page.forms[0].click_button
    f = @agent.page.forms[0]
    f.msn = '電話番号'
    f.password = 'パスワード'
    f.click_button
  end

  def bill_total
    top()
    @agent.page.link_with(:href=>/WCO010001/).click
    @agent.page.forms[0].click_button
  end

  def uchiwake(num)
    yyyyMM = @agent.page.forms[num].fields[0].value
    @agent.page.forms[num].click_button
    @agent.page.forms[0].click_button
    @agent.page.response["content-disposition"] =~ /"(.+?)"/
    @agent.page.filename = yyyyMM + "_" + $~[1].toutf8
    @agent.page.save
    puts @agent.page.filename + " saved."
  end
  
  def one_month_ago
    bill_total()
    uchiwake(1)
  end
  
  def two_month_ago
    bill_total()
    uchiwake(2)
  end

  def three_month_ago
    bill_total()
    uchiwake(3)
  end

  def four_month_ago
    bill_total()
    uchiwake(4)
  end

  def five_month_ago
    bill_total()
    uchiwake(5)
  end

  def six_month_ago
    bill_total()
    uchiwake(6)
  end

  def logout
    top()
    @agent.page.link_with(:href=>/doLogout/).click
    puts "logout."
  end

end


client = MySoftbankClient.new

client.login()

client.one_month_ago()
client.two_month_ago()
client.three_month_ago()
client.four_month_ago()
client.five_month_ago()
client.six_month_ago()

client.logout()


実行結果

$ ruby mysoftbank.rb
201302_電話料金内訳明細書20130318100543.PDF saved.
201301_電話料金内訳明細書20130318100545.PDF saved.
201212_電話料金内訳明細書20130318100547.PDF saved.
201211_電話料金内訳明細書20130318100549.PDF saved.
201210_電話料金内訳明細書20130318100551.PDF saved.
201209_電話料金内訳明細書20130318100553.PDF saved.
logout.

※追記(2013/12/01)
MySoftBankのサイト構成が変更になり、エラーになっていのでソースコードを修正した。