<commit_before>﻿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", "<KEY>"},
                        };

                    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)
        {
            if (string.IsNullOrWhiteSpace(query))
                throw new ArgumentNullException(nameof(query));
            try
            {

                var link = "http://anilist.co/api/anime/search/" + Uri.EscapeUriString(query);
                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);
                }
            }
            catch (Exception ex)
            {
                _log.Warn(ex, "Failed anime search for {0}", query);
                return null;
            }
        }

        public async Task<MangaResult> GetMangaData(string query)
        {
            if (string.IsNullOrWhiteSpace(query))
                throw new ArgumentNullException(nameof(query));
            try
            {
                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);
                }
            }
            catch (Exception ex)
            {
                _log.Warn(ex, "Failed anime search for {0}", query);
                return null;
            }
        }
    }
}<commit_msg>anilist api should work a bit better and is cleaner<commit_after>﻿using Newtonsoft.Json;
using NLog;
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace NadekoBot.Services.Searches
{
    public class AnimeSearchService
    {
        private readonly Logger _log;

        public AnimeSearchService()
        {
            _log = LogManager.GetCurrentClassLogger();
        }

        public async Task<AnimeResult> GetAnimeData(string query)
        {
            if (string.IsNullOrWhiteSpace(query))
                throw new ArgumentNullException(nameof(query));
            try
            {

                var link = "https://aniapi.nadekobot.me/anime/" + Uri.EscapeDataString(query.Replace("/", " "));
                using (var http = new HttpClient())
                {
                    var res = await http.GetStringAsync(link).ConfigureAwait(false);
                    return JsonConvert.DeserializeObject<AnimeResult>(res);
                }
            }
            catch
            {
                return null;
            }
        }

        public async Task<MangaResult> GetMangaData(string query)
        {
            if (string.IsNullOrWhiteSpace(query))
                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(link).ConfigureAwait(false);
                    return JsonConvert.DeserializeObject<MangaResult>(res);
                }
            }
            catch
            {
                return null;
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.10255084453636677, 'fraction_numerical': 0.0015511892450879006, 'mean_word_length': 1.5837043633125556, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 16, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1558728', 'n_tokens_mistral': 1526, 'n_tokens_neox': 1463, 'n_words': 290}