#!/usr/bin/env ruby
# coding: utf-8
# ライセンス条項(仮)
# 1.本プログラムは、
# Tカードの情報提供無効化または有効化(以下、目的)の為に
# 個人が自分のために使用する場合に限り、
# 無料で使用することが出来ます。
#
# 2.本プログラムに関する全ての権利は、Ituki Kirihara/NIに帰属します。
# 著作権等は一切放棄しておりません。
# (c) Copyright 2014 Ituki Kirihara/NI All rights reserved.
#
# 3.権利者は、本プログラムの使用その他全てに関して発生した全ての責任を負いません。
# 必ず個人の責任でご利用ください。
#
# 4.本ライセンスは仮ライセンスです。将来的に変更される可能性があります。
# 将来のライセンスは、「個人は目的の為になら無料で使用できる」という1.の項目以外に関しては、過去に遡って適用される可能性があります。
#
# 5.以下の事を行うことは出来ません
# 5.1.個人が、その個人本人のため以外に本プログラムを利用すること
# 第三者の代理実行、webによる本人以外へのサービス公開などは全て禁止します。
#
# 5.2.再配布(プログラムを改造したものも再配布は禁止します)
# 必ず、下に示すダウンロードURLへのリンクを用いて配布してください
#
# 5.3.ソースコードの一部、もしくは全てを、別の目的のために流用すること
# オープンソースではありますが、別目的への転用はできません
#
# 5.4.商用利用
#
# 5.5.アクセス先への攻撃目的での使用
#
# 5.6.本ライセンスの書き換え
#
# 6.以下の事を行うことが出来ます
# 6.1.個人が、その個人本人のために本プログラムを、目的を達成するために変更し、実行する事ができます。
# 本人が、目的の為に利用する場合に限り、ソースコードを改造することが出来ます。
# ただし、改造したソースコード全体を公開することはできません。
# ※改造方法をwebで公開する事は問題ありません。これにはパッチファイルによる公開も含まれます。
#
# 7.以下の事を承認する必要があります
# 7.1.本プログラムにより発生した全ての責任は、利用者が負う必要があります。
#
# 7.2.プログラムの改造方法などを公開した場合、以下(7.2.4.)の条件のいずれかに該当する場合は、
# 7.2.1.改造者(プログラム変更者)は、譲渡可能な全ての権利を Ituki Kirihara/NIに譲渡し、
# 7.2.2.改造者は、譲渡不可能な全ての権利を行使せず、
# 7.2.3.改造者は、譲渡可能/不可能を問わず、全ての権利行使をItuki Kirihara/NIに一任する
# ※譲渡不可能な権利を行使する場合は、Ituki Kirihara/NIの指示のみにより、改造者が行使する形とする
# 事を承認しなければなりません
# 7.2.4.条件
# 7.2.4.1.プログラムを変更しないことで、セキュリティ上の問題が発生する変更(セキュリティ修正一般)
# 7.2.4.2.プログラムに内包されていたバグの修正・コードの変更(追加機能のないバグ修正一般)
# 7.2.4.3.外部の仕様の変更等により、目的を達成することが出来なくなった場合に
# 公開された以外の方法で目標を達成することが困難な場合のコードの変更(外部の仕様変更による修正一般)
#
# 7.3.プログラムの改造方法などを公開した場合、上記(7.2.)に当てはまらない場合であっても、
# Ituki Kirihara/NIとの交渉により権利の譲渡等をお願いする場合があります。
#
# 7.4.本ライセンスについて、判断が難しい場合は、Ituki Kirihara/NIの判断に従う
#
# 7.5.Ituki Kirihara/NIは本プログラムに関するサポート義務を負わない
#
# 8.その他
# 8.1.本ライセンスによらず、Ituki Kirihara/NIが承認した場合にはその内容に従って使用することが可能です。
#
# 8.2.紹介、改造方法などをItuki Kirihara/NIへ通知する義務はありませんが、教えて貰えると嬉しいです。
#
#
# Ituki Kirihara/NI に関する情報
# Webページ
# http://fc.to/ituki/ いつき over TCP
# http://blog.proj.jp/ituki/ いつきログ
# Twitterアカウント
# @ityuki ( https://twitter.com/ityuki )
# 本プログラムに関する情報
# ダウンロードURL
# http://blog.proj.jp/ituki/data/apps/tools/tsite_uncheck.zip
# パッチ等置き場(準備中の可能性あり)
# http://i-repo.proj.jp/program/Application/Ruby/tools/tsite_uncheck/
#
file_yahoo_id = "account.txt"
file_enable_list = 'enable.txt'
file_output_org_html = "tsite.org.html"
file_output_dest_html = "tsite.html"
file_output_dest_data = "tsite.txt"
file_tmp_cookie_cache = 'cookie.cachedb' # if nil, not cache
@enable_msg = true
@enable_anser_msg = true
@urls = {
# first access page
'tsite-check-login' => 'https://tsite.jp/accounts/optout/index.pl',
# login tsite url header
'tsite-login-url-header' => 'https://tsite.jp/',
# login tsite url 2 header
'tsite-login-url2-header' => 'https://tsite.jp/tm/pc/login/STKIp0001001.do?',
# post id 'form1' -> location Yahoo
'tsite-yahoologin-post' => 'https://tsite.jp/tm/pc/login/STKIp0011002.do',
'tsite-yahoologin-post2' => '../login/STKIp0011002.do',
# yahoo url header -> location Yahoo
'yahoo-login-url-header' => 'https://auth.login.yahoo.co.jp/',
# yahoo url header2
'yahoo-login-url2-header' => 'https://login.yahoo.co.jp/',
# yahoo login post -> post id 'login_form'
'yahoo-login' => 'https://login.yahoo.co.jp/config/login?',
# ok yahoo chap header
'yahoo-login-ok-chap-url-header' => 'https://login.yahoo.co.jp/config/login?',
# ok yahoo location -> location Yahoo
'yahoo-login-ok-url-header' => 'https://auth.login.yahoo.co.jp/',
# ok yahoo location 2 -> location tsite
'yahoo-login-ok-url2-header' => 'https://auth.login.yahoo.co.jp/',
# ok login location tsite -> location tsite
'tsite-login-ok-url-header' => 'https://tsite.jp/tm/pc/login/STKIp0010000.do?',
# login ok and return list, optout_status = ?
'tsite-return' => 'https://tsite.jp/accounts/optout/index.pl',
'tsite-return2' => 'http://tsite.jp/',
# post DISABLE
'tsite-disable' => 'https://tsite.jp/accounts/optout/index.pl',
# tsite-logout
'tsite-logout' => 'http://tsite.jp/logout/index.pl',
# yahoo-logout
'yahoo-logout' => 'http://www.yahoo.co.jp/r/pl2',
}
# 実行開始
puts "> start program" if @enable_msg
require 'net/http'
require 'net/https'
require 'stringio'
require 'zlib'
require 'nokogiri'
require 'uri'
require 'Kconv'
require 'pstore'
@account = {}
@enable_id = []
File.foreach(file_yahoo_id){|l|
l.chomp!
if (@account['id'] == nil) then
@account['id'] = l
else
@account['pass'] = l
break
end
}
puts "* Yahoo ID:" + @account['id'] if @enable_msg
if File.exists?(file_enable_list) then
File.foreach(file_enable_list){|l|
l.chomp!
next if (l == "" or l =~ /^#/)
@enable_id.push(l)
}
end
@tsite_cookie = ()
@yahoo_cookie = ()
@user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36'
@post_content_type = 'application/x-www-form-urlencoded'
cookie_cache = nil
if file_tmp_cookie_cache != nil then
cookie_cache = PStore.new(file_tmp_cookie_cache)
end
if cookie_cache != nil then
cookie_cache.transaction do
if (cookie_cache['tsite.jp'] != nil) then
@tsite_cookie = cookie_cache['tsite.jp']
puts "* tsite.jp cookie cache enable" if @enable_msg
else
puts "* tsite.jp cookie cache disable" if @enable_msg
end
end
end
if (ENV.key?("OCRA_EXECUTABLE")) then
if File.exists?(File.dirname(ENV['OCRA_EXECUTABLE']) + "/cacert.pem") then
puts "* cert file is not original file" if @enable_msg
end
end
def get_cookie(cur,res)
cookie = cur
cookie = {} if (cookie == nil)
if (res.get_fields('set-cookie') != nil) then
res.get_fields('set-cookie').each{|str|
next if (str == "")
k,v = str[0...str.index('; ')].split('=',2)
k = '' if k == nil
v = '' if v == nil
cookie[k] = v
}
end
return nil if (cookie.length == 0)
return cookie
end
def access_site(url,post,cookie,ref)
uri = URI.parse(url)
target = uri.path
target = "/" if (target == nil or target == "" or target !~ /^\//)
target << "?" << uri.query if (uri.query != nil and uri.query != "")
http = nil
req = nil
if (url =~ /^http\:/) then
port = 80
port = uri.port if (uri.port != nil and uri.port != 80)
http = Net::HTTP.new(uri.host,port)
if (post == nil) then
req = Net::HTTP::Get.new(target)
else
req = Net::HTTP::Post.new(target)
req['content-type'] = @post_content_type
req['content-length'] = post.bytesize
req.body = post
end
elsif (url =~ /^https\:/) then
port = 443
port = uri.port if (uri.port != nil and uri.port != 443)
http = Net::HTTP.new(uri.host,port)
http.use_ssl = true
http.ca_path = File.dirname(__FILE__)
#http.ca_file = (File.dirname(__FILE__) + "/cacert.pem")
#http.ca_file = "cacert.pem"
if (ENV.key?("OCRA_EXECUTABLE")) then
if File.exists?(File.dirname(ENV['OCRA_EXECUTABLE']) + "/cacert.pem") then
http.ca_path = File.dirname(ENV['OCRA_EXECUTABLE'])
end
end
http.ca_file = http.ca_path + "/cacert.pem"
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
if (post == nil) then
req = Net::HTTP::Get.new(target)
else
req = Net::HTTP::Post.new(target)
req['content-type'] = @post_content_type
req['content-length'] = post.bytesize
req.body = post
end
else
print "This url can't access method in program. sorry\nURL: ",url
exit
end
# add UA
req.add_field('User-Agent',@user_agent)
# add Accept
req.add_field('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
req.add_field('Accept-Language','ja,en-us;q=0.7,en;q=0.3')
req.add_field('Accept-Encoding','gzip, deflate')
# add referer
if (ref != nil) then
req.add_field('Referer',ref)
end
# add cookie
if cookie != nil then
c = ""
cookie.each{|k,v|
c << "; " if c != ""
c << k << '=' << v
}
if (c != "") then
req.add_field('Cookie',c)
end
end
res = http.request(req)
if (res.code == "301" or res.code == "302") then
return {'location' => res['location'], 'cookie' => get_cookie(cookie,res) }
elsif (res.code == "200") then
body = res.body
if (res['content-encoding'] == nil or res['content-encoding'] == "") then
# ok
elsif (res['content-encoding'] == 'gzip') then
tmp = ""
StringIO.open(body, 'rb'){|sio|
tmp = Zlib::GzipReader.wrap(sio).read
}
body = tmp
elsif(res['content-encoding'] == 'deflate') then
begin
body = Zlib::Inflate.inflate(body)
rescue Zlib::DataError
body = Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(body)
end
else
print "This url can't accept encoding. sorry\ncontent-encoding:",res['content-encoding'],"\nURL: ",url
exit
end
charset = nil
if (res['content-type'] =~ /charset/) then
charset = res['content-type'].sub(/^(?:text|application)\/[A-Za-z0-9\-_]+[\s ]*?\;[\s ]*?charset[\s ]*?\=[\s ]*?([A-Za-z0-9\-_]+)[\s ]*?$/i,'\1')
end
if (!charset) then
if (res['content-type'] =~ /^text\/html[^A-Za-z0-9\-_]?/) then
# html check
charset = body.match(/\/i).to_a[1]
if (!charset) then
charset = body.match(/\]*?\/?[^\>]*?\>/i).to_a[1]
end
elsif (res['content-type'] =~ /^text\/xml[^A-Za-z0-9\-_]?/) then
# xml check
charset = body.match(/<\?xml[\s \t\n\r\f]+?version[\s \t\n\r\f]*?(?:\=|\=\"|\=\')[0-9]+\.[0-9]+(?:\"|\')?[\s \t\n\r\f]+?encoding[\s \t\n\r\f]*?(?:\=|\=\"|\=\')([A-Za-z0-9\-\_]+)(?:\"|\')?/i).to_a[1]
charset = "utf-8" if (!charset)
elsif (res['content-type'] =~ /^application\/(?:[A-Za-z0-9_\-\.]+\+xml|xml|[A-Za-z0-9_\-\.]+\.xml(?:\.[A-Za-z0-9_\-\.]+)?)[^A-Za-z0-9\-_]?/) then
# xmlっぽいの check
charset = body.match(/<\?xml[\s \t\n\r\f]+?version[\s \t\n\r\f]*?(?:\=|\=\"|\=\')[0-9]+\.[0-9]+(?:\"|\')?[\s \t\n\r\f]+?encoding[\s \t\n\r\f]*?(?:\=|\=\"|\=\')([A-Za-z0-9\-\_]+)(?:\"|\')?/i).to_a[1]
# デフォルト文字コードは設定しない
elsif (res['content-type'] =~ /^text\/css[^A-Za-z0-9\-_]?/) then
# css check
charset = body.match(/\@charset[\s]+\"([A-Za-z0-9\-\_]+)\"[\s]*\;/i).to_a[1]
end
if (charset == body) then
charset = nil
end
end
if (!charset) then
case Kconv.guess(body)
when Kconv::JIS
charset="jis"
when Kconv::EUC
charset="euc-jp"
when Kconv::SJIS
charset="sjis"
when Kconv::UTF8
charset="utf-8"
when Kconv::UTF16
charset="utf-16"
when Kconv::ASCII
charset="jis" # hack: ascii mode. $KCODE == "NONE" then UTF-8 mode. UTF-8 encode is yen or backslash problem :)
when Kconv::BINARY
# error
else
# error
end
# end case
end
if (charset =~ /^jis$/i or
charset =~ /^ISO\-2022\-JP$/i or
charset =~ /^csISO2022JP$/i or
charset =~ /^ISO\-2022\-JP\-2$/i or
charset =~ /^csISO2022JP2$/i) then
# jis
body.force_encoding('JIS')
elsif (charset =~ /^euc\-jp$/i or
charset =~ /^csEUCPkdFmtJapanese$/i) then
# euc-jp
body.force_encoding('EUC-JP')
elsif (charset =~ /^sjis$/i or
charset =~ /^Shift_JIS$/i or
charset =~ /^Shift\-JIS$/i or # error
charset =~ /^Windows\-31J$/i or
charset =~ /^csWindows31J$/i or
charset =~ /^MS_Kanji$/i or
charset =~ /^csShiftJIS$/i or
charset =~ /^CP932$/) then
# sjis
body.force_encoding('SJIS')
elsif (charset =~ /^UTF\-8$/i) then
# UTF-8
body.force_encoding('UTF-8')
elsif (charset =~ /^UTF\-16$/i) then
# UTF-16
body.force_encoding('UTF-16')
else
# unknown
body.force_encoding("ASCII-8BIT")
end
return {'body' => body, 'cookie' => get_cookie(cookie,res) }
else
print "This url can't execute this responce code in program. sorry\nRespoce Code:",res.code,"\nURL: ",url
exit
end
end
def check_url_header(url,header)
return if url[0,header.length] == header
print "URL header is not match. break program!\nURL:",url,"\nURL-Header:",header
raise
exit
end
def gen_urldata(current,key,val)
current = "" if (current == nil)
current << '&' if (current != '')
current << URI.encode_www_form_component(key) << "=" << URI.encode_www_form_component(val)
return current
end
def gen_urldatas(datas)
c = ""
datas.each{|k,v|
v.each{|v2|
c = gen_urldata(c,k,v2)
}
}
return c
end
def get_form_elements(doc)
ret = {}
#print doc.inner_html.encode("UTF-8").gsub(/\<\/?noscripts*(?:\/\s*)?\>/,'')
#doc = Nokogiri::HTML.parse(doc.inner_html.encode("UTF-8").gsub(/\<\/?noscripts*(?:\/\s*)?\>/,''))
doc.xpath('//input').each{ |node|
if (node['type'] == "text" or node['type'] == "password" or node['type'] == "hidden" or node['type'] == "submit") then
n = node['name']
n = '' if (n == nil)
ret[n] = [] if (ret[n] == nil)
ret[n].push(node['value'])
elsif(node['type'] == "checkbox" or node['type'] == "radio") then
if (node['checked'] != nil) then
n = node['name']
n = '' if (n == nil)
ret[n] = [] if (ret[n] == nil)
ret[n].push(node['value'])
end
elsif(node['type'] == "file" or node['type'] == "reset" or node['type'] == "button" or node['type'] == "image") then
# noting
else
print "Unknown type input type, skip:",node['type'],"\n"
end
}
doc.xpath('//textarea').each{ |node|
n = node['name']
n = '' if (n == nil)
ret[n] = [] if (ret[n] == nil)
ret[n].push(node['value'])
}
doc.xpath('//select').each{ |node|
n = node['name']
n = '' if (n == nil)
ret[n] = [] if (ret[n] == nil)
node.xpath('option').each{ |snode|
if (snode['selected'] != nil) then
ret[n].push(snode['value'])
end
}
}
return ret
end
# execute
# 1st access
puts "-> access to tsite.jp" if @enable_msg
ref = nil
ret = access_site(@urls['tsite-check-login'],nil,@tsite_cookie,ref)
@tsite_cookie = ret['cookie']
ref = @urls['tsite-check-login']
if (cookie_cache == nil or ret['location'] != nil or ret['body'] == nil or ret['body'] == "") then
puts "* tsite.jp and yahoo.co.jp login mode" if @enable_msg
# default
url = ret['location']
check_url_header(url,@urls['tsite-login-url-header'])
# location
puts "-> access to tsite.jp login page" if @enable_msg
ret = access_site(url,nil,@tsite_cookie,ref)
@tsite_cookie = ret['cookie']
ref = url
url = ret['location']
check_url_header(url,@urls['tsite-login-url2-header'])
# tsite login page access
puts "-> access to tsite.jp login form page" if @enable_msg
ret = access_site(url,nil,@tsite_cookie,ref)
@tsite_cookie = ret['cookie']
doc = Nokogiri::HTML.parse(ret['body'])
form = doc.xpath('//form[@id="form1"]')
if (form.attr('action').value != @urls['tsite-yahoologin-post'] and form.attr('action').value != @urls['tsite-yahoologin-post2']) then
print "tsite login address is fail\nrequest:",@urls['tsite-yahoologin-post'],"\ninput:",form.attr('action').value
exit
end
if (form.attr('method').value.downcase != 'post') then
print "tsite login address method fail\nrequest:POST\ninput:",form.attr('method').value
exit
end
# post login to tsite
ref = url
url = @urls['tsite-yahoologin-post']
puts "-> access to yahoo.co.jp redirect from tsite.jp page" if @enable_msg
ret = access_site(url,gen_urldatas(get_form_elements(form)),@tsite_cookie,ref)
@tsite_cookie = ret['cookie']
# location yahoo login(location)
ref = url
url = ret['location']
check_url_header(url,@urls['yahoo-login-url-header'])
puts "-> access to tsite - yahoo login gw page" if @enable_msg
ret = access_site(url,nil,@yahoo_cookie,ref)
@yahoo_cookie = ret['cookie']
# location yahoo login page
ref = url
url = ret['location']
check_url_header(url,@urls['yahoo-login-url2-header'])
puts "-> access to yaho.co.jp login form page" if @enable_msg
ret = access_site(url,nil,@yahoo_cookie,ref)
@yahoo_cookie = ret['cookie']
doc = Nokogiri::HTML.parse(ret['body'])
form = doc.xpath('//form[@id="login_form"]')
if (form.attr('action').value != @urls['yahoo-login']) then
print "tsite login address is fail\nrequest:",@urls['yahoo-login'],"\ninput:",form.attr('action').value
exit
end
if (form.attr('method').value.downcase != 'post') then
print "tsite login address method fail\nrequest:POST\ninput:",form.attr('method').value
exit
end
# post login to yahoo
puts "* yahoo.co.jp login, wait a 5 sec" if @enable_msg
sleep(5)
ref = url
url = @urls['yahoo-login']
fdata = get_form_elements(form)
fdata['login'] = [@account['id']]
fdata['passwd'] = [@account['pass']]
# javascript : .albatross value change
alb = ret['body'].match(/document\.getElementsByName\(\"\.albatross\"\)\[0\]\.value\s*\=\s*\"([^"]+)\"/)[1]
fdata['.albatross'] = [alb] if alb != nil
fdata.delete('.nojs')
# unchecked
fdata.delete('.persistent')
fdata.delete('')
puts "-> access to yahoo.co.jp login" if @enable_msg
ret = access_site(url,gen_urldatas(fdata),@yahoo_cookie,ref)
@yahoo_cookie = ret['cookie']
authloop = false
while (ret['location'] == nil or ret['location'] == @urls['yahoo-login']) do
puts "! login error, no direct login" if @enable_msg and !authloop
puts "! captcha error, retry" if @enable_msg and authloop
if (ret['body'] != nil and ret['body'] != "" and ret['body'] =~ /captchaAnswer/) then
puts "* yahoo.co.jp request captcha" if @enable_msg
doc = Nokogiri::HTML.parse(ret['body'])
form = doc.xpath('//form[@id="captchaForm"]')
fdata = get_form_elements(form)
imgurl = form.xpath('//img[@id="captchaV5MultiByteCaptchaImg"]')[0]['src']
print "画像認証画面が必要です。\n連続アクセスした時などに必要となります\n"
print "改行(Enter)すると次のURLを開きます。表示されている文字を入力してください\n"
print "URL:",imgurl,"\n"
gets
system("start "+imgurl)
print "表示された文字が読めない場合はそのまま改行を押してください(再トライします)\n終了する場合は、半角スペースと改行を入力してください"
print "表示されている文字を入力してください:"
STDOUT.flush
chap = gets
chap = chap.encode("UTF-8")
chap.chomp!
if (chap != nil and (chap =~ /^[ \t]+$/)) then
print "中止しました\n"
exit
end
fdata['captchaAnswer'] = [chap]
ref = url
puts "-> access to yahoo.co.jp captcha auth" if @enable_msg
ret = access_site(url,gen_urldatas(fdata),@yahoo_cookie,ref)
@yahoo_cookie = ret['cookie']
authloop = true
else
print "Yahoo login fail... ID or PASSWORD is error!\n"
exit
end
end
if authloop == true then
# location to yahoo 0
ref = url
url = ret['location'] # checked
check_url_header(url,@urls['yahoo-login-ok-chap-url-header'])
puts "* captcha auth ok" if @enable_msg
puts "-> access to yahoo.co.jp after captche page" if @enable_msg
ret = access_site(url,nil,@yahoo_cookie,ref)
@yahoo_cookie = ret['cookie']
if (ret['body'] != nil) then
puts "* yahoo.co.jp request retry login" if @enable_msg
doc = Nokogiri::HTML.parse(ret['body'])
form = doc.xpath('//form[@id="login_form"]')
if (form.attr('action').value != @urls['yahoo-login']) then
print "tsite login address is fail\nrequest:",@urls['yahoo-login'],"\ninput:",form.attr('action').value
exit
end
if (form.attr('method').value.downcase != 'post') then
print "tsite login address method fail\nrequest:POST\ninput:",form.attr('method').value
exit
end
# post re-login to yahoo
ref = url
url = @urls['yahoo-login']
fdata = get_form_elements(form)
fdata['login'] = [@account['id']]
fdata['passwd'] = [@account['pass']]
fdata.delete('.nojs')
fdata.delete('')
puts "-> access to yahoo.co.jp retry login" if @enable_msg
ret = access_site(url,gen_urldatas(fdata),@yahoo_cookie,ref)
@yahoo_cookie = ret['cookie']
if (ret['location'] == nil) then
print "Yahoo login fail... ID or PASSWORD is error!\n"
exit
end
end
end
# Login OK
# location to yahoo 1
ref = url
url = ret['location']
check_url_header(url,@urls['yahoo-login-ok-url-header'])
puts "* yahoo.co.jp login ok" if @enable_msg
puts "-> access to yahoo.co.jp to yahoo.co.jp auth data locate page" if @enable_msg
ret = access_site(url,nil,@yahoo_cookie,ref)
@yahoo_cookie = ret['cookie']
# location to yahoo 2
ref = url
url = ret['location'] # checked
check_url_header(url,@urls['yahoo-login-ok-url2-header'])
puts "-> access to yahoo.co.jp redirest to tsite.jp page" if @enable_msg
ret = access_site(url,nil,@yahoo_cookie,ref)
@yahoo_cookie = ret['cookie']
# return to tsite
ref = url
url = ret['location'] # checked
check_url_header(url,@urls['tsite-login-ok-url-header'])
puts "-> access to tsite.jp login ok page" if @enable_msg
ret = access_site(url,nil,@tsite_cookie,ref)
@tsite_cookie = ret['cookie']
# return to original URL
if (ret['location'] != @urls['tsite-return'] and ret['location'] != @urls['tsite-return2']) then
print "tsite/yahoo login error...\nlocation:",ret['location'],"\nreturn:",@urls['tsite-return'],"\nreturn2:",@urls['tsite-return2'],"\n"
print "このエラーは、Tカードと関連付いていないYahooIDでログインした場合も発生します\n"
exit
end
ref = url
url = @urls['tsite-disable']
puts "-> access to tsite.jp optout page" if @enable_msg
ret = access_site(url,nil,@tsite_cookie,ref)
@tsite_cookie = ret['cookie']
if (ret['location'] != nil or ret['body'] == nil or ret['body'] == "") then
print "tsite/yahoo login error(2)..."
exit
end
end
# login end!
# update data
doc = Nokogiri::HTML.parse(ret['body'])
form = nil
doc.xpath('//form').each{|node|
if (node['action'] == @urls['tsite-disable']) then
form = node
break
end
}
if (form == nil or form.attr('method').downcase != 'post') then
print "tsite update error..."
exit
end
fdata = get_form_elements(form)
#if (fdata['optout_status'] != nil) then
# fdata['optout_status'].each{|id|
# print "Enable ID:",id.to_s
# ok = false
# @enable_id.each{|eid|
# if (id == eid) then
# ok = true
# break
# end
# }
# if ok then
# print " -> enable\n"
# else
# print " -> disable\n"
# end
# }
# if (@enable_id.length > 0) then
# # enable id found
# @enable_id.each{|id|
# print "-> Enable ID:",id.to_s,"\n"
# }
# fdata['optout_status'] = @enable_id
# else
# # all clear
# fdata.delete('optout_status')
# end
#end
File.open(file_output_org_html,"w"){|io|
io.write(ret['body'])
}
if (@enable_id.length > 0) then
fdata['optout_status'] = @enable_id
else
fdata.delete('optout_status')
end
# output list
File.open(file_output_dest_data,"w"){|io|
io.puts "update time: " + Time.now.to_s,""
form.xpath('tr').each{|node|
no = Nokogiri::HTML.parse(node.inner_html.encode("UTF-8"))
id = no.xpath('//input[@name="optout_status"]').attr('value').value
name = no.xpath('//div[@class="text_box01"]').inner_text.gsub(/\n.*:/,'(') + ')'
checked = no.xpath('//input[@name="optout_status"]').attr('checked') == nil ? false : true
toOn = false
@enable_id.each{|eid|
toOn = true if id == eid
}
io.print id,":",name,"\n ",checked ? ( toOn ? "*Enable*" : "Enable->disable" ) : (toOn ? "*disable->Enable*" : "disable"),"\n"
}
}
puts "* optout information" if @enable_msg
File.foreach(file_output_dest_data){|l|
l.force_encoding("UTF-8")
print "** " if @enable_anser_msg and @enable_msg
print l if @enable_anser_msg
}
# to disable
ref = url
url = @urls['tsite-disable']
fdata['on_next.x'] = ["20"]
fdata['on_next.y'] = ["30"]
puts "-> access to tsite.jp optout execute" if @enable_msg
ret = access_site(url,gen_urldatas(fdata),@tsite_cookie,ref)
@tsite_cookie = ret['cookie']
if (ret['body'] == nil or ret['body'] == "") then
puts "! optout update error!!!!"
puts "!! can't recovery"
else
puts "* optout update sucsess" if @enable_msg
end
ref = url
url = @urls['tsite-disable']
puts "-> access to tsite.jp optout result page" if @enable_msg
ret = access_site(url,nil,@tsite_cookie,ref)
@tsite_cookie = ret['cookie']
File.open(file_output_dest_html,"w"){|io|
io.write(ret['body'])
}
# logput or save
if cookie_cache != nil then
puts "* save tsite.jp cookie data" if @enable_msg
puts "* tsite.jp is not logout" if @enable_msg
cookie_cache.transaction do
cookie_cache['tsite.jp'] = @tsite_cookie
end
else
puts "-> access to tsite.jp logout page" if @enable_msg
access_site(@urls['tsite-logout'],nil,@tsite_cookie,nil)
end
puts "-> access to yahoo.co.jp logout page" if @enable_msg
access_site(@urls['yahoo-logout'],nil,@yahoo_cookie,nil)
#system("start "+file_output_dest_html)
puts "> program finished" if @enable_msg