public async Task <PreciousMetalModel> GetHTTPPrices() { this.logger.LogWarning("GetHTTPPrices starts at " + Environment.TickCount); PreciousMetalModel metal = null; try { var url = this.configuration["PreciousMetal:Url"]; HtmlWeb webClient = new HtmlWeb(); HtmlDocument document = await webClient.LoadFromWebAsync(url); HtmlNode goldNode = document.GetElementbyId("lblGoldAskAU"); HtmlNode silverNode = document.GetElementbyId("lblSilverAskAU"); HtmlNode platinumNode = document.GetElementbyId("lblPlatinumAskAU"); HtmlNode palladiumNode = document.GetElementbyId("lblPalladiumAskAU"); HtmlNode lastUpdateNode = document.GetElementbyId("lblDatetime"); if (goldNode != null && silverNode != null && platinumNode != null && palladiumNode != null) { metal = new PreciousMetalModel(); decimal valueGold = 0.0M; if (decimal.TryParse(goldNode.InnerText.Replace("$", ""), out valueGold)) { metal.Gold = valueGold; } decimal valueSilver = 0.0M; if (decimal.TryParse(silverNode.InnerText.Replace("$", ""), out valueSilver)) { metal.Silver = valueSilver; } decimal valuePlatinum = 0.0M; if (decimal.TryParse(platinumNode.InnerText.Replace("$", ""), out valuePlatinum)) { metal.Platinum = valuePlatinum; } decimal valuePalladium = 0.0M; if (decimal.TryParse(palladiumNode.InnerText.Replace("$", ""), out valuePalladium)) { metal.Palladium = valuePalladium; } string[] temps = lastUpdateNode.InnerText.Split(" ", StringSplitOptions.RemoveEmptyEntries); if (temps.Length == 5) { string date = temps[2] + " " + temps[3] + " " + temps[4] + " " + temps[0]; metal.LastUpdate = DateTime.Parse(date).AddHours(3); } } } catch (Exception ex) { this.logger.LogError(ex.GetBaseException().ToString()); metal = null; } this.logger.LogWarning("GetHTTPPrices stops at " + Environment.TickCount); return(metal); }
private async void RunPreciousMetal() { logger.LogInformation("RunPreciousMetal starts at " + Environment.TickCount); DataContext dataContext = GetDataContext(); logger.LogInformation("dataContext == null ? " + (dataContext == null)); PreciousMetalService service = new PreciousMetalService(dataContext, configuration, logger); if (service != null) { PreciousMetalModel preciousMetalModel = await service.GetHTTPPrices(); if (preciousMetalModel != null) { PreciousMetal metal = new Models.PreciousMetal(preciousMetalModel); if (metal != null) { bool result = await service.Update(metal); } } } logger.LogInformation("RunPreciousMetal stops at " + Environment.TickCount); }