public decimal WebSelectByCssSelector(Provider Provider) { IFfeWebParser webParser = null; switch (WebParser) { case Parser.Auto: webParser = FfeWebController.AutoWebParserSelection(Provider.Url, cssSelector: Provider.CssSelector, regExPattern: Provider.RegExPattern); break; case Parser.HAP: webParser = new FfeWebHap(Provider.Url); break; case Parser.AngleSharp: webParser = new FfeWebAngleSharp(Provider.Url); break; case Parser.HttpClient: webParser = new FfeWebHttpClient(Provider.Url); break; case Parser.WebClient: webParser = new FfeWebClient(Provider.Url); break; default: break; } string value = webParser.SelectByCssSelector(Provider.CssSelector); decimal quote = decimal.Parse(value); return(quote); }
public decimal WebSelectByRegEx(Provider Provider) { IFfeWebParser webParser = null; switch (WebParser) { case Parser.Auto: webParser = FfeWebController.AutoWebParserSelection(Provider.Url); break; case Parser.HAP: webParser = new FfeWebHap(Provider.Url); break; case Parser.AngleSharp: webParser = new FfeWebAngleSharp(Provider.Url); break; case Parser.HttpClient: webParser = new FfeWebHttpClient(Provider.Url); break; case Parser.WebClient: webParser = new FfeWebClient(Provider.Url); break; default: break; } string value = FfeRegEx.RegExByIndexAndGroup(webParser.GetHtml(), Provider.RegExPattern, Provider.RegExMatchIndex.Value, Provider.RegExGroupName); decimal quote = decimal.Parse(value); return(quote); }
public decimal SelectByCssSelector(Provider Provider) { string html = Resource.ResourceManager.GetString(Provider.Name); IFfeWebParser webParser = null; switch (WebParser) { case Parser.Auto: throw new Exception("No parse method available."); case Parser.HAP: webParser = new FfeWebHap(html); break; case Parser.AngleSharp: webParser = new FfeWebAngleSharp(html); break; case Parser.HttpClient: throw new Exception("No parse method available."); case Parser.WebClient: throw new Exception("No parse method available."); default: break; } string value = webParser.SelectByCssSelector(Provider.CssSelector); decimal price = decimal.Parse(value); return(price); }
public decimal SelectByRegEx(Provider Provider) { string html = Resource.ResourceManager.GetString(Provider.Name); IFfeWebParser webParser = null; switch (WebParser) { case Parser.Auto: throw new Exception("No parse method available."); case Parser.HAP: webParser = new FfeWebHap(html); break; case Parser.AngleSharp: webParser = new FfeWebAngleSharp(html); break; case Parser.HttpClient: throw new Exception("No parse method available."); case Parser.WebClient: throw new Exception("No parse method available."); default: break; } string value = FfeRegEx.RegExByIndexAndGroup(webParser.GetHtml(), Provider.RegExPattern, Provider.RegExMatchIndex.Value, Provider.RegExGroupName); decimal price = decimal.Parse(value); return(price); }
private static (object Value, CbqInfo CbqInfo, IFfeWebParser WebParser) QueryConsorsbank(string isin_wkn, string exchange, string info) { object value = ExcelError.ExcelErrorNA; CbqInfo cbqInfo = CbqInfo.price; // Default IFfeWebParser webParser = null; try { if (!String.IsNullOrEmpty(info)) { info = info.ToLower(); cbqInfo = (CbqInfo)Enum.Parse(typeof(CbqInfo), info); if (!(Enum.IsDefined(typeof(CbqInfo), cbqInfo))) { throw new NotSupportedException("Not supported Info parameter value"); } } // Price if (cbqInfo == CbqInfo.price) { string url = CbqSetting.Default.QCB_URL; url = url.Replace("{ISIN_WKN}", isin_wkn); if (!String.IsNullOrEmpty(exchange)) { url += "?exchange={EXCHANGE}"; url = url.Replace("{EXCHANGE}", exchange); } log.Debug("Querying the stock info {@Info} for {@IsinWkn} from {@Provider}", cbqInfo.ToString(), isin_wkn, "consorsbank.de"); webParser = new FfeWebAngleSharp(new Uri(url)); string xPath = CbqSetting.Default.QCB_Price_XPath_Price; string quote = webParser.SelectByXPath(xPath); decimal price = decimal.Parse(quote, new CultureInfo("de-DE")); value = price; } } catch (FormatException fex) { log.Error(fex.Message); log.Debug("Could not parse {@QuoteValue} to decimal type", value); return(Value : ExcelError.ExcelErrorGettingData, CbqInfo : cbqInfo, WebParser : null); } catch (Exception ex) { log.Error(ex.Message); return(Value : ExcelError.ExcelErrorGettingData, CbqInfo : cbqInfo, WebParser : null); } return(value, cbqInfo, webParser); }
public static (string value, string rawSource) GetValueFromWeb(string url, string xPath = null, string cssSelector = null, string regExPattern = null, string regExGroup = null, int regExMatchIndex = 0, string jsonPath = null, Parser parser = Parser.Auto) { if (xPath == null && cssSelector == null && regExPattern == null && jsonPath == null) { throw new ArgumentException("No selection criteria were provided. Provide a least one of the following selection criteria: xPath, cssSelector, regExPattern or jsonPath."); } string value = null; string rawSource = null; Uri uri = new Uri(url); if (parser != Parser.Newtonsoft) { IFfeWebParser ffeWebParser = null; switch (parser) { case Parser.Auto: ffeWebParser = AutoWebParserSelection(uri, xPath, cssSelector, regExPattern); break; case Parser.HAP: ffeWebParser = new FfeWebHap(uri); break; case Parser.AngleSharp: ffeWebParser = new FfeWebAngleSharp(uri); break; case Parser.HttpClient: ffeWebParser = new FfeWebHttpClient(uri); break; case Parser.WebClient: ffeWebParser = new FfeWebClient(uri); break; default: ffeWebParser = new FfeWebHap(uri); break; } rawSource = ffeWebParser.GetHtml(); if (log.IsEnabled(Serilog.Events.LogEventLevel.Debug)) { rawSource.WriteToFile($"PageSource_{uri.Host}.html", parser.ToString()); } // Input for RegEx (if set). string input = null; if (!String.IsNullOrEmpty(xPath)) { value = ffeWebParser.SelectByXPath(xPath); input = value; } else if (!String.IsNullOrEmpty(cssSelector)) { value = ffeWebParser.SelectByCssSelector(cssSelector); input = value; } else // Select by RegEx (HTML source code = RegEx input). { //HACK: AngelSharp does not provide full HTML source code. if (parser == Parser.AngleSharp) { log.Warning("Regular Expression with AngleSharp WebParser does not work well. HttpClient is used instead."); rawSource = FfeWeb.GetHttpResponseContent(uri); } input = rawSource; } if (!String.IsNullOrEmpty(regExPattern)) { value = FfeRegEx.RegExByIndexAndGroup(input, regExPattern, regExMatchIndex, regExGroup); if (String.IsNullOrEmpty(value)) { if (log.IsEnabled(Serilog.Events.LogEventLevel.Debug)) { input.WriteToFile("RegExInput.html", "RegEx"); } throw new RegExException() { Input = input, Pattern = regExPattern }; } } } else { IFfeJsonParser ffeJsonParser = new FfeJsonNewtonsoft(uri); value = ffeJsonParser.SelectByJsonPath(jsonPath); rawSource = ffeJsonParser.GetJson(); if (log.IsEnabled(Serilog.Events.LogEventLevel.Debug)) { rawSource.WriteToFile($"JsonSource_{uri.Host}.json", parser.ToString()); } } return(value, rawSource); }