Added configuration reading, redis caching basic, hopefully fixed future season issue
This commit is contained in:
@@ -12,6 +12,12 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
|
||||
<PackageReference Include="StackExchange.Redis" Version="2.8.16" />
|
||||
<PackageReference Include="TMDbLib" Version="2.2.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Update="appSettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
35
AnimeAnnouncer/Cache/TMDBCache.cs
Normal file
35
AnimeAnnouncer/Cache/TMDBCache.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System.Text.Json;
|
||||
using StackExchange.Redis;
|
||||
using TMDbLib.Utilities.Converters;
|
||||
|
||||
namespace AnimeAnnouncer.Cache;
|
||||
|
||||
public class TMDBCache
|
||||
{
|
||||
private readonly ConnectionMultiplexer redis;
|
||||
|
||||
public TimeSpan CacheExpiration { get; set; }
|
||||
|
||||
|
||||
public TMDBCache(String redisConnectionString)
|
||||
{
|
||||
CacheExpiration = TimeSpan.FromDays(3);
|
||||
redis = ConnectionMultiplexer.Connect(redisConnectionString);
|
||||
}
|
||||
public async Task SetCacheItem(String key, TMDBCacheItem item)
|
||||
{
|
||||
var db = redis.GetDatabase();
|
||||
String jsonItem = JsonSerializer.Serialize(item);
|
||||
db.StringSetAsync(key, jsonItem, CacheExpiration);
|
||||
}
|
||||
public async Task<TMDBCacheItem> GetCacheItem(String key)
|
||||
{
|
||||
var db = redis.GetDatabase();
|
||||
String jsonItem = await db.StringGetAsync(key);
|
||||
|
||||
if(!String.IsNullOrEmpty(jsonItem))
|
||||
return JsonSerializer.Deserialize<TMDBCacheItem>(jsonItem);
|
||||
else
|
||||
return null;
|
||||
}
|
||||
}
|
||||
12
AnimeAnnouncer/Cache/TMDBCacheItem.cs
Normal file
12
AnimeAnnouncer/Cache/TMDBCacheItem.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
|
||||
namespace AnimeAnnouncer.Cache
|
||||
{
|
||||
public class TMDBCacheItem
|
||||
{
|
||||
public required String Title { get; set; }
|
||||
public int ShowID { get; set; }
|
||||
|
||||
public int LatestSeasonNumber { get; set; }
|
||||
public int LastEpisodeNumber { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using AnimeAnnouncer.RSS;
|
||||
using AnimeAnnouncer.Cache;
|
||||
using AnimeAnnouncer.RSS;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using TMDbLib.Client;
|
||||
|
||||
namespace AnimeAnnouncer
|
||||
@@ -7,13 +9,33 @@ namespace AnimeAnnouncer
|
||||
{
|
||||
private static NyaaIndexer nyaaIndexer= new NyaaIndexer();
|
||||
|
||||
private const String TMDBApiKey = "";
|
||||
|
||||
private static TMDbClient tmdbClient = new TMDbClient(TMDBApiKey);
|
||||
private static TMDbClient tmdbClient;
|
||||
private static TMDBCache tmdbCache;
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Starting...");
|
||||
|
||||
var configurationBuilder = new ConfigurationBuilder();
|
||||
|
||||
configurationBuilder.SetBasePath(System.IO.Directory.GetCurrentDirectory()); // errors here
|
||||
configurationBuilder.AddJsonFile(path: "appSettings.json", optional: false, reloadOnChange: true); // errors here
|
||||
|
||||
var config = configurationBuilder.Build();
|
||||
|
||||
String TMDBApiKey = config["TMDBApiKey"];
|
||||
if(String.IsNullOrEmpty(TMDBApiKey))
|
||||
{
|
||||
Console.WriteLine("ERROR: API key not found in config.");
|
||||
return;
|
||||
}
|
||||
tmdbClient = new TMDbClient(TMDBApiKey);
|
||||
|
||||
String redisCacheConnectionString = config["redisConnectionString"];
|
||||
if(!String.IsNullOrEmpty(redisCacheConnectionString))
|
||||
{
|
||||
tmdbCache = new TMDBCache(redisCacheConnectionString);
|
||||
}
|
||||
|
||||
nyaaIndexer.NewPost += OnNewPost;
|
||||
nyaaIndexer.RssReadFinished += OnRssReadFinished;
|
||||
nyaaIndexer.Start(true);
|
||||
@@ -61,7 +83,13 @@ namespace AnimeAnnouncer
|
||||
|
||||
var showResult = await tmdbClient.GetTvShowAsync(supposedShowId);
|
||||
|
||||
var latestSeason = showResult.Seasons.OrderByDescending(s => s.SeasonNumber).First();
|
||||
var latestSeason = showResult.Seasons.Where(s => s.AirDate.HasValue && s.AirDate.Value < DateTime.Now).OrderByDescending(s => s.SeasonNumber).FirstOrDefault();
|
||||
|
||||
if(latestSeason == null)
|
||||
{
|
||||
Console.WriteLine("Unable to find a viable season from TMDB.");
|
||||
return;
|
||||
}
|
||||
|
||||
if(latestSeason.SeasonNumber != int.Parse(season))
|
||||
{
|
||||
|
||||
4
AnimeAnnouncer/appSettings.json
Normal file
4
AnimeAnnouncer/appSettings.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"TMDBApiKey": "",
|
||||
"redisConnectionString": "localhost"
|
||||
}
|
||||
Reference in New Issue
Block a user