| | | 1 | | using System.Net; |
| | | 2 | | using System.ServiceModel.Syndication; |
| | | 3 | | using System.Xml; |
| | | 4 | | using GistBackend.Exceptions; |
| | | 5 | | using GistBackend.Utils; |
| | | 6 | | |
| | | 7 | | namespace GistBackend.Types; |
| | | 8 | | |
| | 313 | 9 | | public abstract record RssFeed() |
| | | 10 | | { |
| | | 11 | | public abstract Uri RssUrl { get; } |
| | | 12 | | public abstract Language Language { get; } |
| | | 13 | | public abstract FeedType Type { get; } |
| | 0 | 14 | | public virtual IEnumerable<string>? AllowedCategories => null; |
| | 0 | 15 | | public virtual IEnumerable<string>? ForbiddenCategories => null; |
| | 140 | 16 | | private SyndicationFeed? SyndicationFeed { get; set; } |
| | 302 | 17 | | public int? Id { get; set; } |
| | 225 | 18 | | public string? Title { get; set; } |
| | 93 | 19 | | public IEnumerable<RssEntry>? Entries { get; private set; } |
| | | 20 | | public abstract string ExtractText(string content); |
| | | 21 | | |
| | 0 | 22 | | public virtual bool CheckForSponsoredContent(string content) => false; |
| | 0 | 23 | | public virtual bool CheckForPaywall(string content) => false; |
| | | 24 | | |
| | | 25 | | public async Task ParseFeedAsync(HttpClient httpClient, CancellationToken ct) |
| | | 26 | | { |
| | 36 | 27 | | var response = await httpClient.GetAsync(RssUrl, ct); |
| | 36 | 28 | | if (response.StatusCode != HttpStatusCode.OK) |
| | | 29 | | { |
| | 0 | 30 | | throw new ParsingFeedException( |
| | 0 | 31 | | $"Failed to fetch RSS feed from {RssUrl}, status code: {response.StatusCode}"); |
| | | 32 | | } |
| | 36 | 33 | | var feedContent = await response.Content.ReadAsStringAsync(ct); |
| | 36 | 34 | | using var stringReader = new StringReader(feedContent); |
| | 36 | 35 | | using var xmlReader = XmlReader.Create(stringReader); |
| | 36 | 36 | | SyndicationFeed = SyndicationFeed.Load(xmlReader); |
| | 36 | 37 | | Title = SyndicationFeed.Title.Text; |
| | 36 | 38 | | } |
| | | 39 | | |
| | | 40 | | public void ParseEntries(int feedId) |
| | | 41 | | { |
| | 34 | 42 | | if (SyndicationFeed is null || Title is null) |
| | 0 | 43 | | throw new InvalidOperationException($"{nameof(SyndicationFeed)} is null, need to parse feed first"); |
| | 34 | 44 | | Id = feedId; |
| | 34 | 45 | | Entries = SyndicationFeed.Items.Select(SyndicationItemToRssEntry) |
| | 34 | 46 | | .FilterForAllowedCategories(AllowedCategories) |
| | 34 | 47 | | .FilterForForbiddenCategories(ForbiddenCategories) |
| | 34 | 48 | | .FilterPaywallEntries(Title ?? ""); |
| | 34 | 49 | | } |
| | | 50 | | |
| | | 51 | | public RssFeedInfo ToRssFeedInfo() |
| | | 52 | | { |
| | 13 | 53 | | return Title is null |
| | 13 | 54 | | ? throw new ArgumentNullException($"{nameof(Title)} is null, need to parse feed first") |
| | 13 | 55 | | : new RssFeedInfo(Title, RssUrl, Language, Type) { Id = Id }; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | private RssEntry SyndicationItemToRssEntry(SyndicationItem item) => |
| | 187 | 59 | | new( |
| | 187 | 60 | | item.Id.Trim(), |
| | 187 | 61 | | Id!.Value, |
| | 187 | 62 | | item.ExtractAuthor(), |
| | 187 | 63 | | WebUtility.HtmlDecode(item.Title.Text.Trim()), |
| | 187 | 64 | | item.PublishDate.UtcDateTime, |
| | 187 | 65 | | item.ExtractUpdated(), |
| | 187 | 66 | | item.ExtractUrl(), |
| | 187 | 67 | | item.ExtractCategories() |
| | 187 | 68 | | ); |
| | | 69 | | } |