示例#1
0
        private static void GetSchedule()
        {
            try
            {
                var url = string.Format("http://www.nhl.com/ice/schedulebyseason.htm");
                HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
                HttpWebResponse response = (HttpWebResponse) request.GetResponse();
                using (Stream respStream = response.GetResponseStream())
                {
                    if (respStream != null)
                    {
                        var encoding = Encoding.GetEncoding("utf-8");
                        var streamReader = new StreamReader(respStream, encoding);

                        var content = streamReader.ReadToEnd();

                        var doc = new HtmlDocument();
                        doc.LoadHtml(content);

                        foreach (var row in doc.DocumentNode.SelectNodes(".//*/table[@class='data schedTbl']/tbody/tr"))
                        {
                            var dateNode = row.SelectSingleNode("./td/div[@class='skedStartDateSite']");
                            if (dateNode == null)
                            {
                                // For special headings (e.g. "Thanksgiving Showdown", "Winter Classic")
                                continue;
                            }

                            var date = DateTime.Parse(dateNode.InnerText);

                            var awayTeamCode = string.Empty;
                            var homeTeamCode = string.Empty;
                            int? awayScore = null;
                            int? homeScore = null;

                            var teams = row.SelectNodes("./td/div[@class='teamName']/a");
                            if (teams.Count == 2) // always should be
                            {
                                awayTeamCode = teams[0].GetAttributeValue("rel", "");
                                homeTeamCode = teams[1].GetAttributeValue("rel", "");
                            }

                            var result = row.SelectNodes("./td[@class='tvInfo']/span");
                            if (result != null && result.Count == 2) // only if resulted game
                            {
                                // " MTL (5) " without the quotes
                                awayScore = Int32.Parse(Regex.Replace(result[0].InnerText, @"[^\d]", ""));
                                homeScore = Int32.Parse(Regex.Replace(result[1].InnerText, @"[^\d]", ""));
                            }

                            var game = new Game
                            {
                                Date = date,
                                AwayTeam = (Team)Enum.Parse(typeof(Team), awayTeamCode),
                                HomeTeam = (Team)Enum.Parse(typeof(Team), homeTeamCode),
                            };

                            if (awayScore.HasValue)
                            {
                                game.AwayScore = awayScore;
                                game.HomeScore = homeScore;
                            }

                            // TODO: Create mode to only scan for updates since last checked date, instead of overwriting everything.
                            // Not much point checking if it exists then not adding it, not here
                            _gameRepository.Update(game);

                            // get gameid to update prediction if necessary.
                            // REVISIT: I thought there was a swift way for PetaPoco to insert and return the ID :/
                            var gameId = _gameRepository.GetGameId(game);

                            if (awayScore.HasValue && gameId.HasValue)
                            {
                                var prediction = _predictionRepository.IsAPredictedUnresultedGame(gameId.Value);
                                if (prediction != null)
                                {
                                    prediction.ActualWinner = game.AwayScore > game.HomeScore
                                        ? game.AwayTeam
                                        : game.HomeTeam;
                                    _predictionRepository.Save(prediction);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Something went wrong with the schedule: {0}", e);
            }
        }
示例#2
0
        public List<Statline> GetStatlinesForGame(Game game)
        {
            var dtos = _db.Fetch<StatlineDTO>("select top 2 * from Statline where team in (@0,@1) order by date desc",
                                                (int) game.HomeTeam, (int) game.AwayTeam);

            return dtos.Select(statline => new Statline
            {
                Date = statline.Date,
                FaceoffWinPercentage = statline.FaceoffWinPercentage,
                GamesPlayed = statline.GamesPlayed,
                GoalsAgainst = statline.GoalsAgainst,
                GoalsAgainstPerGame = statline.GoalsAgainstPerGame,
                GoalsFor = statline.GoalsFor,
                GoalsForPerGame = statline.GoalsForPerGame,
                Points = statline.Points,
                Losses = statline.Losses,
                Wins = statline.Wins,
                OvertimeLosses = statline.OvertimeLosses,
                RegularPlusOvertimeWins = statline.RegularPlusOvertimeWins,
                PowerPlayPercentage = statline.PowerPlayPercentage,
                PenaltyKillPercentage = statline.PenaltyKillPercentage,
                PointPercentage = statline.PointPercentage,
                SeasonId = statline.SeasonId,
                ShotsForPerGame = statline.ShotsForPerGame,
                ShotsAgainstPerGame = statline.ShotsAgainstPerGame,
                Team = (Team)Enum.Parse(typeof(Team), statline.Team.ToString()),
            }).ToList();
        }