2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2017-06-05 22:46:58 +00:00
|
|
|
|
using NLog;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Modules.Searches.Common;
|
2017-06-05 22:46:58 +00:00
|
|
|
|
|
2017-07-17 19:42:36 +00:00
|
|
|
|
namespace NadekoBot.Modules.Searches.Services
|
2017-06-05 22:46:58 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
public class AnimeSearchService : INService
|
2017-06-05 22:46:58 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly Logger _log;
|
2017-09-15 00:42:51 +00:00
|
|
|
|
private readonly IDataCache _cache;
|
|
|
|
|
private readonly HttpClient _http;
|
2017-06-05 22:46:58 +00:00
|
|
|
|
|
2017-09-15 00:42:51 +00:00
|
|
|
|
public AnimeSearchService(IDataCache cache)
|
2017-06-05 22:46:58 +00:00
|
|
|
|
{
|
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
2017-09-15 00:42:51 +00:00
|
|
|
|
_cache = cache;
|
|
|
|
|
_http = new HttpClient();
|
2017-06-05 22:46:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<AnimeResult> GetAnimeData(string query)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(query))
|
|
|
|
|
throw new ArgumentNullException(nameof(query));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
|
2017-06-24 23:45:11 +00:00
|
|
|
|
var link = "https://aniapi.nadekobot.me/anime/" + Uri.EscapeDataString(query.Replace("/", " "));
|
2017-09-15 00:42:51 +00:00
|
|
|
|
link = link.ToLowerInvariant();
|
|
|
|
|
var (ok, data) = await _cache.TryGetAnimeDataAsync(link).ConfigureAwait(false);
|
|
|
|
|
if (!ok)
|
2017-06-05 22:46:58 +00:00
|
|
|
|
{
|
2017-09-15 00:42:51 +00:00
|
|
|
|
data = await _http.GetStringAsync(link).ConfigureAwait(false);
|
|
|
|
|
await _cache.SetAnimeDataAsync(link, data).ConfigureAwait(false);
|
2017-06-05 22:46:58 +00:00
|
|
|
|
}
|
2017-09-15 00:42:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return JsonConvert.DeserializeObject<AnimeResult>(data);
|
2017-06-05 22:46:58 +00:00
|
|
|
|
}
|
2017-06-24 23:45:11 +00:00
|
|
|
|
catch
|
2017-06-05 22:46:58 +00:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<MangaResult> GetMangaData(string query)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(query))
|
|
|
|
|
throw new ArgumentNullException(nameof(query));
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-06-24 23:45:11 +00:00
|
|
|
|
|
2017-09-15 00:42:51 +00:00
|
|
|
|
var link = "https://aniapi.nadekobot.me/manga/" + Uri.EscapeDataString(query.Replace("/", " "));
|
|
|
|
|
link = link.ToLowerInvariant();
|
|
|
|
|
var (ok, data) = await _cache.TryGetAnimeDataAsync(link).ConfigureAwait(false);
|
|
|
|
|
if (!ok)
|
2017-06-05 22:46:58 +00:00
|
|
|
|
{
|
2017-09-15 00:42:51 +00:00
|
|
|
|
data = await _http.GetStringAsync(link).ConfigureAwait(false);
|
|
|
|
|
await _cache.SetAnimeDataAsync(link, data).ConfigureAwait(false);
|
2017-06-05 22:46:58 +00:00
|
|
|
|
}
|
2017-09-15 00:42:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return JsonConvert.DeserializeObject<MangaResult>(data);
|
2017-06-05 22:46:58 +00:00
|
|
|
|
}
|
2017-06-24 23:45:11 +00:00
|
|
|
|
catch
|
2017-06-05 22:46:58 +00:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|