Safebooru MUCH faster and reliable

This commit is contained in:
Kwoth 2016-12-25 14:00:35 +01:00
parent fc27e662d2
commit d7ac9b3762

View File

@ -22,6 +22,7 @@ using AngleSharp.Parser.Html;
using AngleSharp; using AngleSharp;
using AngleSharp.Dom.Html; using AngleSharp.Dom.Html;
using AngleSharp.Dom; using AngleSharp.Dom;
using System.Xml;
namespace NadekoBot.Modules.Searches namespace NadekoBot.Modules.Searches
{ {
@ -567,7 +568,8 @@ namespace NadekoBot.Modules.Searches
if (link == null) if (link == null)
await channel.SendErrorAsync("No results."); await channel.SendErrorAsync("No results.");
else else
await channel.SendMessageAsync(link).ConfigureAwait(false); await channel.EmbedAsync(new EmbedBuilder().WithOkColor().WithDescription(umsg.Author.Mention + " " + tag).WithImageUrl(link)
.WithFooter(efb => efb.WithText("Safebooru")).Build()).ConfigureAwait(false);
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
@ -651,16 +653,16 @@ namespace NadekoBot.Modules.Searches
public static async Task<string> GetSafebooruImageLink(string tag) public static async Task<string> GetSafebooruImageLink(string tag)
{ {
var rng = new NadekoRandom(); var rng = new NadekoRandom();
var url =
$"http://safebooru.org/index.php?page=dapi&s=post&q=index&limit=100&tags={tag.Replace(" ", "_")}"; var doc = new XmlDocument();
using (var http = new HttpClient()) using (var http = new HttpClient())
{ {
var webpage = await http.GetStringAsync(url).ConfigureAwait(false); var stream = await http.GetStreamAsync($"http://safebooru.org/index.php?page=dapi&s=post&q=index&limit=100&tags={tag.Replace(" ", "_")}")
var matches = Regex.Matches(webpage, "file_url=\"(?<url>.*?)\""); .ConfigureAwait(false);
if (matches.Count == 0) doc.Load(stream);
return null;
var match = matches[rng.Next(0, matches.Count)]; var node = doc.LastChild.ChildNodes[rng.Next(0, doc.LastChild.ChildNodes.Count)];
return "http:" + matches[rng.Next(0, matches.Count)].Groups["url"].Value; return "http:" + node.Attributes["file_url"].Value;
} }
} }