Merge pull request #225 from Gaboer/patch-1

Update SearchHelper.cs
This commit is contained in:
Master Kwoth 2016-04-21 01:49:04 +02:00
commit 0d4d4a138c

View File

@ -1,4 +1,4 @@
using NadekoBot.Classes.JSONModels; using NadekoBot.Classes.JSONModels;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
@ -11,6 +11,7 @@ using System.Net.Http;
using System.Security.Authentication; using System.Security.Authentication;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml.Linq;
namespace NadekoBot.Classes namespace NadekoBot.Classes
{ {
@ -260,11 +261,18 @@ namespace NadekoBot.Classes
internal static async Task<string> GetE621ImageLink(string tags) internal static async Task<string> GetE621ImageLink(string tags)
{ {
var rng = new Random(); try
var url = $"https://e621.net/post/index/{rng.Next(0, 5)}/{Uri.EscapeUriString(tags)}"; {
var webpage = await GetResponseStringAsync(url).ConfigureAwait(false); // first extract the post id and go to that posts page ServicePointManager.Expect100Continue = true;
var matches = Regex.Matches(webpage, "\"file_url\":\"(?<url>.*?)\""); ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
return matches[rng.Next(0, matches.Count)].Groups["url"].Value; XDocument doc = XDocument.Load(" https://e621.net/post/index.xml?tags="+Uri.EscapeUriString(tags)+"%20order:random&limit=1");
int id = Convert.ToInt32(doc.Root.Element("post").Element("id").Value);
return (doc.Root.Element("post").Element("file_url").Value);
}
catch (Exception)
{
return "Error, do you have too many tags?";
}
} }
public static async Task<string> ShortenUrl(string url) public static async Task<string> ShortenUrl(string url)