2023-05-12 23:58:49 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using CodeHollow.FeedReader;
|
|
|
|
using Serilog;
|
|
|
|
using ToolQit.Extensions;
|
|
|
|
|
|
|
|
namespace SharpRss
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Global memory feed cache.
|
|
|
|
/// </summary>
|
|
|
|
public static class FeedCache
|
|
|
|
{
|
|
|
|
private static readonly Dictionary<string, Feed> CachedFeeds = new Dictionary<string, Feed>();
|
|
|
|
public static async Task<Feed> GetFeed(string urlKey)
|
|
|
|
{
|
2023-05-15 15:53:08 +02:00
|
|
|
Log.Verbose("Fetching feed: {UrlKey}", urlKey);
|
2023-05-12 23:58:49 +02:00
|
|
|
if (urlKey.IsNullEmptyWhiteSpace())
|
|
|
|
{
|
|
|
|
Log.Error("RSS Url is empty!");
|
|
|
|
return new Feed();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CachedFeeds.TryGetValue(urlKey, out Feed? fModel))
|
|
|
|
return fModel;
|
|
|
|
string feedUrl;
|
|
|
|
var urls = await FeedReader.GetFeedUrlsFromUrlAsync(urlKey);
|
|
|
|
if (!urls.Any())
|
|
|
|
feedUrl = urlKey;
|
|
|
|
else
|
|
|
|
feedUrl = urls.First().Url;
|
|
|
|
|
|
|
|
Feed feed = await FeedReader.ReadAsync(feedUrl);
|
2023-05-18 20:15:31 +02:00
|
|
|
if (feed != null)
|
|
|
|
{
|
2023-05-12 23:58:49 +02:00
|
|
|
Log.Warning("Could not get feed: {FeedUrl}", feedUrl);
|
2023-05-18 20:15:31 +02:00
|
|
|
CachedFeeds[feedUrl] = feed;
|
|
|
|
}
|
2023-05-12 23:58:49 +02:00
|
|
|
return feed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|