diff --git a/AnimeAnnouncer/Cache/TMDBCacheItem.cs b/AnimeAnnouncer/Cache/TMDBCacheItem.cs index f9248b4..0ef9b4f 100644 --- a/AnimeAnnouncer/Cache/TMDBCacheItem.cs +++ b/AnimeAnnouncer/Cache/TMDBCacheItem.cs @@ -13,5 +13,6 @@ namespace AnimeAnnouncer.Cache public string? LatestSeasonPosterPath { get; internal set; } public string? LatestSeasonOverview { get; internal set; } public double VoteAverage { get; internal set; } + public string? Overview { get; internal set; } } } \ No newline at end of file diff --git a/AnimeAnnouncer/Program.cs b/AnimeAnnouncer/Program.cs index 0bdbfa2..3819a9a 100644 --- a/AnimeAnnouncer/Program.cs +++ b/AnimeAnnouncer/Program.cs @@ -93,24 +93,23 @@ namespace AnimeAnnouncer int latestSeasonNumber = 0, latestEpisodeNumber = 0, supposedShowId = 0; - if(tmdbCache != null) + TMDBCacheItem? cachedShow = null; + + try { - try + cachedShow = await tmdbCache.GetCacheItem($"ShowCache-{title}"); + if(cachedShow != null) { - 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()); + 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 @@ -137,18 +136,20 @@ namespace AnimeAnnouncer { try { - _ = tmdbCache.SetCacheItem($"ShowCache-{title}", new TMDBCacheItem() + cachedShow = new TMDBCacheItem() { Title = title, PosterPath = showResult.PosterPath.Length > 0 ? $"https://image.tmdb.org/t/p/original{showResult.PosterPath}" : String.Empty, BackdropPath = showResult.BackdropPath.Length > 0 ? $"https://image.tmdb.org/t/p/original{showResult.BackdropPath}" : String.Empty, LatestSeasonPosterPath = latestSeason.PosterPath.Length > 0 ? $"https://image.tmdb.org/t/p/original{latestSeason.PosterPath}" : String.Empty, LatestSeasonOverview = latestSeason.Overview, + Overview = showResult.Overview, VoteAverage = showResult.VoteAverage, ShowID = supposedShowId, LatestSeasonNumber = latestSeason.SeasonNumber, LastEpisodeNumber = latestSeason.EpisodeCount - }); + }; + _ = tmdbCache.SetCacheItem($"ShowCache-{title}", cachedShow); Console.WriteLine($"{title} Added to cache"); } catch(Exception ex) @@ -182,11 +183,11 @@ namespace AnimeAnnouncer { _ = tmdbCache.SetPair($"ShowAnnounced-{supposedShowId}", "1"); } - if(mastodonClient != null) + if(mastodonClient != null && cachedShow != null) { try { - _ = mastodonClient.PublishStatus($"

Finished Dubbed Airing Anime

{title} has finished airing! Season {season} Episode {episode} is now available for download. ", Visibility.Public); + PostStatus(cachedShow); } catch(Exception ex) @@ -197,5 +198,28 @@ namespace AnimeAnnouncer } Console.WriteLine($"Choosing {title} as a finished season!"); } + + private static async void PostStatus(TMDBCacheItem cachedShow) + { + //get poster + String? posterUrl = cachedShow.LatestSeasonPosterPath ?? cachedShow.PosterPath; + Mastonet.Entities.Attachment? attachment = null; + if(posterUrl != null) + { + HttpClient httpClient = new HttpClient(); + byte[] data = await httpClient.GetByteArrayAsync(posterUrl); + MediaDefinition postMedia = new MediaDefinition(new MemoryStream(data), $"{cachedShow.Title.Replace(" ", "_")}.png"); + attachment = await mastodonClient.UploadMedia(postMedia); + } + //Prepare Status + String statusText = $"{cachedShow.Title} has finished airing season {cachedShow.LatestSeasonNumber}! Episode {cachedShow.LastEpisodeNumber} is now available for download. {Environment.NewLine + Environment.NewLine}Overview: {cachedShow.LatestSeasonOverview ?? cachedShow.Overview}"; + if(statusText.Length >= 500) + { + statusText = statusText[..497] + ".."; + } + _ = mastodonClient.PublishStatus(statusText, + Visibility.Public, mediaIds: attachment != null ? new String[] {attachment.Id} : null); + + } } }