private static Nhl_Games_Rtss_RosterParticipantItem ParseLinesman(HtmlNode row) { Nhl_Games_Rtss_RosterParticipantItem official = NhlGamesRtssRoster.ParseOfficial(row); official.Designation = Designation.Linesman; return(official); }
private static List <Nhl_Games_Rtss_RosterParticipantItem> ParsePlayers(HtmlNodeCollection rows) { if (null == rows) { return(null); } List <Nhl_Games_Rtss_RosterParticipantItem> players = new List <Nhl_Games_Rtss_RosterParticipantItem>(); foreach (HtmlNode row in rows) { Nhl_Games_Rtss_RosterParticipantItem player = NhlGamesRtssRoster.ParsePlayer(row); if (null != player) { players.Add(player); } } return(players); }
public static Nhl_Games_Rtss_Roster ParseHtmlBlob(int rtssReportId, string html) { if (String.IsNullOrWhiteSpace(html) || html.Equals("404")) { return(null); } Nhl_Games_Rtss_Roster model = new Nhl_Games_Rtss_Roster(); model.NhlRtssReportModelId = rtssReportId; HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(html); HtmlNode documentNode = htmlDocument.DocumentNode; // Special Case // The html for this game doesn't follow the same format as the other games if (null != documentNode.SelectSingleNode(@"./html/head/link[@href='RO010002_files/editdata.mso']")) { return(NhlGamesRtssRoster.BruinsRangersSpecialCase(rtssReportId)); } // Get the teams HtmlNodeCollection teamNodes = documentNode.SelectNodes(@".//tr/td[contains(@class,'teamHeading + border')]"); string team1 = teamNodes[0].InnerText; string team2 = teamNodes[1].InnerText; // Get the tables that contain a header with '#' HtmlNodeCollection rosterTables = documentNode.SelectNodes(@".//table[tbody/tr/td[text()='#'] or tr/td[text()='#']]"); // Pull out the rows for rosters and scratches. Assume the order of: // 1. visitor roster // 2. home roster // 3. visitor scratches // 4. home scratches // Also, ignore the first rows because they are header fields. HtmlNodeCollection team1RosterNodes = null; HtmlNodeCollection team2RosterNodes = null; if (null != rosterTables && rosterTables.Count >= 2) { team1RosterNodes = rosterTables[0].SelectNodes(@".//tr[position() > 1]"); team2RosterNodes = rosterTables[1].SelectNodes(@".//tr[position() > 1]"); Assert.IsTrue(team1RosterNodes.Count > 10, "team1Roster count"); Assert.IsTrue(team2RosterNodes.Count > 10, "team2Roster count"); } HtmlNodeCollection scratchesNodes = documentNode.SelectNodes(@".//tr[@id='Scratches']/td"); HtmlNodeCollection team1ScratchesNodes = null; HtmlNodeCollection team2ScratchesNodes = null; if (null != scratchesNodes) { team1ScratchesNodes = scratchesNodes[0].SelectNodes(@"./table/tr[position() > 1] | ./table/tbody/tr[position() > 1]"); team2ScratchesNodes = scratchesNodes[1].SelectNodes(@"./table/tr[position() > 1] | ./table/tbody/tr[position() > 1]"); } // Parse the players out of the lists List <Nhl_Games_Rtss_RosterParticipantItem> team1Roster = NhlGamesRtssRoster.ParsePlayers(team1RosterNodes); List <Nhl_Games_Rtss_RosterParticipantItem> team2Roster = NhlGamesRtssRoster.ParsePlayers(team2RosterNodes); List <Nhl_Games_Rtss_RosterParticipantItem> team1Scratches = NhlGamesRtssRoster.ParsePlayers(team1ScratchesNodes); List <Nhl_Games_Rtss_RosterParticipantItem> team2Scratches = NhlGamesRtssRoster.ParsePlayers(team2ScratchesNodes); // Find the head coaches HtmlNodeCollection coachNodes = documentNode.SelectNodes(@".//tr[@id='HeadCoaches']/td/table/tbody/tr | .//tr[@id='HeadCoaches']/td/table/tr"); Nhl_Games_Rtss_RosterParticipantItem coach1 = NhlGamesRtssRoster.ParseCoach(coachNodes[0]); Nhl_Games_Rtss_RosterParticipantItem coach2 = NhlGamesRtssRoster.ParseCoach(coachNodes[1]); // Find the officials HtmlNode officialsTableNode = documentNode.SelectSingleNode(@".//table[tbody/tr/td[contains(text(),'Referee')] or tr/td[contains(text(),'Referee')]]"); HtmlNodeCollection officialsSubTableNodes = officialsTableNode.SelectNodes(@".//table"); HtmlNodeCollection refereesNodes = officialsSubTableNodes[0].SelectNodes(@".//tr"); HtmlNodeCollection linesmenNodes = officialsSubTableNodes[1].SelectNodes(@".//tr"); Nhl_Games_Rtss_RosterParticipantItem referee1 = null; if (refereesNodes != null && refereesNodes.Count >= 1) { referee1 = NhlGamesRtssRoster.ParseReferee(refereesNodes[0]); } Nhl_Games_Rtss_RosterParticipantItem referee2 = null; if (refereesNodes != null && refereesNodes.Count >= 2) { referee2 = NhlGamesRtssRoster.ParseReferee(refereesNodes[1]); } Nhl_Games_Rtss_RosterParticipantItem linesman1 = null; if (linesmenNodes != null && linesmenNodes.Count >= 1) { linesman1 = NhlGamesRtssRoster.ParseLinesman(linesmenNodes[0]); } Nhl_Games_Rtss_RosterParticipantItem linesman2 = null; if (linesmenNodes != null && linesmenNodes.Count >= 2) { linesman2 = NhlGamesRtssRoster.ParseLinesman(linesmenNodes[1]); } // Check for standby officials HtmlNodeCollection standbyOfficialsNodes1 = officialsSubTableNodes[2].SelectNodes(@".//tr"); HtmlNodeCollection standbyOfficialsNodes2 = officialsSubTableNodes[3].SelectNodes(@".//tr"); if (null != standbyOfficialsNodes1 || null != standbyOfficialsNodes2) { Console.WriteLine("Encountered potential standby officials in RTSS report {0}", rtssReportId); } // Fill out the model model.VisitorHeadCoach = new List <Nhl_Games_Rtss_RosterParticipantItem> { coach1 }; model.HomeHeadCoach = new List <Nhl_Games_Rtss_RosterParticipantItem> { coach2 }; model.VisitorRoster = team1Roster; model.HomeRoster = team2Roster; model.VisitorScratches = team1Scratches; model.HomeScratches = team2Scratches; model.Linesman = new List <Nhl_Games_Rtss_RosterParticipantItem> { linesman1, linesman2 }; model.Referees = new List <Nhl_Games_Rtss_RosterParticipantItem> { referee1, referee2 }; return(model); }
public static void UpdateSeason([Optional] int year, [Optional] DateTime fromDate, [Optional] bool forceOverwrite) { // Initialize the rtss reports that we are going to read and parse List <Nhl_Games_Rtss> models = NhlHtmlReportBase.GetRtssReports(year, fromDate); List <Nhl_Games_Rtss_Roster> existingModels = null; if (forceOverwrite == false) { // Only query for existing if we are not going to force overwrite all existingModels = NhlGamesRtssRoster.GetHtmlRosterReports(year, fromDate); } // For each report, get the html blob from blob storage and parse the blob to a report List <Nhl_Games_Rtss_Roster> results = new List <Nhl_Games_Rtss_Roster>(); foreach (Nhl_Games_Rtss model in models) { if (forceOverwrite == false && existingModels.Exists(m => m.NhlRtssReportModelId == model.Id)) { // In this case, only get data if it is not already populated continue; } Nhl_Games_Rtss_Roster report = null; if (!model.GameLink.Equals("#")) { string htmlBlob = HtmlBlob.RetrieveBlob(HtmlBlobType.NhlRoster, model.Id.ToString(), new Uri(model.RosterLink), true); report = NhlGamesRtssRoster.ParseHtmlBlob(model.Id, htmlBlob); } if (null != report) { results.Add(report); } } // Save the reports to the db 100 records at a time using (SportsDataContext db = new SportsDataContext()) { int counter = 0; int totalCounter = 0; int batchSize = 10; foreach (Nhl_Games_Rtss_Roster model in results) { Console.WriteLine("Start saving {0} to {1}", results.Count, db.Database.Connection.ConnectionString); db.Configuration.AutoDetectChangesEnabled = false; db.Configuration.ValidateOnSaveEnabled = false; counter++; totalCounter++; if (model.Id != 0) { db.Nhl_Games_Rtss_Roster_DbSet.Attach(model); db.Entry(model).State = EntityState.Modified; } else { db.Entry(model).State = EntityState.Added; } if (counter >= batchSize) { db.SaveChanges(); counter = 0; Console.WriteLine("Saved {0} of {1}", totalCounter, results.Count); } } db.SaveChanges(); Console.WriteLine("Saved {0} of {1}", totalCounter, results.Count); } }