55 lines
1.8 KiB
C#
55 lines
1.8 KiB
C#
|
|
using Mastonet.Entities;
|
|
|
|
namespace AnimeAnnouncer;
|
|
using Kavita.AniListNet; // Contains base classes for interacting with the API
|
|
using Kavita.AniListNet.Parameters; // Contains classes for mutation and filtering
|
|
using Kavita.AniListNet.Objects; // Contains model classes for handling data
|
|
|
|
public class Anilist
|
|
{
|
|
public String Token { get; set; }
|
|
private AniClient client;
|
|
|
|
|
|
public Anilist(String token)
|
|
{
|
|
Token = token;
|
|
client = new AniClient();
|
|
Task.WaitAll(Authenticate());
|
|
}
|
|
|
|
public async Task<Media?> RetrieveAiringShowInformation(String showName)
|
|
{
|
|
var results = await client.SearchMediaAsync(new SearchMediaFilter
|
|
{
|
|
Query = showName, // The term to search for
|
|
Type = MediaType.Anime, // Filters search results to anime only
|
|
Sort = MediaSort.Popularity, // Sorts them by popularity
|
|
Format = new Dictionary<MediaFormat, bool>
|
|
{
|
|
{ MediaFormat.TV, true }, // Set to only search for TV shows and movies
|
|
{ MediaFormat.Movie, true },
|
|
{ MediaFormat.TVShort, false } // Set to not show TV shorts
|
|
}
|
|
});
|
|
int currentYear = DateTime.Now.Year;
|
|
var result = results.Data.Where(p => p.SeasonYear >= currentYear).OrderByDescending(p => p.StartDate.ToDateTime()).ThenByDescending(p => p.Id).FirstOrDefault();
|
|
return result;
|
|
}
|
|
|
|
public async Task<List<String>> GetMediaTags(int mediaId)
|
|
{
|
|
var results = await client.GetMediaTagsAsync(mediaId);
|
|
return results.Select(p => p.Name).ToList();
|
|
}
|
|
|
|
private async Task Authenticate()
|
|
{
|
|
Boolean success = await client.TryAuthenticateAsync(Token);
|
|
if (!success)
|
|
throw new Exception("Unable to authenticate with AniList!");
|
|
}
|
|
}
|
|
|