Example #1
0
        private string GetSelectorValue(Page page, BaseSelector selector)
        {
            string result = string.Empty;

            if (selector.Type == SelectorType.Enviroment)
            {
                if (SelectorUtils.Parse(selector) is EnviromentSelector enviromentSelector)
                {
                    result = SelectorUtils.GetEnviromentValue(enviromentSelector.Field, page, 0)?.ToString();
                }
            }
            else
            {
                result = page.Selectable.Select(SelectorUtils.Parse(selector)).GetValue();
            }

            if (!string.IsNullOrEmpty(result) && TotalPageFormatters != null)
            {
                foreach (var formatter in TotalPageFormatters)
                {
                    result = formatter.Formate(result)?.ToString();
                }
            }

            if (string.IsNullOrEmpty(result))
            {
                throw new SpiderException("The result of total selector is null.");
            }
            else
            {
                return(result);
            }
        }
Example #2
0
        private object ExtractField(ISelectable item, Page page, Column field, int index)
        {
            if (field == null)
            {
                return(null);
            }
            ISelector selector = SelectorUtils.Parse(field.Selector);

            if (selector == null)
            {
                return(null);
            }

            if (selector is EnviromentSelector)
            {
                var enviromentSelector = selector as EnviromentSelector;
                var value = SelectorUtils.GetEnviromentValue(enviromentSelector.Field, page, index);
                foreach (var formatter in field.Formatters)
                {
#if DEBUG
                    try
                    {
#endif
                    value = formatter.Formate(value);
#if DEBUG
                }
                catch (Exception e)
                {
                    Logger.Error(e.ToString());
                }
#endif
                }
                return(TryConvert(value, field.DataType));
            }
            else
            {
                bool needCount = field.Option == PropertyDefine.Options.Count;
                if (needCount)
                {
                    var values = item.SelectList(selector).Nodes();
                    return(values.Count);
                }
                else
                {
                    var value = (object)item.Select(selector)?.GetValue(field.Option == PropertyDefine.Options.PlainText);

                    foreach (var formatter in field.Formatters)
                    {
#if DEBUG
                        try
                        {
#endif
                        value = formatter.Formate(value);
#if DEBUG
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e.ToString());
                    }
#endif
                    }

                    return(TryConvert(value, field.DataType));
                }
            }
        }
        private dynamic ExtractField(ISelectable item, Page page, Column field, int index)
        {
            if (field == null)
            {
                return(null);
            }
            ISelector selector = SelectorUtils.Parse(field.Selector);

            if (selector == null)
            {
                return(null);
            }

            if (selector is EnviromentSelector)
            {
                var enviromentSelector = selector as EnviromentSelector;
                var tmpValue           = SelectorUtils.GetEnviromentValue(enviromentSelector.Field, page, index);
                foreach (var formatter in field.Formatters)
                {
#if DEBUG
                    try
                    {
#endif
                    tmpValue = formatter.Formate(tmpValue);
#if DEBUG
                }
                catch (Exception e)
                {
                }
#endif
                }
                return(tmpValue == null ? null : Convert.ChangeType(tmpValue, field.DataType));
            }
            else
            {
                bool needPlainText = field.Option == PropertyDefine.Options.PlainText;
                if (field.Multi)
                {
                    var propertyValues = item.SelectList(selector).Nodes();

                    List <dynamic> results = new List <dynamic>();
                    foreach (var propertyValue in propertyValues)
                    {
                        results.Add(Convert.ChangeType(propertyValue.GetValue(needPlainText), field.DataType));
                    }
                    foreach (var formatter in field.Formatters)
                    {
#if DEBUG
                        try
                        {
#endif
                        results = formatter.Formate(results);
#if DEBUG
                    }
                    catch (Exception e)
                    {
                    }
#endif
                    }
                    return(results);
                }
                else
                {
                    bool needCount = field.Option == PropertyDefine.Options.Count;
                    if (needCount)
                    {
                        var values = item.SelectList(selector).Nodes();
                        return(values.Count);
                    }
                    else
                    {
                        var     value    = item.Select(selector)?.GetValue(needPlainText);
                        dynamic tmpValue = value;
                        foreach (var formatter in field.Formatters)
                        {
#if DEBUG
                            try
                            {
#endif
                            tmpValue = formatter.Formate(tmpValue);
#if DEBUG
                        }
                        catch (Exception e)
                        {
                        }
#endif
                        }

                        return(tmpValue == null ? null : Convert.ChangeType(tmpValue, field.DataType));
                    }
                }
            }
        }