Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#coding: utf-8
# @author https://github.com/takuya
#
# Copyright (C) 2012-2013 takuya. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA
#
module BillScraper
class MyDoCoMo
require "rubygems"
require 'headless'
require "watir"
require "watir-webdriver"
attr_accessor :browser,:id,:pw
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,application/x-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("docomo"+Date.today.to_s, "/tmp")
temp
end
def login()
login_by_docomo_id(@id,@pw)
end
def login_by_docomo_id(id=nil,pw=nil)
id = id || @id
pw = pw || @pw
browser.goto "https://id.smt.docomo.ne.jp/cgi7/id/menu"
browser.wait
browser.text_field(:id,"Di_Uid").set(id)
browser.text_field(:id,"Di_Pass").set(pw)
browser.checkbox(:id => /persistent/).set true
browser.forms[0].submit
browser.wait
self.go_home
end
def ryokin_page
browser.link(:text , /ご利用料金の確認/).click
end
def ryokin_matome_page #一括請求ページ
# 一括
browser.select_list(:name, /root_GKFAGW001_DENWABANGOPULLDOWN/).select_value(0)
browser.button(:name , /root_GKFAGW001SubmitHyoujiPull/).click
browser.wait
browser.html
end
def latest_ryokin_meisai # go
#直近で請求が確定している月
table = browser.elements(:css, "table.new-amountclaimschedule").last
year_month = table.elements(:css, "tr th p.strong")[1].text
table.element(:css, "tr input[alt='利用内訳']").click
browser.wait
year_month
end
def get_latest_meisai_pdf
# 位置をリセット
self.go_home
#料金ページヘ
self.ryokin_page
self.ryokin_matome_page
#取得可能な最新月
y_m = self.latest_ryokin_meisai
browser.element(:title , /PDFデータ保存/).click
browser.wait
sleep 5 # ダウンロードの待ちを入れる
browser.element(:onclick , /doui/).click
browser.wait
pwd = Dir.pwd
Dir.chdir @download_dir
list = Dir.glob("./*")
raise "ダウンロード失敗" if list.size == 0
pdf_body = open(list[0]).read
Dir.glob("*").each{|e| File.unlink(e)}
Dir.chdir pwd
return {"file_name" => "docomo-#{y_m}-meisai.pdf", "body"=> pdf_body , "month" => y_m }
end
def get_latest_gaiyo_pdf
begin
# 位置をリセット
self.go_home
#料金ページヘ
self.ryokin_page
self.ryokin_matome_page
#取得可能な最新月
y_m = self.latest_ryokin_gaiyou
browser.element(:title , /PDFデータ保存/).click
browser.wait
browser.element(:onclick , /doui/).click
browser.wait
sleep 5 # ダウンロードの待ちを入れる
pwd = Dir.pwd
Dir.chdir @download_dir
list = Dir.glob("*")
raise "ダウンロード失敗" if list.size == 0
pdf_body = open(list[0]).read
Dir.glob("*").each{|e| File.unlink(e)}
Dir.chdir pwd
raise if pdf_body.size == 0
rescue
retry
end
return {"file_name" => "docomo-#{y_m}-gaiyo.pdf", "body"=>pdf_body , "month" => y_m }
end
def latest_ryokin_gaiyou # go
# 一括料金表示ページを解析
#直近で請求が確定している月
table = browser.elements(:css, "table.new-amountclaimschedule").last
year_month = table.elements(:css, "tr th p.strong")[1].text
table.element(:css, "tr input[alt='お知らせ']").click
browser.wait
year_month
end
def get_docomo_information
pwd = Dir.pwd
Dir.chdir @download_dir
# 位置をリセット
self.go_home
#料金ページヘ
self.ryokin_page
self.ryokin_matome_page
self.latest_ryokin_gaiyou
## 同封物
browser.link(:text ,"請求書に同封の小冊子").click
links = browser.links(:onclick ,/setDofubutsu/)
links = links.map{|link| link.text }
result = []
links.each{|name|
# 位置をリセット
self.go_home
#料金ページヘ
self.ryokin_page
self.ryokin_matome_page
self.latest_ryokin_gaiyou
browser.link(:text , name).click
browser.element(:onclick , /doui/).click
browser.wait
sleep 5 # ダウンロードの待ちを入れる
list = Dir.glob("*")
raise "ダウンロード失敗" if list.size == 0
puts `/usr/bin/env qpdf --decrypt #{list[0]} decrypted.pdf `
pdf_body = open("decrypted.pdf").read
Dir.glob("*").each{|e| File.unlink(e)}
result.push({"file_name" => "docomo-#{name}.pdf", "body"=>pdf_body })
}
result.each{|e|
open( e["file_name"], "w"){|f| f.write e["body"]}
}
`/usr/bin/env pdftk #{result.map{|e| e["file_name"] }.join(" ")} cat output out.pdf`
pdf_body = open("out.pdf").read
result = {"file_name" => "information.pdf", "body"=>pdf_body }
Dir.glob("*").each{|e| File.unlink(e)}
Dir.chdir pwd
return result
end
def get_latest_pdf_download
# 最新の料金の概要と明細をダウンロードして、1枚のPDFに結合する
pdf_files = []
# 料金概要
gaiyo = nil
gaiyo = self.get_latest_gaiyo_pdf
y_m = gaiyo["month"]
pdf_files.push(gaiyo)
# 明細を取得する
meisai = nil
meisai = self.get_latest_meisai_pdf
pdf_files.push(meisai)
## ドコモインフォ
info = nil
info = get_docomo_information
pdf_files.push(info)
pdf_files=pdf_files.flatten
out_path = "docomo-#{y_m}.pdf"
body = nil
pwd = Dir.pwd
Dir.chdir(@download_dir){
pdf_files.each{|e|
# ファイルの保存
open(e["file_name"], "w"){|f| f.print e["body"] }
}
puts "/usr/bin/env pdftk $(ls *.pdf) cat output #{out_path} "
puts `/usr/bin/env pdftk $(ls *.pdf) cat output #{out_path} `
body = open(out_path).read
Dir.glob("*").each{|e| File.unlink(e)}
}
return { "file_name"=> out_path, "month" => "#{y_m}", "body"=>body }
end
def go_home()
#@m.get "https://www.mydocomo.com/dcm/dfw/web/pub3/r/header/home.html"
browser.goto "https://www.nttdocomo.co.jp/mydocomo/"
browser.wait
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment