From 4be7000381ae1592e194f9baa216fd1b5dbefe27 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sun, 25 Dec 2016 16:26:13 +0100 Subject: [PATCH] Hentai optimized, should be much faster --- src/NadekoBot/Modules/NSFW/NSFW.cs | 236 +++++++-------------- src/NadekoBot/Modules/Searches/Searches.cs | 95 ++++++--- 2 files changed, 146 insertions(+), 185 deletions(-) diff --git a/src/NadekoBot/Modules/NSFW/NSFW.cs b/src/NadekoBot/Modules/NSFW/NSFW.cs index 62603807..e2db77b8 100644 --- a/src/NadekoBot/Modules/NSFW/NSFW.cs +++ b/src/NadekoBot/Modules/NSFW/NSFW.cs @@ -10,6 +10,7 @@ using System.Net.Http; using System.Text.RegularExpressions; using System.Xml.Linq; using NadekoBot.Extensions; +using System.Xml; namespace NadekoBot.Modules.NSFW { @@ -32,7 +33,7 @@ namespace NadekoBot.Modules.NSFW var rng = new NadekoRandom(); Task provider = Task.FromResult(""); - switch (rng.Next(0,4)) + switch (rng.Next(0, 4)) { case 0: provider = GetDanbooruImageLink(tag); @@ -56,6 +57,7 @@ namespace NadekoBot.Modules.NSFW await channel.SendMessageAsync(link).ConfigureAwait(false); } + [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] public async Task HentaiBomb(IUserMessage umsg, [Remainder] string tag = null) @@ -65,7 +67,7 @@ namespace NadekoBot.Modules.NSFW tag = tag?.Trim() ?? ""; tag = "rating%3Aexplicit+" + tag; - var links = await Task.WhenAll(GetGelbooruImageLink(tag), + var links = await Task.WhenAll(GetGelbooruImageLink(tag), GetDanbooruImageLink(tag), GetKonachanImageLink(tag), GetYandereImageLink(tag)).ConfigureAwait(false); @@ -78,39 +80,7 @@ namespace NadekoBot.Modules.NSFW await channel.SendMessageAsync(String.Join("\n\n", links)).ConfigureAwait(false); } - - public static async Task GetYandereImageLink(string tag) - { - var rng = new NadekoRandom(); - var url = - $"https://yande.re/post.xml?" + - $"limit=25" + - $"&page={rng.Next(0, 15)}" + - $"&tags={tag.Replace(" ", "_")}"; - using (var http = new HttpClient()) - { - var webpage = await http.GetStringAsync(url).ConfigureAwait(false); - var matches = Regex.Matches(webpage, "file_url=\"(?.*?)\""); - //var rating = Regex.Matches(webpage, "rating=\"(?.*?)\""); - if (matches.Count == 0) - return null; - return matches[rng.Next(0, matches.Count)].Groups["url"].Value; - } - } - - [NadekoCommand, Usage, Description, Aliases] - [RequireContext(ContextType.Guild)] - public async Task Yandere(IUserMessage umsg, [Remainder] string tag = null) - { - var channel = (ITextChannel)umsg.Channel; - tag = tag?.Trim() ?? ""; - var link = await GetYandereImageLink(tag).ConfigureAwait(false); - if (string.IsNullOrWhiteSpace(link)) - await channel.SendErrorAsync("No results found.").ConfigureAwait(false); - else - await channel.SendMessageAsync(link).ConfigureAwait(false); - } [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] @@ -119,54 +89,38 @@ namespace NadekoBot.Modules.NSFW var channel = (ITextChannel)umsg.Channel; tag = tag?.Trim() ?? ""; - var link = await GetDanbooruImageLink(tag).ConfigureAwait(false); - if (string.IsNullOrWhiteSpace(link)) - await channel.SendErrorAsync("No results found.").ConfigureAwait(false); + + var url = await GetDanbooruImageLink(tag).ConfigureAwait(false); + + if (url == null) + await channel.SendErrorAsync(umsg.Author.Mention + " No results."); else - await channel.SendMessageAsync(link).ConfigureAwait(false); + await channel.EmbedAsync(new EmbedBuilder().WithOkColor() + .WithDescription(umsg.Author.Mention + " " + tag) + .WithImageUrl(url) + .WithFooter(efb => efb.WithText("Danbooru")) + .Build()).ConfigureAwait(false); } [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] - public async Task Konachan(IUserMessage umsg, [Remainder] string tag = null) - { - var channel = (ITextChannel)umsg.Channel; - - tag = tag?.Trim() ?? ""; - var link = await GetKonachanImageLink(tag).ConfigureAwait(false); - if (string.IsNullOrWhiteSpace(link)) - await channel.SendErrorAsync("No results found.").ConfigureAwait(false); - else - await channel.SendMessageAsync(link).ConfigureAwait(false); - } + public Task Yandere(IUserMessage umsg, [Remainder] string tag = null) + => Searches.Searches.InternalDapiSearch(tag, Searches.Searches.DapiSearchType.Yandere); [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] - public async Task Gelbooru(IUserMessage umsg, [Remainder] string tag = null) - { - var channel = (ITextChannel)umsg.Channel; - - tag = tag?.Trim() ?? ""; - var link = await GetGelbooruImageLink(tag).ConfigureAwait(false); - if (string.IsNullOrWhiteSpace(link)) - await channel.SendErrorAsync("No results found.").ConfigureAwait(false); - else - await channel.SendMessageAsync(link).ConfigureAwait(false); - } + public Task Konachan(IUserMessage umsg, [Remainder] string tag = null) + => Searches.Searches.InternalDapiCommand(umsg, tag, Searches.Searches.DapiSearchType.Konachan); [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] - public async Task Rule34(IUserMessage umsg, [Remainder] string tag = null) - { - var channel = (ITextChannel)umsg.Channel; + public Task Gelbooru(IUserMessage umsg, [Remainder] string tag = null) + => Searches.Searches.InternalDapiCommand(umsg, tag, Searches.Searches.DapiSearchType.Gelbooru); - tag = tag?.Trim() ?? ""; - var link = await GetRule34ImageLink(tag).ConfigureAwait(false); - if (string.IsNullOrWhiteSpace(link)) - await channel.SendErrorAsync("No results found.").ConfigureAwait(false); - else - await channel.SendMessageAsync(link).ConfigureAwait(false); - } + [NadekoCommand, Usage, Description, Aliases] + [RequireContext(ContextType.Guild)] + public Task Rule34(IUserMessage umsg, [Remainder] string tag = null) + => Searches.Searches.InternalDapiCommand(umsg, tag, Searches.Searches.DapiSearchType.Rule34); [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] @@ -175,11 +129,17 @@ namespace NadekoBot.Modules.NSFW var channel = (ITextChannel)umsg.Channel; tag = tag?.Trim() ?? ""; - var link = await GetE621ImageLink(tag).ConfigureAwait(false); - if (string.IsNullOrWhiteSpace(link)) - await channel.SendErrorAsync("No results found.").ConfigureAwait(false); + + var url = await GetE621ImageLink(tag).ConfigureAwait(false); + + if (url == null) + await channel.SendErrorAsync(umsg.Author.Mention + " No results."); else - await channel.SendMessageAsync(link).ConfigureAwait(false); + await channel.EmbedAsync(new EmbedBuilder().WithOkColor() + .WithDescription(umsg.Author.Mention + " " + tag) + .WithImageUrl(url) + .WithFooter(efb => efb.WithText("e621")) + .Build()).ConfigureAwait(false); } [NadekoCommand, Usage, Description, Aliases] @@ -232,99 +192,61 @@ namespace NadekoBot.Modules.NSFW } } - public static async Task GetKonachanImageLink(string tag) - { - var rng = new NadekoRandom(); - - var link = $"http://konachan.com/post?" + - $"page={rng.Next(0, 5)}"; - if (!string.IsNullOrWhiteSpace(tag)) - link += $"&tags={tag.Replace(" ", "_")}"; - using (var http = new HttpClient()) - { - var webpage = await http.GetStringAsync(link).ConfigureAwait(false); - var matches = Regex.Matches(webpage, ".*?)\">"); - - if (matches.Count == 0) - return null; - return "http:" + matches[rng.Next(0, matches.Count)].Groups["ll"].Value; - } - } - public static async Task GetDanbooruImageLink(string tag) - { - var rng = new NadekoRandom(); - - if (tag == "loli") //loli doesn't work for some reason atm - tag = "flat_chest"; - - var link = $"http://danbooru.donmai.us/posts?" + - $"page={rng.Next(0, 15)}"; - if (!string.IsNullOrWhiteSpace(tag)) - link += $"&tags={tag.Replace(" ", "_")}"; - using (var http = new HttpClient()) - { - var webpage = await http.GetStringAsync(link).ConfigureAwait(false); - var matches = Regex.Matches(webpage, "data-large-file-url=\"(?.*?)\""); - - if (matches.Count == 0) - return null; - return $"http://danbooru.donmai.us" + - $"{matches[rng.Next(0, matches.Count)].Groups["id"].Value}"; - } - } - - public static async Task GetGelbooruImageLink(string tag) - { - using (var http = new HttpClient()) - { - http.AddFakeHeaders(); - - var webpage = await http.GetStringAsync("http://gelbooru.com/index.php?page=dapi&s=post&q=index&limit=100&tags="+ tag.Replace(" ", "_")).ConfigureAwait(false); - var matches = Regex.Matches(webpage, "file_url=\"(?.*?)\""); - if (matches.Count == 0) - return null; - - var rng = new NadekoRandom(); - var match = matches[rng.Next(0, matches.Count)]; - return matches[rng.Next(0, matches.Count)].Groups["url"].Value; - } - } - - public static async Task GetRule34ImageLink(string tag) - { - var rng = new NadekoRandom(); - var url = - $"http://rule34.xxx/index.php?page=dapi&s=post&q=index&limit=100&tags={tag.Replace(" ", "_")}"; - using (var http = new HttpClient()) - { - var webpage = await http.GetStringAsync(url).ConfigureAwait(false); - var matches = Regex.Matches(webpage, "file_url=\"(?.*?)\""); - if (matches.Count == 0) - return null; - var match = matches[rng.Next(0, matches.Count)]; - return "http:" + matches[rng.Next(0, matches.Count)].Groups["url"].Value; - } - } - - - public static async Task GetE621ImageLink(string tags) { try { using (var http = new HttpClient()) { http.AddFakeHeaders(); - var data = await http.GetStreamAsync("http://e621.net/post/index.xml?tags=" + Uri.EscapeUriString(tags) + "%20order:random&limit=1"); - var doc = XDocument.Load(data); - return doc.Descendants("file_url").FirstOrDefault().Value; + var data = await http.GetStreamAsync("https://danbooru.donmai.us/posts.xml?limit=100&tags=" + tag); + var doc = new XmlDocument(); + doc.Load(data); + var nodes = doc.GetElementsByTagName("file-url"); + + var node = nodes[new NadekoRandom().Next(0, nodes.Count)]; + return "https://danbooru.donmai.us" + node.InnerText; } } - catch (Exception ex) + catch { - Console.WriteLine("Error in e621 search: \n" + ex); - return "Error, do you have too many tags?"; + return null; } } + + + public static async Task GetE621ImageLink(string tag) + { + try + { + using (var http = new HttpClient()) + { + http.AddFakeHeaders(); + var data = await http.GetStreamAsync("http://e621.net/post/index.xml?tags=" + tag); + var doc = new XmlDocument(); + doc.Load(data); + var nodes = doc.GetElementsByTagName("file_url"); + + var node = nodes[new NadekoRandom().Next(0, nodes.Count)]; + return node.InnerText; + } + } + catch + { + return null; + } + } + + public static Task GetYandereImageLink(string tag) => + Searches.Searches.InternalDapiSearch(tag, Searches.Searches.DapiSearchType.Yandere); + + public static Task GetKonachanImageLink(string tag) => + Searches.Searches.InternalDapiSearch(tag, Searches.Searches.DapiSearchType.Konachan); + + public static Task GetGelbooruImageLink(string tag) => + Searches.Searches.InternalDapiSearch(tag, Searches.Searches.DapiSearchType.Gelbooru); + + public static Task GetRule34ImageLink(string tag) => + Searches.Searches.InternalDapiSearch(tag, Searches.Searches.DapiSearchType.Rule34); } -} +} \ No newline at end of file diff --git a/src/NadekoBot/Modules/Searches/Searches.cs b/src/NadekoBot/Modules/Searches/Searches.cs index fe485137..df85301a 100644 --- a/src/NadekoBot/Modules/Searches/Searches.cs +++ b/src/NadekoBot/Modules/Searches/Searches.cs @@ -23,6 +23,7 @@ using AngleSharp; using AngleSharp.Dom.Html; using AngleSharp.Dom; using System.Xml; +using System.Xml.Linq; namespace NadekoBot.Modules.Searches { @@ -559,18 +560,8 @@ namespace NadekoBot.Modules.Searches [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] - public async Task Safebooru(IUserMessage umsg, [Remainder] string tag = null) - { - var channel = (ITextChannel)umsg.Channel; - - tag = tag?.Trim() ?? ""; - var link = await GetSafebooruImageLink(tag).ConfigureAwait(false); - if (link == null) - await channel.SendErrorAsync("No results."); - else - await channel.EmbedAsync(new EmbedBuilder().WithOkColor().WithDescription(umsg.Author.Mention + " " + tag).WithImageUrl(link) - .WithFooter(efb => efb.WithText("Safebooru")).Build()).ConfigureAwait(false); - } + public Task Safebooru(IUserMessage umsg, [Remainder] string tag = null) + => InternalDapiCommand(umsg, tag, DapiSearchType.Safebooru); [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] @@ -650,22 +641,6 @@ namespace NadekoBot.Modules.Searches await channel.SendMessageAsync(await NadekoBot.Google.ShortenUrl(usr.AvatarUrl).ConfigureAwait(false)).ConfigureAwait(false); } - public static async Task GetSafebooruImageLink(string tag) - { - var rng = new NadekoRandom(); - - var doc = new XmlDocument(); - using (var http = new HttpClient()) - { - var stream = await http.GetStreamAsync($"http://safebooru.org/index.php?page=dapi&s=post&q=index&limit=100&tags={tag.Replace(" ", "_")}") - .ConfigureAwait(false); - doc.Load(stream); - - var node = doc.LastChild.ChildNodes[rng.Next(0, doc.LastChild.ChildNodes.Count)]; - return "http:" + node.Attributes["file_url"].Value; - } - } - [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] public async Task Wikia(IUserMessage umsg, string target, [Remainder] string query = null) @@ -774,6 +749,70 @@ namespace NadekoBot.Modules.Searches } } + public enum DapiSearchType + { + Safebooru, + Gelbooru, + Konachan, + Rule34, + Yandere + } + + public static async Task InternalDapiCommand(IUserMessage umsg, string tag, DapiSearchType type) + { + var channel = (ITextChannel)umsg.Channel; + + tag = tag?.Trim() ?? ""; + + var url = await InternalDapiSearch(tag, type).ConfigureAwait(false); + + if (url == null) + await channel.SendErrorAsync(umsg.Author.Mention + " No results."); + else + await channel.EmbedAsync(new EmbedBuilder().WithOkColor() + .WithDescription(umsg.Author.Mention + " " + tag) + .WithImageUrl(url) + .WithFooter(efb => efb.WithText(type.ToString())) + .Build()).ConfigureAwait(false); + } + + public static async Task InternalDapiSearch(string tag, DapiSearchType type) + { + tag = tag.Replace(" ", "_"); + string website = ""; + switch (type) + { + case DapiSearchType.Safebooru: + website = $"https://safebooru.org/index.php?page=dapi&s=post&q=index&limit=100&tags={tag}"; + break; + case DapiSearchType.Gelbooru: + website = $"http://gelbooru.com/index.php?page=dapi&s=post&q=index&limit=100&tags={tag}"; + break; + case DapiSearchType.Rule34: + website = $"https://rule34.xxx/index.php?page=dapi&s=post&q=index&limit=100&tags={tag}"; + break; + case DapiSearchType.Konachan: + website = $"https://konachan.com/post.xml?s=post&q=index&limit=100&tags={tag}"; + break; + case DapiSearchType.Yandere: + website = $"https://yande.re/post.xml?limit=100&tags={tag}"; + break; + } + using (var http = new HttpClient()) + { + http.AddFakeHeaders(); + var data = await http.GetStreamAsync(website); + var doc = new XmlDocument(); + doc.Load(data); + + var node = doc.LastChild.ChildNodes[new NadekoRandom().Next(0, doc.LastChild.ChildNodes.Count)]; + + var url = node.Attributes["file_url"].Value; + if (!url.StartsWith("http")) + url = "https:" + url; + return url; + } + } public static async Task ValidateQuery(ITextChannel ch, string query) { if (!string.IsNullOrEmpty(query.Trim())) return true;