drastic nsfw and safebooru improvements

This commit is contained in:
Master Kwoth
2017-06-29 17:58:16 +02:00
parent db1581f67d
commit 728539528f
6 changed files with 262 additions and 200 deletions

View File

@ -12,6 +12,7 @@ using System.Xml;
using System.Threading;
using System.Collections.Concurrent;
using NadekoBot.Services.Searches;
using NadekoBot.DataStructures;
namespace NadekoBot.Modules.NSFW
{
@ -28,29 +29,12 @@ namespace NadekoBot.Modules.NSFW
private async Task InternalHentai(IMessageChannel channel, string tag, bool noError)
{
tag = tag?.Trim() ?? "";
tag = "rating%3Aexplicit+" + tag;
var rng = new NadekoRandom();
var provider = Task.FromResult("");
switch (rng.Next(0, 4))
{
case 0:
provider = GetDanbooruImageLink(tag);
break;
case 1:
provider = GetGelbooruImageLink(tag);
break;
case 2:
provider = GetKonachanImageLink(tag);
break;
case 3:
provider = GetYandereImageLink(tag);
break;
}
var link = await provider.ConfigureAwait(false);
if (string.IsNullOrWhiteSpace(link))
var arr = Enum.GetValues(typeof(DapiSearchType));
var type = (DapiSearchType)arr.GetValue(new NadekoRandom().Next(2, arr.Length));
var img = await _service.DapiSearch(tag, type, Context.Guild?.Id, true).ConfigureAwait(false);
if (img == null)
{
if (!noError)
await ReplyErrorLocalized("not_found").ConfigureAwait(false);
@ -58,8 +42,8 @@ namespace NadekoBot.Modules.NSFW
}
await channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithImageUrl(link)
.WithDescription($"[{GetText("tag")}: {tag}]({link})"))
.WithImageUrl(img.FileUrl)
.WithDescription($"[{GetText("tag")}: {tag}]({img})"))
.ConfigureAwait(false);
}
@ -108,114 +92,62 @@ namespace NadekoBot.Modules.NSFW
return t;
});
await ReplyConfirmLocalized("autohentai_started",
interval,
await ReplyConfirmLocalized("autohentai_started",
interval,
string.Join(", ", tagsArr)).ConfigureAwait(false);
}
#endif
[NadekoCommand, Usage, Description, Aliases]
public async Task HentaiBomb([Remainder] string tag = null)
{
if (!_hentaiBombBlacklist.Add(Context.User.Id))
if (!_hentaiBombBlacklist.Add(Context.Guild?.Id ?? Context.User.Id))
return;
try
{
tag = tag?.Trim() ?? "";
tag = "rating%3Aexplicit+" + tag;
var images = await Task.WhenAll(_service.DapiSearch(tag, DapiSearchType.Gelbooru, Context.Guild?.Id, true),
_service.DapiSearch(tag, DapiSearchType.Danbooru, Context.Guild?.Id, true),
_service.DapiSearch(tag, DapiSearchType.Konachan, Context.Guild?.Id, true),
_service.DapiSearch(tag, DapiSearchType.Yandere, Context.Guild?.Id, true)).ConfigureAwait(false);
var links = await Task.WhenAll(GetGelbooruImageLink(tag),
GetDanbooruImageLink(tag),
GetKonachanImageLink(tag),
GetYandereImageLink(tag)).ConfigureAwait(false);
var linksEnum = links?.Where(l => l != null).ToArray();
if (links == null || !linksEnum.Any())
var linksEnum = images?.Where(l => l != null).ToArray();
if (images == null || !linksEnum.Any())
{
await ReplyErrorLocalized("not_found").ConfigureAwait(false);
return;
}
await Context.Channel.SendMessageAsync(string.Join("\n\n", linksEnum)).ConfigureAwait(false);
await Context.Channel.SendMessageAsync(string.Join("\n\n", linksEnum.Select(x => x.FileUrl))).ConfigureAwait(false);
}
finally
{
await Task.Delay(5000).ConfigureAwait(false);
_hentaiBombBlacklist.TryRemove(Context.User.Id);
_hentaiBombBlacklist.TryRemove(Context.Guild?.Id ?? Context.User.Id);
}
}
#endif
[NadekoCommand, Usage, Description, Aliases]
public Task Yandere([Remainder] string tag = null)
=> InternalDapiCommand(tag, DapiSearchType.Yandere);
=> InternalDapiCommand(tag, DapiSearchType.Yandere, false);
[NadekoCommand, Usage, Description, Aliases]
public Task Konachan([Remainder] string tag = null)
=> InternalDapiCommand(tag, DapiSearchType.Konachan);
=> InternalDapiCommand(tag, DapiSearchType.Konachan, false);
[NadekoCommand, Usage, Description, Aliases]
public async Task E621([Remainder] string tag = null)
{
tag = tag?.Trim() ?? "";
var url = await GetE621ImageLink(tag).ConfigureAwait(false);
if (url == null)
await ReplyErrorLocalized("not_found").ConfigureAwait(false);
else
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithDescription(Context.User.Mention + " " + tag)
.WithImageUrl(url)
.WithFooter(efb => efb.WithText("e621")))
.ConfigureAwait(false);
}
public Task E621([Remainder] string tag = null)
=> InternalDapiCommand(tag, DapiSearchType.E621, false);
[NadekoCommand, Usage, Description, Aliases]
public Task Rule34([Remainder] string tag = null)
=> InternalDapiCommand(tag, DapiSearchType.Rule34);
=> InternalDapiCommand(tag, DapiSearchType.Rule34, false);
[NadekoCommand, Usage, Description, Aliases]
public async Task Danbooru([Remainder] string tag = null)
{
tag = tag?.Trim() ?? "";
var url = await GetDanbooruImageLink(tag).ConfigureAwait(false);
if (url == null)
await ReplyErrorLocalized("not_found").ConfigureAwait(false);
else
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithDescription(Context.User.Mention + " " + tag)
.WithImageUrl(url)
.WithFooter(efb => efb.WithText("Danbooru")))
.ConfigureAwait(false);
}
public static Task<string> GetDanbooruImageLink(string tag) => Task.Run(async () =>
{
try
{
using (var http = new HttpClient())
{
http.AddFakeHeaders();
var data = await http.GetStreamAsync("https://danbooru.donmai.us/posts.xml?limit=100&tags=" + tag).ConfigureAwait(false);
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
{
return null;
}
});
public Task Danbooru([Remainder] string tag = null)
=> InternalDapiCommand(tag, DapiSearchType.Danbooru, false);
[NadekoCommand, Usage, Description, Aliases]
public Task Gelbooru([Remainder] string tag = null)
=> InternalDapiCommand(tag, DapiSearchType.Gelbooru);
=> InternalDapiCommand(tag, DapiSearchType.Gelbooru, false);
[NadekoCommand, Usage, Description, Aliases]
public async Task Boobs()
@ -253,52 +185,16 @@ namespace NadekoBot.Modules.NSFW
}
}
public static Task<string> GetE621ImageLink(string tag) => Task.Run(async () =>
public async Task InternalDapiCommand(string tag, DapiSearchType type, bool forceExplicit)
{
try
{
using (var http = new HttpClient())
{
http.AddFakeHeaders();
var data = await http.GetStreamAsync("http://e621.net/post/index.xml?tags=" + tag).ConfigureAwait(false);
var doc = new XmlDocument();
doc.Load(data);
var nodes = doc.GetElementsByTagName("file_url");
var imgObj = await _service.DapiSearch(tag, type, Context.Guild?.Id, forceExplicit).ConfigureAwait(false);
var node = nodes[new NadekoRandom().Next(0, nodes.Count)];
return node.InnerText;
}
}
catch
{
return null;
}
});
public Task<string> GetRule34ImageLink(string tag) =>
_service.DapiSearch(tag, DapiSearchType.Rule34);
public Task<string> GetYandereImageLink(string tag) =>
_service.DapiSearch(tag, DapiSearchType.Yandere);
public Task<string> GetKonachanImageLink(string tag) =>
_service.DapiSearch(tag, DapiSearchType.Konachan);
public Task<string> GetGelbooruImageLink(string tag) =>
_service.DapiSearch(tag, DapiSearchType.Gelbooru);
public async Task InternalDapiCommand(string tag, DapiSearchType type)
{
tag = tag?.Trim() ?? "";
var url = await _service.DapiSearch(tag, type).ConfigureAwait(false);
if (url == null)
if (imgObj == null)
await ReplyErrorLocalized("not_found").ConfigureAwait(false);
else
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithDescription($"{Context.User} [{tag}]({url}) ")
.WithImageUrl(url)
.WithDescription($"{Context.User} [{tag ?? "url"}]({imgObj}) ")
.WithImageUrl(imgObj.FileUrl)
.WithFooter(efb => efb.WithText(type.ToString()))).ConfigureAwait(false);
}
}

View File

@ -21,6 +21,7 @@ using NadekoBot.Attributes;
using Discord.Commands;
using ImageSharp;
using NadekoBot.Services.Searches;
using NadekoBot.DataStructures;
namespace NadekoBot.Modules.Searches
{
@ -791,14 +792,14 @@ namespace NadekoBot.Modules.Searches
tag = tag?.Trim() ?? "";
var url = await _searches.DapiSearch(tag, type).ConfigureAwait(false);
var imgObj = await _searches.DapiSearch(tag, type, Context.Guild?.Id).ConfigureAwait(false);
if (url == null)
if (imgObj == null)
await channel.SendErrorAsync(umsg.Author.Mention + " " + GetText("no_results"));
else
await channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithDescription($"{umsg.Author.Mention} [{tag}]({url})")
.WithImageUrl(url)
.WithDescription($"{umsg.Author.Mention} [{tag ?? "url"}]({imgObj.FileUrl})")
.WithImageUrl(imgObj.FileUrl)
.WithFooter(efb => efb.WithText(type.ToString()))).ConfigureAwait(false);
}