Implemented redis caching; made docker build script for VS code builds

This commit is contained in:
2024-09-24 15:57:37 -04:00
parent 92d35cae2f
commit 21ea17e906
2 changed files with 73 additions and 18 deletions

View File

@@ -1,6 +1,8 @@
using AnimeAnnouncer.Cache;
using System.ComponentModel;
using AnimeAnnouncer.Cache;
using AnimeAnnouncer.RSS;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using TMDbLib.Client;
namespace AnimeAnnouncer
@@ -34,6 +36,7 @@ namespace AnimeAnnouncer
if(!String.IsNullOrEmpty(redisCacheConnectionString))
{
tmdbCache = new TMDBCache(redisCacheConnectionString);
Console.WriteLine("Using cache");
}
nyaaIndexer.NewPost += OnNewPost;
@@ -77,29 +80,79 @@ namespace AnimeAnnouncer
return;
}
var searchResults = await tmdbClient.SearchTvShowAsync(title);
int latestSeasonNumber = 0, latestEpisodeNumber = 0, supposedShowId = 0;
var supposedShowId = searchResults.Results.First().Id;
var showResult = await tmdbClient.GetTvShowAsync(supposedShowId);
var latestSeason = showResult.Seasons.Where(s => s.AirDate.HasValue && s.AirDate.Value < DateTime.Now).OrderByDescending(s => s.SeasonNumber).FirstOrDefault();
if(latestSeason == null)
if(tmdbCache != null)
{
Console.WriteLine("Unable to find a viable season from TMDB.");
try
{
var cachedShow = await tmdbCache.GetCacheItem($"ShowCache-{title}");
if(cachedShow != null)
{
Console.WriteLine("Cache hit");
latestSeasonNumber = cachedShow.LatestSeasonNumber;
latestEpisodeNumber = cachedShow.LastEpisodeNumber;
supposedShowId = cachedShow.ShowID;
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
if(supposedShowId == 0)
{ //no cache hit
var searchResults = await tmdbClient.SearchTvShowAsync(title);
supposedShowId = searchResults.Results.First().Id;
var showResult = await tmdbClient.GetTvShowAsync(supposedShowId);
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;
}
latestSeasonNumber = latestSeason.SeasonNumber;
latestEpisodeNumber = latestSeason.EpisodeCount;
if(tmdbCache != null && searchResults != null)
{
try
{
tmdbCache.SetCacheItem($"ShowCache-{title}", new TMDBCacheItem()
{
Title = title,
ShowID = supposedShowId,
LatestSeasonNumber = latestSeason.SeasonNumber,
LastEpisodeNumber = latestSeason.EpisodeCount
}).Start();
Console.WriteLine($"{title} Added to cache");
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
if(latestSeasonNumber != int.Parse(season))
{
Console.WriteLine($"Failing release due to TMDB's season number {latestSeasonNumber} not matching title season {season}");
return;
}
if(latestSeason.SeasonNumber != int.Parse(season))
if(latestEpisodeNumber != int.Parse(episode))
{
Console.WriteLine($"Failing release due to TMDB's season number {latestSeason.SeasonNumber} not matching title season {season}");
return;
}
if(latestSeason.EpisodeCount != int.Parse(episode))
{
Console.WriteLine($"Failing release due to TMDB's last episode number of {latestSeason.EpisodeCount} not matching title episode number {episode}");
Console.WriteLine($"Failing release due to TMDB's last episode number of {latestEpisodeNumber} not matching title episode number {episode}");
return;
}

2
build.sh Executable file
View File

@@ -0,0 +1,2 @@
docker build --pull --rm -f "AnimeAnnouncer/Dockerfile" -t dtr.chrispr.org/animeannouncer:latest .
docker push dtr.chrispr.org/animeannouncer:latest