SharpRSS/SharpRss/Services/RssService.cs
2023-06-10 20:27:26 +02:00

135 lines
5.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Argotic.Common;
using Argotic.Syndication;
using Serilog;
using SharpRss.Models;
namespace SharpRss.Services
{
/// <summary>
/// Managing RSS feeds and groups.
/// </summary>
public class RssService
{
public RssService()
{
SetupTestGroupsAndFeedsAsync();
}
public async Task<HashSet<object>> GetCategoriesFeedsAsync()
{
HashSet<object> items = new HashSet<object>();
items.UnionWith(await GetCategoriesAsync());
items.UnionWith(await GetUngroupedFeedsAsync());
return items;
}
public async Task<bool> CreateGroupAsync(CategoryModel group) => await DbAccess_Old.SetCategoryAsync(group);
public async Task<HashSet<CategoryModel>> GetCategoriesAsync() => await DbAccess.GetCategoriesAsync();
//TODO: Rework this!
// Subscribe to a feed.
public async Task<bool> 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<HashSet<FeedModel>> GetFeedsAsync(string? categoryId = null) => await DbAccess.GetFeedsAsync(categoryId == null ? null : new[]{ categoryId });
public async Task<HashSet<FeedModel>> GetUngroupedFeedsAsync() => await DbAccess.GetFeedsAsync(new []{""});
public async Task<HashSet<FeedItemModel>> GetFeedItemsAsync(string feedId, string? groupId = null) => await GetFeedItemsFromFeedsAsync(new[] { feedId }, groupId);
public async Task<HashSet<FeedItemModel>> 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()
{
CategoryModel NewsCat = new CategoryModel() { Name = "News" };
await AddSubscriptionAsync("https://www.nu.nl/rss/Algemeen", NewsCat);
await AddSubscriptionAsync("https://www.nu.nl/rss/Economie", NewsCat);
await AddSubscriptionAsync("http://fedoramagazine.org/feed/");
await AddSubscriptionAsync("https://itsfoss.com/feed");
//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);*/
}
}
}