private static async Task <FuelScaleChargeGroup> GetValues(string uri) { Console.Write($"Fetching {uri} ... "); Console.WriteLine("done"); var doc = new HtmlDocument(); var req = WebRequest.Create(uri); req.Headers.Add("Referer", IndexUri); req.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"); using (var resp = await req.GetResponseAsync()) { using (var stream = resp.GetResponseStream()) { doc.Load(stream); } } var dates = GetDates(doc); Console.WriteLine($"\t{dates.Item1} -> {dates.Item2}"); if (dates.Item1 < MinDate) { Console.WriteLine($"\tNot supported for dates before {MinDate}."); return(null); } var tables = doc.DocumentNode.SelectNodes("//table"); var annual = tables[tables.Count - 3]; var quarterly = tables[tables.Count - 2]; var monthly = tables[tables.Count - 1]; var group = new FuelScaleChargeGroup() { From = dates.Item1, To = dates.Item2 }; group.Annually.AddRange(GetValues(annual)); group.Quarterly.AddRange(GetValues(quarterly)); group.Monthly.AddRange(GetValues(monthly)); ValidateGroup(group); return(group); }
private static void ValidateGroup(FuelScaleChargeGroup group) { var errors = new List <string>(); if (group.Annually.Count == 0) { errors.Add($"{nameof(group.Annually)} cannot be empty."); } if (group.Quarterly.Count == 0) { errors.Add($"{nameof(group.Quarterly)} cannot be empty."); } if (group.Monthly.Count == 0) { errors.Add($"{nameof(group.Monthly)} cannot be empty."); } if (group.Quarterly.Count != group.Annually.Count) { errors.Add($"{nameof(group.Quarterly)} is not the same length as {nameof(group.Annually)}"); } if (group.Monthly.Count != group.Annually.Count) { errors.Add($"{nameof(group.Monthly)} is not the same length as {nameof(group.Annually)}"); } ValidateList(group.Annually, errors); ValidateList(group.Quarterly, errors); ValidateList(group.Monthly, errors); if (errors.Count == 0) { return; } Console.ForegroundColor = ConsoleColor.Red; foreach (var item in errors) { Console.WriteLine(item); } Console.WriteLine(JsonConvert.SerializeObject(group, Formatting.Indented)); Console.ResetColor(); Console.Error.WriteLine(); throw new Exception("Validation failed"); }