Added PostStatus method and attempted to attach posters for anime

This commit is contained in:
2024-09-27 17:08:58 -04:00
parent a10f8c9c2f
commit 9ffe32447a
2 changed files with 43 additions and 18 deletions

View File

@@ -13,5 +13,6 @@ namespace AnimeAnnouncer.Cache
public string? LatestSeasonPosterPath { get; internal set; } public string? LatestSeasonPosterPath { get; internal set; }
public string? LatestSeasonOverview { get; internal set; } public string? LatestSeasonOverview { get; internal set; }
public double VoteAverage { get; internal set; } public double VoteAverage { get; internal set; }
public string? Overview { get; internal set; }
} }
} }

View File

@@ -93,24 +93,23 @@ namespace AnimeAnnouncer
int latestSeasonNumber = 0, latestEpisodeNumber = 0, supposedShowId = 0; 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}"); Console.WriteLine("Cache hit");
if(cachedShow != null) latestSeasonNumber = cachedShow.LatestSeasonNumber;
{ latestEpisodeNumber = cachedShow.LastEpisodeNumber;
Console.WriteLine("Cache hit"); supposedShowId = cachedShow.ShowID;
latestSeasonNumber = cachedShow.LatestSeasonNumber;
latestEpisodeNumber = cachedShow.LastEpisodeNumber;
supposedShowId = cachedShow.ShowID;
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
} }
} }
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
if(supposedShowId == 0) if(supposedShowId == 0)
{ //no cache hit { //no cache hit
@@ -137,18 +136,20 @@ namespace AnimeAnnouncer
{ {
try try
{ {
_ = tmdbCache.SetCacheItem($"ShowCache-{title}", new TMDBCacheItem() cachedShow = new TMDBCacheItem()
{ {
Title = title, Title = title,
PosterPath = showResult.PosterPath.Length > 0 ? $"https://image.tmdb.org/t/p/original{showResult.PosterPath}" : String.Empty, 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, 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, LatestSeasonPosterPath = latestSeason.PosterPath.Length > 0 ? $"https://image.tmdb.org/t/p/original{latestSeason.PosterPath}" : String.Empty,
LatestSeasonOverview = latestSeason.Overview, LatestSeasonOverview = latestSeason.Overview,
Overview = showResult.Overview,
VoteAverage = showResult.VoteAverage, VoteAverage = showResult.VoteAverage,
ShowID = supposedShowId, ShowID = supposedShowId,
LatestSeasonNumber = latestSeason.SeasonNumber, LatestSeasonNumber = latestSeason.SeasonNumber,
LastEpisodeNumber = latestSeason.EpisodeCount LastEpisodeNumber = latestSeason.EpisodeCount
}); };
_ = tmdbCache.SetCacheItem($"ShowCache-{title}", cachedShow);
Console.WriteLine($"{title} Added to cache"); Console.WriteLine($"{title} Added to cache");
} }
catch(Exception ex) catch(Exception ex)
@@ -182,11 +183,11 @@ namespace AnimeAnnouncer
{ {
_ = tmdbCache.SetPair($"ShowAnnounced-{supposedShowId}", "1"); _ = tmdbCache.SetPair($"ShowAnnounced-{supposedShowId}", "1");
} }
if(mastodonClient != null) if(mastodonClient != null && cachedShow != null)
{ {
try try
{ {
_ = mastodonClient.PublishStatus($"<p>Finished Dubbed Airing Anime</p> {title} has finished airing! Season {season} Episode {episode} is now available for download. ", Visibility.Public); PostStatus(cachedShow);
} }
catch(Exception ex) catch(Exception ex)
@@ -197,5 +198,28 @@ namespace AnimeAnnouncer
} }
Console.WriteLine($"Choosing {title} as a finished season!"); 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);
}
} }
} }