mirror of
https://github.com/hmaxnl/SharpRSS.git
synced 2024-11-10 07:54:20 +01:00
183 lines
7.5 KiB
C#
183 lines
7.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.Design;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Argotic.Common;
|
|
using Argotic.Syndication;
|
|
using Microsoft.Data.Sqlite;
|
|
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 GetCategoriesAsync());
|
|
items.UnionWith(await GetUngroupedFeedsAsync());
|
|
return items;
|
|
}
|
|
public async Task<bool> CreateGroupAsync(CategoryModel group) => await DbAccess.SetCategoryAsync(group);
|
|
public async Task<HashSet<CategoryModel>> GetCategoriesAsync() => await DbAccess.GetCategoriesAsync();
|
|
|
|
//TODO: Rework this!
|
|
// Subscribe to a feed.
|
|
public async Task<bool> AddSubscriptionAsync(string url, CategoryModel? group = null)
|
|
{
|
|
// Check for valid feed url
|
|
bool validate = SyndicationDiscoveryUtility.UriExists(new Uri(url));
|
|
if (!validate) return false;
|
|
// Check if feed exists in db
|
|
FeedModel? fModel = await DbAccess.GetFeedAsync(url);
|
|
// If not exists fetch feed & add.
|
|
if (fModel == null)
|
|
{
|
|
GenericSyndicationFeed genFeed = GenericSyndicationFeed.Create(new Uri(url));
|
|
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.Url = 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.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();
|
|
CategoryModel testGroup = groups.Single(x => x.Name == "News");
|
|
await AddSubscriptionAsync("https://www.nu.nl/rss/Algemeen", testGroup);*/
|
|
/*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()
|
|
{
|
|
}
|
|
}
|
|
} |