コード例 #1
0
        public virtual string FetchContent(HtmlDocument doc, string key, NameValueCollection mappings)
        {
            string rowValue = string.Empty;
            bool   textOnly = false;

            try
            {
                int        index       = mappings[key].IndexOf('_');
                string     toWhatField = mappings[key].Substring(0, index);
                IBaseField mappingItem = FieldDefinitions.FirstOrDefault(f =>
                                                                         f.InnerItem.Fields[FromWhatField].Value == key &&
                                                                         f.InnerItem.Fields["To What Field"].Value == toWhatField);

                textOnly = mappingItem.InnerItem.Fields["Strip HTML"]?.Value == "1" || ImportTextOnly;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString(), rowValue, LogType.GetImportDataError, key, rowValue);
            }

            var node = HtmlService.HandleNodesLookup(key, doc);

            if (node == null)
            {
                return(rowValue);
            }

            rowValue = textOnly ? node.InnerText : node.InnerHtml;

            return(rowValue);
        }
コード例 #2
0
        public string FetchContent(HtmlDocument doc, object key, NameValueCollection mappings, ImportConfig storedConfig)
        {
            string rowValue = string.Empty;
            bool   textOnly = false;
            bool   useXPath = false;

            try
            {
                int        index       = mappings[key.ToString()].IndexOf('_');
                string     toWhatField = mappings[key.ToString()].Substring(0, index);
                IBaseField mappingItem = FieldDefinitions.FirstOrDefault(f =>
                                                                         f.InnerItem.Fields[HtmlScraper.Constants.FieldNames.FromWhatField].Value == key.ToString() &&
                                                                         f.InnerItem.Fields[HtmlScraper.Constants.FieldNames.ToWhatField].Value == toWhatField);

                useXPath = mappingItem.InnerItem.Fields[HtmlScraper.Constants.FieldNames.UseXpath].Value == "1";

                textOnly = mappingItem.InnerItem.Fields[HtmlScraper.Constants.FieldNames.ImportTextOnly].Value == "1" ||
                           storedConfig.ImportTextOnly;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString(), rowValue, ProcessStatus.GetImportDataError, key.ToString(), rowValue);
            }

            string   htmlObj     = string.Empty;
            bool     isAttrValue = false;
            string   attrName    = string.Empty;
            HtmlNode node        = null;

            htmlObj = key.ToString();

            if (htmlObj.Contains("/@"))
            {
                isAttrValue = true;
                List <string> attrItems = htmlObj.Split('/').Where(a => a.StartsWith("@")).ToList();
                attrName = attrItems.FirstOrDefault(a => !a.Contains("="));
                htmlObj  = htmlObj.Replace("/" + attrName, "");
                attrName = attrName.Replace("@", "");
            }

            node = useXPath
                ? Helper.HandleNodesLookup(htmlObj, doc, true)
                : Helper.HandleNodesLookup(htmlObj, doc);

            if (node == null)
            {
                return(rowValue);
            }

            rowValue = isAttrValue && node.Attributes[attrName] != null
                ? node.Attributes[attrName].Value
                : (textOnly ? node.InnerText : node.InnerHtml);

            return(rowValue);
        }
コード例 #3
0
ファイル: ModelDefinition.cs プロジェクト: cody82/DownSite
 public FieldDefinition GetFieldDefinition(string fieldName)
 {
     return(FieldDefinitions.FirstOrDefault(f => f.Name == fieldName));
 }