Class: MLBTerminal::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/mlb_terminal/game.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gameday) ⇒ Game

Returns a new instance of Game.



32
33
34
# File 'lib/mlb_terminal/game.rb', line 32

def initialize(gameday)
  @base_url = Game.parse_gameday_id_to_url gameday
end

Class Method Details

.base_url_for_date(date = Time.now.to_date) ⇒ Object



159
160
161
# File 'lib/mlb_terminal/game.rb', line 159

def self.base_url_for_date(date = Time.now.to_date)
  "#{MLB_BASE_URL}/year_#{date.year}/month_#{"%02d" % date.month}/day_#{"%02d" % date.day}"
end

.list(date = Time.now.to_date) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mlb_terminal/game.rb', line 9

def self.list(date = Time.now.to_date)
  doc = Nokogiri::HTML(open "#{base_url_for_date date}/epg.xml")

  doc.xpath("//epg/game").map{|game|
    {:home_team => {
       :name   => game["home_team_name"],
       :wins   => game["home_win"],
       :losses => game["home_loss"]},
     :away_team => {
       :name   => game["away_team_name"],
       :wins   => game["away_win"],
       :losses => game["away_loss"]},
     :score => {
       :home => game["home_team_runs"],
       :away => game["away_team_runs"]},
     :starts => "#{game["time"]} #{game["time_zone"]}",
     :status => "#{game["status"]}" \
       "#{game["status"] == "In Progress" ? 
         ", #{game["top_inning"]=="Y" ? "Top" : "Bot"} of #{game["inning"].to_i.ordinalize}" :
         ""}",
     :game_id => game["gameday"]}}
end

.parse_gameday_id(gameday) ⇒ Object



174
175
176
# File 'lib/mlb_terminal/game.rb', line 174

def self.parse_gameday_id(gameday)
  Hash[[:year, :month, :day, :away_team, :home_team, :game_number].zip(/[gid_]*([0-9]{4})_([0-9]{2})_([0-9]{2})_([a-z]{3})mlb_([a-z]{3})mlb_([0-9])/.match(gameday).to_a.slice(1,6))]
end

.parse_gameday_id_to_url(gameday) ⇒ Object



163
164
165
166
167
168
169
170
171
172
# File 'lib/mlb_terminal/game.rb', line 163

def self.parse_gameday_id_to_url(gameday)
  game_info = Game.parse_gameday_id gameday
  "#{Game.base_url_for_date(Date.new(game_info[:year].to_i, game_info[:month].to_i, game_info[:day].to_i))}/gid_" \
    "#{game_info[:year]}_" \
    "#{game_info[:month]}_" \
    "#{game_info[:day]}_" \
    "#{game_info[:away_team]}mlb_" \
    "#{game_info[:home_team]}mlb_" \
    "#{game_info[:game_number]}"
end

Instance Method Details

#events(delay = 2, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mlb_terminal/game.rb', line 36

def events(delay = 2, &block)
  last_atbat = 0

  Enumerator.new do |y|
    begin
      doc = Nokogiri::HTML(open "#{@base_url}/game_events.xml")

      if doc.xpath("//game/inning/*/atbat").count == 0
        break
      end
      
      doc.xpath("//game/inning[*/atbat/@num > #{last_atbat}]").each do |inning|
        inning.xpath("*/atbat[@num > #{last_atbat}]").each do |at_bat|
          y.yield({
            :inning => inning["num"],
            :inning_loc => at_bat.xpath("..").first.name.camelcase,
            :num => at_bat["num"],
            :balls => at_bat["b"],
            :strikes => at_bat["s"],
            :outs => at_bat["o"],
            :time => Time.parse(at_bat["start_tfs_zulu"], "%Y-%m-%dT%H:%M:%SZ"),
            :desc => at_bat["des"].strip})
        end
      end

      last_atbat = doc.xpath("//game/inning/*/atbat/@num").map(&:value).map(&:to_i).max

      if game_status != "F"
        sleep delay
      end
    end while game_status != "F"
  end.each(&block)
end

#hits(delay = 2, &block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mlb_terminal/game.rb', line 70

def hits(delay = 2, &block)
  players = player_lookup
  next_hit = 0
  Enumerator.new do |y|
    begin
      doc = Nokogiri::HTML(open "#{@base_url}/inning/inning_hit.xml")
      doc.xpath("//hitchart/hip").slice(next_hit..-1).each do |hit|
        y.yield({
          :inning => hit["inning"],
          :inning_loc => (hit["team"]=="A" ? "Top" : "Bottom"),
          :batter => players[hit["batter"]],
          :pitcher => players[hit["pitcher"]],
          :type => hit["type"],
          :desc => hit["des"],
          :x => hit["x"].to_f,
          :y => hit["y"].to_f})
      end
      if game_status != "F"
        sleep delay
      end
      next_hit = doc.xpath("//hitchart/hip").count
    end while game_status != "F"
  end.each(&block)
end

#pitches(delay = 2, &block) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/mlb_terminal/game.rb', line 95

def pitches(delay = 2, &block)
  last_pitch = 0
  players = player_lookup

  Enumerator.new do |y|
    begin
      doc = Nokogiri::HTML(open "#{@base_url}/inning/inning_all.xml")
      doc.xpath("//game/inning[*/*/pitch/@id > #{last_pitch}]").each do |inning|

        inning.xpath("*/atbat").each do |at_bat|
          pitcher = players[at_bat["pitcher"]]
          batter = players[at_bat["batter"]]

          at_bat.xpath("pitch").each do |pitch|
            y.yield({
             :time => Time.parse(pitch["tfs_zulu"], "%Y-%m-%dT%H:%M:%SZ"),
             :inning => inning["num"],
             :inning_loc => at_bat.xpath("..").first.name.camelcase,
             :pitcher => pitcher,
             :batter => batter,
             :type => pitch["type"],
             :x => pitch["x"],
             :y => pitch["y"],
             :start_speed => pitch["start_speed"],
             :end_speed => pitch["end_speed"],
             :sz_top => pitch["sz_top"],
             :sz_bot => pitch["sz_bot"],
             :pfx_x => pitch["pfx_x"],
             :pfx_z => pitch["pfx_z"],
             :px => pitch["px"],
             :pz => pitch["pz"],
             :x0 => pitch["x0"],
             :y0 => pitch["y0"],
             :z0 => pitch["z0"],
             :vx0 => pitch["vx0"],
             :vy0 => pitch["vy0"],
             :vz0 => pitch["vz0"],
             :ax => pitch["ax"],
             :ay => pitch["ay"],
             :az => pitch["az"],
             :break_y => pitch["break_y"],
             :break_angle => pitch["break_angle"],
             :break_length => pitch["break_length"],
             :pitch_type => pitch["pitch_type"],
             :type_confidence => pitch["type_confidence"],
             :zone => pitch["zone"],
             :nasty => pitch["nasty"],
             :spin_dir => pitch["spin_dir"],
             :spin_rate => pitch["spin_rate"],
             :cc => pitch["cc"],
             :mt => pitch["mt"]})
          end
        end
      end

      last_pitch = doc.xpath("//game/inning/*/*/pitch/@id").map(&:value).map(&:to_i).max

      if doc.xpath("//game").first["ind"] != "F"
        sleep delay
      end
    end while game_status != "F"
  end.each(&block)
end