示例#1
0
        private string ParseHtml(string html)
        {
            if (html.IsEmptyOrWhiteSpace())
            {
                return(string.Empty);
            }

            var htmlDoc = new HtmlDocument();

            try
            {
                html = MonkeyHelpers.CleanHtmlString(html);
                htmlDoc.LoadHtml(html);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Error parsing html");
                return(html);
            }

            var sb = new StringBuilder();

            var textNodes = htmlDoc?.DocumentNode?.SelectNodes("//text()");
            var iframes   = htmlDoc?.DocumentNode?.SelectNodes("//iframe[@src]");

            if (textNodes != null)
            {
                foreach (HtmlNode node in textNodes)
                {
                    if (!node.InnerText.IsEmptyOrWhiteSpace())
                    {
                        sb.Append(node.InnerText.Trim() + "|");
                    }
                }
            }
            if (iframes != null)
            {
                foreach (HtmlNode node in iframes)
                {
                    if (node.HasAttributes &&
                        node.Attributes.Contains("src") &&
                        !node.Attributes["src"].Value.IsEmptyOrWhiteSpace())
                    {
                        sb.Append(node.Attributes["src"].Value);
                    }
                }
            }
            var result = sb.ToString();

            if (!result.IsEmpty())
            {
                return(result);
            }
            return(html);
        }
示例#2
0
 private static OTDBQuestion CleanQuestion(OTDBQuestion x)
 {
     return(new OTDBQuestion
     {
         Category = MonkeyHelpers.CleanHtmlString(x.Category),
         Question = MonkeyHelpers.CleanHtmlString(x.Question),
         CorrectAnswer = MonkeyHelpers.CleanHtmlString(x.CorrectAnswer),
         IncorrectAnswers = x.IncorrectAnswers.Select(MonkeyHelpers.CleanHtmlString).ToList(),
         Type = x.Type,
         Difficulty = x.Difficulty
     });
 }
示例#3
0
        private async Task <string> GetJokeAsync(Uri uri)
        {
            HttpClient httpClient = clientFactory.CreateClient();
            string     json       = await httpClient.GetStringAsync(uri).ConfigureAwait(false);

            if (!json.IsEmpty())
            {
                ChuckResponse chuckResponse = JsonSerializer.Deserialize <ChuckResponse>(json);
                if (chuckResponse.Type == "success" && chuckResponse.Value != null)
                {
                    return(MonkeyHelpers.CleanHtmlString(chuckResponse.Value.Joke));
                }
            }
            return(string.Empty);
        }
示例#4
0
        public async Task <string> GetChuckFactAsync()
        {
            using (var httpClient = new HttpClient())
            {
                var json = await httpClient.GetStringAsync(randomJokeApiUrl).ConfigureAwait(false);

                if (!json.IsEmpty())
                {
                    var chuckResponse = JsonConvert.DeserializeObject <ChuckResponse>(json);
                    if (chuckResponse.Type == "success" && chuckResponse.Value != null)
                    {
                        return(MonkeyHelpers.CleanHtmlString(chuckResponse.Value.Joke));
                    }
                }
                return(string.Empty);
            }
        }
示例#5
0
        public async Task <string> GetChuckFactAsync(string userName)
        {
            if (userName.IsEmpty())
            {
                return(string.Empty);
            }
            using (var httpClient = new HttpClient())
            {
                var url = new UriBuilder(randomJokeApiUrl);
                url.Query = $"firstName={userName}";
                var json = await httpClient.GetStringAsync(url.Uri).ConfigureAwait(false);

                if (!json.IsEmpty())
                {
                    var chuckResponse = JsonConvert.DeserializeObject <ChuckResponse>(json);
                    if (chuckResponse.Type == "success" && chuckResponse.Value != null)
                    {
                        return(MonkeyHelpers.CleanHtmlString(chuckResponse.Value.Joke.Replace("Norris", "").Replace("  ", " ")));
                    }
                }
                return(string.Empty);
            }
        }
示例#6
0
        private (string TextContent, string ImgLink) ParseHtml(string html)
        {
            if (html.IsEmptyOrWhiteSpace())
            {
                return(new(string.Empty, string.Empty));
            }

            var htmlDoc = new HtmlDocument();

            try
            {
                html = MonkeyHelpers.CleanHtmlString(html);
                htmlDoc.LoadHtml(html);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error parsing html");
                return(new(html, string.Empty));
            }

            var sb = new StringBuilder();

            HtmlNodeCollection textNodes = htmlDoc?.DocumentNode?.SelectNodes("//text()");
            HtmlNodeCollection iframes   = htmlDoc?.DocumentNode?.SelectNodes("//iframe[@src]");

            if (textNodes != null)
            {
                foreach (HtmlNode node in textNodes)
                {
                    if (!node.InnerText.IsEmptyOrWhiteSpace())
                    {
                        sb.Append(node.InnerText.Trim() + "|");
                    }
                }
            }
            if (iframes != null)
            {
                foreach (HtmlNode node in iframes)
                {
                    if (node.HasAttributes &&
                        node.Attributes.Contains("src") &&
                        !node.Attributes["src"].Value.IsEmptyOrWhiteSpace())
                    {
                        sb.Append(node.Attributes["src"].Value);
                    }
                }
            }
            string textContent = sb.Length > 0 ? sb.ToString().Trim('|') : html;

            var imgNodes = htmlDoc?
                           .DocumentNode?
                           .SelectNodes("//img");
            string imgLink =
                imgNodes?
                .Where(x => x.HasAttributes && x.Attributes.Contains("src") && !x.Attributes["src"].Value.IsEmptyOrWhiteSpace())?
                .Select(x => x.Attributes["src"].Value)?
                .Where(l => !l.EndsWith("gif"))?
                .FirstOrDefault() ?? string.Empty;

            return(new(textContent.Trim(), imgLink));
        }