public Matchup(NHLTeam visitorTeam, NHLTeam localTeam, DateTime date, int nchlWeek) { VisitorTeam = visitorTeam; LocalTeam = localTeam; Date = date; NCHLWeek = nchlWeek; }
private void ParsePositionHTML(string htmlCode, NHLTeam team, PlayerPosition pos) { int startPosition = 0; int endPosition = 0; int startPositionToDocumentEndLenght = 0; string positionHTMLString = string.Empty; switch (pos) { case PlayerPosition.G: startPosition = htmlCode.IndexOf("<td>Goalie"); break; case PlayerPosition.D: startPosition = htmlCode.IndexOf("<td>Defenceman"); break; case PlayerPosition.C: startPosition = htmlCode.IndexOf("<td>Center"); break; case PlayerPosition.R: startPosition = htmlCode.IndexOf("<td>Right Wing"); break; case PlayerPosition.L: startPosition = htmlCode.IndexOf("<td>Left Wing"); break; } startPositionToDocumentEndLenght = htmlCode.Length - startPosition; endPosition = htmlCode.Substring(startPosition, startPositionToDocumentEndLenght).IndexOf("</tr>"); positionHTMLString = htmlCode.Substring(startPosition, endPosition); string[] playersRaw = positionHTMLString.Split(new string[] { "</a" }, StringSplitOptions.None); foreach (string player in playersRaw) { if (player.Contains("<a")) { for (int i = player.Length - 1; i > 0; i--) { if (player[i] == '>') { Players.Add(new Player() { Name = player.Substring(i + 1, player.Length - (i + 1)), NHLPos = pos, NHLTeam = team }); break; } } } } }
private void ParseFullHTML(string htmlCode, NHLTeam team) { foreach (PlayerPosition pos in Enum.GetValues(typeof(PlayerPosition))) { if (pos == PlayerPosition.G) { continue; } ParsePositionHTML(htmlCode, team, pos); } }
internal void GetWebData(NHLTeam nhlTeam) { string htmlCode; using (WebClient client = new WebClient()) { string teamUrl = string.Format("http://forecaster.thehockeynews.com/depthchart/{0}", nhlTeam); htmlCode = client.DownloadString(teamUrl); } ParseFullHTML(htmlCode, nhlTeam); }
internal void CreateMatchups() { DateTime startDate = new DateTime(2018, 10, 3); int parsedYear = startDate.Year; // This list contains the positions where the nchl weeks starts (from 1-366). We will use it as reference when assigning the week List <int> weekStart = new List <int>(); // Add the first week, regardles of it's day in the week weekStart.Add(startDate.DayOfYear); // If the first game of the season was not on tuesday, we need to position the second week on a tuesday. // If the first game of the season was on tuesday, we are already correctly positionned. if (startDate.DayOfWeek != DayOfWeek.Tuesday) { // Get the first tuesday following the start date of the season, to determine nchl weeks while (startDate.DayOfWeek != DayOfWeek.Tuesday) { startDate = startDate.AddDays(1); } weekStart.Add(startDate.DayOfYear); } //Fill the list with all the days of the years which are on a tuesday (do it for 40 weeks even if it's overkill) for (int i = 0; i < 40; i++) { startDate = startDate.AddDays(7); weekStart.Add(startDate.DayOfYear); } int currentWeek = 1; bool newYearTransition = false; using (WebClient client = new WebClient()) { //client.Proxy = new MtxProxy(); string gamesJsonHTMLLink = $"https://statsapi.web.nhl.com/api/v1/schedule?site=en_nhl&startDate=2018-10-03&endDate=2019-04-06"; string result = client.DownloadString(gamesJsonHTMLLink); JsonDates jsonDates = JsonConvert.DeserializeObject <JsonDates>(result); foreach (var date in jsonDates.Dates) { var matchupDate = DateTime.Parse(date.Date); foreach (var game in date.Games) { try { NHLTeam awayTeam = TeamName[Convert.ToInt32(game.Teams.Away.Team.Id)]; NHLTeam homeTeam = TeamName[Convert.ToInt32(game.Teams.Home.Team.Id)]; if (newYearTransition && matchupDate.DayOfYear <= weekStart[currentWeek]) { newYearTransition = false; } if (matchupDate.DayOfYear >= weekStart[currentWeek]) { if (!newYearTransition) { int prevWeek = weekStart[currentWeek]; currentWeek++; if (weekStart[currentWeek] < prevWeek) { newYearTransition = true; } } } _matchups.Add(new Matchup(awayTeam, homeTeam, matchupDate, currentWeek)); } catch (Exception ex) { Console.WriteLine($"New team or european team: {ex.Message}"); continue; } } } } _numberOfWeeks = currentWeek; }
internal List <Matchup> GetTeamMatchupsForWeek(NHLTeam team, int week) { return(_matchups.Where(match => (match.VisitorTeam == team || match.LocalTeam == team) && match.NCHLWeek == week).ToList()); }