SharpRSS/SharpRss/Services/SyndicationService.cs
2023-06-23 19:36:58 +02:00

150 lines
7.0 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 feeds and categories.
/// </summary>
public class SyndicationService
{
public SyndicationService()
{
Log.Information("Constructing SyndicationService...");
SetupTestCategoriesAndFeedsAsync();
}
public async Task<HashSet<object>> GetCategoriesAndSyndicationsAsync()
{
HashSet<object> items = new HashSet<object>();
items.UnionWith(await GetCategoriesAsync());
items.UnionWith(await GetUngroupedSyndicationsAsync());
return items;
}
public async Task<CategoryModel?> CreateCategoryAsync(CategoryModel category) =>
await DbAccess.SetCategoryAsync(category);
public async Task<HashSet<CategoryModel>> GetCategoriesAsync() => await DbAccess.GetCategoriesAsync();
public async Task<bool> AddSubscriptionAsync(string url, CategoryModel? category = null)
{
var syndication = SyndicationManager.CreateSyndication(url);
if (!syndication.Fetched)
{
Log.Warning("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.Information("Fetching feeds...");
var feeds = await GetFeedsAsync();
}
public async Task<HashSet<SyndicationModel>> GetFeedsAsync(string? categoryId = null) => await DbAccess.GetSyndicationsAsync(categoryId == null ? null : new[]{ categoryId });
public async Task<HashSet<SyndicationModel>> GetUngroupedSyndicationsAsync() => await DbAccess.GetSyndicationsAsync(new []{""});
public async Task<HashSet<SyndicationItemModel>> GetSyndicationItemsAsync(string feedId, string? groupId = null) => await GetSyndicationItemsFromSyndicationsAsync(new[] { feedId }, groupId);
public async Task<HashSet<SyndicationItemModel>> GetSyndicationItemsFromSyndicationsAsync(string[] feedIds, string? categoryId = null)
{
var items = await DbAccess.GetSyndicationItemsAsync(feedIds);
return items;
}
private static SyndicationModel FromResource(ISyndicationResource resource)
{
SyndicationModel model = new SyndicationModel();
switch (resource.Format)
{
case SyndicationContentFormat.Rss:
RssFeed rssFeed = (RssFeed)resource;
model.SyndicationType = 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()
{
/*Log.Information("Setting up test data...");
try
{
CategoryModel? newsCategory = await CreateCategoryAsync(new CategoryModel() { Name = "News" });
if (newsCategory != null)
{
await AddSubscriptionAsync("https://www.nu.nl/rss/Algemeen", newsCategory);
await AddSubscriptionAsync("https://www.ad.nl/home/rss.xml", newsCategory);
await AddSubscriptionAsync("https://www.nasa.gov/rss/dyn/breaking_news.rss", newsCategory);
await AddSubscriptionAsync("http://news.google.com/?output=atom", 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");
}
catch (Exception e)
{
Log.Error(e, "Exception!");
}*/
}
}
}