From f11429b7146212ac4030b02a8734951e7ea13ba4 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Sun, 25 Jun 2017 01:45:11 +0200 Subject: [PATCH] anilist api should work a bit better and is cleaner --- .../Services/Searches/AnimeSearchService.cs | 59 +++---------------- 1 file changed, 9 insertions(+), 50 deletions(-) diff --git a/src/NadekoBot/Services/Searches/AnimeSearchService.cs b/src/NadekoBot/Services/Searches/AnimeSearchService.cs index 2e4ef756..a4187da3 100644 --- a/src/NadekoBot/Services/Searches/AnimeSearchService.cs +++ b/src/NadekoBot/Services/Searches/AnimeSearchService.cs @@ -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 - { - {"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 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(aniData); } catch { return null; } }).ConfigureAwait(false); + var res = await http.GetStringAsync(link).ConfigureAwait(false); + return JsonConvert.DeserializeObject(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(aniData); } catch { return null; } }).ConfigureAwait(false); + var res = await http.GetStringAsync(link).ConfigureAwait(false); + return JsonConvert.DeserializeObject(res); } } - catch (Exception ex) + catch { - _log.Warn(ex, "Failed anime search for {0}", query); return null; } }