mirror of
https://github.com/hmaxnl/SharpRSS.git
synced 2024-09-20 01:54:20 +02:00
139 lines
5.5 KiB
C#
139 lines
5.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
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 : IDisposable
|
|
{
|
|
public RssService()
|
|
{
|
|
SetupTestGroupsAndFeedsAsync();
|
|
}
|
|
|
|
public async Task<HashSet<object>> GetGroupsFeedsAsync()
|
|
{
|
|
HashSet<object> items = new HashSet<object>();
|
|
items.UnionWith(await GetGroupsAsync());
|
|
items.UnionWith(await GetUngroupedFeedsAsync());
|
|
return items;
|
|
}
|
|
public async Task<bool> CreateGroupAsync(GroupModel group) => await DbAccess.SetGroupAsync(group);
|
|
public async Task<HashSet<GroupModel>> GetGroupsAsync() => await DbAccess.GetGroupsAsync();
|
|
|
|
//TODO: Need to rework this implementation!!!
|
|
public async Task<bool> AddFeedsAsync(string[]? rssUrls, GroupModel? group = null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public async Task UpdateFeeds()
|
|
{
|
|
Log.Verbose("Fetching feeds...");
|
|
var feeds = await GetFeedsAsync();
|
|
}
|
|
|
|
public async Task<HashSet<FeedModel>> GetFeedsAsync(string? groupId = null) => await DbAccess.GetFeedsAsync(groupId);
|
|
public async Task<HashSet<FeedModel>> GetUngroupedFeedsAsync() => await DbAccess.GetFeedsAsync("");
|
|
|
|
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 async Task<int> AddFeedItems(IList<FeedItem> items, FeedModel feedModel)
|
|
{
|
|
int result = 0;
|
|
if (!items.Any())
|
|
return result;
|
|
HashSet<FeedItemModel> itemModels = new HashSet<FeedItemModel>();
|
|
// TODO: implement!!!
|
|
result = await DbAccess.SetFeedItemsAsync(itemModels);
|
|
return result;
|
|
}*/
|
|
|
|
/*private IList<FeedItemModel> ConstructFeedItems(IList<FeedItem> items, FeedModel feed)
|
|
{
|
|
IList<FeedItemModel> itemModels = new List<FeedItemModel>();
|
|
if (!items.Any())
|
|
return itemModels;
|
|
foreach (FeedItem item in items)
|
|
{
|
|
itemModels.Add(new FeedItemModel()
|
|
{
|
|
Id = item.Id,
|
|
FeedId = feed.Id,
|
|
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
|
|
});
|
|
}
|
|
return itemModels;
|
|
}*/
|
|
|
|
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()
|
|
{
|
|
//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();
|
|
GroupModel 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);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
}
|
|
}
|
|
} |