anilist api should work a bit better and is cleaner
This commit is contained in:
parent
e27e1005eb
commit
f11429b714
@ -1,53 +1,18 @@
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace NadekoBot.Services.Searches
|
||||
{
|
||||
//todo move to the website
|
||||
public class AnimeSearchService
|
||||
{
|
||||
private readonly Timer _anilistTokenRefresher;
|
||||
private readonly Logger _log;
|
||||
|
||||
private static string anilistToken { get; set; }
|
||||
|
||||
public AnimeSearchService()
|
||||
{
|
||||
_log = LogManager.GetCurrentClassLogger();
|
||||
_anilistTokenRefresher = new Timer(async (state) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var headers = new Dictionary<string, string>
|
||||
{
|
||||
{"grant_type", "client_credentials"},
|
||||
{"client_id", "kwoth-w0ki9"},
|
||||
{"client_secret", "Qd6j4FIAi1ZK6Pc7N7V4Z"},
|
||||
};
|
||||
|
||||
using (var http = new HttpClient())
|
||||
{
|
||||
//http.AddFakeHeaders();
|
||||
http.DefaultRequestHeaders.Clear();
|
||||
var formContent = new FormUrlEncodedContent(headers);
|
||||
var response = await http.PostAsync("https://anilist.co/api/auth/access_token", formContent).ConfigureAwait(false);
|
||||
var stringContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
anilistToken = JObject.Parse(stringContent)["access_token"].ToString();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
}, null, TimeSpan.FromSeconds(0), TimeSpan.FromMinutes(29));
|
||||
}
|
||||
|
||||
public async Task<AnimeResult> GetAnimeData(string query)
|
||||
@ -57,19 +22,15 @@ namespace NadekoBot.Services.Searches
|
||||
try
|
||||
{
|
||||
|
||||
var link = "http://anilist.co/api/anime/search/" + Uri.EscapeUriString(query);
|
||||
var link = "https://aniapi.nadekobot.me/anime/" + Uri.EscapeDataString(query.Replace("/", " "));
|
||||
using (var http = new HttpClient())
|
||||
{
|
||||
var res = await http.GetStringAsync(link + $"?access_token={anilistToken}").ConfigureAwait(false);
|
||||
var smallObj = JArray.Parse(res)[0];
|
||||
var aniData = await http.GetStringAsync("http://anilist.co/api/anime/" + smallObj["id"] + $"?access_token={anilistToken}").ConfigureAwait(false);
|
||||
|
||||
return await Task.Run(() => { try { return JsonConvert.DeserializeObject<AnimeResult>(aniData); } catch { return null; } }).ConfigureAwait(false);
|
||||
var res = await http.GetStringAsync(link).ConfigureAwait(false);
|
||||
return JsonConvert.DeserializeObject<AnimeResult>(res);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch
|
||||
{
|
||||
_log.Warn(ex, "Failed anime search for {0}", query);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -80,18 +41,16 @@ namespace NadekoBot.Services.Searches
|
||||
throw new ArgumentNullException(nameof(query));
|
||||
try
|
||||
{
|
||||
|
||||
var link = "https://aniapi.nadekobot.me/manga/" + Uri.EscapeDataString(query.Replace("/", " "));
|
||||
using (var http = new HttpClient())
|
||||
{
|
||||
var res = await http.GetStringAsync("http://anilist.co/api/manga/search/" + Uri.EscapeUriString(query) + $"?access_token={anilistToken}").ConfigureAwait(false);
|
||||
var smallObj = JArray.Parse(res)[0];
|
||||
var aniData = await http.GetStringAsync("http://anilist.co/api/manga/" + smallObj["id"] + $"?access_token={anilistToken}").ConfigureAwait(false);
|
||||
|
||||
return await Task.Run(() => { try { return JsonConvert.DeserializeObject<MangaResult>(aniData); } catch { return null; } }).ConfigureAwait(false);
|
||||
var res = await http.GetStringAsync(link).ConfigureAwait(false);
|
||||
return JsonConvert.DeserializeObject<MangaResult>(res);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch
|
||||
{
|
||||
_log.Warn(ex, "Failed anime search for {0}", query);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user