Class: YahooCrawler
- Inherits:
-
Object
- Object
- YahooCrawler
- Defined in:
- lib/options-lib/yahoo_crawler.rb
Overview
A Yahoo!Finance crawler implemented using Mechanize to parse HTML and extract options quotes for given stock and expiration date.
Instance Method Summary collapse
- #auto_reload(period = 60) ⇒ Object
- #call_options ⇒ Object
- #call_strikes ⇒ Object
- #fetch ⇒ Object
- #get_call_option_quote(strike) ⇒ Object
- #get_option_quote(type, strike) ⇒ Object
- #get_put_option_quote(strike) ⇒ Object
-
#initialize(stock, exp) ⇒ YahooCrawler
constructor
A new instance of YahooCrawler.
- #join_reload_thread ⇒ Object
- #put_options ⇒ Object
- #put_strikes ⇒ Object
- #show_calls ⇒ Object
- #show_puts ⇒ Object
- #stock_price ⇒ Object
- #stop ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(stock, exp) ⇒ YahooCrawler
Returns a new instance of YahooCrawler.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/options-lib/yahoo_crawler.rb', line 12 def initialize(stock, exp) @mech = Mechanize.new @stock, @exp = stock.upcase, exp @url = "http://finance.yahoo.com/q/op?s=#{stock}&m=#{exp[0,7]}" @stock_price = nil = Hash.new = Hash.new @call_strikes = Array.new @put_strikes = Array.new @lock = Mutex.new @t_lock = Mutex.new @thread = nil end |
Instance Method Details
#auto_reload(period = 60) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/options-lib/yahoo_crawler.rb', line 30 def auto_reload(period = 60) if not @thread.nil? stop end @thread = Thread.new do loop do begin fetch yield # callback rescue => e puts "Error fetching data: #{e.message}" end sleep period end end end |
#call_options ⇒ Object
134 135 136 |
# File 'lib/options-lib/yahoo_crawler.rb', line 134 def @lock.synchronize { } end |
#call_strikes ⇒ Object
126 127 128 |
# File 'lib/options-lib/yahoo_crawler.rb', line 126 def call_strikes @lock.synchronize { @call_strikes } end |
#fetch ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/options-lib/yahoo_crawler.rb', line 60 def fetch , = Hash.new, Hash.new c_strikes, p_strikes = Array.new, Array.new stock_price = nil # A small hack to make my hashes accept integers instead of floats for the strike prices add_convert_method_for_keys(, ) @t_lock.synchronize { # don't step into each other in case someone calls fetch page = @mech.get(@url).body curr_price = page.scan(/\: \<.+?\<\/big\>/) lines = parse_data(curr_price[0]) stock_price = lines[0].to_f calls = page.scan(/Strike\<.+Put Options/) puts = page.scan(/Put Options\<.+Highlighted/) lines = parse_data(calls[0]) lines = lines.chunk(lines.length / 8) lines.each do |array| quote = parse_quote(array, Option::CALL, stock_price) [quote.option.strike] = quote end lines = parse_data(puts[0]) lines = lines.chunk(lines.length / 8) lines.each do |array| quote = parse_quote(array, Option::PUT, stock_price) [quote.option.strike] = quote end .keys.sort.each { |key| c_strikes << key } .keys.sort.each { |key| p_strikes << key } } @lock.synchronize { , = , @call_strikes, @put_strikes = c_strikes, p_strikes @stock_price = stock_price } nil end |
#get_call_option_quote(strike) ⇒ Object
118 119 120 |
# File 'lib/options-lib/yahoo_crawler.rb', line 118 def get_call_option_quote(strike) [strike] end |
#get_option_quote(type, strike) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/options-lib/yahoo_crawler.rb', line 110 def get_option_quote(type, strike) if type == Option::CALL get_call_option_quote(strike) else get_put_option_quote(strike) end end |
#get_put_option_quote(strike) ⇒ Object
122 123 124 |
# File 'lib/options-lib/yahoo_crawler.rb', line 122 def get_put_option_quote(strike) [strike] end |
#join_reload_thread ⇒ Object
56 57 58 |
# File 'lib/options-lib/yahoo_crawler.rb', line 56 def join_reload_thread @thread.join if @thread end |
#put_options ⇒ Object
138 139 140 |
# File 'lib/options-lib/yahoo_crawler.rb', line 138 def @lock.synchronize { } end |
#put_strikes ⇒ Object
130 131 132 |
# File 'lib/options-lib/yahoo_crawler.rb', line 130 def put_strikes @lock.synchronize { @put_strikes } end |
#show_calls ⇒ Object
146 147 148 |
# File 'lib/options-lib/yahoo_crawler.rb', line 146 def show_calls call_strikes.each { |key| puts [key] } end |
#show_puts ⇒ Object
150 151 152 |
# File 'lib/options-lib/yahoo_crawler.rb', line 150 def show_puts put_strikes.each { |key| puts [key] } end |
#stock_price ⇒ Object
142 143 144 |
# File 'lib/options-lib/yahoo_crawler.rb', line 142 def stock_price @lock.synchronize { @stock_price } end |
#stop ⇒ Object
49 50 51 52 53 54 |
# File 'lib/options-lib/yahoo_crawler.rb', line 49 def stop if not @thread.nil? @thread.kill @thread = nil end end |
#to_s ⇒ Object
26 27 28 |
# File 'lib/options-lib/yahoo_crawler.rb', line 26 def to_s "YahooCrawler('#{@stock}','#{@exp})" end |