Created
January 8, 2015 20:07
-
-
Save takuya/f487223c9337cf4b3c53 to your computer and use it in GitHub Desktop.
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 'headless' | |
require "watir" | |
require "watir-webdriver" | |
module BillScraper | |
class SoftbankWatir | |
attr_accessor :browser | |
def initialize(id,pw) | |
@download_dir = self.mk_temp_dir | |
profile = Selenium::WebDriver::Firefox::Profile.new | |
profile['browser.download.folderList'] = 2 | |
profile['browser.download.dir'] = @download_dir | |
profile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv,application/pdf,application/octet-stream" | |
b = Watir::Browser.new :firefox, :profile => profile | |
@browser=b | |
@id,@pw = id,pw | |
@pwd = Dir.pwd | |
end | |
def mk_temp_dir | |
require "tmpdir" | |
temp = Dir.mktmpdir("softbank"+Date.today.to_s, "/tmp") | |
temp | |
end | |
def login() | |
login_by_yahoo(@id,@pw) | |
end | |
def login_by_yahoo(id,pw) | |
@browser.goto "https://my.softbank.jp/msb/d/webLink/doSend/WCO010001" | |
@browser.wait | |
@browser.forms[1].input.click | |
@browser.text_field( :id => /username/ ).set id | |
@browser.text_field( :id => /passwd/ ).set pw | |
@browser.form.submit | |
@browser.wait | |
end | |
def latest_meisai | |
self.download_ryokin_pdf | |
end | |
def download_ryokin_pdf | |
Dir.chdir(@download_dir) | |
@browser.goto "https://my.softbank.jp/msb/d/top" | |
@browser.wait | |
@browser.goto "https://my.softbank.jp/msb/d/webLink/doSend/WCO010005" | |
@browser.wait | |
@browser.input( :name=>/print/i ).click | |
@browser.wait | |
list = Dir.glob("./*") | |
raise "ダウンロード失敗" if list.size == 0 | |
f_name = @browser.element(:id=>"target02").text() | |
f_name.gsub!(/\s/,"") | |
f_name.gsub!(/\//,"/") | |
pdf = open(list[0]).read | |
Dir.chdir @pwd | |
FileUtils.remove_entry_secure @download_dir | |
{ | |
"file_name" => "softbank"+f_name +".pdf", | |
"body" => pdf | |
} | |
end | |
end | |
end | |
# headless = Headless.new | |
# headless.start | |
# sb = SoftbankWatir.new(id,pw) | |
# ret = sb.download_ryokin_pdf | |
# open(File.expand_path("~/Desktop/"+ret["f_name"]) , "w" ){|f| f.write ret["pdf"] } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment