From 5cfcecc5c3af7beba200e43f54e003fd42603bc6 Mon Sep 17 00:00:00 2001 From: chrispr Date: Tue, 25 Mar 2025 00:12:03 -0400 Subject: [PATCH] Numerous fixes to improve functionality --- AnimeAnnouncer/Cache/AiringSoonItem.cs | 1 + AnimeAnnouncer/Cache/TMDBCache.cs | 6 ++++++ AnimeAnnouncer/Program.cs | 19 ++++++++++++++----- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/AnimeAnnouncer/Cache/AiringSoonItem.cs b/AnimeAnnouncer/Cache/AiringSoonItem.cs index 2a7ac9c..3d2aa2e 100644 --- a/AnimeAnnouncer/Cache/AiringSoonItem.cs +++ b/AnimeAnnouncer/Cache/AiringSoonItem.cs @@ -6,5 +6,6 @@ namespace AnimeAnnouncer.Cache public required String Title { get; set; } public int ShowID { get; set; } public DateTime? LastAirDate { get; set; } + public int LatestEpisodeNumber { get; set; } } } \ No newline at end of file diff --git a/AnimeAnnouncer/Cache/TMDBCache.cs b/AnimeAnnouncer/Cache/TMDBCache.cs index 587a882..10e26d9 100644 --- a/AnimeAnnouncer/Cache/TMDBCache.cs +++ b/AnimeAnnouncer/Cache/TMDBCache.cs @@ -35,6 +35,12 @@ public class TMDBCache return null; } + public async Task RemoveCacheItem(String key) + { + var db =redis.GetDatabase(); + _ = await db.KeyDeleteAsync(key); + } + public async Task SetAiringSoon(List list) { var db = redis.GetDatabase(); diff --git a/AnimeAnnouncer/Program.cs b/AnimeAnnouncer/Program.cs index 78723b3..3ecda13 100644 --- a/AnimeAnnouncer/Program.cs +++ b/AnimeAnnouncer/Program.cs @@ -223,7 +223,7 @@ namespace AnimeAnnouncer _ = tmdbCache.SetCacheItem($"ShowCache-{title}", cachedShow); Console.WriteLine($"{title} Added to cache"); if(latestSeason.AirDate < DateTime.Now && (latestSeason.AirDate + TimeSpan.FromDays(180)) > DateTime.Now && - ((showResult.Status == "Ended" ? showResult.LastAirDate : showResult.NextEpisodeToAir.AirDate) + TimeSpan.FromDays(30)) > DateTime.Now) + ((showResult.Status == "Ended" || showResult.NextEpisodeToAir == null ? showResult.LastAirDate : showResult.NextEpisodeToAir.AirDate) + TimeSpan.FromDays(30)) > DateTime.Now) UpdateAiringShowList(cachedShow); } catch(Exception ex) @@ -253,7 +253,7 @@ namespace AnimeAnnouncer { //Episode_Type isn't making it with the current client, guess based on ordering var targetFinale = targetSeason.Episodes.OrderByDescending(g => g.Order).FirstOrDefault(e => e.EpisodeType.Equals("finale", StringComparison.InvariantCultureIgnoreCase)); //Confirm it's aired within the last week - if(targetFinale != null && targetEpisodeGroup.Groups.OrderByDescending(g => g.Order).FirstOrDefault() == targetSeason) + if(targetFinale != null && targetEpisodeGroup.Groups.Where(g => g.Episodes.Count != 0).OrderByDescending(g => g.Order).FirstOrDefault() == targetSeason) { //confirm the episode is marked as finale and that this season is the latest one in the episode group Console.WriteLine($"!!Choosing S{titleSeason}E{targetFinale.Order + 1} airing on {targetFinale.AirDate} for finale using episode groups"); cachedShow.LatestSeasonNumber = latestSeasonNumber = titleSeason; @@ -311,6 +311,8 @@ namespace AnimeAnnouncer else if (!finaleConfirmed) { Console.WriteLine($"Would have announced finale for {title}, on S{cachedShow.LatestSeasonNumber}E{cachedShow.LastEpisodeNumber} but the finale episode type was not found."); + //Remove from cache so subsequent releases cause a fresh lookup + await tmdbCache.RemoveCacheItem($"ShowCache-{title}"); return; } if(mastodonClient != null && cachedShow != null) @@ -336,7 +338,7 @@ namespace AnimeAnnouncer DateTime beginningOfMonth = DateTime.Today; DateTime endOfMonth = DateTime.Today.AddMonths(1); var airingThisMonth = airingSoon.Where(p => p.LastAirDate >= beginningOfMonth && p.LastAirDate <= endOfMonth).ToList(); - String statusText = $"DUBBED anime airing this month (probably){Environment.NewLine + Environment.NewLine}"; + String statusText = $"DUBBED airing anime ending this month (probably){Environment.NewLine + Environment.NewLine}"; int cnt = 1; for(int i = 0;i < airingThisMonth.Count;i++) { @@ -416,14 +418,21 @@ namespace AnimeAnnouncer { Title = cachedShow.Title, ShowID = cachedShow.ShowID, - LastAirDate = cachedShow.LastAirDate ?? DateTime.MaxValue + LastAirDate = cachedShow.LastAirDate ?? DateTime.MaxValue, + LatestEpisodeNumber = cachedShow.LatestEpisodeNumber ?? 0 }; - if(!airingSoonList.Any(s => s.ShowID == cachedShow.ShowID)) + var existingAiringItem = airingSoonList.FirstOrDefault(s => s.ShowID == cachedShow.ShowID); + if(existingAiringItem == null) { airingSoonList.Add(airingSoonItem); } else { + if(existingAiringItem.LatestEpisodeNumber >= cachedShow.LatestEpisodeNumber) + { + Console.WriteLine($"Not updating {cachedShow.Title} airdate due to episode number not incrementing"); + return; + } airingSoonList.RemoveAll(s => s.ShowID == cachedShow.ShowID); airingSoonList.Add(airingSoonItem); }