| | | 1 | | using GistBackend.Exceptions; |
| | | 2 | | using GistBackend.Types; |
| | | 3 | | using HtmlAgilityPack; |
| | | 4 | | using static System.Net.WebUtility; |
| | | 5 | | using static GistBackend.Types.FeedType; |
| | | 6 | | using static GistBackend.Types.Language; |
| | | 7 | | using static GistBackend.Utils.RssFeedUtils; |
| | | 8 | | |
| | | 9 | | namespace GistBackend.Handlers.RssFeedHandler.Feeds; |
| | | 10 | | |
| | | 11 | | public record GolemSecurity : RssFeed |
| | | 12 | | { |
| | 23 | 13 | | public override Uri RssUrl { get; } = new("https://rss.golem.de/rss.php?ms=security&feed=ATOM1.0"); |
| | 0 | 14 | | public override Language Language => De; |
| | 0 | 15 | | public override FeedType Type => News; |
| | | 16 | | |
| | | 17 | | public override string ExtractText(string content) |
| | | 18 | | { |
| | 0 | 19 | | var doc = new HtmlDocument(); |
| | 0 | 20 | | doc.LoadHtml(content); |
| | | 21 | | |
| | 0 | 22 | | var entryContainer = doc.DocumentNode.SelectSingleNode($"//article[{ContainsClassSpecifier("go-article")}]"); |
| | 0 | 23 | | if (entryContainer == null) |
| | | 24 | | { |
| | 0 | 25 | | throw new ExtractingEntryTextException("Missing container element"); |
| | | 26 | | } |
| | | 27 | | |
| | 0 | 28 | | var paragraphsAndHeadings = entryContainer.SelectNodes(".//p | .//h1 | .//h2 | .//h3 | .//h4 | .//h5 | .//h6"); |
| | 0 | 29 | | if (paragraphsAndHeadings == null || paragraphsAndHeadings.Count == 0) |
| | | 30 | | { |
| | 0 | 31 | | throw new ExtractingEntryTextException("Missing paragraph or heading elements"); |
| | | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | var combinedTextContents = string.Join("\n", paragraphsAndHeadings.Select(node => node.InnerText)); |
| | 0 | 35 | | if (string.IsNullOrWhiteSpace(combinedTextContents)) |
| | | 36 | | { |
| | 0 | 37 | | throw new ExtractingEntryTextException("No text found in container"); |
| | | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | var decodedText = HtmlDecode(combinedTextContents); |
| | 0 | 41 | | return decodedText; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | public override bool CheckForSponsoredContent(string content) |
| | | 45 | | { |
| | 0 | 46 | | var doc = new HtmlDocument(); |
| | 0 | 47 | | doc.LoadHtml(content); |
| | 0 | 48 | | var kickerSpan = |
| | 0 | 49 | | doc.DocumentNode.SelectSingleNode($"//span[{ContainsClassSpecifier("go-article-header__kicker")}]"); |
| | | 50 | | // ReSharper disable once ConditionalAccessQualifierIsNonNullableAccordingToAPIContract |
| | 0 | 51 | | var sponsoredLabel = kickerSpan?.SelectSingleNode($".//span[{ContainsClassSpecifier("go-label--sponsored")}]"); |
| | 0 | 52 | | return sponsoredLabel != null; |
| | | 53 | | } |
| | | 54 | | } |