SharpRSS/SharpRss/Services/SyndicationService.cs

150 lines
7.0 KiB
C#
Raw Normal View History

2023-05-20 00:04:45 +02:00
using System;
2023-05-18 01:27:11 +02:00
using System.Collections.Generic;
using System.Threading.Tasks;
2023-05-29 18:02:24 +02:00
using Argotic.Common;
using Argotic.Syndication;
2023-05-22 15:55:21 +02:00
using Serilog;
2023-05-18 01:27:11 +02:00
using SharpRss.Models;
namespace SharpRss.Services
{
/// <summary>
2023-06-15 19:26:54 +02:00
/// Managing feeds and categories.
2023-05-18 01:27:11 +02:00
/// </summary>
2023-06-15 19:26:54 +02:00
public class SyndicationService
2023-05-18 01:27:11 +02:00
{
2023-06-15 19:26:54 +02:00
public SyndicationService()
2023-05-18 01:27:11 +02:00
{
2023-06-19 17:12:12 +02:00
Log.Information("Constructing SyndicationService...");
SetupTestCategoriesAndFeedsAsync();
2023-05-18 01:27:11 +02:00
}
2023-05-23 15:04:02 +02:00
2023-06-16 22:53:26 +02:00
public async Task<HashSet<object>> GetCategoriesAndSyndicationsAsync()
2023-05-23 15:04:02 +02:00
{
HashSet<object> items = new HashSet<object>();
2023-06-02 15:45:10 +02:00
items.UnionWith(await GetCategoriesAsync());
2023-06-16 22:53:26 +02:00
items.UnionWith(await GetUngroupedSyndicationsAsync());
2023-05-23 15:04:02 +02:00
return items;
}
public async Task<CategoryModel?> CreateCategoryAsync(CategoryModel category) =>
await DbAccess.SetCategoryAsync(category);
2023-06-04 19:00:46 +02:00
public async Task<HashSet<CategoryModel>> GetCategoriesAsync() => await DbAccess.GetCategoriesAsync();
public async Task<bool> AddSubscriptionAsync(string url, CategoryModel? category = null)
2023-05-22 15:55:21 +02:00
{
2023-06-04 19:00:46 +02:00
var syndication = SyndicationManager.CreateSyndication(url);
if (!syndication.Fetched)
{
2023-06-19 17:12:12 +02:00
Log.Warning("Failed to fetch syndication feed!");
return false;
}
2023-06-04 19:00:46 +02:00
if (category != null)
syndication.Category = category;
try
{
await DbAccess.SetSyndicationAsync(syndication);
}
catch (Exception e)
{
Log.Error(e,"Error adding feed: {FeedUrl} to database!", url);
}
2023-06-02 11:56:48 +02:00
return true;
2023-05-29 16:14:26 +02:00
}
public async Task UpdateFeeds()
{
2023-06-19 17:12:12 +02:00
Log.Information("Fetching feeds...");
var feeds = await GetFeedsAsync();
}
2023-06-16 22:53:26 +02:00
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)
{
2023-06-16 22:53:26 +02:00
var items = await DbAccess.GetSyndicationItemsAsync(feedIds);
return items;
}
2023-06-16 22:53:26 +02:00
private static SyndicationModel FromResource(ISyndicationResource resource)
{
2023-06-16 22:53:26 +02:00
SyndicationModel model = new SyndicationModel();
2023-06-02 10:56:30 +02:00
switch (resource.Format)
{
2023-06-02 10:56:30 +02:00
case SyndicationContentFormat.Rss:
RssFeed rssFeed = (RssFeed)resource;
2023-06-16 22:53:26 +02:00
model.SyndicationType = rssFeed.Format.ToString();
2023-06-02 11:56:48 +02:00
model.Title = rssFeed.Channel.Title;
model.Description = rssFeed.Channel.Description;
model.Copyright = rssFeed.Channel.Copyright;
model.EncodedUrl = rssFeed.Channel.SelfLink.ToString();
2023-06-04 19:00:46 +02:00
model.ImageUrl = rssFeed.Channel.Image?.Url.ToString() ?? string.Empty;
2023-06-02 11:56:48 +02:00
model.Language = rssFeed.Channel.Language?.ToString();
2023-06-02 10:56:30 +02:00
break;
case SyndicationContentFormat.Atom:
AtomFeed atomFeed = (AtomFeed)resource;
break;
default:
Log.Information("Feed implementation missing!");
break;
}
return model;
}
2023-05-29 18:02:24 +02:00
private GenericSyndicationFeed? CreateFeed(string url)
2023-05-22 15:55:21 +02:00
{
2023-05-29 18:02:24 +02:00
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));
2023-05-22 15:55:21 +02:00
}
private async void SetupTestCategoriesAndFeedsAsync()
2023-05-18 01:27:11 +02:00
{
2023-06-23 19:36:58 +02:00
/*Log.Information("Setting up test data...");
2023-06-19 17:12:12 +02:00
try
{
2023-06-17 14:04:26 +02:00
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);
}
2023-06-19 17:12:12 +02:00
2023-06-17 14:04:26 +02:00
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);
}
2023-06-04 19:00:46 +02:00
2023-06-17 14:04:26 +02:00
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);
}
2023-06-17 14:04:26 +02:00
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!");
2023-06-23 19:36:58 +02:00
}*/
2023-05-18 01:27:11 +02:00
}
2023-05-29 16:14:26 +02:00
}
2023-05-18 01:27:11 +02:00
}