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-05-22 15:55:21 +02:00
|
|
|
/// Managing RSS feeds and groups.
|
2023-05-18 01:27:11 +02:00
|
|
|
/// </summary>
|
2023-06-04 19:00:46 +02:00
|
|
|
public class RssService
|
2023-05-18 01:27:11 +02:00
|
|
|
{
|
|
|
|
public RssService()
|
|
|
|
{
|
2023-05-25 21:51:05 +02:00
|
|
|
SetupTestGroupsAndFeedsAsync();
|
2023-05-18 01:27:11 +02:00
|
|
|
}
|
2023-05-23 15:04:02 +02:00
|
|
|
|
2023-06-04 02:16:47 +02:00
|
|
|
public async Task<HashSet<object>> GetCategoriesFeedsAsync()
|
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-05-23 15:04:02 +02:00
|
|
|
items.UnionWith(await GetUngroupedFeedsAsync());
|
|
|
|
return items;
|
|
|
|
}
|
2023-06-04 02:16:47 +02:00
|
|
|
public async Task<bool> CreateGroupAsync(CategoryModel group) => await DbAccess_Old.SetCategoryAsync(group);
|
2023-06-04 19:00:46 +02:00
|
|
|
public async Task<HashSet<CategoryModel>> GetCategoriesAsync() => await DbAccess.GetCategoriesAsync();
|
2023-05-27 00:32:05 +02:00
|
|
|
|
2023-06-01 15:55:15 +02:00
|
|
|
//TODO: Rework this!
|
|
|
|
// Subscribe to a feed.
|
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 (category != null)
|
|
|
|
syndication.Category = category;
|
|
|
|
if (!syndication.Fetched) return false;
|
2023-06-05 15:15:18 +02:00
|
|
|
await DbAccess.SetSyndicationAsync(syndication);
|
2023-06-02 11:56:48 +02:00
|
|
|
return true;
|
2023-05-29 16:14:26 +02:00
|
|
|
}
|
2023-06-01 15:55:15 +02:00
|
|
|
private static FeedModel FromResource(ISyndicationResource resource)
|
|
|
|
{
|
2023-06-02 10:56:30 +02:00
|
|
|
FeedModel model = new FeedModel();
|
|
|
|
switch (resource.Format)
|
2023-06-01 15:55:15 +02:00
|
|
|
{
|
2023-06-02 10:56:30 +02:00
|
|
|
case SyndicationContentFormat.Rss:
|
|
|
|
RssFeed rssFeed = (RssFeed)resource;
|
2023-06-02 11:56:48 +02:00
|
|
|
model.FeedType = rssFeed.Format.ToString();
|
|
|
|
model.Title = rssFeed.Channel.Title;
|
|
|
|
model.Description = rssFeed.Channel.Description;
|
|
|
|
model.Copyright = rssFeed.Channel.Copyright;
|
2023-06-04 02:16:47 +02:00
|
|
|
model.OriginalUrl = 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!");
|
2023-06-01 15:55:15 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return model;
|
|
|
|
}
|
2023-05-29 18:02:24 +02:00
|
|
|
|
|
|
|
public async Task UpdateFeeds()
|
|
|
|
{
|
|
|
|
Log.Verbose("Fetching feeds...");
|
|
|
|
var feeds = await GetFeedsAsync();
|
|
|
|
}
|
|
|
|
|
2023-06-04 19:00:46 +02:00
|
|
|
public async Task<HashSet<FeedModel>> GetFeedsAsync(string? groupId = null) => await DbAccess.GetFeedsAsync();
|
|
|
|
public async Task<HashSet<FeedModel>> GetUngroupedFeedsAsync() => await DbAccess.GetFeedsAsync();
|
2023-05-24 19:27:22 +02:00
|
|
|
|
2023-05-25 15:48:56 +02:00
|
|
|
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)
|
2023-05-24 19:27:22 +02:00
|
|
|
{
|
2023-06-04 19:00:46 +02:00
|
|
|
var items = await DbAccess.GetFeedItemsAsync(feedIds);
|
2023-05-24 19:27:22 +02:00
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2023-05-29 18:02:24 +02:00
|
|
|
/*private async Task<int> AddFeedItems(IList<FeedItem> items, FeedModel feedModel)
|
2023-05-22 15:55:21 +02:00
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
if (!items.Any())
|
|
|
|
return result;
|
|
|
|
HashSet<FeedItemModel> itemModels = new HashSet<FeedItemModel>();
|
2023-05-29 16:14:26 +02:00
|
|
|
// TODO: implement!!!
|
|
|
|
result = await DbAccess.SetFeedItemsAsync(itemModels);
|
|
|
|
return result;
|
2023-05-29 18:02:24 +02:00
|
|
|
}*/
|
2023-05-29 16:14:26 +02:00
|
|
|
|
2023-05-29 18:02:24 +02:00
|
|
|
/*private IList<FeedItemModel> ConstructFeedItems(IList<FeedItem> items, FeedModel feed)
|
2023-05-29 16:14:26 +02:00
|
|
|
{
|
|
|
|
IList<FeedItemModel> itemModels = new List<FeedItemModel>();
|
|
|
|
if (!items.Any())
|
|
|
|
return itemModels;
|
2023-05-22 15:55:21 +02:00
|
|
|
foreach (FeedItem item in items)
|
|
|
|
{
|
|
|
|
itemModels.Add(new FeedItemModel()
|
|
|
|
{
|
|
|
|
Id = item.Id,
|
2023-05-29 16:14:26 +02:00
|
|
|
FeedId = feed.Id,
|
2023-05-22 15:55:21 +02:00
|
|
|
Title = item.Title,
|
|
|
|
Description = item.Description,
|
|
|
|
Link = item.Link,
|
|
|
|
LastUpdated = DateTimeOffset.Now,
|
|
|
|
PublishingDate = item.PublishingDate != null ? new DateTimeOffset((DateTime)item.PublishingDate) : null,
|
|
|
|
Author = item.Author,
|
|
|
|
Categories = item.Categories.ToArray(),
|
|
|
|
Content = item.Content
|
|
|
|
});
|
|
|
|
}
|
2023-05-29 16:14:26 +02:00
|
|
|
return itemModels;
|
2023-05-29 18:02:24 +02:00
|
|
|
}*/
|
2023-05-29 16:14:26 +02:00
|
|
|
|
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
|
|
|
}
|
2023-05-25 21:51:05 +02:00
|
|
|
private async void SetupTestGroupsAndFeedsAsync()
|
2023-05-18 01:27:11 +02:00
|
|
|
{
|
2023-06-04 19:00:46 +02:00
|
|
|
/*CategoryModel newModel = new CategoryModel() { Name = "Test" };
|
|
|
|
bool added = await DbAccess.SetCategoryAsync(newModel);*/
|
2023-06-05 15:15:18 +02:00
|
|
|
await AddSubscriptionAsync("https://en.wikipedia.org/w/api.php?hidebots=1&hidecategorization=1&hideWikibase=1&urlversion=1&days=7&limit=50&action=feedrecentchanges&feedformat=atom");
|
2023-05-25 21:51:05 +02:00
|
|
|
//TODO: Make multiple adding of feed to a transaction, now throws an exception.
|
|
|
|
/*var groupRes = await CreateGroupAsync(new GroupModel() { Name = "Test" });
|
2023-05-22 19:09:01 +02:00
|
|
|
groupRes = await CreateGroupAsync(new GroupModel() { Name = "News" });
|
|
|
|
groupRes = await CreateGroupAsync(new GroupModel() { Name = "Tech" });
|
2023-05-25 21:51:05 +02:00
|
|
|
groupRes = await CreateGroupAsync(new GroupModel() { Name = "Science" });*/
|
2023-05-27 00:32:05 +02:00
|
|
|
/*Log.Verbose("Fetching feeds...");
|
|
|
|
var groups = await GetGroupsAsync();
|
2023-05-22 15:55:21 +02:00
|
|
|
GroupModel testGroup = groups.Single(x => x.Name == "Test");
|
2023-05-27 00:32:05 +02:00
|
|
|
try
|
2023-05-25 21:51:05 +02:00
|
|
|
{
|
2023-05-27 00:32:05 +02:00
|
|
|
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;
|
|
|
|
}*/
|
2023-06-02 10:56:30 +02:00
|
|
|
/*var groups = await GetGroupsAsync();
|
2023-06-04 02:16:47 +02:00
|
|
|
CategoryModel testGroup = groups.Single(x => x.Name == "News");*/
|
2023-06-04 19:00:46 +02:00
|
|
|
|
2023-06-01 15:55:15 +02:00
|
|
|
/*await AddFeedsAsync(new[]
|
2023-05-29 18:02:24 +02:00
|
|
|
{
|
|
|
|
"https://www.nu.nl/rss/Algemeen",
|
|
|
|
"https://www.nu.nl/rss/Economie",
|
|
|
|
"https://www.nu.nl/rss/Sport",
|
|
|
|
"http://news.google.com/?output=atom"
|
2023-06-01 15:55:15 +02:00
|
|
|
}, testGroup);*/
|
2023-05-18 01:27:11 +02:00
|
|
|
}
|
2023-05-29 16:14:26 +02:00
|
|
|
}
|
2023-05-18 01:27:11 +02:00
|
|
|
}
|