using System; using System.Collections.Generic; using System.Threading.Tasks; using Argotic.Common; using Argotic.Syndication; using Serilog; using SharpRss.Models; namespace SharpRss.Services { /// /// Managing RSS feeds and categories. /// public class RssService { public RssService() { SetupTestCategoriesAndFeedsAsync(); } public async Task> GetCategoriesAndFeedsAsync() { HashSet items = new HashSet(); items.UnionWith(await GetCategoriesAsync()); items.UnionWith(await GetUngroupedFeedsAsync()); return items; } public async Task CreateCategoryAsync(CategoryModel category) => await DbAccess.SetCategoryAsync(category); public async Task> GetCategoriesAsync() => await DbAccess.GetCategoriesAsync(); public async Task AddSubscriptionAsync(string url, CategoryModel? category = null) { var syndication = SyndicationManager.CreateSyndication(url); if (!syndication.Fetched) { Log.Debug("Failed to fetch syndication feed!"); return false; } if (category != null) syndication.Category = category; try { await DbAccess.SetSyndicationAsync(syndication); } catch (Exception e) { Log.Error(e,"Error adding feed: {FeedUrl} to database!", url); } return true; } public async Task UpdateFeeds() { Log.Verbose("Fetching feeds..."); var feeds = await GetFeedsAsync(); } public async Task> GetFeedsAsync(string? categoryId = null) => await DbAccess.GetFeedsAsync(categoryId == null ? null : new[]{ categoryId }); public async Task> GetUngroupedFeedsAsync() => await DbAccess.GetFeedsAsync(new []{""}); public async Task> GetFeedItemsAsync(string feedId, string? groupId = null) => await GetFeedItemsFromFeedsAsync(new[] { feedId }, groupId); public async Task> GetFeedItemsFromFeedsAsync(string[] feedIds, string? groupId = null) { var items = await DbAccess.GetFeedItemsAsync(feedIds); return items; } private static FeedModel FromResource(ISyndicationResource resource) { FeedModel model = new FeedModel(); switch (resource.Format) { case SyndicationContentFormat.Rss: RssFeed rssFeed = (RssFeed)resource; model.FeedType = rssFeed.Format.ToString(); model.Title = rssFeed.Channel.Title; model.Description = rssFeed.Channel.Description; model.Copyright = rssFeed.Channel.Copyright; model.EncodedUrl = rssFeed.Channel.SelfLink.ToString(); model.ImageUrl = rssFeed.Channel.Image?.Url.ToString() ?? string.Empty; model.Language = rssFeed.Channel.Language?.ToString(); break; case SyndicationContentFormat.Atom: AtomFeed atomFeed = (AtomFeed)resource; break; default: Log.Information("Feed implementation missing!"); break; } return model; } private GenericSyndicationFeed? CreateFeed(string url) { Uri feedUri = new Uri(url); Log.Verbose("Checking feed: {FeedUrl}", feedUri.ToString()); if (!SyndicationDiscoveryUtility.UriExists(feedUri)) { Log.Warning("Feed: {FeedUri} does not exists!", feedUri.ToString()); return null; } Log.Verbose("Fetching feed: {FeedUrl}", feedUri.ToString()); return GenericSyndicationFeed.Create(new Uri(url)); } private async void SetupTestCategoriesAndFeedsAsync() { /*CategoryModel? newsCategory = await CreateCategoryAsync(new CategoryModel() { Name = "News" }); if (newsCategory != null) { await AddSubscriptionAsync("https://www.nu.nl/rss/Algemeen", newsCategory); await AddSubscriptionAsync("https://www.nasa.gov/rss/dyn/breaking_news.rss", newsCategory); await AddSubscriptionAsync("http://news.google.com/?output=atom", newsCategory); await AddSubscriptionAsync("https://www.ad.nl/home/rss.xml", newsCategory); } CategoryModel? techCategory = await CreateCategoryAsync(new CategoryModel() { Name = "Tech" }); if (techCategory != null) { await AddSubscriptionAsync("https://itsfoss.com/feed", techCategory); await AddSubscriptionAsync("http://fedoramagazine.org/feed/", techCategory); await AddSubscriptionAsync("https://arstechnica.com/feed/", techCategory); await AddSubscriptionAsync("https://feeds.arstechnica.com/arstechnica/gadgets", techCategory); } CategoryModel? youtubeCategory = await CreateCategoryAsync(new CategoryModel() { Name = "YouTube" }); if (youtubeCategory != null) { await AddSubscriptionAsync("https://www.youtube.com/feeds/videos.xml?channel_id=UCXuqSBlHAE6Xw-yeJA0Tunw", youtubeCategory); await AddSubscriptionAsync("https://www.youtube.com/feeds/videos.xml?channel_id=UC1Et9K-hHf-P_LzQkE_Q3Jw", youtubeCategory); await AddSubscriptionAsync("https://www.youtube.com/feeds/videos.xml?channel_id=UCsXVk37bltHxD1rDPwtNM8Q", youtubeCategory); } await AddSubscriptionAsync("http://www.digitaleoverheid.nl/feed/"); await AddSubscriptionAsync("http://www.digitaleoverheid.nl/agenda/feed/"); await AddSubscriptionAsync("https://feeds.rijksoverheid.nl/nieuws.rss"); await AddSubscriptionAsync("https://nl.wikipedia.org/w/index.php?title=Speciaal:RecenteWijzigingen&feed=atom"); await AddSubscriptionAsync("https://feeds.aivd.nl/nieuws.rss"); await AddSubscriptionAsync("https://blogs.microsoft.com/feed"); await AddSubscriptionAsync("https://www.europarl.europa.eu/rss/doc/top-stories/nl.xml");*/ } } }