Added anime and manga search w/o restsharp
This commit is contained in:
parent
598865609e
commit
465b598286
@ -1,99 +1,134 @@
|
|||||||
//using Discord;
|
using Discord;
|
||||||
//using Discord.Commands;
|
using Discord.Commands;
|
||||||
//using NadekoBot.Attributes;
|
using NadekoBot.Attributes;
|
||||||
//using System;
|
using NadekoBot.Extensions;
|
||||||
//using System.Collections.Generic;
|
using NadekoBot.Modules.Searches.Models;
|
||||||
//using System.Linq;
|
using Newtonsoft.Json;
|
||||||
//using System.Threading.Tasks;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using NLog;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
//// todo RestSharp
|
// todo RestSharp
|
||||||
//namespace NadekoBot.Modules.Searches
|
namespace NadekoBot.Modules.Searches
|
||||||
//{
|
{
|
||||||
// public partial class SearchesModule
|
public partial class Searches
|
||||||
// {
|
{
|
||||||
// [LocalizedCommand, LocalizedDescription, LocalizedSummary]
|
[Group]
|
||||||
// [RequireContext(ContextType.Guild)]
|
public class AnimeSearchCommands
|
||||||
// public async Task Anime(IUserMessage umsg, [Remainder] string query = null)
|
{
|
||||||
// {
|
private Logger _log;
|
||||||
// var channel = (ITextChannel)umsg.Channel;
|
|
||||||
|
|
||||||
// if (!(await ValidateQuery(umsg.Channel as ITextChannel, query).ConfigureAwait(false))) return;
|
private string anilistToken { get; set; }
|
||||||
// string result;
|
private DateTime lastRefresh { get; set; }
|
||||||
// try
|
|
||||||
// {
|
|
||||||
// result = (await GetAnimeData(query).ConfigureAwait(false)).ToString();
|
|
||||||
// }
|
|
||||||
// catch
|
|
||||||
// {
|
|
||||||
// await channel.SendMessageAsync("Failed to find that anime.").ConfigureAwait(false);
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// await channel.SendMessageAsync(result.ToString()).ConfigureAwait(false);
|
public AnimeSearchCommands()
|
||||||
// }
|
{
|
||||||
|
_log = LogManager.GetCurrentClassLogger();
|
||||||
|
}
|
||||||
|
|
||||||
// [LocalizedCommand, LocalizedDescription, LocalizedSummary]
|
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
|
||||||
// [RequireContext(ContextType.Guild)]
|
[RequireContext(ContextType.Guild)]
|
||||||
// public async Task Manga(IUserMessage umsg, [Remainder] string query = null)
|
public async Task Anime(IUserMessage umsg, [Remainder] string query)
|
||||||
// {
|
{
|
||||||
// var channel = (ITextChannel)umsg.Channel;
|
var channel = (ITextChannel)umsg.Channel;
|
||||||
|
|
||||||
// if (!(await ValidateQuery(umsg.Channel as ITextChannel, query).ConfigureAwait(false))) return;
|
if (string.IsNullOrWhiteSpace(query))
|
||||||
// string result;
|
return;
|
||||||
// try
|
|
||||||
// {
|
|
||||||
// result = (await GetMangaData(query).ConfigureAwait(false)).ToString();
|
|
||||||
// }
|
|
||||||
// catch
|
|
||||||
// {
|
|
||||||
// await channel.SendMessageAsync("Failed to find that manga.").ConfigureAwait(false);
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// await channel.SendMessageAsync(result).ConfigureAwait(false);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// public static async Task<AnimeResult> GetAnimeData(string query)
|
var result = await GetAnimeData(query).ConfigureAwait(false);
|
||||||
// {
|
|
||||||
// if (string.IsNullOrWhiteSpace(query))
|
|
||||||
// throw new ArgumentNullException(nameof(query));
|
|
||||||
|
|
||||||
// await RefreshAnilistToken().ConfigureAwait(false);
|
await channel.SendMessageAsync(result.ToString() ?? "`No anime found.`").ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
// var link = "http://anilist.co/api/anime/search/" + Uri.EscapeUriString(query);
|
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
|
||||||
// var smallContent = "";
|
[RequireContext(ContextType.Guild)]
|
||||||
// var cl = new RestSharp.RestClient("http://anilist.co/api");
|
public async Task Manga(IUserMessage umsg, [Remainder] string query)
|
||||||
// var rq = new RestSharp.RestRequest("/anime/search/" + Uri.EscapeUriString(query));
|
{
|
||||||
// rq.AddParameter("access_token", token);
|
var channel = (ITextChannel)umsg.Channel;
|
||||||
// smallContent = cl.Execute(rq).Content;
|
|
||||||
// var smallObj = JArray.Parse(smallContent)[0];
|
|
||||||
|
|
||||||
// rq = new RestSharp.RestRequest("/anime/" + smallObj["id"]);
|
if (string.IsNullOrWhiteSpace(query))
|
||||||
// rq.AddParameter("access_token", token);
|
return;
|
||||||
// var content = cl.Execute(rq).Content;
|
|
||||||
|
|
||||||
// return await Task.Run(() => JsonConvert.DeserializeObject<AnimeResult>(content)).ConfigureAwait(false);
|
var result = await GetMangaData(query).ConfigureAwait(false);
|
||||||
// }
|
|
||||||
|
|
||||||
// public static async Task<MangaResult> GetMangaData(string query)
|
await channel.SendMessageAsync(result.ToString() ?? "`No manga found.`").ConfigureAwait(false);
|
||||||
// {
|
}
|
||||||
// if (string.IsNullOrWhiteSpace(query))
|
|
||||||
// throw new ArgumentNullException(nameof(query));
|
|
||||||
|
|
||||||
// await RefreshAnilistToken().ConfigureAwait(false);
|
private async Task<AnimeResult> GetAnimeData(string query)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(query))
|
||||||
|
throw new ArgumentNullException(nameof(query));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await RefreshAnilistToken().ConfigureAwait(false);
|
||||||
|
|
||||||
// var link = "http://anilist.co/api/anime/search/" + Uri.EscapeUriString(query);
|
var link = "http://anilist.co/api/anime/search/" + Uri.EscapeUriString(query);
|
||||||
// var smallContent = "";
|
using (var http = new HttpClient())
|
||||||
// var cl = new RestSharp.RestClient("http://anilist.co/api");
|
{
|
||||||
// var rq = new RestSharp.RestRequest("/manga/search/" + Uri.EscapeUriString(query));
|
var res = await http.GetStringAsync("http://anilist.co/api/anime/search/" + Uri.EscapeUriString(query) + $"?access_token={anilistToken}").ConfigureAwait(false);
|
||||||
// rq.AddParameter("access_token", token);
|
var smallObj = JArray.Parse(res)[0];
|
||||||
// smallContent = cl.Execute(rq).Content;
|
var aniData = await http.GetStringAsync("http://anilist.co/api/anime/" + smallObj["id"] + $"?access_token={anilistToken}").ConfigureAwait(false);
|
||||||
// var smallObj = JArray.Parse(smallContent)[0];
|
|
||||||
|
|
||||||
// rq = new RestSharp.RestRequest("/manga/" + smallObj["id"]);
|
return await Task.Run(() => JsonConvert.DeserializeObject<AnimeResult>(aniData)).ConfigureAwait(false);
|
||||||
// rq.AddParameter("access_token", token);
|
}
|
||||||
// var content = cl.Execute(rq).Content;
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
_log.Warn(ex, "Failed anime search for {0}", query);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// return await Task.Run(() => JsonConvert.DeserializeObject<MangaResult>(content)).ConfigureAwait(false);
|
private async Task RefreshAnilistToken()
|
||||||
// }
|
{
|
||||||
// }
|
if (DateTime.Now - lastRefresh > TimeSpan.FromMinutes(29))
|
||||||
//}
|
lastRefresh = DateTime.Now;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var headers = new Dictionary<string, string> {
|
||||||
|
{"grant_type", "client_credentials"},
|
||||||
|
{"client_id", "kwoth-w0ki9"},
|
||||||
|
{"client_secret", "Qd6j4FIAi1ZK6Pc7N7V4Z"},
|
||||||
|
};
|
||||||
|
using (var http = new HttpClient())
|
||||||
|
{
|
||||||
|
http.AddFakeHeaders();
|
||||||
|
var formContent = new FormUrlEncodedContent(headers);
|
||||||
|
var response = await http.PostAsync("http://anilist.co/api/auth/access_token", formContent).ConfigureAwait(false);
|
||||||
|
var stringContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||||
|
anilistToken = JObject.Parse(stringContent)["access_token"].ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<MangaResult> GetMangaData(string query)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(query))
|
||||||
|
throw new ArgumentNullException(nameof(query));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await RefreshAnilistToken().ConfigureAwait(false);
|
||||||
|
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];
|
||||||
|
//todo check if successfull
|
||||||
|
var aniData = await http.GetStringAsync("http://anilist.co/api/manga/" + smallObj["id"] + $"?access_token={anilistToken}").ConfigureAwait(false);
|
||||||
|
|
||||||
|
return await Task.Run(() => JsonConvert.DeserializeObject<MangaResult>(aniData)).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_log.Warn(ex, "Failed anime search for {0}", query);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
namespace NadekoBot.Classes.JSONModels
|
namespace NadekoBot.Modules.Searches.Models
|
||||||
{
|
{
|
||||||
public class AnimeResult
|
public class AnimeResult
|
||||||
{
|
{
|
@ -1,4 +1,4 @@
|
|||||||
namespace NadekoBot.Classes.JSONModels
|
namespace NadekoBot.Modules.Searches.Models
|
||||||
{
|
{
|
||||||
public class MangaResult
|
public class MangaResult
|
||||||
{
|
{
|
Loading…
Reference in New Issue
Block a user