Files
AnimeAnnouncer/AnimeAnnouncer/Program.cs

113 lines
4.1 KiB
C#

using AnimeAnnouncer.Cache;
using AnimeAnnouncer.RSS;
using Microsoft.Extensions.Configuration;
using TMDbLib.Client;
namespace AnimeAnnouncer
{
internal class Program
{
private static NyaaIndexer nyaaIndexer= new NyaaIndexer();
private static TMDbClient tmdbClient;
private static TMDBCache tmdbCache;
static void Main(string[] args)
{
Console.WriteLine("Starting...");
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.SetBasePath(System.IO.Directory.GetCurrentDirectory()); // errors here
configurationBuilder.AddJsonFile(path: "appSettings.json", optional: false, reloadOnChange: true); // errors here
var config = configurationBuilder.Build();
String TMDBApiKey = config["TMDBApiKey"];
if(String.IsNullOrEmpty(TMDBApiKey))
{
Console.WriteLine("ERROR: API key not found in config.");
return;
}
tmdbClient = new TMDbClient(TMDBApiKey);
String redisCacheConnectionString = config["redisConnectionString"];
if(!String.IsNullOrEmpty(redisCacheConnectionString))
{
tmdbCache = new TMDBCache(redisCacheConnectionString);
}
nyaaIndexer.NewPost += OnNewPost;
nyaaIndexer.RssReadFinished += OnRssReadFinished;
nyaaIndexer.Start(true);
Console.WriteLine("Press enter to quit...");
Console.ReadLine();
}
static async void OnNewPost(RSS.NyaaItem item)
{
try
{
ProcessNewItem(item).Wait();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
static void OnRssReadFinished()
{
}
static async Task ProcessNewItem(NyaaItem item)
{
Console.WriteLine($"Processing {item.Title}");
//Parse release name using Anitomy library
var items = AnitomySharp.AnitomySharp.Parse(item.Title);
var title = items.FirstOrDefault(p => p.Category == AnitomySharp.Element.ElementCategory.ElementAnimeTitle)?.Value;
var episodeTitle = items.FirstOrDefault(p => p.Category == AnitomySharp.Element.ElementCategory.ElementEpisodeTitle)?.Value;
var season = items.FirstOrDefault(p => p.Category == AnitomySharp.Element.ElementCategory.ElementAnimeSeason)?.Value;
var episode = items.FirstOrDefault(p => p.Category == AnitomySharp.Element.ElementCategory.ElementEpisodeNumber)?.Value;
if(title == null || season == null || episode == null)
{
Console.WriteLine("Skipping release due to inability to determine title, season, or episode number.");
return;
}
var searchResults = await tmdbClient.SearchTvShowAsync(title);
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)
{
Console.WriteLine("Unable to find a viable season from TMDB.");
return;
}
if(latestSeason.SeasonNumber != int.Parse(season))
{
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}");
return;
}
//
//AnnounceFinishedSeason(title);
Console.WriteLine($"Choosing {title} as a finished season!");
}
}
}