public static IEnumerable<Incident> GetIncidents(int currentPage = IncidentsDefaultCurrentPage, int itemsPerPage = IncidentsDefaultItemsPerPage, IncidentsSortOrder sortOrder = IncidentsDefaultSortOrder) { Task<IEnumerable<Incident>> task = GetIncidentsAsync(currentPage, itemsPerPage, sortOrder); task.Wait(); if (task.IsFaulted) { throw task.Exception; } return task.Result; }
public static async Task<IEnumerable<Incident>> GetIncidentsAsync(int currentPage = IncidentsDefaultCurrentPage, int itemsPerPage = IncidentsDefaultItemsPerPage, IncidentsSortOrder sortOrder = IncidentsDefaultSortOrder) { if (currentPage < 0) { throw new ArgumentOutOfRangeException("currentPage"); } if (itemsPerPage < 0) { throw new ArgumentOutOfRangeException("itemsPerPage"); } string sortOrderString = ""; switch (sortOrder) { case IncidentsSortOrder.Name: sortOrderString = "incident_name"; break; case IncidentsSortOrder.County: sortOrderString = "incident_county"; break; case IncidentsSortOrder.AdministrativeUnit: sortOrderString = "incident_administrative_unit"; break; case IncidentsSortOrder.DateStarted: sortOrderString = "incident_date_created"; break; case IncidentsSortOrder.DateLastUpdated: sortOrderString = "incident_date_last_update"; break; case IncidentsSortOrder.Priority: default: sortOrderString = "incident_priority"; break; } // Download the webpage string url = String.Format(GetIncidentsUrl, currentPage, itemsPerPage, sortOrderString); HtmlWeb web = new HtmlWeb(); HtmlDocument doc = await web.LoadFromWebAsync(url); List<Incident> incidents = new List<Incident>(); // Find incident tables foreach (HtmlNode tableNode in doc.DocumentNode.Descendants("table")) { if (tableNode.HasAttributes && tableNode.Attributes.Contains("class") && tableNode.Attributes["class"].Value == "incident_table") { string name = tableNode.Attributes["title"].Value; if (name != "Search for a fire") { int id = Incident.InvalidId; Dictionary<string, string> details = ParseIncidentTable(tableNode); // Parse out the hidden id if (details.ContainsKey(IncidentIDKey)) { id = Int32.Parse(details[IncidentIDKey]); } Incident incident = (id == Incident.InvalidId) ? new Incident(name, details) : new Incident(id, name, details); incidents.Add(incident); } } } return incidents; }