gameday_api 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/team.rb ADDED
@@ -0,0 +1,336 @@
1
+ require 'gameday_util'
2
+ require 'game'
3
+ require 'gameday'
4
+ require 'schedule'
5
+
6
+
7
+ # This class
8
+ class Team
9
+
10
+ START_MONTH = 4 # April
11
+ END_MONTH = 10 # October
12
+
13
+ attr_accessor :abrev, :city, :name, :league, :games
14
+
15
+ # Setup team names, abbreviations, and league
16
+ def initialize(abrev)
17
+ if (abrev && abrev != '')
18
+ @abrev = abrev
19
+ if Team.teams[@abrev]
20
+ @city = Team.teams[@abrev][0]
21
+ @name = Team.teams[@abrev][1]
22
+ if Team.teams[@abrev].length > 2
23
+ @league = Team.teams[@abrev][2]
24
+ end
25
+ else
26
+ @city = @abrev
27
+ @name = @abrev
28
+ @league = ''
29
+ end
30
+ else
31
+ @city = ''
32
+ @name = ''
33
+ @league = ''
34
+ end
35
+ end
36
+
37
+ @@abrevs = {}
38
+ @@abrevs['ana'] = ['Anaheim','Angels','American']
39
+ @@abrevs['bos'] = ['Boston','Red Sox','American']
40
+ @@abrevs['cha'] = ['Chicago','White Sox','American']
41
+ @@abrevs['chn'] = ['Chicago','Cubs','National']
42
+ @@abrevs['det'] = ['Detroit','Tigers','American']
43
+ @@abrevs['ari'] = ['Arizona','Diamondbacks','National']
44
+ @@abrevs['bal'] = ['Baltimore','Orioles','American']
45
+ @@abrevs['cle'] = ['Cleveland','Indians','American']
46
+ @@abrevs['col'] = ['Colorado','Rockies','National']
47
+ @@abrevs['flo'] = ['Florida','Marlins','National']
48
+ @@abrevs['cin'] = ['Cincinnati','Reds','National']
49
+ @@abrevs['atl'] = ['Atlanta','Braves','National']
50
+ @@abrevs['hou'] = ['Houston','Astros','National']
51
+ @@abrevs['kca'] = ['Kansas City','Royals','American']
52
+ @@abrevs['min'] = ['Minnesota','Twins','American']
53
+ @@abrevs['mil'] = ['Milwaukee','Brewers','National']
54
+ @@abrevs['mon'] = ['Montreal','Expos','National']
55
+ @@abrevs['nya'] = ['New York','Yankees','American']
56
+ @@abrevs['nyn'] = ['New York','Mets','National']
57
+ @@abrevs['oak'] = ['Oakland','As','American']
58
+ @@abrevs['lan'] = ['Los Angeles','Dodgers','National']
59
+ @@abrevs['pit'] = ['Pittsburgh','Pirates','National']
60
+ @@abrevs['phi'] = ['Philadelphi','Phillies','National']
61
+ @@abrevs['usa'] = ['USA','All-Stars']
62
+ @@abrevs['jpn'] = ['Japan','All-Stars']
63
+ @@abrevs['sln'] = ['St. Louis','Cardinals','National']
64
+ @@abrevs['sfn'] = ['SanFrancisco','Giants','National']
65
+ @@abrevs['sea'] = ['Seattle','Mariners','American']
66
+ @@abrevs['sdn'] = ['San Diego','Padres','National']
67
+ @@abrevs['tba'] = ['Tampa Bay','Devil Rays','American']
68
+ @@abrevs['tex'] = ['Texas','Rangers','American']
69
+ @@abrevs['tor'] = ['Toronto','Blue Jays','American']
70
+ @@abrevs['was'] = ['Washington','Nationals','National']
71
+
72
+
73
+ def self.teams
74
+ @@abrevs
75
+ end
76
+
77
+
78
+ # Returns a 2 element array specifying the two teams associated with the game id (gid) passed in.
79
+ # teams[0] = visitor
80
+ # teams[1] = home
81
+ def self.get_teams_for_gid(gid)
82
+ teams = []
83
+ info = GamedayUtil.parse_gameday_id('gid_'+gid)
84
+ home_team_abbrev = info["home_team_abbrev"]
85
+ visit_team_abbrev = info["visiting_team_abbrev"]
86
+ teams << Team.new(visit_team_abbrev)
87
+ teams << Team.new(home_team_abbrev)
88
+ end
89
+
90
+
91
+ # Returns an array of all games for this team for the specified season
92
+ def all_games(year)
93
+ if !@games
94
+ puts 'Finding all games for team...'
95
+ results = []
96
+ (START_MONTH..END_MONTH).each do |month|
97
+ puts "Month: " + month.to_s
98
+ month_s = GamedayUtil.convert_digit_to_string(month)
99
+ (1..31).each do |date|
100
+ if !GamedayUtil.is_date_valid(month, date)
101
+ next
102
+ end
103
+ date_s = GamedayUtil.convert_digit_to_string(date)
104
+ games = games_for_date(year, month_s, date_s)
105
+ if games
106
+ # make sure game was not postponed
107
+ good_games = games.select { |g| g.get_boxscore.status_ind != 'P' }
108
+ good_games.each do |game|
109
+ results << game
110
+ end
111
+ end
112
+ end
113
+ end
114
+ @games = results
115
+ end
116
+ @games
117
+ end
118
+
119
+
120
+ # Returns an array of all home games for this team for the specified season
121
+ def all_home_games(year)
122
+ games = all_games(year)
123
+ results = games.select {|g| g.home_team_abbrev == @abrev }
124
+ end
125
+
126
+
127
+ # Returns an array of all away games for this team for the specified season
128
+ def all_away_games(year)
129
+ games = all_games(year)
130
+ results = games.select {|g| g.visit_team_abbrev == @abrev }
131
+ end
132
+
133
+
134
+ # Returns an array of the team's game objects for the date passed in.
135
+ def games_for_date(year, month, day)
136
+ games_page = GamedayFetcher.fetch_games_page(year, month, day)
137
+ gids = find_gid_for_date(year, month, day, games_page)
138
+ if gids
139
+ results = gids.collect {|gid| Game.new(gid) }
140
+ else
141
+ results = nil
142
+ end
143
+ results
144
+ end
145
+
146
+
147
+ # Returns an array of BattingAppearance containing the leadoff hitters for each game of the specified season.
148
+ def get_leadoff_hitters_by_year(year)
149
+ results = []
150
+ games = all_games(year)
151
+ games.each do |game|
152
+ boxscore = game.get_boxscore
153
+ leadoffs = boxscore.get_leadoff_hitters
154
+ if game.home_team_abbrev == @abrev
155
+ results << leadoffs[1]
156
+ else
157
+ results << leadoffs[0]
158
+ end
159
+ end
160
+ results
161
+ end
162
+
163
+
164
+ # Returns an array of BattingAppearance of all hitters who have led off at least one game during the specified season
165
+ def get_leadoff_hitters_unique(year)
166
+ hitters = get_leadoff_hitters_by_year(year)
167
+ h = {}
168
+ hitters.each {|hitter| h[hitter.batter_name]=hitter}
169
+ h.values
170
+ end
171
+
172
+
173
+ # Returns an array containing the cleanup hitters for each game of the specified season.
174
+ # The cleanup hitter is the 4th hitter in the batting order
175
+ def get_cleanup_hitters_by_year(year)
176
+ results = []
177
+ games = all_games(year)
178
+ games.each do |game|
179
+ boxscore = game.get_boxscore
180
+ hitters = boxscore.get_cleanup_hitters
181
+ if game.home_team_abbrev == @abrev
182
+ results << hitters[1]
183
+ else
184
+ results << hitters[0]
185
+ end
186
+ end
187
+ results
188
+ end
189
+
190
+
191
+ # Returns an array of all hitters who have hit in the cleanup spot (4) at least one game during the specified season
192
+ def get_cleanup_hitters_unique(year)
193
+ hitters = get_cleanup_hitters_by_year(year)
194
+ h = {}
195
+ hitters.each {|hitter| h[hitter.batter_name]=hitter}
196
+ h.values
197
+ end
198
+
199
+
200
+ def get_start_pitcher_appearances_by_year(year)
201
+ pitchers = []
202
+ games = all_games(year)
203
+ games.each do |game|
204
+ starters = game.get_starting_pitchers
205
+ if game.home_team_abbrev == @abrev
206
+ pitchers << starters[1]
207
+ else
208
+ pitchers << starters[0]
209
+ end
210
+ end
211
+ pitchers
212
+ end
213
+
214
+
215
+ # Returns an array of all pitchers who have started at least one game during the specified season
216
+ def get_starters_unique(year)
217
+ pitchers = get_start_pitcher_appearances_by_year(year)
218
+ h = {}
219
+ pitchers.each {|pitcher| h[pitcher.pitcher_name]=pitcher}
220
+ h.values
221
+ end
222
+
223
+
224
+ def get_close_pitcher_appearances_by_year(year)
225
+ pitchers = []
226
+ games = all_games(year)
227
+ games.each do |game|
228
+ closers = game.get_closing_pitchers
229
+ if game.home_team_abbrev == @abrev
230
+ pitchers << closers[1]
231
+ else
232
+ pitchers << closers[0]
233
+ end
234
+ end
235
+ pitchers
236
+ end
237
+
238
+
239
+ # Returns an array of all pitchers who have closed at least one game during the specified season
240
+ def get_closers_unique(year)
241
+ pitchers = get_close_pitcher_appearances_by_year(year)
242
+ h = {}
243
+ pitchers.each {|pitcher| h[pitcher.pitcher_name]=pitcher}
244
+ h.values
245
+ end
246
+
247
+
248
+ # Returns a count of the number of quality starts for this team for the specified year.
249
+ def quality_starts_count(year)
250
+ count = 0
251
+ games = all_games(year)
252
+ games.each do |game|
253
+ starters = game.get_starting_pitchers
254
+ if game.home_team_abbrev == @abrev
255
+ if starters[1].quality_start?
256
+ count = count + 1
257
+ end
258
+ else
259
+ if starters[0].quality_start?
260
+ count = count + 1
261
+ end
262
+ end
263
+ end
264
+ count
265
+ end
266
+
267
+
268
+ # Returns an array of all players who have played at least one game for this team during the specified season.
269
+ def players_for_season(year)
270
+
271
+ end
272
+
273
+
274
+ # Returns an array of all the games for this team for the year and month specified
275
+ def get_games_for_month(year, month)
276
+
277
+ end
278
+
279
+
280
+ # Returns a game object representing the opening day game for this team for
281
+ # the season passed in.
282
+ def get_opening_day_game(year)
283
+ schedule = Schedule.new(year)
284
+ oday = schedule.get_opening_day
285
+ oday_array = GamedayUtil.parse_date_string(oday)
286
+ games = games_for_date(oday_array[0], oday_array[1], oday_array[2])
287
+ if games[0] == nil
288
+ games = games_for_date(oday_array[0],
289
+ oday_array[1],
290
+ GamedayUtil.convert_digit_to_string(oday_array[2].to_i + 1))
291
+ end
292
+ return games[0]
293
+ end
294
+
295
+
296
+ # Returns a Roster object representing the opening day roster for this team
297
+ # for the specified year.
298
+ def opening_day_roster(year)
299
+ game = get_opening_day_game(year)
300
+ rosters = game.get_rosters
301
+ rosters[0].team_name == city + ' ' + name ? rosters[0] : rosters[1]
302
+ end
303
+
304
+
305
+ private
306
+
307
+ # Returns an array of the game ids associated with the given date and team
308
+ # because of double-headers it is possible for one team to play more than one game
309
+ # on a single date.
310
+ # Each game listing looks like this:
311
+ # <li><a href="gid_2009_09_15_kcamlb_detmlb_1/">gid_2009_09_15_kcamlb_detmlb_1/</a></li>
312
+ def find_gid_for_date(year, month, day, games_page)
313
+ begin
314
+ results = []
315
+ if games_page
316
+ # look for game listings
317
+ @hp = Hpricot(games_page)
318
+ a = @hp.at('ul')
319
+ (a/"a").each do |link|
320
+ # game listings include the 'gid' characters
321
+ if link.inner_html.include?('gid') && link.inner_html.include?(@abrev)
322
+ str = link.inner_html
323
+ results << str[5..str.length-2]
324
+ end
325
+ end
326
+ return results
327
+ end
328
+ puts "No games data found for #{year}, #{month}, #{day}, #{@abrev}."
329
+ return nil
330
+ rescue
331
+ puts "Exception in find_gid_for_date: No games data found for #{year}, #{month}, #{day}, #{@abrev}."
332
+ end
333
+ end
334
+
335
+
336
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gameday_api
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 0
10
- version: 0.5.0
9
+ - 1
10
+ version: 0.5.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - timothyf
@@ -29,6 +29,45 @@ extra_rdoc_files:
29
29
  - LICENSE
30
30
  - README
31
31
  files:
32
+ - lib/at_bat.rb
33
+ - lib/batter.rb
34
+ - lib/batting_appearance.rb
35
+ - lib/box_score.rb
36
+ - lib/cache_fetcher.rb
37
+ - lib/coach.rb
38
+ - lib/data_downloader.rb
39
+ - lib/db_importer.rb
40
+ - lib/event.rb
41
+ - lib/event_log.rb
42
+ - lib/game.rb
43
+ - lib/game_status.rb
44
+ - lib/gameday.rb
45
+ - lib/gameday_fetcher.rb
46
+ - lib/gameday_local_fetcher.rb
47
+ - lib/gameday_parser.rb
48
+ - lib/gameday_path_builder.rb
49
+ - lib/gameday_remote_fetcher.rb
50
+ - lib/gameday_url_builder.rb
51
+ - lib/gameday_util.rb
52
+ - lib/hip.rb
53
+ - lib/hitchart.rb
54
+ - lib/import_data.rb
55
+ - lib/inning.rb
56
+ - lib/line_score.rb
57
+ - lib/media.rb
58
+ - lib/media_highlight.rb
59
+ - lib/media_mobile.rb
60
+ - lib/pitch.rb
61
+ - lib/pitcher.rb
62
+ - lib/pitchfx_db_manager.rb
63
+ - lib/pitching_appearance.rb
64
+ - lib/player.rb
65
+ - lib/players.rb
66
+ - lib/roster.rb
67
+ - lib/schedule.rb
68
+ - lib/schedule_game.rb
69
+ - lib/scoreboard.rb
70
+ - lib/team.rb
32
71
  - LICENSE
33
72
  - README
34
73
  has_rdoc: true