Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cffecbb469 | ||
|
|
a1ec0ea7f5 |
@@ -43,7 +43,7 @@ public class IndexModel : PageModel
|
||||
TMDBCacheItem? cacheItem = null;
|
||||
try
|
||||
{
|
||||
cacheItem = await _tmdbCache.GetCacheItem($"ShowCache-{airingItem.Title}");
|
||||
cacheItem = await _tmdbCache.GetCacheItem<TMDBCacheItem>($"ShowCache-{airingItem.Title}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -68,7 +68,7 @@ public class IndexModel : PageModel
|
||||
}).ToList();
|
||||
|
||||
AiringShows = displayable
|
||||
.Where(x => x.EffectiveLastAirDate.HasValue && x.EffectiveLastAirDate.Value > DateTime.Now)
|
||||
.Where(x => x.EffectiveLastAirDate.HasValue && x.EffectiveLastAirDate.Value.Date >= DateTime.Now.Date)
|
||||
.OrderBy(x => x.EffectiveLastAirDate)
|
||||
.ThenBy(x => x.Title)
|
||||
.ToList();
|
||||
@@ -107,8 +107,8 @@ public class IndexModel : PageModel
|
||||
return CachedDetails.LatestSeasonPosterPath;
|
||||
if (!string.IsNullOrWhiteSpace(CachedDetails?.PosterPath))
|
||||
return CachedDetails.PosterPath;
|
||||
if (!string.IsNullOrWhiteSpace(CachedDetails?.AnilistPosterPath))
|
||||
return CachedDetails.AnilistPosterPath;
|
||||
//if (!string.IsNullOrWhiteSpace(CachedDetails?.AnilistPosterPath))
|
||||
// return CachedDetails.AnilistPosterPath;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
using Kavita.AniListNet.Objects;
|
||||
|
||||
namespace AnimeAnnouncer.Cache
|
||||
{
|
||||
public class AnilistCacheItem
|
||||
{
|
||||
public required String Title { get; set; }
|
||||
public int ShowID { get; set; }
|
||||
public string? EnglishTitle { get; set; }
|
||||
public string? RomajiTitle { get; set; }
|
||||
public string? NativeTitle { get; set; }
|
||||
public int? Episodes { get; set; }
|
||||
public string? Status { get; set; }
|
||||
public int? SeasonYear { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? BannerImageUrl { get; set; }
|
||||
public string? CoverImageUrl { get; set; }
|
||||
public int? AverageScore { get; set; }
|
||||
|
||||
public DateTime? Updated { get; set; }
|
||||
|
||||
public static AnilistCacheItem FromMedia(Media media)
|
||||
{
|
||||
return new AnilistCacheItem
|
||||
{
|
||||
Title = media.Title.EnglishTitle ?? media.Title.RomajiTitle,
|
||||
ShowID = media.Id,
|
||||
EnglishTitle = media.Title.EnglishTitle,
|
||||
RomajiTitle = media.Title.RomajiTitle,
|
||||
NativeTitle = media.Title.NativeTitle,
|
||||
Episodes = media.Episodes,
|
||||
Status = media.Status?.ToString(),
|
||||
SeasonYear = media.SeasonYear,
|
||||
Description = media.Description,
|
||||
BannerImageUrl = media.BannerImageUrl?.ToString(),
|
||||
CoverImageUrl = media.Cover?.ExtraLargeImageUrl?.ToString(),
|
||||
AverageScore = media.AverageScore
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,22 +17,22 @@ public class TMDBCache
|
||||
CacheExpiration = TimeSpan.FromDays(3);
|
||||
redis = ConnectionMultiplexer.Connect(redisConnectionString);
|
||||
}
|
||||
public async Task SetCacheItem(String key, TMDBCacheItem item)
|
||||
public async Task SetCacheItem<T>(String key, T item)
|
||||
{
|
||||
var db = redis.GetDatabase();
|
||||
|
||||
String jsonItem = JsonSerializer.Serialize(item);
|
||||
await db.StringSetAsync(key, jsonItem, CacheExpiration);
|
||||
}
|
||||
public async Task<TMDBCacheItem> GetCacheItem(String key)
|
||||
public async Task<T?> GetCacheItem<T>(String key)
|
||||
{
|
||||
var db = redis.GetDatabase();
|
||||
String jsonItem = await db.StringGetAsync(key);
|
||||
String? jsonItem = await db.StringGetAsync(key);
|
||||
|
||||
if (!String.IsNullOrEmpty(jsonItem))
|
||||
return JsonSerializer.Deserialize<TMDBCacheItem>(jsonItem);
|
||||
return JsonSerializer.Deserialize<T>(jsonItem);
|
||||
else
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
|
||||
public async Task RemoveCacheItem(String key)
|
||||
|
||||
@@ -21,7 +21,9 @@ namespace AnimeAnnouncer.Cache
|
||||
public int? LatestEpisodeNumber { get; set; }
|
||||
|
||||
public Boolean? AnilistPopulated { get; set; }
|
||||
public String? AnilistPosterPath { get; set; }
|
||||
|
||||
public DateTime? Updated { get; set; }
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
+27
-13
@@ -145,11 +145,14 @@ namespace AnimeAnnouncer
|
||||
DateTime? lastAirDate = null;
|
||||
|
||||
TMDBCacheItem? cachedShow = null;
|
||||
|
||||
latestEpisodeNumber = int.Parse(episode);
|
||||
|
||||
try
|
||||
{
|
||||
cachedShow = await tmdbCache.GetCacheItem($"ShowCache-{title}");
|
||||
if(cachedShow != null)
|
||||
cachedShow = await tmdbCache.GetCacheItem<TMDBCacheItem>($"ShowCache-{title}");
|
||||
|
||||
if(cachedShow != null && (cachedShow.Updated.HasValue && DateTime.Now.Subtract(cachedShow.Updated.Value).Days < 6))
|
||||
{
|
||||
Console.WriteLine("Cache hit");
|
||||
latestSeasonNumber = cachedShow.LatestSeasonNumber;
|
||||
@@ -194,7 +197,7 @@ namespace AnimeAnnouncer
|
||||
|
||||
lastEpisodeNumber = latestSeason.EpisodeCount;
|
||||
|
||||
latestEpisodeNumber = int.Parse(episode);
|
||||
|
||||
|
||||
//if nearing the end of a season, confirm it's marked with season finale
|
||||
|
||||
@@ -224,21 +227,32 @@ namespace AnimeAnnouncer
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine("Looking up Anilist title " + title);
|
||||
var anilistMedia = await anilistClient.RetrieveAiringShowInformation(title);
|
||||
if (anilistMedia != null)
|
||||
AnilistCacheItem? anilistCacheItem = await tmdbCache.GetCacheItem<AnilistCacheItem>($"AnilistCache-{title}");
|
||||
if (anilistCacheItem == null)
|
||||
{
|
||||
if (anilistMedia.Episodes.HasValue && anilistMedia.Episodes != lastEpisodeNumber)
|
||||
Console.WriteLine("Looking up Anilist title " + title);
|
||||
var anilistMedia = await anilistClient.RetrieveAiringShowInformation(title);
|
||||
if (anilistMedia != null)
|
||||
{
|
||||
anilistCacheItem = AnilistCacheItem.FromMedia(anilistMedia);
|
||||
anilistCacheItem.Updated = DateTime.Now;
|
||||
_ = tmdbCache.SetCacheItem($"AnilistCache-{title}", anilistCacheItem);
|
||||
}
|
||||
else
|
||||
Console.WriteLine("0 anilist results found");
|
||||
}
|
||||
|
||||
if (anilistCacheItem != null)
|
||||
{
|
||||
if (anilistCacheItem.Episodes.HasValue && anilistCacheItem.Episodes != lastEpisodeNumber)
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"Overriding episode count of {lastEpisodeNumber} using Anilist's showID {anilistMedia.Id} titled {anilistMedia.Title.EnglishTitle} with {anilistMedia.Episodes} episodes.");
|
||||
lastEpisodeNumber = anilistMedia.Episodes.Value;
|
||||
$"Overriding episode count of {lastEpisodeNumber} using Anilist's showID {anilistCacheItem.ShowID} titled {anilistCacheItem.EnglishTitle} with {anilistCacheItem.Episodes} episodes.");
|
||||
lastEpisodeNumber = anilistCacheItem.Episodes.Value;
|
||||
}
|
||||
|
||||
anilistPosterPath = anilistMedia.BannerImageUrl?.ToString();
|
||||
anilistPosterPath = anilistCacheItem.BannerImageUrl;
|
||||
}
|
||||
else
|
||||
Console.WriteLine("0 anilist results found");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -276,7 +290,7 @@ namespace AnimeAnnouncer
|
||||
LastAirDate = lastAirDate,
|
||||
LatestEpisodeNumber = latestEpisodeNumber,
|
||||
AnilistPopulated = anilistPosterPath != null,
|
||||
AnilistPosterPath = anilistPosterPath
|
||||
Updated = DateTime.Now
|
||||
};
|
||||
_ = tmdbCache.SetCacheItem($"ShowCache-{title}", cachedShow);
|
||||
Console.WriteLine($"{title} Added to cache");
|
||||
|
||||
Reference in New Issue
Block a user