예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
파일: Ssq.cs 프로젝트: LelandGrunt/FFE
        private object Q(string stockIdentifier, bool tryQ)
        {
            object value;

            try
            {
                log.Verbose($"UDF={UDF_NAME}" +
                            $"; URL={Url}" +
                            $"; XPath={XPath};" +
                            $"; CssSelector={CssSelector};" +
                            $"; RegExPattern={RegExPattern}; RegExMatchIndex={RegExMatchIndex}; RegExGroupName={RegExGroupName}" +
                            $"; JsonPath={JsonPath};" +
                            $"; Culture={Culture.Name}; StockIdentifierPlaceholder={StockIdentifierPlaceholder}" +
                            $"; Parser={Parser}");

                RequiredAttribute.Check(this);

                if (String.IsNullOrEmpty(XPath) &&
                    String.IsNullOrEmpty(CssSelector) &&
                    String.IsNullOrEmpty(RegExPattern) &&
                    String.IsNullOrEmpty(JsonPath))
                {
                    throw new SsqException("XPath, CSS Selector, Regular Expression or JSON Path is required.");
                }

                if (!String.IsNullOrEmpty(RegExPattern))
                {
                    if (!Regex.IsMatch(RegExPattern, $@"\(\?\<{RegExGroupName}\>.*\)"))
                    {
                        throw new SsqException("RegEx pattern must contain the RegEx group name.");
                    }
                }

                if (!String.IsNullOrEmpty(StockIdentifierPlaceholder) &&
                    Url.Split(new[] { StockIdentifierPlaceholder }, StringSplitOptions.None).Length == 1)    //1 = No split.
                {
                    throw new SsqException($"URL must contain stock identifier placeholder {StockIdentifierPlaceholder}");
                }

                log.Debug("Querying the stock info for {@WknIsinTicker}", stockIdentifier);

                string url   = Url.Replace(StockIdentifierPlaceholder, stockIdentifier);
                var    quote = FfeWebController.GetValueFromWeb(url, xPath: XPath,
                                                                cssSelector: CssSelector,
                                                                regExPattern: RegExPattern, regExGroup: RegExGroupName, regExMatchIndex: RegExMatchIndex,
                                                                jsonPath: JsonPath,
                                                                parser: Parser);

                //TODO: Implement type converter?

                /* TypeCode tc = (TypeCode)Enum.Parse(typeof(TypeCode), "Decimal");
                 * value = Convert.ChangeType(quote.value, tc, Culture); */
                var parseSucceeded = decimal.TryParse(quote.value, NumberStyles.Any, Culture, out decimal price);
                if (parseSucceeded)
                {
                    value = price;
                }
                else
                {
                    value = quote.value;
                }
            }
            catch (XPathException ex)
            {
                log.Error(ex.Message);
                log.Error("XPath=" + ex.XPath);
                log.Error("HTML=" + ex.Html);

                if (tryQ)
                {
                    return(SsqExcelError("XPATH_ERROR"));
                }
                else
                {
                    throw;
                }
            }
            catch (CssSelectorException ex)
            {
                log.Error(ex.Message);
                log.Error("CssSelector=" + ex.CssSelector);
                log.Error("HTML=" + ex.Html);

                if (tryQ)
                {
                    return(SsqExcelError("CSS_SELECTOR_ERROR"));
                }
                else
                {
                    throw;
                }
            }
            catch (RegExException ex)
            {
                log.Error(ex.Message);
                log.Error("RegExPattern=" + ex.Pattern);
                log.Error("Input=" + ex.Input);

                if (tryQ)
                {
                    return(SsqExcelError("REGEX_ERROR"));
                }
                else
                {
                    throw;
                }
            }
            catch (JsonPathException ex)
            {
                log.Error(ex.Message);
                log.Error("JsonPath=" + ex.JsonPath);
                log.Error("JSON=" + ex.Json);

                if (tryQ)
                {
                    return(SsqExcelError("JSON_PATH_ERROR"));
                }
                else
                {
                    throw;
                }
            }
            catch (Exception ex)
            {
                log.Error(string.Concat("{@ExceptionMessage}", $" {ex.InnerException?.Message}"), ex.Message);

                if (tryQ)
                {
                    if (ex is FormatException || ex is InvalidCastException)
                    {
                        return(SsqExcelError("PARSE_ERROR"));
                    }
                    if (ex is FFE.WebException)
                    {
                        return(SsqExcelError("WEB_ERROR"));
                    }
                    if (ex is System.Net.WebException)
                    {
                        return(SsqExcelError("WEB_ERROR"));
                    }
                    return(ExcelError.ExcelErrorGettingData);
                }
                else
                {
                    throw;
                }
            }

            return(value);
        }