geekbot/Geekbot.net/Lib/Clients/MalClient.cs

63 lines
2.2 KiB
C#
Raw Normal View History

2017-10-01 23:41:25 +02:00
using System.Threading.Tasks;
using Geekbot.net.Lib.GlobalSettings;
using Geekbot.net.Lib.Logger;
2017-10-01 23:41:25 +02:00
using MyAnimeListSharp.Auth;
using MyAnimeListSharp.Core;
using MyAnimeListSharp.Facade.Async;
namespace Geekbot.net.Lib.Clients
2017-10-01 23:41:25 +02:00
{
public class MalClient : IMalClient
{
private readonly IGlobalSettings _globalSettings;
private readonly IGeekbotLogger _logger;
2017-10-01 23:41:25 +02:00
private ICredentialContext _credentials;
private AnimeSearchMethodsAsync _animeSearch;
private MangaSearchMethodsAsync _mangaSearch;
public MalClient(IGlobalSettings globalSettings, IGeekbotLogger logger)
2017-10-01 23:41:25 +02:00
{
_globalSettings = globalSettings;
2017-10-01 23:41:25 +02:00
_logger = logger;
2018-04-30 23:44:19 +02:00
ReloadClient();
2017-10-01 23:41:25 +02:00
}
private bool ReloadClient()
2017-10-01 23:41:25 +02:00
{
var malCredentials = _globalSettings.GetKey("MalCredentials");
if (!string.IsNullOrEmpty(malCredentials))
2017-10-01 23:41:25 +02:00
{
var credSplit = malCredentials.Split('|');
_credentials = new CredentialContext()
2017-10-01 23:41:25 +02:00
{
UserName = credSplit[0],
Password = credSplit[1]
};
2017-10-01 23:41:25 +02:00
_animeSearch = new AnimeSearchMethodsAsync(_credentials);
_mangaSearch = new MangaSearchMethodsAsync(_credentials);
_logger.Debug(LogSource.Geekbot, "Logged in to MAL");
2017-10-01 23:41:25 +02:00
return true;
}
_logger.Debug(LogSource.Geekbot, "No MAL Credentials Set!");
2017-10-01 23:41:25 +02:00
return false;
}
2018-04-30 23:44:19 +02:00
public bool IsLoggedIn()
2017-10-01 23:41:25 +02:00
{
return _credentials != null;
}
2018-04-30 23:44:19 +02:00
public async Task<AnimeEntry> GetAnime(string query)
2017-10-01 23:41:25 +02:00
{
var response = await _animeSearch.SearchDeserializedAsync(query);
return response.Entries.Count == 0 ? null : response.Entries[0];
}
2018-04-30 23:44:19 +02:00
public async Task<MangaEntry> GetManga(string query)
2017-10-01 23:41:25 +02:00
{
var response = await _mangaSearch.SearchDeserializedAsync(query);
return response.Entries.Count == 0 ? null : response.Entries[0];
}
}
}