SharpRSS/SharpRss/Services/SyndicationService.cs

162 lines
8.6 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>
/// Managing syndication 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...");
Task.Run(async () => await 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-24 10:15:51 +02:00
public async Task<HashSet<CategoryModel>> GetCategoriesAsync()
{
HashSet<CategoryModel> categories = await DbAccess.GetCategoriesAsync();
foreach (CategoryModel catModel in categories)
{
catModel.Syndications = await DbAccess.GetSyndicationsAsync(new[] { catModel.Id });
}
return categories;
}
private async void ForEachCategories(CategoryModel x) => x.Syndications = await DbAccess.GetSyndicationsAsync(new[] { x.Id });
2023-06-04 19:00:46 +02:00
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 syndication: {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()
{
Log.Information("Fetching...");
var feeds = await GetSyndicationsAsync();
}
public async Task<HashSet<SyndicationModel>> GetSyndicationsAsync(string? categoryId = null) => await DbAccess.GetSyndicationsAsync(categoryId == null ? null : new[]{ categoryId });
2023-06-16 22:53:26 +02:00
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("Syndication implementation missing!");
break;
}
return model;
}
private GenericSyndicationFeed? CreateSyndication(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 syndication: {FeedUrl}", feedUri.ToString());
2023-05-29 18:02:24 +02:00
if (!SyndicationDiscoveryUtility.UriExists(feedUri))
{
Log.Warning("Syndication: {FeedUri} does not exists!", feedUri.ToString());
2023-05-29 18:02:24 +02:00
return null;
}
Log.Verbose("Fetching syndication: {FeedUrl}", feedUri.ToString());
2023-05-29 18:02:24 +02:00
return GenericSyndicationFeed.Create(new Uri(url));
2023-05-22 15:55:21 +02:00
}
private async Task SetupTestCategoriesAndFeedsAsync()
2023-05-18 01:27:11 +02:00
{
2023-07-02 20:54:18 +02:00
/*Log.Information("Setting up test data...");
2023-06-19 17:12:12 +02:00
try
{
CategoryModel? newsCategory = await CreateCategoryAsync(new CategoryModel() { Name = "News", Icon = "<g><rect fill=\"none\" height=\"24\" width=\"24\"/></g><g><path d=\"M22,3l-1.67,1.67L18.67,3L17,4.67L15.33,3l-1.66,1.67L12,3l-1.67,1.67L8.67,3L7,4.67L5.33,3L3.67,4.67L2,3v16 c0,1.1,0.9,2,2,2l16,0c1.1,0,2-0.9,2-2V3z M11,19H4v-6h7V19z M20,19h-7v-2h7V19z M20,15h-7v-2h7V15z M20,11H4V8h16V11z\"/></g>"});
2023-06-17 14:04:26 +02:00
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
CategoryModel? techCategory = await CreateCategoryAsync(new CategoryModel() { Name = "Tech", Icon = "<g><rect fill=\"none\" height=\"24\" width=\"24\"/></g><g><path d=\"M17,10.43V2H7v8.43c0,0.35,0.18,0.68,0.49,0.86l4.18,2.51l-0.99,2.34l-3.41,0.29l2.59,2.24L9.07,22L12,20.23L14.93,22 l-0.78-3.33l2.59-2.24l-3.41-0.29l-0.99-2.34l4.18-2.51C16.82,11.11,17,10.79,17,10.43z M13,12.23l-1,0.6l-1-0.6V3h2V12.23z\"/></g>"});
2023-06-17 14:04:26 +02:00
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
CategoryModel? youtubeCategory = await CreateCategoryAsync(new CategoryModel() { Name = "YouTube", Icon = "<path d=\"M10 15l5.19-3L10 9v6m11.56-7.83c.13.47.22 1.1.28 1.9.07.8.1 1.49.1 2.09L22 12c0 2.19-.16 3.8-.44 4.83-.25.9-.83 1.48-1.73 1.73-.47.13-1.33.22-2.65.28-1.3.07-2.49.1-3.59.1L12 19c-4.19 0-6.8-.16-7.83-.44-.9-.25-1.48-.83-1.73-1.73-.13-.47-.22-1.1-.28-1.9-.07-.8-.1-1.49-.1-2.09L2 12c0-2.19.16-3.8.44-4.83.25-.9.83-1.48 1.73-1.73.47-.13 1.33-.22 2.65-.28 1.3-.07 2.49-.1 3.59-.1L12 5c4.19 0 6.8.16 7.83.44.9.25 1.48.83 1.73 1.73z\"/>"});
2023-06-17 14:04:26 +02:00
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-07-02 20:54:18 +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
}