|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
From the Blogosphere How to Generate Boxscores with Ruby from Live MLB Data
As an avid baseball fan, I’ve always been interested in the statistics that surround baseball
By: Timothy Fisher
Jul. 20, 2009 05:15 PM
As an avid baseball fan, I’ve always been interested in the statistics that surround baseball. More so than in any other sport, baseball is a game ruled by statistics. In this post, I will describe a program that I wrote in the Ruby language to generate box scores for any Major Leage Baseball(MLB) game by using the live XML data provided by MLB. Ruby makes it easy to do using plain Ruby along with just a few libraries. Live MLB Data Building a boxscore template with ERb Here is an example of ERb taken from my boxscore template. This section is used to display the linescore of a game: <div id="linescore">
<label><%= cities[0] %></label>
<% (0..8).each do |num| %>
<%= innings[num][0] %>
<% end %>
<b><%= tots[0][0] + ' ' + tots[0][1] + ' ' + tots[0][2] %></b>
<br/>
<label><%= cities[1] %></label>
<% (0..8).each do |num| %>
<%= innings[num][1] %>
<% end %>
<b><%= tots[1][0] + ' ' + tots[1][1] + ' ' + tots[1][2] %></b>
</div>
At the bottom of this post you can find links to download the complete source code for this project including the full boxscore ERb template. Steps to generate a boxscore
The sections below describe each of these steps in more detail. Find the correct Gameday ID for the game you are interested in Each MLB game can be identified on the gameday server with a gameday ID. The gameday ID is a string that contains the date of the game concatenated with team information. A gameday ID has the following format: 2009_06_09_chnmlb_houmlb_1 Finding the gameday ID for a game you are interested in is done by looking at the subdirectories contained in a directory that corresponds to the game date you are looking for. That directory will contain a subdirectory for each game played on that date. The game ID can be parsed from the name of that game’s subdirectory. For example, if you were interested in the Detroit Tigers game played on June 6, 2009, you would scan through the subdirectories contained in this directotry: http://gd2.mlb.com/components/game/mlb/year_2009/month_06/day_21/ Within that directory, you would find a subdirectory named: gid_2009_06_21_milmlb_detmlb_1/ and from that you can find the game id by stripping off the leading ‘gid_’ from the directory name. Retreive the correct boxscore.xml file from gd2.mlb.com I have defined a constant to represent the root path for the gameday server. GD2_MLB_BASE = "http://gd2.mlb.com/components/game" Below is the ruby code used to retrieve the boxscore.xml file. def get_boxscore(year, month, day, gid)
url = "#{GD2_MLB_BASE}/mlb/year_" + year +
"/month_" + month + "/day_" + day +
"/gid_"+gid+"/boxscore.xml"
xml_data = Net::HTTP.get_response(URI.parse(url)).body
return xml_data
end
Parse the boxscore XML to pull out relevant data # Returns an array of hashes where each hash holds data
# for a batter whom appeared in the game. Specify either
# home or away team batters.
def get_batters(home_or_away)
doc = REXML::Document.new(@xml_data)
batters = []
doc.elements.each(
"boxscore/batting[@team_flag='#{home_or_away}']/batter")
{ |element|
batter = {}
batter['name'] = element.attributes['name']
batter['pos'] = element.attributes['pos']
batter['ab'] = element.attributes['ab']
batter['r'] = element.attributes['r']
batter['bb'] = element.attributes['bb']
batter['sf'] = element.attributes['sf']
batter['h'] = element.attributes['h']
batter['e'] = element.attributes['e']
batter['d'] = element.attributes['d']
batter['t'] = element.attributes['t']
batter['hbp'] = element.attributes['hbp']
batter['so'] = element.attributes['so']
batter['hr'] = element.attributes['hr']
batter['rbi'] = element.attributes['rbi']
batter['sb'] = element.attributes['sb']
batter['avg'] = element.attributes['avg']
batters.push batter
}
return batters
end
Merge the data into the boxscore template and save the result # Converts the boxscore into a formatted HTML representation.
def to_html
cities = get_cities
innings = get_innings
tots = get_linescore_totals
home_pitchers = get_pitchers('home')
away_pitchers = get_pitchers('away')
home_batters = get_batters('home')
away_batters = get_batters('away')
home_batters_text = get_batting_text('home')
away_batters_text = get_batting_text('away')
game_info = get_game_info
gameday_info = parse_gameday_id('gid_' + gid)
template = ERB.new File.new("boxscore.html.erb").read, nil, "%"
return template.result(binding)
end
View the results and download the code MLB copyright notice Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||