Пример #1
0
 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();
     }
 }
Пример #2
0
        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;
        }