mirror of
https://github.com/hmaxnl/SharpRSS.git
synced 2024-11-09 23:44:20 +01:00
42 lines
1.2 KiB
C#
42 lines
1.2 KiB
C#
|
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)
|
||
|
{
|
||
|
Log.Verbose("Request for: {UrlKey}", urlKey);
|
||
|
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);
|
||
|
if (feed == null)
|
||
|
Log.Warning("Could not get feed: {FeedUrl}", feedUrl);
|
||
|
CachedFeeds.Add(urlKey, feed);
|
||
|
return feed;
|
||
|
}
|
||
|
}
|
||
|
}
|