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 groups. /// public class RssService { public RssService() { SetupTestGroupsAndFeedsAsync(); } public async Task> GetCategoriesFeedsAsync() { HashSet items = new HashSet(); items.UnionWith(await GetCategoriesAsync()); items.UnionWith(await GetUngroupedFeedsAsync()); return items; } public async Task CreateGroupAsync(CategoryModel group) => await DbAccess_Old.SetCategoryAsync(group); public async Task> GetCategoriesAsync() => await DbAccess.GetCategoriesAsync(); //TODO: Rework this! // Subscribe to a feed. public async Task AddSubscriptionAsync(string url, CategoryModel? category = null) { var syndication = SyndicationManager.CreateSyndication(url); if (category != null) syndication.Category = category; if (!syndication.Fetched) return false; await DbAccess.SetSyndicationAsync(syndication); return true; } 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.OriginalUrl = 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; } public async Task UpdateFeeds() { Log.Verbose("Fetching feeds..."); var feeds = await GetFeedsAsync(); } public async Task> GetFeedsAsync(string? groupId = null) => await DbAccess.GetFeedsAsync(); public async Task> GetUngroupedFeedsAsync() => await DbAccess.GetFeedsAsync(); 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 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 SetupTestGroupsAndFeedsAsync() { /*await AddSubscriptionAsync("https://www.nu.nl/rss/Algemeen");*/ //TODO: Make multiple adding of feed to a transaction, now throws an exception. /*var groupRes = await CreateGroupAsync(new GroupModel() { Name = "Test" }); groupRes = await CreateGroupAsync(new GroupModel() { Name = "News" }); groupRes = await CreateGroupAsync(new GroupModel() { Name = "Tech" }); groupRes = await CreateGroupAsync(new GroupModel() { Name = "Science" });*/ /*Log.Verbose("Fetching feeds..."); var groups = await GetGroupsAsync(); GroupModel testGroup = groups.Single(x => x.Name == "Test"); try { var res = await AddFeed("http://fedoramagazine.org/feed/", testGroup); res = await AddFeed("https://www.nasa.gov/rss/dyn/breaking_news.rss", testGroup); res = await AddFeed("https://journals.plos.org/plosone/feed/atom", testGroup); res = await AddFeed("https://itsfoss.com/feed", testGroup); res = await AddFeed("https://advisories.ncsc.nl/rss/advisories", testGroup); } catch (Exception e) { Log.Error(e, "Error fetching feeds!"); throw; }*/ /*var groups = await GetGroupsAsync(); CategoryModel testGroup = groups.Single(x => x.Name == "News");*/ /*await AddFeedsAsync(new[] { "https://www.nu.nl/rss/Algemeen", "https://www.nu.nl/rss/Economie", "https://www.nu.nl/rss/Sport", "http://news.google.com/?output=atom" }, testGroup);*/ } } }