From 7ef0b6a3195a379f06ca7f33056131dbd192aea5 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 24 Sep 2020 12:21:21 +0200 Subject: [PATCH] Swap the my anime list wrapper for the !anime and !manga commands --- src/Bot/Bot.csproj | 2 +- src/Bot/Commands/Integrations/Mal.cs | 116 +++++++++++---------------- src/Bot/Program.cs | 3 - src/Core/Core.csproj | 1 - src/Core/MalClient/IMalClient.cs | 12 --- src/Core/MalClient/MalClient.cs | 63 --------------- 6 files changed, 47 insertions(+), 150 deletions(-) delete mode 100644 src/Core/MalClient/IMalClient.cs delete mode 100644 src/Core/MalClient/MalClient.cs diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj index 60f6473..4cd5b39 100644 --- a/src/Bot/Bot.csproj +++ b/src/Bot/Bot.csproj @@ -24,8 +24,8 @@ + - diff --git a/src/Bot/Commands/Integrations/Mal.cs b/src/Bot/Commands/Integrations/Mal.cs index ae30493..ab8c021 100644 --- a/src/Bot/Commands/Integrations/Mal.cs +++ b/src/Bot/Commands/Integrations/Mal.cs @@ -1,23 +1,23 @@ using System; +using System.Linq; using System.Threading.Tasks; using System.Web; -using System.Xml; using Discord; using Discord.Commands; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; -using Geekbot.Core.MalClient; +using JikanDotNet; namespace Geekbot.Bot.Commands.Integrations { public class Mal : ModuleBase { private readonly IErrorHandler _errorHandler; - private readonly IMalClient _malClient; + private readonly IJikan _client; - public Mal(IMalClient malClient, IErrorHandler errorHandler) + public Mal(IErrorHandler errorHandler) { - _malClient = malClient; + _client = new Jikan(); _errorHandler = errorHandler; } @@ -27,46 +27,34 @@ namespace Geekbot.Bot.Commands.Integrations { try { - if (_malClient.IsLoggedIn()) + var results = await _client.SearchAnime(animeName); + var anime = results.Results.FirstOrDefault(); + if (anime != null) { - var anime = await _malClient.GetAnime(animeName); - if (anime != null) - { - var eb = new EmbedBuilder(); + var eb = new EmbedBuilder(); - var description = HttpUtility.HtmlDecode(anime.Synopsis) - .Replace("
", "") - .Replace("[i]", "*") - .Replace("[/i]", "*"); + var description = HttpUtility.HtmlDecode(anime.Description) + .Replace("
", "") + .Replace("[i]", "*") + .Replace("[/i]", "*"); - eb.Title = anime.Title; - eb.Description = description; - eb.ImageUrl = anime.Image; - eb.AddInlineField("Premiered", $"{anime.StartDate}"); - eb.AddInlineField("Ended", anime.EndDate == "0000-00-00" ? "???" : anime.EndDate); - eb.AddInlineField("Status", anime.Status); - eb.AddInlineField("Episodes", anime.Episodes); - eb.AddInlineField("MAL Score", anime.Score); - eb.AddInlineField("Type", anime.Type); - eb.AddField("MAL Link", $"https://myanimelist.net/anime/{anime.Id}"); + eb.Title = anime.Title; + eb.Description = description; + eb.ImageUrl = anime.ImageURL; + eb.AddInlineField("Premiered", $"{anime.StartDate.Value.ToShortDateString()}"); + eb.AddInlineField("Ended", anime.Airing ? "Present" : anime.EndDate.Value.ToShortDateString()); + eb.AddInlineField("Episodes", anime.Episodes); + eb.AddInlineField("MAL Score", anime.Score); + eb.AddInlineField("Type", anime.Type); + eb.AddField("MAL Link", $"https://myanimelist.net/anime/{anime.MalId}"); - await ReplyAsync("", false, eb.Build()); - } - else - { - await ReplyAsync("No anime found with that name..."); - } + await ReplyAsync("", false, eb.Build()); } else { - await ReplyAsync( - "Unfortunally i'm not connected to MyAnimeList.net, please tell my senpai to connect me"); + await ReplyAsync("No anime found with that name..."); } } - catch (XmlException e) - { - await _errorHandler.HandleCommandException(e, Context, "The MyAnimeList.net API refused to answer"); - } catch (Exception e) { await _errorHandler.HandleCommandException(e, Context); @@ -79,46 +67,34 @@ namespace Geekbot.Bot.Commands.Integrations { try { - if (_malClient.IsLoggedIn()) + var results = await _client.SearchManga(mangaName); + var manga = results.Results.FirstOrDefault(); + if (manga != null) { - var manga = await _malClient.GetManga(mangaName); - if (manga != null) - { - var eb = new EmbedBuilder(); - - var description = HttpUtility.HtmlDecode(manga.Synopsis) - .Replace("
", "") - .Replace("[i]", "*") - .Replace("[/i]", "*"); - - eb.Title = manga.Title; - eb.Description = description; - eb.ImageUrl = manga.Image; - eb.AddInlineField("Premiered", $"{manga.StartDate}"); - eb.AddInlineField("Ended", manga.EndDate == "0000-00-00" ? "???" : manga.EndDate); - eb.AddInlineField("Status", manga.Status); - eb.AddInlineField("Volumes", manga.Volumes); - eb.AddInlineField("Chapters", manga.Chapters); - eb.AddInlineField("MAL Score", manga.Score); - eb.AddField("MAL Link", $"https://myanimelist.net/manga/{manga.Id}"); - - await ReplyAsync("", false, eb.Build()); - } - else - { - await ReplyAsync("No manga found with that name..."); - } + var eb = new EmbedBuilder(); + + var description = HttpUtility.HtmlDecode(manga.Description) + .Replace("
", "") + .Replace("[i]", "*") + .Replace("[/i]", "*"); + + eb.Title = manga.Title; + eb.Description = description; + eb.ImageUrl = manga.ImageURL; + eb.AddInlineField("Premiered", $"{manga.StartDate.Value.ToShortDateString()}"); + eb.AddInlineField("Ended", manga.Publishing ? "Present" : manga.EndDate.Value.ToShortDateString()); + eb.AddInlineField("Volumes", manga.Volumes); + eb.AddInlineField("Chapters", manga.Chapters); + eb.AddInlineField("MAL Score", manga.Score); + eb.AddField("MAL Link", $"https://myanimelist.net/manga/{manga.MalId}"); + + await ReplyAsync("", false, eb.Build()); } else { - await ReplyAsync( - "Unfortunally i'm not connected to MyAnimeList.net, please tell my senpai to connect me"); + await ReplyAsync("No manga found with that name..."); } } - catch (XmlException e) - { - await _errorHandler.HandleCommandException(e, Context, "The MyAnimeList.net API refused to answer"); - } catch (Exception e) { await _errorHandler.HandleCommandException(e, Context); diff --git a/src/Bot/Program.cs b/src/Bot/Program.cs index 5994f8c..dee6ec3 100644 --- a/src/Bot/Program.cs +++ b/src/Bot/Program.cs @@ -18,7 +18,6 @@ using Geekbot.Core.Highscores; using Geekbot.Core.KvInMemoryStore; using Geekbot.Core.Levels; using Geekbot.Core.Logger; -using Geekbot.Core.MalClient; using Geekbot.Core.Media; using Geekbot.Core.RandomNumberGenerator; using Geekbot.Core.ReactionListener; @@ -159,7 +158,6 @@ namespace Geekbot.Bot _reactionListener = new ReactionListener(_databaseInitializer.Initialize()); _guildSettingsManager = new GuildSettingsManager(_databaseInitializer.Initialize()); var fortunes = new FortunesProvider(_logger); - var malClient = new MalClient(_globalSettings, _logger); var levelCalc = new LevelCalc(); var emojiConverter = new EmojiConverter(); var mtgManaConverter = new MtgManaConverter(); @@ -176,7 +174,6 @@ namespace Geekbot.Bot services.AddSingleton(emojiConverter); services.AddSingleton(fortunes); services.AddSingleton(mediaProvider); - services.AddSingleton(malClient); services.AddSingleton(mtgManaConverter); services.AddSingleton(wikipediaClient); services.AddSingleton(randomNumberGenerator); diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 0b3ff44..780d0e2 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -22,7 +22,6 @@ - diff --git a/src/Core/MalClient/IMalClient.cs b/src/Core/MalClient/IMalClient.cs deleted file mode 100644 index 70851c1..0000000 --- a/src/Core/MalClient/IMalClient.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Threading.Tasks; -using MyAnimeListSharp.Core; - -namespace Geekbot.Core.MalClient -{ - public interface IMalClient - { - bool IsLoggedIn(); - Task GetAnime(string query); - Task GetManga(string query); - } -} \ No newline at end of file diff --git a/src/Core/MalClient/MalClient.cs b/src/Core/MalClient/MalClient.cs deleted file mode 100644 index 3f5f165..0000000 --- a/src/Core/MalClient/MalClient.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System.Threading.Tasks; -using Geekbot.Core.GlobalSettings; -using Geekbot.Core.Logger; -using MyAnimeListSharp.Auth; -using MyAnimeListSharp.Core; -using MyAnimeListSharp.Facade.Async; - -namespace Geekbot.Core.MalClient -{ - public class MalClient : IMalClient - { - private readonly IGlobalSettings _globalSettings; - private readonly IGeekbotLogger _logger; - private ICredentialContext _credentials; - private AnimeSearchMethodsAsync _animeSearch; - private MangaSearchMethodsAsync _mangaSearch; - - public MalClient(IGlobalSettings globalSettings, IGeekbotLogger logger) - { - _globalSettings = globalSettings; - _logger = logger; - ReloadClient(); - } - - private bool ReloadClient() - { - var malCredentials = _globalSettings.GetKey("MalCredentials"); - if (!string.IsNullOrEmpty(malCredentials)) - { - var credSplit = malCredentials.Split('|'); - _credentials = new CredentialContext() - { - UserName = credSplit[0], - Password = credSplit[1] - }; - _animeSearch = new AnimeSearchMethodsAsync(_credentials); - _mangaSearch = new MangaSearchMethodsAsync(_credentials); - _logger.Debug(LogSource.Geekbot, "Logged in to MAL"); - return true; - } - _logger.Debug(LogSource.Geekbot, "No MAL Credentials Set!"); - return false; - - } - - public bool IsLoggedIn() - { - return _credentials != null; - } - - public async Task GetAnime(string query) - { - var response = await _animeSearch.SearchDeserializedAsync(query); - return response.Entries.Count == 0 ? null : response.Entries[0]; - } - - public async Task GetManga(string query) - { - var response = await _mangaSearch.SearchDeserializedAsync(query); - return response.Entries.Count == 0 ? null : response.Entries[0]; - } - } -} \ No newline at end of file