Merge pull request #193 from GoBJIuH/fixpr

Fixed gelbooru, added safebooru and rule34xxx
This commit is contained in:
Master Kwoth 2016-04-06 21:12:33 +02:00
commit 5471012504
2 changed files with 47 additions and 9 deletions

View File

@ -196,22 +196,46 @@ namespace NadekoBot.Classes {
$"{matches[rng.Next(0, matches.Count)].Groups["id"].Value}";
}
public static async Task<string> GetGelbooruImageLink(string tag) {
public static async Task<string> 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=\"(?<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<string> 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\/" +
@"(?<folder>.*\/.*?)\/thumbnail_(?<id>.*?)\""");
$"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=\"(?<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<string> 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=\"(?<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<string> GetE621ImageLink(string tags) {
var rng = new Random();
var url = $"https://e621.net/post/index/{rng.Next(0, 5)}/{Uri.EscapeUriString(tags)}";

View File

@ -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)