Created
October 24, 2011 11:33
-
-
Save mwfrost/1308821 to your computer and use it in GitHub Desktop.
Box Scores from MLB.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # References: | |
| # http://www.r-bloggers.com/mlb-baseball-pitching-matchups-downloading-pitch-fx-data-using-the-xml-package-in-r%C2%A0updatedx6/ | |
| # http://blogisticreflections.wordpress.com/2009/10/04/using-r-to-analyze-baseball-games-in-real-time/ | |
| # http://gd2.mlb.com/components/game/mlb/year_2010/month_10/day_12/miniscoreboard.xml | |
| ################################################################################ | |
| # Program Name: xml-mlb-gameday.R | |
| # Author: Erik | |
| # Created: 10/04/2009 | |
| # | |
| # | |
| # Purpose: show current scoreboard in R | |
| # | |
| ################################################################################ | |
| ## need XML package, may need to install w/ install.packages() | |
| library(XML) | |
| ## create a boxscore object from an XML description of a game | |
| createBoxScore <- function(x) { | |
| status <- if(x$.attrs["status"] != "In Progress") | |
| "Final" else if(x$.attrs["top_inning"] == "Y") | |
| "Top" else "Bot" | |
| bs <- list(status = status, | |
| inning = as.numeric(x$.attrs["inning"]), | |
| away.team = x$.attrs["away_name_abbrev"], | |
| away.runs = as.numeric(x$.attrs["away_team_runs"]), | |
| away.hits = as.numeric(x$.attrs["away_team_hits"]), | |
| away.errors = as.numeric(x$.attrs["away_team_errors"]), | |
| home.team = x$.attrs["home_name_abbrev"], | |
| home.runs = as.numeric(x$.attrs["home_team_runs"]), | |
| home.hits = as.numeric(x$.attrs["home_team_hits"]), | |
| home.errors = as.numeric(x$.attrs["home_team_errors"])) | |
| class(bs) <- "boxscore" | |
| bs | |
| } | |
| ## print the boxscore object in traditional format | |
| print.boxscore <- function(x, ...) { | |
| cat(" ", "R ", "H ", "E (", | |
| x$status, " ", | |
| x$inning, ")\n", | |
| format(x$away.team, width = 3), " ", | |
| format(x$away.runs, width = 2), " ", | |
| format(x$away.hits, width = 2), " ", | |
| x$away.errors, "\n", | |
| format(x$home.team, width = 3), " ", | |
| format(x$home.runs, width = 2), " ", | |
| format(x$home.hits, width = 2), " ", | |
| x$home.errors, "\n\n", sep = "") | |
| } | |
| ## utility function ... | |
| as.data.frame.boxscore <- function(x, row.names, optional, ...) { | |
| class(x) <- "list" | |
| as.data.frame(x) | |
| } | |
| ## This is the "user accessible" public function you should be calling! | |
| ## downloads the XML data, and prints out boxscores for games on "date" | |
| boxscore <- function(date = Sys.Date()) { | |
| if(date > Sys.Date()) | |
| stop("Cannot retrieve scores from the future.") | |
| year <- paste("year_", format(date, "%Y"), "/", sep = "") | |
| month <- paste("month_", format(date, "%m"), "/", sep = "") | |
| day <- paste("day_", format(date, "%d"), "/", sep = "") | |
| xmlFile <- | |
| paste("http://gd2.mlb.com/components/game/mlb/", | |
| year, month, day, "miniscoreboard.xml", sep = "") | |
| xmlTree <- xmlTreeParse(xmlFile, useInternalNodes = TRUE) | |
| xp <- xpathApply(xmlTree, "//game") | |
| xmlList <- lapply(xp, xmlToList) | |
| bs.list <- lapply(xmlList, createBoxScore) | |
| names(bs.list) <- | |
| paste(sapply(bs.list, "[[", "away.team"), | |
| "@", | |
| sapply(bs.list, "[[", "home.team")) | |
| bs.list | |
| } | |
| gamexml <- function(date = Sys.Date()) { | |
| if(date > Sys.Date()) | |
| stop("Cannot retrieve scores from the future.") | |
| year <- paste("year_", format(date, "%Y"), "/", sep = "") | |
| month <- paste("month_", format(date, "%m"), "/", sep = "") | |
| day <- paste("day_", format(date, "%d"), "/", sep = "") | |
| xmlFile <- | |
| paste("http://gd2.mlb.com/components/game/mlb/", | |
| year, month, day, "miniscoreboard.xml", sep = "") | |
| xmlTree <- xmlTreeParse(xmlFile, useInternalNodes = TRUE) | |
| xp <- xpathApply(xmlTree, "//game") | |
| xp | |
| } | |
| bs <- boxscore(date = Sys.Date()-1) | |
| bs.df <- do.call(rbind, lapply(bs, as.data.frame)) | |
| bs.df$updated <- Sys.time() | |
| write.table(bs.df,file="~/Dropbox/Public/boxscore.csv",sep=",",na="",row.names=FALSE) | |
| write.table(bs.df,file="~/Dropbox/Public/boxscore.txt",sep="\t",na="",row.names=FALSE) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment