ã·ã¹ãã ãã¬ã¼ããæµè¡ãã ãä¸ç¬ã§åãå¤åãã追ããããããã®ã¯ãGSã¨ã大æã®ç¤¾å¡ã ãã ãç´ äººã«ã¯ã·ã¹ãã ãã¬ã¼ãã®APIãå ¬éãããªãã
ããªç§åä½ã®ãã¬ã¼ãã¨ããããã
æ±äº¬è¨¼å¸åå¼æã¯ãããªç§åä½ã§æ³¨æãæãããããããä¸è¬ç´ 人ã¯ã§ããªãã
ãªã®ã§ã大æã®ããã¸ãã¡ã³ãã®ç©ãå ´ã«ãªã£ã¦ããæ°ãããã
ç´ äººã§ãã§ããã ãæ©ãçºæ³¨ã§ããªãããã¨èãã¦ã¹ã¯ãªããçµãã§ã¿ãã
ããããæ ªä¾¡ããã§ãã¯ã§ããªãã¨ããªç§åä½ã§çºæ³¨ã§ããªãã®ã§çºæ³¨ã¹ã¯ãªããã ãã§ã¯å¤§é¨ã§ããªãããã©ããããã®ããã§ããã ãé 延ãªããªã¢ã«ã¿ã¤ã æ ªä¾¡åºãæ¹æ³ãªãããªã
SBI証å¸ã®æ³¨æç¨ã®ã¯ã©ã¤ã¢ã³ã
SBIã®ããã¯ã¢ãããµã¤ãããã¼ã¹ã«ä½ã£ã¦ããã®ã§ãããªãã«ãé·æéåãã¨æãã¾ãã
gems ã«ç»é²ãããã¨æã£ããã©ãæ¢ã«åããããªãã®ããã£ããã§é æ
®ãã¦ããã
ãã¨ãããã¯ã¢ãããµã¤ãã¯4/3ç§ã¤ã¾ãã1.333ç§/åããè¶
ãããªã¯ã¨ã¹ãã¯å¼¾ãã®ã§ãã©ããããã®ããã©ãã ãé å¼µã£ã¦ã1.5ç§ä»¥ä¸ã®çºæ³¨ã¯ã§ããªã模æ§ããµã¼ã
#coding: utf-8 require 'mechanize' class Mechanize::HTTP::Agent def post_connect uri, response, body_io # :yields: agent, uri, response, body @post_connect_hooks.each do |hook| begin hook.call self, uri, response, body_io ensure body_io.rewind end end end end class Mechanize::HTTP::Agent @@interval = 0.75 alias_method :original_fetch, :fetch def fetch( uri, method = :get, headers = {}, params = [], referer = current_page, redirects = 0) page = nil cnt = 0 page = self.original_fetch( uri, method, headers, params, referer , redirects) until (page.title =~ /error/).nil? do # SBIããã¯ã¢ãããµã¤ãã¯ç§éï¼ï¼ï¼åã®ãªã¯ã¨ã¹ã以ä¸ãåãä»ãã¦ãããªãã puts "æ¥ç¶æ°ã¨ã©ã¼..ååå¾ãã¾ã(#{cnt+1}åç®)" page = self.original_fetch( uri, method, headers, params, referer , redirects) sleep 0.8 + 0.1 * cnt cnt = cnt + 1 end return page end end # SBIã®ããã¯ã¢ãããµã¤ããscraperãã¦ã注æãç°¡åã«åºã class SBISec attr_accessor :m def initialize( login_name, login_pass,order_pass ) @@login_name, @@login_pass,@@order_pass = login_name, login_pass,order_pass @m = Mechanize.new m.ssl_version = 'SSLv3' @cookie = "saved.sbi_cookie.jar.yml" m.post_connect_hooks << lambda{|ua,uri,res,body_io| body = body_io.read.toutf8 body.gsub! /Shift_JIS/,"utf-8" body_io.truncate body_io.pos body_io.rewind body_io.puts body } self.login end def login m.get "https://k.sbisec.co.jp/" m.page.forms[0].field_with( :name => "password").value= @@login_pass m.page.forms[0].field_with( :name => "username").value= @@login_name m.page.forms[0].submit end def go_to_top begin m.page.link_with( :text => /ããã/).click rescue => e m.get "https://k.sbisec.co.jp/bsite/member/menu.do" end cnt = 0 until (m.page.title =~ /error/).nil? do puts "ã¨ã©ã¼..ååå¾#{cnt+1}åç®" m.get "https://k.sbisec.co.jp/bsite/member/menu.do" puts m.page.title sleep 1 + 0.1 * cnt cnt = cnt + 1 end end def price_now(stock_id) m.page.link_with( :text => /æ ªä¾¡/).click m.page.forms[0].field_with( :name=>"ipm_product_code").value=stock_id m.page.forms[0].click_button(m.page.forms[0].button_with :name => /id/) price = m.page.at("td[text()^='ç¾å¨']").parent.at("font").text # ç¾å¨å¤ return price end def keep_session self.go_to_top end def orders m.page.link_with( :text =>/注æåæ¶ï¼è¨æ£/).click m.page.search("div//tr//table[cellspacing='0'][cellpadding='0'][width='100%'][border='0']") list = m.page.search('div//table[border="0"][cellspacing="0"][cellpadding="0"][width="100%"][style="margin-top:2px;"]/tr/td/table/tr') list = list.map{|e| e.to_html.gsub(/\r|\n|\t/, "") } orders = list.each_slice(4).map orders = orders.map{|e| e.join } orders = orders.map{|e| "<table>#{e}</table>" } orders = orders.map{|e| Nokogiri::HTML(e) } orders = orders.map{|e| { "éæ" => e.at("//tr[1]/td/table/tr/td[1]").text, "注æå価" => e.at("//tr[5]/td[4]").text, "æ ªæ°" => e.at("//tr[5]/td[2]").text, "種é¡" => e.at("//tr[4]/td[2]").text, "詳細ãªã³ã¯" => e.at("//tr[1]/td/table/tr/td[1]/a").attr("href"), "åæ¶ãªã³ã¯" => e.at("//tr[1]/td/table/tr/td[2]/a[2]").attr("href"), "è¨æ£ãªã³ã¯" => e.at("//tr[1]/td/table/tr/td[2]/a[1]").attr("href"), } } end def cancel_all_order(stock=nil) puts stock.nil? self.orders.select{|e| if stock.nil? then true elsif e["éæ"] =~ /#{stock}/ then true else false end }.each{|e| puts e["åæ¶ãªã³ã¯"] m.get e["åæ¶ãªã³ã¯"] m.page.forms[0].field_with( :name=>"password").value=@@order_pass m.page.forms[0].submit } self.go_to_top end def force_buy(stock_id,quantity) #æè¡ã§è²·ãã self.buy(stock_id,quantity,nil,"æè¡:ç¡æ¡ä»¶") end def wait_buy(stock_id,quantity,price) #æå¤ã§è²·ãã self.buy(stock_id,quantity,price ) end alias_method :at_market_order, :force_buy alias_method :limit_order , :wait_buy def buy( stock_id, quantity, price=nil, order_type="æå¤:ç¡æ¡ä»¶") #æ ªä¾¡ãã¼ã¸ m.page.link_with( :text => /æ ªä¾¡/).click m.page.forms[0].field_with( :name=>"ipm_product_code").value= stock_id m.page.forms[0].click_button(m.page.forms[0].button_with :name => /id/) m.page.link_with( :text =>/ä¿¡ç¨æ°è¦è²·/).click self.submit_order( stock_id, quantity, price, order_type) self.go_to_top end def force_sell(stock_id,quantity) #æè¡ã§è²·ãã self.sell(stock_id,quantity,nil,"æè¡:ç¡æ¡ä»¶") end def wait_sell(stock_id,quantity,price) #æå¤ã§è²·ãã self.sell(stock_id,quantity,price ) end alias_method :at_market_order, :force_buy alias_method :limit_order , :wait_buy def sell( stock_id, quantity, price=nil, order_type="æå¤:ç¡æ¡ä»¶") #æ ªä¾¡ãã¼ã¸ m.page.link_with( :text => /æ ªä¾¡/).click m.page.forms[0].field_with( :name=>"ipm_product_code").value= stock_id m.page.forms[0].click_button(m.page.forms[0].button_with :name => /id/) m.page.link_with( :text =>/ä¿¡ç¨æ°è¦å£²/).click self.submit_order( stock_id, quantity, price, order_type) self.go_to_top end def submit_order( stock_id, quantity, price=nil, order_type) #çºæ³¨ãã¼ã¸ # <select name="sasinari_kbn"> # <option value="DEFAULT_SELECT_VALUE" selected="">é¸æãã¦ä¸ãã</option> # <option value=" ">æå¤:ç¡æ¡ä»¶</option> # <option value="Z">æå¤:å¯æ</option> # <option value="I">æå¤:å¼æ</option> # <option value="F">æå¤:ä¸æ</option> # <option value="P">æå¤:IOC</option> # <option value="N">æè¡:ç¡æ¡ä»¶</option> # <option value="Y">æè¡:å¯æ</option> # <option value="H">æè¡:å¼æ</option> # <option value="O">æè¡:IOC</option> # </select> m.page.forms[0].field_with( :name=> "price").value = price m.page.forms[0].field_with( :name=> /quantity/).value = quantity #è¿æ¸æ³¨æã®ã¨ã㯠sum_quantity m.page.forms[0].field_with( :name=>"sasinari_kbn").option_with( :text=>/#{order_type}/).select m.page.forms[0].field_with( :name=>"password").value=@@order_pass m.page.forms[0].submit m.page.forms[0].submit end def return_sell( stock_id, quantity, price, order_type="æå¤:ç¡æ¡ä»¶") #è¿æ¸å£²ã m.page.link_with( :text => /æ ªä¾¡/).click m.page.forms[0].field_with( :name=>"ipm_product_code").value=stock_id m.page.forms[0].click_button(m.page.forms[0].button_with :name => /id/) m.page.link_with( :text =>/ä¿¡ç¨è¿æ¸ï¼ç¾å¼ç¾æ¸¡/).click m.page.link_with( :text =>/è¿æ¸å£²/).click self.submit_order( stock_id, quantity, price, order_type) self.go_to_top end end