mirror of
https://github.com/hmaxnl/SharpRSS.git
synced 2024-11-10 07:54:20 +01:00
181 lines
7.7 KiB
C#
181 lines
7.7 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 RSS feeds and groups.
|
|
/// </summary>
|
|
public class RssService : IDisposable
|
|
{
|
|
public RssService()
|
|
{
|
|
SetupTestGroupsAndFeedsAsync();
|
|
}
|
|
|
|
public async Task<HashSet<object>> GetCategoriesFeedsAsync()
|
|
{
|
|
HashSet<object> items = new HashSet<object>();
|
|
items.UnionWith(await GetCategoriesAsync());
|
|
items.UnionWith(await GetUngroupedFeedsAsync());
|
|
return items;
|
|
}
|
|
public async Task<bool> CreateGroupAsync(CategoryModel group) => await DbAccess_Old.SetCategoryAsync(group);
|
|
public async Task<HashSet<CategoryModel>> GetCategoriesAsync() => await DbAccess_Old.GetCategoriesAsync();
|
|
|
|
//TODO: Rework this!
|
|
// Subscribe to a feed.
|
|
public async Task<bool> AddSubscriptionAsync(string url, CategoryModel? group = null)
|
|
{
|
|
/*if (!SyndicationManager.GetFeed(url, out GenericSyndicationFeed? genFeed)) return false;*/
|
|
var feed = SyndicationManager.CreateSyndication(url);
|
|
// Check if feed exists in db
|
|
FeedModel? fModel = await DbAccess_Old.GetFeedAsync(url);
|
|
// If not exists fetch feed & add.
|
|
if (fModel == null)
|
|
{
|
|
/*if (!SyndicationManager.TryGetGenericFeed(url, out GenericSyndicationFeed? genFeed)) return false;*/
|
|
/*if (genFeed == null) return false;
|
|
fModel = FromResource(genFeed.Resource);
|
|
fModel.GroupId = group?.Id;*/
|
|
// Add feed
|
|
//FeedModel? dbFeed = await DbAccess.SetFeedAsync(fModel);
|
|
// Update/fetch items
|
|
//await DbAccess.FetchFeedItemsAsync(new string[] { fModel.Url });
|
|
}
|
|
return true;
|
|
}
|
|
private static FeedModel FromResource(ISyndicationResource resource)
|
|
{
|
|
FeedModel model = new FeedModel();
|
|
switch (resource.Format)
|
|
{
|
|
case SyndicationContentFormat.Rss:
|
|
RssFeed rssFeed = (RssFeed)resource;
|
|
model.FeedType = rssFeed.Format.ToString();
|
|
model.Title = rssFeed.Channel.Title;
|
|
model.Description = rssFeed.Channel.Description;
|
|
model.Copyright = rssFeed.Channel.Copyright;
|
|
model.OriginalUrl = rssFeed.Channel.SelfLink.ToString();
|
|
model.ImageUrl = rssFeed.Channel.Image?.Url.ToString();
|
|
model.Language = rssFeed.Channel.Language?.ToString();
|
|
break;
|
|
case SyndicationContentFormat.Atom:
|
|
AtomFeed atomFeed = (AtomFeed)resource;
|
|
break;
|
|
default:
|
|
Log.Information("Feed implementation missing!");
|
|
break;
|
|
}
|
|
return model;
|
|
}
|
|
|
|
public async Task UpdateFeeds()
|
|
{
|
|
Log.Verbose("Fetching feeds...");
|
|
var feeds = await GetFeedsAsync();
|
|
}
|
|
|
|
public async Task<HashSet<FeedModel>> GetFeedsAsync(string? groupId = null) => await DbAccess_Old.GetFeedsAsync(groupId);
|
|
public async Task<HashSet<FeedModel>> GetUngroupedFeedsAsync() => await DbAccess_Old.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_Old.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()
|
|
{
|
|
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");
|
|
//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();
|
|
CategoryModel 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()
|
|
{
|
|
}
|
|
}
|
|
} |