diff --git a/NadekoBot/Classes/SearchHelper.cs b/NadekoBot/Classes/SearchHelper.cs index c7bf18dc..ccd25a93 100644 --- a/NadekoBot/Classes/SearchHelper.cs +++ b/NadekoBot/Classes/SearchHelper.cs @@ -196,22 +196,46 @@ namespace NadekoBot.Classes { $"{matches[rng.Next(0, matches.Count)].Groups["id"].Value}"; } - public static async Task GetGelbooruImageLink(string tag) { + public static async Task GetGelbooruImageLink(string tag) + { + var url = + $"http://gelbooru.com/index.php?page=dapi&s=post&q=index&limit=100&tags={tag.Replace(" ", "_")}"; + var webpage = await GetResponseStringAsync(url); + var matches = Regex.Matches(webpage, "file_url=\"(?.*?)\""); + if (matches.Count == 0) + throw new FileNotFoundException(); + var rng = new Random(); + var match = matches[rng.Next(0, matches.Count)]; + return matches[rng.Next(0, matches.Count)].Groups["url"].Value; + } + + public static async Task GetSafebooruImageLink(string tag) + { var rng = new Random(); var url = - $"http://gelbooru.com/index.php?page=post&s=list&pid={rng.Next(0, 10) * 42}&tags={tag.Replace(" ", "_")}"; - var webpage = await GetResponseStringAsync(url); // first extract the post id and go to that posts page - //src="htp://gelbooru.com/thumbnails/1b/5e/thumbnail_1b5e1dae36237ef0cd030575b93b5bd2.jpg?3064956" - var matches = Regex.Matches(webpage, @"src=\""http:\/\/gelbooru\.com\/thumbnails\/" + - @"(?.*\/.*?)\/thumbnail_(?.*?)\"""); + $"http://safebooru.org/index.php?page=dapi&s=post&q=index&limit=100&tags={tag.Replace(" ", "_")}"; + var webpage = await GetResponseStringAsync(url); + var matches = Regex.Matches(webpage, "file_url=\"(?.*?)\""); if (matches.Count == 0) throw new FileNotFoundException(); var match = matches[rng.Next(0, matches.Count)]; - //http://simg4.gelbooru.com//images/58/20/58209047098e86c2f96c323fb85b8691.jpg?3076643 - return $"http://simg4.gelbooru.com//images/" + - $"{match.Groups["folder"]}/{match.Groups["id"]}"; + return matches[rng.Next(0, matches.Count)].Groups["url"].Value; } + public static async Task GetRule34ImageLink(string tag) + { + var rng = new Random(); + var url = + $"http://rule34.xxx/index.php?page=dapi&s=post&q=index&limit=100&tags={tag.Replace(" ", "_")}"; + var webpage = await GetResponseStringAsync(url); + var matches = Regex.Matches(webpage, "file_url=\"(?.*?)\""); + if (matches.Count == 0) + throw new FileNotFoundException(); + var match = matches[rng.Next(0, matches.Count)]; + return "http:" + matches[rng.Next(0, matches.Count)].Groups["url"].Value; + } + + internal static async Task GetE621ImageLink(string tags) { var rng = new Random(); var url = $"https://e621.net/post/index/{rng.Next(0, 5)}/{Uri.EscapeUriString(tags)}"; diff --git a/NadekoBot/Modules/NSFW.cs b/NadekoBot/Modules/NSFW.cs index efbcf8d1..d18717cd 100644 --- a/NadekoBot/Modules/NSFW.cs +++ b/NadekoBot/Modules/NSFW.cs @@ -38,6 +38,20 @@ namespace NadekoBot.Modules { var tag = e.GetArg("tag")?.Trim() ?? ""; await e.Channel.SendMessage(await SearchHelper.GetGelbooruImageLink(tag)); }); + cgb.CreateCommand(Prefix + "safebooru") + .Description("Shows a random image from safebooru with a given tag. Tag is optional but preffered. (multiple tags are appended with +)\n**Usage**: ~gelbooru yuri+kissing") + .Parameter("tag", ParameterType.Unparsed) + .Do(async e => { + var tag = e.GetArg("tag")?.Trim() ?? ""; + await e.Channel.SendMessage(await SearchHelper.GetSafebooruImageLink(tag)); + }); + cgb.CreateCommand(Prefix + "rule34") + .Description("Shows a random image from rule34.xx with a given tag. Tag is optional but preffered. (multiple tags are appended with +)\n**Usage**: ~gelbooru yuri+kissing") + .Parameter("tag", ParameterType.Unparsed) + .Do(async e => { + var tag = e.GetArg("tag")?.Trim() ?? ""; + await e.Channel.SendMessage(await SearchHelper.GetRule34ImageLink(tag)); + }); cgb.CreateCommand(Prefix + "e621") .Description("Shows a random hentai image from e621.net with a given tag. Tag is optional but preffered. Use spaces for multiple tags.\n**Usage**: ~e621 yuri+kissing") .Parameter("tag", ParameterType.Unparsed)