Class: YahooCrawler

Inherits:
Object
  • Object
show all
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

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
  @call_options = Hash.new
  @put_options = 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_optionsObject



134
135
136
# File 'lib/options-lib/yahoo_crawler.rb', line 134

def call_options
  @lock.synchronize { @call_options }
end

#call_strikesObject



126
127
128
# File 'lib/options-lib/yahoo_crawler.rb', line 126

def call_strikes
  @lock.synchronize { @call_strikes }
end

#fetchObject



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

  c_options, p_options = 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(c_options, p_options)

  @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)
      c_options[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)
      p_options[quote.option.strike] = quote
    end

    c_options.keys.sort.each { |key| c_strikes << key }

    p_options.keys.sort.each { |key| p_strikes << key }

  }

  @lock.synchronize {
    @call_options, @put_options = c_options, p_options
    @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)
  call_options[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)
  put_options[strike]
end

#join_reload_threadObject



56
57
58
# File 'lib/options-lib/yahoo_crawler.rb', line 56

def join_reload_thread
    @thread.join if @thread
end

#put_optionsObject



138
139
140
# File 'lib/options-lib/yahoo_crawler.rb', line 138

def put_options
  @lock.synchronize { @put_options }
end

#put_strikesObject



130
131
132
# File 'lib/options-lib/yahoo_crawler.rb', line 130

def put_strikes
  @lock.synchronize { @put_strikes }
end

#show_callsObject



146
147
148
# File 'lib/options-lib/yahoo_crawler.rb', line 146

def show_calls
  call_strikes.each { |key| puts call_options[key] }
end

#show_putsObject



150
151
152
# File 'lib/options-lib/yahoo_crawler.rb', line 150

def show_puts
  put_strikes.each { |key| puts put_options[key] }
end

#stock_priceObject



142
143
144
# File 'lib/options-lib/yahoo_crawler.rb', line 142

def stock_price
  @lock.synchronize { @stock_price }
end

#stopObject



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_sObject



26
27
28
# File 'lib/options-lib/yahoo_crawler.rb', line 26

def to_s
  "YahooCrawler('#{@stock}','#{@exp})"
end