/// <summary> /// Парсить иконку и флаг. /// </summary> /// <param name="str">Строка.</param> /// <returns>Иконка и флаг.</returns> public FlagAndIcon ParseFlagAndIcon(string str) { var emptyResult = new FlagAndIcon() { Icon = null, Country = null }; if (string.IsNullOrWhiteSpace(str)) { return(emptyResult); } try { var html = _htmlDocumentFactory.Load(str); if (html.DocumentNode?.ChildNodes == null) { return(emptyResult); } var images = html.DocumentNode .ChildNodes .Where(n => n.NodeType == typeof(IHtmlNode)) .Where(n => n.Name.EqualsNc("img")) .ToArray(); BoardPostIcon icon = null; BoardPostCountryFlag country = null; icon = images .Where(obj => obj.GetAttributeValue("src", null) != null && obj.GetAttributeValue("title", null) != null) .Select(obj => new BoardPostIcon() { ImageLink = new EngineMediaLink() { Engine = MakabaConstants.MakabaEngineId, Uri = obj.GetAttributeValue("src", null) }, Description = obj.GetAttributeValue("title", null) }) .FirstOrDefault(); country = images .Where(obj => obj.GetAttributeValue("src", null) != null) .Where(obj => (obj.GetAttributeValue("src", null) ?? "").StartsWith("/flags/", StringComparison.OrdinalIgnoreCase)) .Select(obj => new BoardPostCountryFlag() { ImageLink = new EngineMediaLink() { Engine = MakabaConstants.MakabaEngineId, Uri = obj.GetAttributeValue("src", null) }, }) .FirstOrDefault(); return(new FlagAndIcon() { Icon = icon, Country = country }); } catch { return(emptyResult); } }
/// <summary> /// Парсить иконку и флаг. /// </summary> /// <param name="str">Строка.</param> /// <returns>Иконка и флаг.</returns> public FlagAndIcon ParseFlagAndIcon(string str) { var emptyResult = new FlagAndIcon() { Icon = null, Country = null }; if (string.IsNullOrWhiteSpace(str)) { return emptyResult; } try { var html = new HtmlDocument(); html.LoadHtml(str); if (html.DocumentNode?.ChildNodes == null) { return emptyResult; } var images = html.DocumentNode .ChildNodes .Where(n => n.NodeType == HtmlNodeType.Element) .Where(n => n.Name.EqualsNc("img")) .ToArray(); PostTreeIconExtension icon = null; PostTreeCountryExtension country = null; icon = images .Where(obj => obj.GetAttributeValue("src", null) != null && obj.GetAttributeValue("title", null) != null) .Select(obj => new PostTreeIconExtension() { Uri = obj.GetAttributeValue("src", null), Description = obj.GetAttributeValue("title", null) }) .FirstOrDefault(); country = images .Where(obj => obj.GetAttributeValue("src", null) != null) .Where(obj => (obj.GetAttributeValue("src", null) ?? "").StartsWith("/flags/", StringComparison.OrdinalIgnoreCase)) .Select(obj => new PostTreeCountryExtension() { Uri = obj.GetAttributeValue("src", null), }) .FirstOrDefault(); return new FlagAndIcon() { Icon = icon, Country = country}; } catch { return emptyResult; } }