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.
@@ -0,0 +1,305 @@
1
+ require 'rexml/document'
2
+
3
+
4
+ # This class parses various Gameday data files
5
+ # This works with Rails and ActiveRecord
6
+ class GamedayParser
7
+
8
+ def self.parse_player_data(doc, team, logger)
9
+ updated_player_count = 0
10
+ new_player_count = 0
11
+ doc.root.each_element do |player|
12
+ gameday_id = player.attributes['id']
13
+ if (gameday_id)
14
+ old_player = Player.find(:first, :conditions=>'gameday_id='+gameday_id)
15
+ if old_player
16
+ # update player
17
+ old_player.first_name = player.attributes['first']
18
+ old_player.last_name = player.attributes['last']
19
+ old_player.position = player.attributes['pos']
20
+ old_player.team_id = team.id
21
+ old_player.save
22
+ updated_player_count = updated_player_count+1
23
+ else
24
+ # create player
25
+ Player.create(:gameday_id => gameday_id,
26
+ :first_name => player.attributes['first'],
27
+ :last_name => player.attributes['last'],
28
+ :position => player.attributes['pos'],
29
+ :team_id => team.id)
30
+ new_player_count = new_player_count+1
31
+ end
32
+ end
33
+ end
34
+ logger.info "Team: #{team.name}"
35
+ logger.info "Updated Players: #{updated_player_count}"
36
+ logger.info "New Players: #{new_player_count}"
37
+ puts "Completed #{team.name}"
38
+ end
39
+
40
+
41
+ def self.parse_pitcher_game_data(doc, game, logger)
42
+ player = doc.root
43
+ gameday_id = player.attributes['id']
44
+ if (gameday_id)
45
+ team_abbrev = player.attributes['team']
46
+ team = Team.find(:first, :conditions=>"abbreviation='#{team_abbrev}'")
47
+ old_player = Player.find(:first, :conditions=>'gameday_id='+gameday_id)
48
+ if old_player
49
+ # update player
50
+ old_player.first_name = player.attributes['first_name']
51
+ old_player.last_name = player.attributes['last_name']
52
+ old_player.position = player.attributes['pos']
53
+ old_player.jersey_number = player.attributes['jersey_number'].to_i
54
+ old_player.height = player.attributes['height']
55
+ old_player.weight = player.attributes['weight'].to_i
56
+ old_player.bats = player.attributes['bats']
57
+ old_player.throws = player.attributes['throws']
58
+ #old_player.birthdate = player.attributes['dob']
59
+ old_player.team_id = team.id
60
+ old_player.save
61
+ else
62
+ # create player
63
+ Player.create(:gameday_id => gameday_id,
64
+ :first_name => player.attributes['first_name'],
65
+ :last_name => player.attributes['last_name'],
66
+ :position => player.attributes['pos'],
67
+ :jersey_number => player.attributes['jersey_number'].to_i,
68
+ :height => player.attributes['height'],
69
+ :weight => player.attributes['weight'].to_i,
70
+ :bats => player.attributes['bats'],
71
+ :throws => player.attributes['throws'],
72
+ #:birthdate => player.attributes['dob'],
73
+ :team_id => team.id)
74
+ end
75
+ end
76
+ end
77
+
78
+
79
+ def self.parse_scoreboard_data(doc, game)
80
+ doc.root.each_element do |scoreboard_element|
81
+ if scoreboard_element.name == 'post_game'
82
+ scoreboard_element.each_element do |post_game_element|
83
+ if post_game_element.name == 'winning_pitcher'
84
+ gd_id = post_game_element.attributes['id']
85
+ player = Player.find(:first, :conditions=>"gameday_id=#{gd_id}")
86
+ if (player)
87
+ game.update_attribute(:winning_pitcher_id, player.id)
88
+ end
89
+ elsif post_game_element.name == 'losing_pitcher'
90
+ gd_id = post_game_element.attributes['id']
91
+ player = Player.find(:first, :conditions=>"gameday_id=#{gd_id}")
92
+ if (player)
93
+ game.update_attribute(:losing_pitcher_id, player.id)
94
+ end
95
+ elsif post_game_element.name == 'save_pitcher'
96
+ gd_id = post_game_element.attributes['id']
97
+ player = Player.find(:first, :conditions=>"gameday_id=#{gd_id}")
98
+ if (player)
99
+ game.update_attribute(:save_pitcher_id, player.id)
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+
107
+
108
+ def self.parse_inning_hip_data(doc, game_id)
109
+ if !doc.root
110
+ return
111
+ end
112
+ doc.root.each_element do |hip|
113
+ begin
114
+ batter = Player.find(:first, :conditions=>'gameday_id='+hip.attributes['batter'])
115
+ pitcher = Player.find(:first, :conditions=>'gameday_id='+hip.attributes['pitcher'])
116
+ hit = Hit.new
117
+ hit.description = hip.attributes['des']
118
+ hit.x = hip.attributes['x']
119
+ hit.y = hip.attributes['y']
120
+ if batter
121
+ hit.batter_id = batter.id
122
+ end
123
+ if pitcher
124
+ hit.pitcher_id = pitcher.id
125
+ end
126
+ hit.gd_type = hip.attributes['type']
127
+ hit.team = hip.attributes['team']
128
+ hit.inning = hip.attributes['inning']
129
+ hit.game_id = game_id
130
+ hit.save
131
+ end
132
+ end
133
+ end
134
+
135
+
136
+ def self.parse_innings_ab_data(doc, game_id)
137
+ if !doc
138
+ return
139
+ end
140
+ current_atbat=0
141
+ current_order=0
142
+ inning = doc.root.attributes['num']
143
+ doc.root.each_element do |inning_half|
144
+ begin
145
+ half = inning_half.name
146
+ inning_half.each_element do |atbat_action|
147
+ current_order = current_order+1
148
+ if atbat_action.name == 'atbat'
149
+ batter = Player.find(:first, :conditions=>'gameday_id='+atbat_action.attributes['batter'])
150
+ pitcher = Player.find(:first, :conditions=>'gameday_id='+atbat_action.attributes['pitcher'])
151
+ atbat = AtBat.new
152
+ atbat.inning = inning
153
+ atbat.half = half
154
+ atbat.number = atbat_action.attributes['num']
155
+ current_atbat = atbat.number
156
+ atbat.ball = atbat_action.attributes['b']
157
+ atbat.strike = atbat_action.attributes['s']
158
+ atbat.out = atbat_action.attributes['o']
159
+ atbat.batter_gd_id = atbat_action.attributes['batter']
160
+ atbat.pitcher_gd_id = atbat_action.attributes['pitcher']
161
+ atbat.stand = atbat_action.attributes['stand']
162
+ atbat.description = atbat_action.attributes['des']
163
+ atbat.event = atbat_action.attributes['event']
164
+ if batter
165
+ atbat.batter_id = batter.id
166
+ end
167
+ if pitcher
168
+ atbat.pitcher_id = pitcher.id
169
+ end
170
+ atbat.game_id = game_id
171
+ atbat.order = current_order
172
+ atbat.gd_type = 'atbat'
173
+ atbat.save
174
+ atbat_action.each_element do |pitch_runner|
175
+ if pitch_runner.name == 'pitch'
176
+ pitch = Pitch.new
177
+ pitch.at_bat_id = atbat.id
178
+ pitch.description = pitch_runner.attributes['des']
179
+ pitch.pitch_type = pitch_runner.attributes['type']
180
+ pitch.gd_pitch_id = pitch_runner.attributes['id']
181
+ pitch.x = pitch_runner.attributes['x']
182
+ pitch.y = pitch_runner.attributes['y']
183
+ pitch.on_1b = pitch_runner.attributes['on_1b']
184
+ pitch.on_2b = pitch_runner.attributes['on_2b']
185
+ pitch.on_3b = pitch_runner.attributes['on_3b']
186
+ pitch.save
187
+ elsif pitch_runner.name == 'runner'
188
+ runner = Runner.new
189
+ player = Player.find(:first, :conditions=>"gameday_id=#{pitch_runner.attributes['id']}")
190
+ runner.player_id = player.id
191
+ runner.start = pitch_runner.attributes['start']
192
+ runner.end = pitch_runner.attributes['end']
193
+ runner.event = pitch_runner.attributes['event']
194
+ runner.score = pitch_runner.attributes['score']
195
+ runner.rbi = pitch_runner.attributes['rbi']
196
+ runner.earned = pitch_runner.attributes['earned']
197
+ runner.at_bat_id = atbat.id
198
+ runner.save
199
+ end
200
+ end
201
+ elsif atbat_action.name == 'action'
202
+ action = AtBat.new
203
+ action.ball = atbat_action.attributes['b']
204
+ action.strike = atbat_action.attributes['s']
205
+ action.out = atbat_action.attributes['o']
206
+ action.description = atbat_action.attributes['des']
207
+ action.event = atbat_action.attributes['event']
208
+ action.pitch = atbat_action.attributes['pitch']
209
+ action.score = atbat_action.attributes['score']
210
+ action.game_id = game_id
211
+ player = Player.find(:first, :conditions=>"gameday_id=#{atbat_action.attributes['player']}")
212
+ action.player_id = player.id
213
+ action.inning = inning
214
+ action.half = half
215
+ action.order = current_order
216
+ action.gd_type = 'action'
217
+ # action.next_atbat = current_atbat + 1
218
+ action.save
219
+ end
220
+ end
221
+ end
222
+ end
223
+ end
224
+
225
+
226
+ def self.parse_event_log_data(doc, game_id)
227
+ doc.root.each_element do |team|
228
+ team_id = team.attributes['id']
229
+ team.each_element do |event|
230
+ event_obj = Event.new
231
+ event_obj.number = event.attributes['number']
232
+ event_obj.inning = event.attributes['inning']
233
+ event_obj.description = event.attributes['description']
234
+ event_obj.team_id = team_id
235
+ event_obj.game_id = game_id
236
+ event_obj.save
237
+ end
238
+ end
239
+ end
240
+
241
+
242
+ def self.parse_batter_pbp_data(doc, game)
243
+ doc.root.each_element do |atbat|
244
+ batter = Player.find(:first, :conditions=>'gameday_id='+atbat.attributes['batter'])
245
+ pitcher = Player.find(:first, :conditions=>'gameday_id='+atbat.attributes['pitcher'])
246
+
247
+ existing_ab = AtBat.find(:first, :conditions=>"game_id=#{game.id} AND inning=#{atbat.attributes['inning']} AND number=#{atbat.attributes['num']}")
248
+ if !existing_ab
249
+ at_bat_obj = AtBat.new
250
+ at_bat_obj.inning = atbat.attributes['inning']
251
+ at_bat_obj.number = atbat.attributes['num']
252
+ at_bat_obj.ball = atbat.attributes['b']
253
+ at_bat_obj.strike = atbat.attributes['s']
254
+ at_bat_obj.out = atbat.attributes['o']
255
+ at_bat_obj.batter_gd_id = atbat.attributes['batter']
256
+ at_bat_obj.pitcher_gd_id = atbat.attributes['pitcher']
257
+ at_bat_obj.stand = atbat.attributes['stand']
258
+ at_bat_obj.description = atbat.attributes['des']
259
+ at_bat_obj.event = atbat.attributes['event']
260
+ at_bat_obj.brief_event = atbat.attributes['brief_event']
261
+ at_bat_obj.batter_id = batter.id
262
+ at_bat_obj.pitcher_id = pitcher.id
263
+ at_bat_obj.game_id = game.id
264
+ at_bat_obj.save
265
+
266
+ atbat.each_element do |pitch|
267
+ pitch_obj = Pitch.new
268
+ pitch_obj.at_bat_id = at_bat_obj.id
269
+ pitch_obj.description = pitch.attributes['des']
270
+ pitch_obj.pitch_type = pitch.attributes['type']
271
+ pitch_obj.gd_pitch_id = pitch.attributes['id']
272
+ pitch_obj.x = pitch.attributes['x']
273
+ pitch_obj.y = pitch.attributes['y']
274
+ if pitch.attributes['start_speed']
275
+ pitch_obj.start_speed = pitch.attributes['start_speed']
276
+ pitch_obj.end_speed = pitch.attributes['end_speed']
277
+ pitch_obj.sz_top = pitch.attributes['sz_top']
278
+ pitch_obj.sz_bot = pitch.attributes['sz_bot']
279
+ pitch_obj.pfx_x = pitch.attributes['pfx_x']
280
+ pitch_obj.pfx_z = pitch.attributes['pfx_z']
281
+ pitch_obj.px = pitch.attributes['px']
282
+ pitch_obj.pz = pitch.attributes['pz']
283
+ pitch_obj.x0 = pitch.attributes['x0']
284
+ pitch_obj.y0 = pitch.attributes['y0']
285
+ pitch_obj.z0 = pitch.attributes['z0']
286
+ pitch_obj.vx0 = pitch.attributes['vx0']
287
+ pitch_obj.vy0 = pitch.attributes['vy0']
288
+ pitch_obj.vz0 = pitch.attributes['vz0']
289
+ pitch_obj.ax = pitch.attributes['ax']
290
+ pitch_obj.ay = pitch.attributes['ay']
291
+ pitch_obj.az = pitch.attributes['az']
292
+ pitch_obj.break_y = pitch.attributes['break_y']
293
+ pitch_obj.break_angle = pitch.attributes['break_angle']
294
+ pitch_obj.break_length = pitch.attributes['break_length']
295
+ pitch_obj.at_bat_id = at_bat_obj.id
296
+ end
297
+ pitch_obj.save
298
+ end
299
+ else
300
+ #puts "At bat already exists..."
301
+ end
302
+ end
303
+ end
304
+ end
305
+
@@ -0,0 +1,132 @@
1
+ require 'gameday'
2
+
3
+
4
+ class GamedayPathBuilder
5
+
6
+ FILE_BASE_PATH = 'components/game/mlb'
7
+
8
+ def self.year_month_day_path
9
+ "year_" + @@year + "/month_" + @@month + "/day_" + @@day
10
+ end
11
+
12
+
13
+ def self.build_game_base_path(gid)
14
+ gameday_info = GamedayUtil.parse_gameday_id('gid_' + gid)
15
+ "#{FILE_BASE_PATH}/year_" + gameday_info['year'] + "/month_" + gameday_info['month'] + "/day_" + gameday_info['day'] + "/gid_"+gid
16
+ end
17
+
18
+
19
+ def self.build_eventlog_path(year, month, day, gid)
20
+ set_date_vars(year, month, day)
21
+ "#{FILE_BASE_PATH}/" + year_month_day_path + "/gid_"+gid+"/eventLog.xml"
22
+ end
23
+
24
+
25
+ def self.build_epg_path(year, month, day)
26
+ set_date_vars(year, month, day)
27
+ "#{FILE_BASE_PATH}/" + year_month_day_path + "/epg.xml"
28
+ end
29
+
30
+
31
+ def self.build_scoreboard_path(year, month, day)
32
+ set_date_vars(year, month, day)
33
+ "#{FILE_BASE_PATH}/" + year_month_day_path + "/master_scoreboard.xml"
34
+ end
35
+
36
+
37
+ def self.build_day_highlights_path(year, month, day)
38
+ set_date_vars(year, month, day)
39
+ "#{FILE_BASE_PATH}/" + year_month_day_path + "/media/highlights.xml"
40
+ end
41
+
42
+
43
+ def self.build_boxscore_path(year, month, day, gid)
44
+ set_date_vars(year, month, day)
45
+ "#{FILE_BASE_PATH}/" + year_month_day_path + "/gid_"+gid+"/boxscore.xml"
46
+ end
47
+
48
+
49
+ def self.build_game_path(year, month, day, gid)
50
+ set_date_vars(year, month, day)
51
+ "#{FILE_BASE_PATH}/" + year_month_day_path + "/gid_"+gid+"/game.xml"
52
+ end
53
+
54
+
55
+ def self.build_game_events_path(year, month, day, gid)
56
+ set_date_vars(year, month, day)
57
+ "#{FILE_BASE_PATH}/" + year_month_day_path + "/gid_"+gid+"/game_events.xml"
58
+ end
59
+
60
+
61
+ def self.build_gamecenter_path(year, month, day, gid)
62
+ set_date_vars(year, month, day)
63
+ "#{FILE_BASE_PATH}/" + year_month_day_path + "/gid_"+gid+"/gamecenter.xml"
64
+ end
65
+
66
+
67
+ def self.build_linescore_path(year, month, day, gid)
68
+ set_date_vars(year, month, day)
69
+ "#{FILE_BASE_PATH}/" + year_month_day_path + "/gid_"+gid+"/linescore.xml"
70
+ end
71
+
72
+
73
+ def self.build_players_path(year, month, day, gid)
74
+ set_date_vars(year, month, day)
75
+ "#{FILE_BASE_PATH}/" + year_month_day_path + "/gid_"+gid+"/players.xml"
76
+ end
77
+
78
+
79
+ def self.build_batter_path(year, month, day, gid, pid)
80
+ set_date_vars(year, month, day)
81
+ "#{FILE_BASE_PATH}/" + year_month_day_path + "/gid_"+gid+"/batters/" + pid + '.xml'
82
+ end
83
+
84
+
85
+ def self.build_pitcher_path(year, month, day, gid, pid)
86
+ set_date_vars(year, month, day)
87
+ "#{FILE_BASE_PATH}/" + year_month_day_path + "/gid_"+gid+"/pitchers/" + pid + '.xml'
88
+ end
89
+
90
+
91
+ def self.build_inningx_path(year, month, day, gid, inning_num)
92
+ set_date_vars(year, month, day)
93
+ "#{FILE_BASE_PATH}/" + year_month_day_path + "/gid_"+gid+"/inning/inning_#{inning_num}.xml"
94
+ end
95
+
96
+
97
+ def self.build_inning_scores_path(year, month, day, gid)
98
+ set_date_vars(year, month, day)
99
+ "#{FILE_BASE_PATH}/" + year_month_day_path + "/gid_"+gid+"/inning/inning_Scores.xml"
100
+ end
101
+
102
+
103
+ def self.build_inning_hit_path(year, month, day, gid)
104
+ set_date_vars(year, month, day)
105
+ "#{FILE_BASE_PATH}/" + year_month_day_path + "/gid_"+gid+"/inning/inning_hit.xml"
106
+ end
107
+
108
+
109
+ def self.build_day_path(year, month, day)
110
+ set_date_vars(year, month, day)
111
+ "#{FILE_BASE_PATH}/year_#{@@year}/month_#{@@month}/day_#{@@day}/"
112
+ end
113
+
114
+
115
+ def self.build_month_path(year, month)
116
+ set_date_vars(year, month, nil)
117
+ "#{FILE_BASE_PATH}/year_#{@@year}/month_#{@@month}/"
118
+ end
119
+
120
+
121
+ private
122
+
123
+ def self.set_date_vars(year, month, day)
124
+ @@year = GamedayUtil.convert_digit_to_string(year.to_i)
125
+ @@month = GamedayUtil.convert_digit_to_string(month.to_i)
126
+ if day
127
+ @@day = GamedayUtil.convert_digit_to_string(day.to_i)
128
+ end
129
+ end
130
+
131
+
132
+ end