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/at_bat.rb +40 -0
- data/lib/batter.rb +101 -0
- data/lib/batting_appearance.rb +55 -0
- data/lib/box_score.rb +197 -0
- data/lib/cache_fetcher.rb +31 -0
- data/lib/coach.rb +16 -0
- data/lib/data_downloader.rb +231 -0
- data/lib/db_importer.rb +205 -0
- data/lib/event.rb +14 -0
- data/lib/event_log.rb +103 -0
- data/lib/game.rb +529 -0
- data/lib/game_status.rb +6 -0
- data/lib/gameday.rb +58 -0
- data/lib/gameday_fetcher.rb +245 -0
- data/lib/gameday_local_fetcher.rb +236 -0
- data/lib/gameday_parser.rb +305 -0
- data/lib/gameday_path_builder.rb +132 -0
- data/lib/gameday_remote_fetcher.rb +292 -0
- data/lib/gameday_url_builder.rb +125 -0
- data/lib/gameday_util.rb +136 -0
- data/lib/hip.rb +18 -0
- data/lib/hitchart.rb +26 -0
- data/lib/import_data.rb +8 -0
- data/lib/inning.rb +52 -0
- data/lib/line_score.rb +38 -0
- data/lib/media.rb +35 -0
- data/lib/media_highlight.rb +33 -0
- data/lib/media_mobile.rb +13 -0
- data/lib/pitch.rb +92 -0
- data/lib/pitcher.rb +131 -0
- data/lib/pitchfx_db_manager.rb +393 -0
- data/lib/pitching_appearance.rb +118 -0
- data/lib/player.rb +145 -0
- data/lib/players.rb +42 -0
- data/lib/roster.rb +61 -0
- data/lib/schedule.rb +53 -0
- data/lib/schedule_game.rb +24 -0
- data/lib/scoreboard.rb +23 -0
- data/lib/team.rb +336 -0
- metadata +42 -3
data/lib/db_importer.rb
ADDED
@@ -0,0 +1,205 @@
|
|
1
|
+
require 'pitchfx_db_manager'
|
2
|
+
require 'game'
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
class DbImporter
|
8
|
+
|
9
|
+
# player
|
10
|
+
|
11
|
+
# team
|
12
|
+
# game
|
13
|
+
# atbat
|
14
|
+
# pitch
|
15
|
+
# pitch type
|
16
|
+
# game type
|
17
|
+
# umpire
|
18
|
+
|
19
|
+
def initialize(host, user, password, db_name)
|
20
|
+
@db = PitchfxDbManager.new(host, user, password, db_name)
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
def import_team_for_month(team_abbrev, year, month)
|
25
|
+
start_date = Date.new(year.to_i, month.to_i) # first day of month
|
26
|
+
end_date = (start_date >> 1)-1 # last day of month
|
27
|
+
((start_date)..(end_date)).each do |dt|
|
28
|
+
puts year.to_s + '/' + month.to_s + '/' + dt.day
|
29
|
+
team = Team.new('det')
|
30
|
+
games = team.games_for_date(year, month, dt.day.to_s)
|
31
|
+
games.each do |game|
|
32
|
+
import_for_game(game.gid)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
def import_for_month(year, month)
|
39
|
+
start_date = Date.new(year.to_i, month.to_i) # first day of month
|
40
|
+
end_date = (start_date >> 1)-1 # last day of month
|
41
|
+
((start_date)..(end_date)).each do |dt|
|
42
|
+
puts dt.year.to_s + '/' + dt.month.to_s + '/' + dt.day.to_s
|
43
|
+
import_for_date(dt.year.to_s, dt.month.to_s, dt.day.to_s)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
def import_for_range(year, start_month, start_day, end_month, end_day)
|
49
|
+
start_date = Date.new(year.to_i, start_month.to_i, start_day.to_i)
|
50
|
+
end_date = Date.new(year.to_i, end_month.to_i, end_day.to_i)
|
51
|
+
((start_date)..(end_date)).each do |dt|
|
52
|
+
puts dt.year.to_s + '/' + dt.month.to_s + '/' + dt.day.to_s
|
53
|
+
import_for_date(dt.year.to_s, dt.month.to_s, dt.day.to_s)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
def import_for_date(year, month, day)
|
59
|
+
games = Game.find_by_date(year, month, day)
|
60
|
+
if games && games.length > 0
|
61
|
+
games.each do |game|
|
62
|
+
import_for_game(game.gid)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
def import_for_game(gid)
|
69
|
+
game = Game.new(gid)
|
70
|
+
visitor_id = @db.find_or_create_team(game.visiting_team)
|
71
|
+
home_id = @db.find_or_create_team(game.home_team)
|
72
|
+
import_players_for_game(gid, visitor_id, home_id)
|
73
|
+
game_id = @db.find_or_create_game(game, visitor_id, home_id)
|
74
|
+
abs = game.get_atbats
|
75
|
+
abs.each do |ab|
|
76
|
+
atbat_id = @db.find_or_create_atbat(ab, game_id)
|
77
|
+
pitches = ab.pitches
|
78
|
+
pitches.each do |pitch|
|
79
|
+
@db.find_or_create_pitch(pitch, atbat_id)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
import_pitcher_lines_for_game(gid)
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
def import_players_for_game(gid, visitor_id, home_id)
|
87
|
+
players = Players.new
|
88
|
+
players.load_from_id(gid)
|
89
|
+
away = players.rosters[0]
|
90
|
+
home = players.rosters[1]
|
91
|
+
away.players.each do |player|
|
92
|
+
@db.find_or_create_player(player, visitor_id)
|
93
|
+
end
|
94
|
+
home.players.each do |player|
|
95
|
+
@db.find_or_create_player(player, home_id)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
def import_pitcher_lines_for_month(year, month)
|
101
|
+
start_date = Date.new(year.to_i, month.to_i) # first day of month
|
102
|
+
end_date = (start_date >> 1)-1 # last day of month
|
103
|
+
((start_date)..(end_date)).each do |dt|
|
104
|
+
puts dt.year.to_s + '/' + dt.month.to_s + '/' + dt.day.to_s
|
105
|
+
import_pitcher_lines_for_date(dt.year.to_s, dt.month.to_s, dt.day.to_s)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
def import_pitcher_lines_for_date(year, month, day)
|
111
|
+
games = Game.find_by_date(year, month, day)
|
112
|
+
if games && games.length > 0
|
113
|
+
games.each do |game|
|
114
|
+
import_pitcher_lines_for_game(game.gid)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
|
120
|
+
def import_pitcher_lines_for_range(year, start_month, start_day, end_month, end_day)
|
121
|
+
start_date = Date.new(year.to_i, start_month.to_i, start_day.to_i)
|
122
|
+
end_date = Date.new(year.to_i, end_month.to_i, end_day.to_i)
|
123
|
+
((start_date)..(end_date)).each do |dt|
|
124
|
+
puts dt.year.to_s + '/' + dt.month.to_s + '/' + dt.day.to_s
|
125
|
+
import_pitcher_lines_for_date(dt.year.to_s, dt.month.to_s, dt.day.to_s)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
def import_pitcher_lines_for_game(gid)
|
131
|
+
game = Game.new(gid)
|
132
|
+
pitchers = game.get_boxscore.pitchers
|
133
|
+
pitchers.each do |h_or_a_pitchers|
|
134
|
+
h_or_a_pitchers.each do |pitcher|
|
135
|
+
@db.find_or_create_pitcher_line(pitcher, game)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
|
141
|
+
def set_status_for_range(year, start_month, start_day, end_month, end_day)
|
142
|
+
start_date = Date.new(year.to_i, start_month.to_i, start_day.to_i)
|
143
|
+
end_date = Date.new(year.to_i, end_month.to_i, end_day.to_i)
|
144
|
+
((start_date)..(end_date)).each do |dt|
|
145
|
+
puts dt.year.to_s + '/' + dt.month.to_s + '/' + dt.day.to_s
|
146
|
+
set_status_for_date(dt.year.to_s, dt.month.to_s, dt.day.to_s)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
|
151
|
+
def set_status_for_date(year, month, day)
|
152
|
+
games = Game.find_by_date(year, month, day)
|
153
|
+
if games && games.length > 0
|
154
|
+
games.each do |game|
|
155
|
+
set_status_for_game(game.gid)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
|
161
|
+
def set_status_for_game(gid)
|
162
|
+
game = Game.new(gid)
|
163
|
+
@db.update_status_for_game(game)
|
164
|
+
end
|
165
|
+
|
166
|
+
|
167
|
+
def import_rosters_for_range(year, start_month, start_day, end_month, end_day)
|
168
|
+
start_date = Date.new(year.to_i, start_month.to_i, start_day.to_i)
|
169
|
+
end_date = Date.new(year.to_i, end_month.to_i, end_day.to_i)
|
170
|
+
((start_date)..(end_date)).each do |dt|
|
171
|
+
puts dt.year.to_s + '/' + dt.month.to_s + '/' + dt.day.to_s
|
172
|
+
import_rosters_for_date(dt.year.to_s, dt.month.to_s, dt.day.to_s)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
|
177
|
+
def import_rosters_for_date(year, month, day)
|
178
|
+
games = Game.find_by_date(year, month, day)
|
179
|
+
if games && games.length > 0
|
180
|
+
games.each do |game|
|
181
|
+
import_rosters_for_game(game.gid)
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
|
187
|
+
def import_rosters_for_game(gid)
|
188
|
+
game = Game.new(gid)
|
189
|
+
away_id = @db.find_or_create_team(game.visiting_team)
|
190
|
+
home_id = @db.find_or_create_team(game.home_team)
|
191
|
+
players = Players.new
|
192
|
+
players.load_from_id(gid)
|
193
|
+
away = players.rosters[0]
|
194
|
+
home = players.rosters[1]
|
195
|
+
roster_ids = @db.find_or_create_rosters(gid, away_id, home_id)
|
196
|
+
away.players.each do |player|
|
197
|
+
@db.find_or_create_roster_player(player, roster_ids[0])
|
198
|
+
end
|
199
|
+
home.players.each do |player|
|
200
|
+
@db.find_or_create_roster_player(player, roster_ids[1])
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
|
205
|
+
end
|
data/lib/event.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
class Event
|
2
|
+
|
3
|
+
attr_accessor :number, :inning, :description, :team
|
4
|
+
|
5
|
+
def load(element, home_or_away)
|
6
|
+
@xml_doc = element
|
7
|
+
@team = home_or_away
|
8
|
+
@number = element.attributes["number"]
|
9
|
+
@inning = element.attributes["inning"]
|
10
|
+
@description = element.attributes["description"]
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
end
|
data/lib/event_log.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'event'
|
2
|
+
require 'gameday_fetcher'
|
3
|
+
|
4
|
+
|
5
|
+
# Parses the MLB Gameday eventLog.xml file
|
6
|
+
class EventLog
|
7
|
+
|
8
|
+
attr_accessor :home_team, :away_team, :gid
|
9
|
+
attr_accessor :home_events, :away_events
|
10
|
+
attr_accessor :max_inning
|
11
|
+
|
12
|
+
|
13
|
+
# Loads the eventLog XML from the MLB gameday server and parses it using REXML
|
14
|
+
def load_from_id(gid)
|
15
|
+
@gid = gid
|
16
|
+
@xml_data = GamedayFetcher.fetch_eventlog(gid)
|
17
|
+
@xml_doc = REXML::Document.new(@xml_data)
|
18
|
+
if @xml_doc.root
|
19
|
+
set_teams
|
20
|
+
set_events
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
# Sets the team names for the teams involved in this game
|
26
|
+
def set_teams
|
27
|
+
@xml_doc.elements.each("game/team[@home_team='false']") do |element|
|
28
|
+
@away_team = element.attributes["name"]
|
29
|
+
end
|
30
|
+
@xml_doc.elements.each("game/team[@home_team='true']") do |element|
|
31
|
+
@home_team = element.attributes["name"]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
def set_events
|
37
|
+
@home_events, @away_events = [], []
|
38
|
+
@max_inning = 0
|
39
|
+
# set away team events
|
40
|
+
@xml_doc.elements.each("game/team[@home_team='false']/event") { |element|
|
41
|
+
event = Event.new
|
42
|
+
event.load(element, 'away')
|
43
|
+
@max_inning = event.inning.to_i unless event.inning.to_i < @max_inning
|
44
|
+
@away_events.push event
|
45
|
+
}
|
46
|
+
# set home team events
|
47
|
+
@xml_doc.elements.each("game/team[@home_team='true']/event") { |element|
|
48
|
+
event = Event.new
|
49
|
+
event.load(element, 'home')
|
50
|
+
@home_events.push event
|
51
|
+
}
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
def events_by_inning(inning)
|
56
|
+
events = []
|
57
|
+
@away_events.each do |event|
|
58
|
+
if event.inning == inning
|
59
|
+
events << event
|
60
|
+
end
|
61
|
+
end
|
62
|
+
@home_events.each do |event|
|
63
|
+
if event.inning == inning
|
64
|
+
events << event
|
65
|
+
end
|
66
|
+
end
|
67
|
+
events
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
def dump
|
72
|
+
(1..@max_inning).each do |inning|
|
73
|
+
dump_inning(inning.to_s)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
def dump_inning(inning)
|
79
|
+
puts 'Inning - ' + inning.to_s
|
80
|
+
events = events_by_inning(inning)
|
81
|
+
events.each do |event|
|
82
|
+
puts event.description
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
# NOT IMPLEMENTED YET
|
88
|
+
def dump_to_file
|
89
|
+
GamedayUtil.save_file("eventlog.html", self.to_html('eventlog.html.erb'))
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
# NOT IMPLEMENTED YET
|
94
|
+
# Converts the eventlog into a formatted HTML representation.
|
95
|
+
# Relies on the eventlog.html.erb template for describing the layout
|
96
|
+
def to_html(template_filename)
|
97
|
+
gameday_info = GamedayUtil.parse_gameday_id('gid_' + gid)
|
98
|
+
template = ERB.new File.new(File.expand_path(File.dirname(__FILE__) + "/" + template_filename)).read, nil, "%"
|
99
|
+
return template.result(binding)
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
end
|