2017-10-01 23:41:25 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2018-05-15 01:18:26 +02:00
|
|
|
|
using Geekbot.net.Lib.GlobalSettings;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.Logger;
|
2017-10-01 23:41:25 +02:00
|
|
|
|
using MyAnimeListSharp.Auth;
|
|
|
|
|
using MyAnimeListSharp.Core;
|
|
|
|
|
using MyAnimeListSharp.Facade.Async;
|
|
|
|
|
|
2018-05-03 00:56:06 +02:00
|
|
|
|
namespace Geekbot.net.Lib.Clients
|
2017-10-01 23:41:25 +02:00
|
|
|
|
{
|
|
|
|
|
public class MalClient : IMalClient
|
|
|
|
|
{
|
2018-05-15 01:18:26 +02:00
|
|
|
|
private readonly IGlobalSettings _globalSettings;
|
2018-01-20 01:38:49 +01:00
|
|
|
|
private readonly IGeekbotLogger _logger;
|
2017-10-01 23:41:25 +02:00
|
|
|
|
private ICredentialContext _credentials;
|
|
|
|
|
private AnimeSearchMethodsAsync _animeSearch;
|
|
|
|
|
private MangaSearchMethodsAsync _mangaSearch;
|
|
|
|
|
|
2018-05-15 01:18:26 +02:00
|
|
|
|
public MalClient(IGlobalSettings globalSettings, IGeekbotLogger logger)
|
2017-10-01 23:41:25 +02:00
|
|
|
|
{
|
2018-05-15 01:18:26 +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
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public bool ReloadClient()
|
2017-10-01 23:41:25 +02:00
|
|
|
|
{
|
2018-05-15 01:18:26 +02:00
|
|
|
|
var malCredentials = _globalSettings.GetKey("MalCredentials");
|
|
|
|
|
if (!string.IsNullOrEmpty(malCredentials))
|
2017-10-01 23:41:25 +02:00
|
|
|
|
{
|
2018-05-15 01:18:26 +02:00
|
|
|
|
var credSplit = malCredentials.Split('|');
|
|
|
|
|
_credentials = new CredentialContext()
|
2017-10-01 23:41:25 +02:00
|
|
|
|
{
|
2018-05-15 01:18:26 +02:00
|
|
|
|
UserName = credSplit[0],
|
|
|
|
|
Password = credSplit[1]
|
|
|
|
|
};
|
2017-10-01 23:41:25 +02:00
|
|
|
|
_animeSearch = new AnimeSearchMethodsAsync(_credentials);
|
|
|
|
|
_mangaSearch = new MangaSearchMethodsAsync(_credentials);
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Debug(LogSource.Geekbot, "Logged in to MAL");
|
2017-10-01 23:41:25 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_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];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|