private static List <MlbGameSummaryModel> ParseRows(HtmlNodeCollection rows, MlbTeamShortName mlbTeam, int seasonYear) { // error checks if (null == rows || rows.Count == 0) { return(null); } HtmlNode firstRow = rows[0]; HtmlNode firstColumn = firstRow.SelectSingleNode(@".//td"); if (firstColumn.InnerText.IndexOf("no schedule", StringComparison.InvariantCultureIgnoreCase) >= 0) { // There are no games in this page return(null); } List <MlbGameSummaryModel> games = new List <MlbGameSummaryModel>(); foreach (HtmlNode row in rows) { HtmlNodeCollection columns = row.SelectNodes(@".//td"); MlbGameSummaryModel game = MlbAttendanceQuery.ParseRow(columns, mlbTeam, seasonYear, true); if (null != game) { games.Add(game); } } return(games); }
public static List<MlbGameSummaryModel> UpdateSeasonForTeam(MlbSeasonType mlbSeasonType, MlbTeamShortName mlbTeam, int seasonYear) { // Get latest results List<MlbGameSummaryModel> gamesToAdd = MlbAttendanceQuery.GetSeasonForTeam(mlbSeasonType, mlbTeam, seasonYear); // Remove existing results from DB and save new ones using (SportsDataContext db = new SportsDataContext()) { string mlbTeamString = mlbTeam.ToString(); // http://stackoverflow.com/questions/5899683/linq-to-entities-does-not-recognize-the-method-system-string-tostring-method var gamesToRemove = from g in db.MlbGameSummaryModel_DbSet where g.MlbSeasonType == mlbSeasonType && g.Year == seasonYear && g.Home.Equals(mlbTeamString, StringComparison.InvariantCultureIgnoreCase) select g; foreach (MlbGameSummaryModel gameToRemove in gamesToRemove) { db.MlbGameSummaryModel_DbSet.Remove(gameToRemove); } if (null != gamesToAdd) { foreach (MlbGameSummaryModel gameToAdd in gamesToAdd) { db.MlbGameSummaryModel_DbSet.Add(gameToAdd); } } db.SaveChanges(); } return gamesToAdd; }
public void Mlb_GetPostSeasonForTeamWithNoPostSeasonTest() { MlbSeasonType seasonType = MlbSeasonType.PostSeason; MlbTeamShortName teamName = MlbTeamShortName.MIA; int seasonYear = 2007; List <MlbGameSummaryModel> result = MlbAttendanceQuery.GetSeasonForTeam(seasonType, teamName, seasonYear); Assert.AreEqual(MlbSeasonType.PostSeason, result[0].MlbSeasonType, "Verify that the game is a Postseason game"); Assert.AreEqual(null, result, "Verify that there are 0 games returned"); }
public void Mlb_GetSpringSeasonForTeamTest() { MlbSeasonType seasonType = MlbSeasonType.Spring; MlbTeamShortName teamName = MlbTeamShortName.NYY; int seasonYear = 2012; List <MlbGameSummaryModel> result = MlbAttendanceQuery.GetSeasonForTeam(seasonType, teamName, seasonYear); Assert.AreEqual(12, result.Count, "Verify that there are 12 home games returned"); Assert.AreEqual(MlbSeasonType.Spring, result[0].MlbSeasonType, "Verify that the game is a Spring season game"); Assert.AreEqual(new DateTime(2012, 3, 2), result[0].Date, "Verify that the date of the first game is 3/2/2012"); Assert.AreEqual(2012, result[0].Year, "Verify that the Season is 2012"); Assert.AreEqual(MlbTeamShortName.NYY.ToString(), result[1].Home, "Verify that the home team is NYY"); Assert.AreEqual(MlbTeamShortName.TB.ToString(), result[2].Visitor, "Verify that the away team is TB"); }
public static List<MlbGameSummaryModel> GetSeasonForTeam(MlbSeasonType mlbSeasonType, MlbTeamShortName mlbTeam, int seasonYear) { string relativeUrl; List<MlbGameSummaryModel> results; switch (mlbSeasonType) { case MlbSeasonType.Spring: relativeUrl = String.Format(MlbAttendanceQuery.preSeasonFormatString, mlbTeam.ToString(), seasonYear); results = MlbAttendanceQuery.GetPage(relativeUrl, mlbTeam, seasonYear); break; case MlbSeasonType.Regular: // Collect first half of the season relativeUrl = String.Format(MlbAttendanceQuery.regSeasonFormatString, mlbTeam.ToString(), seasonYear, 1); List<MlbGameSummaryModel> firstHalf = MlbAttendanceQuery.GetPage(relativeUrl, mlbTeam, seasonYear); // Collect second half of the season relativeUrl = String.Format(MlbAttendanceQuery.regSeasonFormatString, mlbTeam.ToString(), seasonYear, 2); List<MlbGameSummaryModel> secondHalf = MlbAttendanceQuery.GetPage(relativeUrl, mlbTeam, seasonYear); // Merge them together results = new List<MlbGameSummaryModel>(); results.AddRange(firstHalf); results.AddRange(secondHalf); break; case MlbSeasonType.PostSeason: relativeUrl = String.Format(MlbAttendanceQuery.postSeasonFormatString, mlbTeam.ToString(), seasonYear); results = MlbAttendanceQuery.GetPage(relativeUrl, mlbTeam, seasonYear); break; default: throw new ArgumentException(String.Format("Unrecognized season type {0}", mlbSeasonType.ToString())); } // Add the season type and year to every item if (null != results) { results.ForEach(x => { x.MlbSeasonType = mlbSeasonType; x.Year = seasonYear; }); } return results; }
private static void DetermineHomeAndAway(HtmlNode gameStatusNode, MlbTeamShortName mlbTeam, string opponentTeamCity, ref MlbGameSummaryModel game) { //Figure out home/away teams and then return if (gameStatusNode.InnerText.Equals("vs", StringComparison.InvariantCultureIgnoreCase)) { // home game for mlbTeam game.Home = mlbTeam.ToString(); string shortName = MlbAttendanceQuery.LookupShortName(opponentTeamCity); game.Visitor = String.IsNullOrWhiteSpace(shortName) ? opponentTeamCity : shortName; } else if (gameStatusNode.InnerText.Equals("@", StringComparison.InvariantCultureIgnoreCase)) { // away game for mlbTeam string shortName = MlbAttendanceQuery.LookupShortName(opponentTeamCity); game.Home = String.IsNullOrWhiteSpace(shortName) ? opponentTeamCity : shortName; game.Visitor = mlbTeam.ToString(); } }
public void Mlb_UpdateRegularSeasonForTeamTest() { MlbSeasonType seasonType = MlbSeasonType.Regular; MlbTeamShortName teamName = MlbTeamShortName.SD; int seasonYear = 2005; List <MlbGameSummaryModel> result = MlbAttendanceData.UpdateSeasonForTeam(seasonType, teamName, seasonYear); Assert.AreEqual(81, result.Count, "Verify that there are 81 home games returned"); using (SportsDataContext db = new SportsDataContext()) { string teamNameString = teamName.ToString(); // http://stackoverflow.com/questions/5899683/linq-to-entities-does-not-recognize-the-method-system-string-tostring-method Assert.AreEqual(81, db.MlbGameSummaryModel_DbSet.Where(g => g.MlbSeasonType == seasonType && g.Home.Equals(teamNameString, StringComparison.InvariantCultureIgnoreCase) && g.Year == seasonYear).Count(), "Verify that there are 81 games in the db"); } }
private static List <MlbGameSummaryModel> GetPage(string relativeUrl, MlbTeamShortName mlbTeam, int seasonYear) { // Construct the url Uri url = new Uri(relativeUrl, UriKind.Relative); // Make an http request HttpClient httpClient = new HttpClient(); httpClient.BaseAddress = new Uri(MlbAttendanceQuery.baseAddress); Task <string> httpResponseMessage = httpClient.GetStringAsync(url); string responseString = null; int numRetries = 5; for (int i = 0; i < numRetries; i++) { try { httpResponseMessage = httpClient.GetStringAsync(url); responseString = httpResponseMessage.Result; break; } catch (Exception e) { if (i == numRetries - 1) { throw; } } } HtmlDocument document = new HtmlDocument(); document.LoadHtml(responseString); HtmlNode tableNode = document.DocumentNode.SelectSingleNode(@"//table[@class='tablehead']"); HtmlNodeCollection rows = tableNode.SelectNodes(@".//tr[contains(@class,'evenrow') or contains(@class,'oddrow')]"); List <MlbGameSummaryModel> games = MlbAttendanceQuery.ParseRows(rows, mlbTeam, seasonYear); return(games); }
public void Mlb_GetRegularSeasonForTeamTest() { MlbSeasonType seasonType = MlbSeasonType.Regular; MlbTeamShortName teamName = MlbTeamShortName.SEA; int seasonYear = 2010; List <MlbGameSummaryModel> result = MlbAttendanceQuery.GetSeasonForTeam(seasonType, teamName, seasonYear); Assert.AreEqual(81, result.Count, "Verify that there are 81 home games returned"); Assert.AreEqual(MlbSeasonType.Regular, result[0].MlbSeasonType, "Verify that the game is a Regular season game"); Assert.AreEqual(new DateTime(2010, 10, 3), result[80].Date, "Verify that the date of the last game is 10/3/2010"); Assert.AreEqual(MlbTeamShortName.SEA.ToString(), result[80].Home, "Verify that the home team is SEA"); Assert.AreEqual(MlbTeamShortName.OAK.ToString(), result[80].Visitor, "Verify that the away team is OAK"); Assert.AreEqual(3, result[80].HomeScore, "Verify that the home score is 3"); Assert.AreEqual(4, result[80].VisitorScore, "Verify that visitor score is 4"); Assert.AreEqual(61, result[80].WinsToDate, "Verify that the wins to date is 61"); Assert.AreEqual(101, result[80].LossesToDate, "Verify that losses to date is 101"); Assert.AreEqual("Braden", result[80].WPitcher, "Verify that the W Pitcher is Braden"); Assert.AreEqual("Varvaro", result[80].LPitcher, "Verify that the L Pitcher is Varvaro"); Assert.AreEqual("Breslow", result[80].SavePitcher, "Verify that the Save Pitcher is Breslow"); Assert.AreEqual(23278, result[80].Attendance, "Verify that attendance is 23278"); }
public void Mlb_GetPostSeasonForTeamTest() { MlbSeasonType seasonType = MlbSeasonType.PostSeason; MlbTeamShortName teamName = MlbTeamShortName.BOS; int seasonYear = 2008; List <MlbGameSummaryModel> result = MlbAttendanceQuery.GetSeasonForTeam(seasonType, teamName, seasonYear); Assert.AreEqual(5, result.Count, "Verify that there are 5 home games returned"); Assert.AreEqual(2008, result[0].Year, "Verify that the Season is 2008"); Assert.AreEqual(MlbSeasonType.PostSeason, result[0].MlbSeasonType, "Verify that the game is a Postseason game"); Assert.AreEqual(new DateTime(2008, 10, 6), result[1].Date, "Verify that the date of the last game is 10/6/2008"); Assert.AreEqual(MlbTeamShortName.BOS.ToString(), result[1].Home, "Verify that the home team is BOS"); Assert.AreEqual(MlbTeamShortName.LAA.ToString(), result[1].Visitor, "Verify that the away team is LAA"); Assert.AreEqual(3, result[1].HomeScore, "Verify that the home score is 3"); Assert.AreEqual(2, result[1].VisitorScore, "Verify that visitor score is 2"); Assert.AreEqual(3, result[1].WinsToDate, "Verify that the wins to date is 3"); Assert.AreEqual(1, result[1].LossesToDate, "Verify that losses to date is 1"); Assert.AreEqual("Delcarmen", result[1].WPitcher, "Verify that the W Pitcher is Delcarmen"); Assert.AreEqual("Shields", result[1].LPitcher, "Verify that the L Pitcher is Shields"); Assert.AreEqual(null, result[1].SavePitcher, "Verify that the Save Pitcher is empty"); Assert.AreEqual(38785, result[1].Attendance, "Verify that attendance is 38785"); }
private static List<MlbGameSummaryModel> ParseRows(HtmlNodeCollection rows, MlbTeamShortName mlbTeam, int seasonYear) { // error checks if (null == rows || rows.Count == 0) { return null; } HtmlNode firstRow = rows[0]; HtmlNode firstColumn = firstRow.SelectSingleNode(@".//td"); if (firstColumn.InnerText.IndexOf("no schedule", StringComparison.InvariantCultureIgnoreCase) >= 0) { // There are no games in this page return null; } List<MlbGameSummaryModel> games = new List<MlbGameSummaryModel>(); foreach (HtmlNode row in rows) { HtmlNodeCollection columns = row.SelectNodes(@".//td"); MlbGameSummaryModel game = MlbAttendanceQuery.ParseRow(columns, mlbTeam, seasonYear, true); if (null != game) { games.Add(game); } } return games; }
private static MlbGameSummaryModel ParseRow(HtmlNodeCollection columns, MlbTeamShortName mlbTeam, int seasonYear, bool includeHomeGamesOnly = true) { MlbGameSummaryModel game = new MlbGameSummaryModel(); // Check if this is a home or away game and if we should proceed parsing HtmlNode gameStatusNode = columns[1].SelectSingleNode(@".//li[@class='game-status']"); if (gameStatusNode.InnerText.Equals("@") && includeHomeGamesOnly) { // This is an away game and we don't want to include away games so return null return null; } // Game Date HtmlNode dateNode = columns[0]; DateTime date = Convert.ToDateTime(dateNode.InnerText + " " + seasonYear); game.Date = date; if (date >= DateTime.Now.Date) // Don't retrieve games from today or future { return null; } // Determine the Opponent HtmlNode opponentNode = columns[1]; HtmlNode opponentTeamNode = opponentNode.SelectSingleNode(@".//li[@class='team-name']"); string opponentTeamCity = opponentTeamNode.InnerText; // Check if the game was postponed if (columns[2].InnerText.Equals("postponed", StringComparison.InvariantCultureIgnoreCase) || columns[3].InnerText.Equals("delayed", StringComparison.InvariantCultureIgnoreCase) || columns[3].InnerText.Equals("suspended", StringComparison.InvariantCultureIgnoreCase)) { // The game was postponed. Figure out home/away teams and then return game.Postponed = true; MlbAttendanceQuery.DetermineHomeAndAway(gameStatusNode, mlbTeam, opponentTeamCity, ref game); return game; } else { game.Postponed = false; } // Game Result HtmlNode gameResultNode = columns[2]; HtmlNode scoreNode = gameResultNode.SelectSingleNode(@".//li[contains(@class,'score')]"); // Check if the score can be parsed out and if not then return if (null == scoreNode) { MlbAttendanceQuery.DetermineHomeAndAway(gameStatusNode, mlbTeam, opponentTeamCity, ref game); return game; } // Check if there were extra innings string score = scoreNode.InnerText; string fToken = "f/"; // This string implies there were extra innings int fIndex = score.IndexOf(fToken, StringComparison.InvariantCultureIgnoreCase); if (fIndex >= 0) { game.Innings = Convert.ToInt32(score.Substring(fIndex + fToken.Length)); score = score.Substring(0, fIndex).Trim(); } else { game.Innings = 9; } int winningScore; int losingScore; try { winningScore = score.Split('-').Max(x => Convert.ToInt32(x)); losingScore = score.Split('-').Min(x => Convert.ToInt32(x)); } catch (FormatException) { winningScore = 0; losingScore = 0; MlbAttendanceQuery.DetermineHomeAndAway(gameStatusNode, mlbTeam, opponentTeamCity, ref game); return game; } // Figure out if the home team won or lost HtmlNode winLossNode = gameResultNode.SelectSingleNode(@".//li[contains(@class,'game-status')]"); int mlbTeamScore; int opponentScore; if (winLossNode.Attributes["class"].Value.IndexOf("win", StringComparison.InvariantCultureIgnoreCase) >= 0) // case-insensitive 'contains' { mlbTeamScore = winningScore; opponentScore = losingScore; } else if (winLossNode.Attributes["class"].Value.IndexOf("loss", StringComparison.InvariantCultureIgnoreCase) >= 0) // case-insensitive 'contains' { mlbTeamScore = losingScore; opponentScore = winningScore; } else if (winLossNode.Attributes["class"].Value.IndexOf("tie", StringComparison.InvariantCultureIgnoreCase) >= 0) // case-insensitive 'contains' { mlbTeamScore = winningScore; opponentScore = winningScore; } else { throw new Exception("Could not determine win or loss"); } // Get Win-Loss record for mlbTeam HtmlNode winLossRecordNode = columns[3]; string[] winLossRecord = winLossRecordNode.InnerText.Split('-'); if (null != winLossRecord && winLossRecord.Length == 2) { game.WinsToDate = Convert.ToInt32(winLossRecord[0]); game.LossesToDate = Convert.ToInt32(winLossRecord[1]); } else { // do not set any values since we couldn't parse out the win-loss record } // Get pitchers HtmlNode winningPitcherNode = columns[4].SelectSingleNode(@".//a"); game.WPitcher = winningPitcherNode != null ? winningPitcherNode.InnerText : null; HtmlNode losingPitcherNode = columns[5].SelectSingleNode(@".//a"); game.LPitcher = losingPitcherNode != null ? losingPitcherNode.InnerText : null; HtmlNode savingPitcherNode = columns[6].SelectSingleNode(@".//a"); game.SavePitcher = savingPitcherNode != null ? savingPitcherNode.InnerText : null; // Determine home and away teams and which was the winner or loser // Note: gameStatusNode was initialized at the start of the method if (gameStatusNode.InnerText.Equals("vs", StringComparison.InvariantCultureIgnoreCase)) { // home game for mlbTeam game.Home = mlbTeam.ToString(); game.HomeScore = mlbTeamScore; string shortName = MlbAttendanceQuery.LookupShortName(opponentTeamCity); game.Visitor = String.IsNullOrWhiteSpace(shortName) ? opponentTeamCity : shortName; game.VisitorScore = opponentScore; } else if (gameStatusNode.InnerText.Equals("@", StringComparison.InvariantCultureIgnoreCase)) { // away game for mlbTeam string shortName = MlbAttendanceQuery.LookupShortName(opponentTeamCity); game.Home = String.IsNullOrWhiteSpace(shortName) ? opponentTeamCity : shortName; game.HomeScore = opponentScore; game.Visitor = mlbTeam.ToString(); game.VisitorScore = mlbTeamScore; } // Get Attendance Data HtmlNode attendanceNode = columns[7]; int attendance = 0; Int32.TryParse(attendanceNode.InnerText, NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out attendance); game.Attendance = attendance; return game; }
private static List<MlbGameSummaryModel> GetPage(string relativeUrl, MlbTeamShortName mlbTeam, int seasonYear) { // Construct the url Uri url = new Uri(relativeUrl, UriKind.Relative); // Make an http request HttpClient httpClient = new HttpClient(); httpClient.BaseAddress = new Uri(MlbAttendanceQuery.baseAddress); Task<string> httpResponseMessage = httpClient.GetStringAsync(url); string responseString = null; int numRetries = 5; for (int i = 0; i < numRetries; i++) { try { httpResponseMessage = httpClient.GetStringAsync(url); responseString = httpResponseMessage.Result; break; } catch (Exception e) { if (i == numRetries - 1) { throw; } } } HtmlDocument document = new HtmlDocument(); document.LoadHtml(responseString); HtmlNode tableNode = document.DocumentNode.SelectSingleNode(@"//table[@class='tablehead']"); HtmlNodeCollection rows = tableNode.SelectNodes(@".//tr[contains(@class,'evenrow') or contains(@class,'oddrow')]"); List<MlbGameSummaryModel> games = MlbAttendanceQuery.ParseRows(rows, mlbTeam, seasonYear); return games; }
public static List <MlbGameSummaryModel> UpdateSeasonForTeam(MlbSeasonType mlbSeasonType, MlbTeamShortName mlbTeam, int seasonYear) { // Get latest results List <MlbGameSummaryModel> gamesToAdd = MlbAttendanceQuery.GetSeasonForTeam(mlbSeasonType, mlbTeam, seasonYear); // Remove existing results from DB and save new ones using (SportsDataContext db = new SportsDataContext()) { string mlbTeamString = mlbTeam.ToString(); // http://stackoverflow.com/questions/5899683/linq-to-entities-does-not-recognize-the-method-system-string-tostring-method var gamesToRemove = from g in db.MlbGameSummaryModel_DbSet where g.MlbSeasonType == mlbSeasonType && g.Year == seasonYear && g.Home.Equals(mlbTeamString, StringComparison.InvariantCultureIgnoreCase) select g; foreach (MlbGameSummaryModel gameToRemove in gamesToRemove) { db.MlbGameSummaryModel_DbSet.Remove(gameToRemove); } if (null != gamesToAdd) { foreach (MlbGameSummaryModel gameToAdd in gamesToAdd) { db.MlbGameSummaryModel_DbSet.Add(gameToAdd); } } db.SaveChanges(); } return(gamesToAdd); }
private static MlbGameSummaryModel ParseRow(HtmlNodeCollection columns, MlbTeamShortName mlbTeam, int seasonYear, bool includeHomeGamesOnly = true) { MlbGameSummaryModel game = new MlbGameSummaryModel(); // Check if this is a home or away game and if we should proceed parsing HtmlNode gameStatusNode = columns[1].SelectSingleNode(@".//li[@class='game-status']"); if (gameStatusNode.InnerText.Equals("@") && includeHomeGamesOnly) { // This is an away game and we don't want to include away games so return null return(null); } // Game Date HtmlNode dateNode = columns[0]; DateTime date = Convert.ToDateTime(dateNode.InnerText + " " + seasonYear); game.Date = date; if (date >= DateTime.Now.Date) // Don't retrieve games from today or future { return(null); } // Determine the Opponent HtmlNode opponentNode = columns[1]; HtmlNode opponentTeamNode = opponentNode.SelectSingleNode(@".//li[@class='team-name']"); string opponentTeamCity = opponentTeamNode.InnerText; // Check if the game was postponed if (columns[2].InnerText.Equals("postponed", StringComparison.InvariantCultureIgnoreCase) || columns[3].InnerText.Equals("delayed", StringComparison.InvariantCultureIgnoreCase) || columns[3].InnerText.Equals("suspended", StringComparison.InvariantCultureIgnoreCase)) { // The game was postponed. Figure out home/away teams and then return game.Postponed = true; MlbAttendanceQuery.DetermineHomeAndAway(gameStatusNode, mlbTeam, opponentTeamCity, ref game); return(game); } else { game.Postponed = false; } // Game Result HtmlNode gameResultNode = columns[2]; HtmlNode scoreNode = gameResultNode.SelectSingleNode(@".//li[contains(@class,'score')]"); // Check if the score can be parsed out and if not then return if (null == scoreNode) { MlbAttendanceQuery.DetermineHomeAndAway(gameStatusNode, mlbTeam, opponentTeamCity, ref game); return(game); } // Check if there were extra innings string score = scoreNode.InnerText; string fToken = "f/"; // This string implies there were extra innings int fIndex = score.IndexOf(fToken, StringComparison.InvariantCultureIgnoreCase); if (fIndex >= 0) { game.Innings = Convert.ToInt32(score.Substring(fIndex + fToken.Length)); score = score.Substring(0, fIndex).Trim(); } else { game.Innings = 9; } int winningScore; int losingScore; try { winningScore = score.Split('-').Max(x => Convert.ToInt32(x)); losingScore = score.Split('-').Min(x => Convert.ToInt32(x)); } catch (FormatException) { winningScore = 0; losingScore = 0; MlbAttendanceQuery.DetermineHomeAndAway(gameStatusNode, mlbTeam, opponentTeamCity, ref game); return(game); } // Figure out if the home team won or lost HtmlNode winLossNode = gameResultNode.SelectSingleNode(@".//li[contains(@class,'game-status')]"); int mlbTeamScore; int opponentScore; if (winLossNode.Attributes["class"].Value.IndexOf("win", StringComparison.InvariantCultureIgnoreCase) >= 0) // case-insensitive 'contains' { mlbTeamScore = winningScore; opponentScore = losingScore; } else if (winLossNode.Attributes["class"].Value.IndexOf("loss", StringComparison.InvariantCultureIgnoreCase) >= 0) // case-insensitive 'contains' { mlbTeamScore = losingScore; opponentScore = winningScore; } else if (winLossNode.Attributes["class"].Value.IndexOf("tie", StringComparison.InvariantCultureIgnoreCase) >= 0) // case-insensitive 'contains' { mlbTeamScore = winningScore; opponentScore = winningScore; } else { throw new Exception("Could not determine win or loss"); } // Get Win-Loss record for mlbTeam HtmlNode winLossRecordNode = columns[3]; string[] winLossRecord = winLossRecordNode.InnerText.Split('-'); if (null != winLossRecord && winLossRecord.Length == 2) { game.WinsToDate = Convert.ToInt32(winLossRecord[0]); game.LossesToDate = Convert.ToInt32(winLossRecord[1]); } else { // do not set any values since we couldn't parse out the win-loss record } // Get pitchers HtmlNode winningPitcherNode = columns[4].SelectSingleNode(@".//a"); game.WPitcher = winningPitcherNode != null ? winningPitcherNode.InnerText : null; HtmlNode losingPitcherNode = columns[5].SelectSingleNode(@".//a"); game.LPitcher = losingPitcherNode != null ? losingPitcherNode.InnerText : null; HtmlNode savingPitcherNode = columns[6].SelectSingleNode(@".//a"); game.SavePitcher = savingPitcherNode != null ? savingPitcherNode.InnerText : null; // Determine home and away teams and which was the winner or loser // Note: gameStatusNode was initialized at the start of the method if (gameStatusNode.InnerText.Equals("vs", StringComparison.InvariantCultureIgnoreCase)) { // home game for mlbTeam game.Home = mlbTeam.ToString(); game.HomeScore = mlbTeamScore; string shortName = MlbAttendanceQuery.LookupShortName(opponentTeamCity); game.Visitor = String.IsNullOrWhiteSpace(shortName) ? opponentTeamCity : shortName; game.VisitorScore = opponentScore; } else if (gameStatusNode.InnerText.Equals("@", StringComparison.InvariantCultureIgnoreCase)) { // away game for mlbTeam string shortName = MlbAttendanceQuery.LookupShortName(opponentTeamCity); game.Home = String.IsNullOrWhiteSpace(shortName) ? opponentTeamCity : shortName; game.HomeScore = opponentScore; game.Visitor = mlbTeam.ToString(); game.VisitorScore = mlbTeamScore; } // Get Attendance Data HtmlNode attendanceNode = columns[7]; int attendance = 0; Int32.TryParse(attendanceNode.InnerText, NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out attendance); game.Attendance = attendance; return(game); }
private static void DetermineHomeAndAway(HtmlNode gameStatusNode, MlbTeamShortName mlbTeam, string opponentTeamCity, ref MlbGameSummaryModel game) { //Figure out home/away teams and then return if (gameStatusNode.InnerText.Equals("vs", StringComparison.InvariantCultureIgnoreCase)) { // home game for mlbTeam game.Home = mlbTeam.ToString(); string shortName = MlbAttendanceQuery.LookupShortName(opponentTeamCity); game.Visitor = String.IsNullOrWhiteSpace(shortName) ? opponentTeamCity : shortName; } else if (gameStatusNode.InnerText.Equals("@", StringComparison.InvariantCultureIgnoreCase)) { // away game for mlbTeam string shortName = MlbAttendanceQuery.LookupShortName(opponentTeamCity); game.Home = String.IsNullOrWhiteSpace(shortName) ? opponentTeamCity : shortName; game.Visitor = mlbTeam.ToString(); } }
public static List <MlbGameSummaryModel> GetSeasonForTeam(MlbSeasonType mlbSeasonType, MlbTeamShortName mlbTeam, int seasonYear) { string relativeUrl; List <MlbGameSummaryModel> results; switch (mlbSeasonType) { case MlbSeasonType.Spring: relativeUrl = String.Format(MlbAttendanceQuery.preSeasonFormatString, mlbTeam.ToString(), seasonYear); results = MlbAttendanceQuery.GetPage(relativeUrl, mlbTeam, seasonYear); break; case MlbSeasonType.Regular: // Collect first half of the season relativeUrl = String.Format(MlbAttendanceQuery.regSeasonFormatString, mlbTeam.ToString(), seasonYear, 1); List <MlbGameSummaryModel> firstHalf = MlbAttendanceQuery.GetPage(relativeUrl, mlbTeam, seasonYear); // Collect second half of the season relativeUrl = String.Format(MlbAttendanceQuery.regSeasonFormatString, mlbTeam.ToString(), seasonYear, 2); List <MlbGameSummaryModel> secondHalf = MlbAttendanceQuery.GetPage(relativeUrl, mlbTeam, seasonYear); // Merge them together results = new List <MlbGameSummaryModel>(); results.AddRange(firstHalf); results.AddRange(secondHalf); break; case MlbSeasonType.PostSeason: relativeUrl = String.Format(MlbAttendanceQuery.postSeasonFormatString, mlbTeam.ToString(), seasonYear); results = MlbAttendanceQuery.GetPage(relativeUrl, mlbTeam, seasonYear); break; default: throw new ArgumentException(String.Format("Unrecognized season type {0}", mlbSeasonType.ToString())); } // Add the season type and year to every item if (null != results) { results.ForEach(x => { x.MlbSeasonType = mlbSeasonType; x.Year = seasonYear; }); } return(results); }