Possible fix for #1523, im not testing that though :D
This commit is contained in:
parent
531633b018
commit
4adf85a9eb
@ -4,7 +4,6 @@ using Newtonsoft.Json.Linq;
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Net.Http;
|
|
||||||
using NadekoBot.Extensions;
|
using NadekoBot.Extensions;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
@ -15,7 +14,6 @@ using NadekoBot.Modules.Searches.Common;
|
|||||||
using NadekoBot.Modules.Searches.Services;
|
using NadekoBot.Modules.Searches.Services;
|
||||||
using NadekoBot.Modules.NSFW.Exceptions;
|
using NadekoBot.Modules.NSFW.Exceptions;
|
||||||
|
|
||||||
//todo static httpclient
|
|
||||||
namespace NadekoBot.Modules.NSFW
|
namespace NadekoBot.Modules.NSFW
|
||||||
{
|
{
|
||||||
public class NSFW : NadekoTopLevelModule<SearchesService>
|
public class NSFW : NadekoTopLevelModule<SearchesService>
|
||||||
@ -160,10 +158,7 @@ namespace NadekoBot.Modules.NSFW
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
JToken obj;
|
JToken obj;
|
||||||
using (var http = new HttpClient())
|
obj = JArray.Parse(await _service.Http.GetStringAsync($"http://api.oboobs.ru/boobs/{new NadekoRandom().Next(0, 10330)}").ConfigureAwait(false))[0];
|
||||||
{
|
|
||||||
obj = JArray.Parse(await http.GetStringAsync($"http://api.oboobs.ru/boobs/{new NadekoRandom().Next(0, 10330)}").ConfigureAwait(false))[0];
|
|
||||||
}
|
|
||||||
await Context.Channel.SendMessageAsync($"http://media.oboobs.ru/{obj["preview"]}").ConfigureAwait(false);
|
await Context.Channel.SendMessageAsync($"http://media.oboobs.ru/{obj["preview"]}").ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -178,10 +173,7 @@ namespace NadekoBot.Modules.NSFW
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
JToken obj;
|
JToken obj;
|
||||||
using (var http = new HttpClient())
|
obj = JArray.Parse(await _service.Http.GetStringAsync($"http://api.obutts.ru/butts/{new NadekoRandom().Next(0, 4335)}").ConfigureAwait(false))[0];
|
||||||
{
|
|
||||||
obj = JArray.Parse(await http.GetStringAsync($"http://api.obutts.ru/butts/{new NadekoRandom().Next(0, 4335)}").ConfigureAwait(false))[0];
|
|
||||||
}
|
|
||||||
await Context.Channel.SendMessageAsync($"http://media.obutts.ru/{obj["preview"]}").ConfigureAwait(false);
|
await Context.Channel.SendMessageAsync($"http://media.obutts.ru/{obj["preview"]}").ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
@ -20,18 +20,25 @@ namespace NadekoBot.Modules.Searches.Common
|
|||||||
|
|
||||||
private readonly SortedSet<ImageCacherObject> _cache;
|
private readonly SortedSet<ImageCacherObject> _cache;
|
||||||
private readonly Logger _log;
|
private readonly Logger _log;
|
||||||
|
private readonly HttpClient _http;
|
||||||
|
|
||||||
public SearchImageCacher()
|
public SearchImageCacher()
|
||||||
{
|
{
|
||||||
|
_http = new HttpClient();
|
||||||
|
_http.AddFakeHeaders();
|
||||||
|
|
||||||
_log = LogManager.GetCurrentClassLogger();
|
_log = LogManager.GetCurrentClassLogger();
|
||||||
_rng = new NadekoRandom();
|
_rng = new NadekoRandom();
|
||||||
_cache = new SortedSet<ImageCacherObject>();
|
_cache = new SortedSet<ImageCacherObject>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ImageCacherObject> GetImage(string tag, bool forceExplicit, DapiSearchType type)
|
public async Task<ImageCacherObject> GetImage(string tag, bool forceExplicit, DapiSearchType type,
|
||||||
|
HashSet<string> blacklistedTags = null)
|
||||||
{
|
{
|
||||||
tag = tag?.ToLowerInvariant();
|
tag = tag?.ToLowerInvariant();
|
||||||
|
|
||||||
|
blacklistedTags = blacklistedTags ?? new HashSet<string>();
|
||||||
|
|
||||||
if (type == DapiSearchType.E621)
|
if (type == DapiSearchType.E621)
|
||||||
tag = tag?.Replace("yuri", "female/female");
|
tag = tag?.Replace("yuri", "female/female");
|
||||||
|
|
||||||
@ -63,6 +70,9 @@ namespace NadekoBot.Modules.Searches.Common
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
var images = await DownloadImages(tag, forceExplicit, type).ConfigureAwait(false);
|
var images = await DownloadImages(tag, forceExplicit, type).ConfigureAwait(false);
|
||||||
|
images = images
|
||||||
|
.Where(x => x.Tags.All(t => !blacklistedTags.Contains(t)))
|
||||||
|
.ToArray();
|
||||||
if (images.Length == 0)
|
if (images.Length == 0)
|
||||||
return null;
|
return null;
|
||||||
var toReturn = images[_rng.Next(images.Length)];
|
var toReturn = images[_rng.Next(images.Length)];
|
||||||
@ -116,48 +126,40 @@ namespace NadekoBot.Modules.Searches.Common
|
|||||||
website = $"https://yande.re/post.json?limit=100&tags={tag}";
|
website = $"https://yande.re/post.json?limit=100&tags={tag}";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
using (var http = new HttpClient())
|
|
||||||
{
|
|
||||||
http.AddFakeHeaders();
|
|
||||||
|
|
||||||
if (type == DapiSearchType.Konachan || type == DapiSearchType.Yandere ||
|
if (type == DapiSearchType.Konachan || type == DapiSearchType.Yandere ||
|
||||||
type == DapiSearchType.E621 || type == DapiSearchType.Danbooru)
|
type == DapiSearchType.E621 || type == DapiSearchType.Danbooru)
|
||||||
{
|
{
|
||||||
var data = await http.GetStringAsync(website).ConfigureAwait(false);
|
var data = await _http.GetStringAsync(website).ConfigureAwait(false);
|
||||||
return JsonConvert.DeserializeObject<DapiImageObject[]>(data)
|
return JsonConvert.DeserializeObject<DapiImageObject[]>(data)
|
||||||
.Where(x => x.File_Url != null)
|
.Where(x => x.File_Url != null)
|
||||||
.Select(x => new ImageCacherObject(x, type))
|
.Select(x => new ImageCacherObject(x, type))
|
||||||
.ToArray();
|
.ToArray();
|
||||||
}
|
|
||||||
|
|
||||||
return (await LoadXmlAsync(website, type)).ToArray();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return (await LoadXmlAsync(website, type)).ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<ImageCacherObject[]> LoadXmlAsync(string website, DapiSearchType type)
|
private async Task<ImageCacherObject[]> LoadXmlAsync(string website, DapiSearchType type)
|
||||||
{
|
{
|
||||||
var list = new List<ImageCacherObject>();
|
var list = new List<ImageCacherObject>();
|
||||||
using (var http = new HttpClient())
|
using (var reader = XmlReader.Create(await _http.GetStreamAsync(website), new XmlReaderSettings()
|
||||||
{
|
{
|
||||||
using (var reader = XmlReader.Create(await http.GetStreamAsync(website), new XmlReaderSettings()
|
Async = true,
|
||||||
|
}))
|
||||||
|
{
|
||||||
|
while (await reader.ReadAsync())
|
||||||
{
|
{
|
||||||
Async = true,
|
if (reader.NodeType == XmlNodeType.Element &&
|
||||||
}))
|
reader.Name == "post")
|
||||||
{
|
|
||||||
while (await reader.ReadAsync())
|
|
||||||
{
|
{
|
||||||
if (reader.NodeType == XmlNodeType.Element &&
|
list.Add(new ImageCacherObject(new DapiImageObject()
|
||||||
reader.Name == "post")
|
|
||||||
{
|
{
|
||||||
list.Add(new ImageCacherObject(new DapiImageObject()
|
File_Url = reader["file_url"],
|
||||||
{
|
Tags = reader["tags"],
|
||||||
File_Url = reader["file_url"],
|
Rating = reader["rating"] ?? "e"
|
||||||
Tags = reader["tags"],
|
|
||||||
Rating = reader["rating"] ?? "e"
|
|
||||||
|
|
||||||
}, type));
|
}, type));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,11 +14,14 @@ using NadekoBot.Services.Database.Models;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using NadekoBot.Modules.NSFW.Exceptions;
|
using NadekoBot.Modules.NSFW.Exceptions;
|
||||||
|
using System.Net.Http;
|
||||||
|
|
||||||
namespace NadekoBot.Modules.Searches.Services
|
namespace NadekoBot.Modules.Searches.Services
|
||||||
{
|
{
|
||||||
public class SearchesService : INService
|
public class SearchesService : INService
|
||||||
{
|
{
|
||||||
|
public HttpClient Http { get; }
|
||||||
|
|
||||||
private readonly DiscordSocketClient _client;
|
private readonly DiscordSocketClient _client;
|
||||||
private readonly IGoogleApiService _google;
|
private readonly IGoogleApiService _google;
|
||||||
private readonly DbService _db;
|
private readonly DbService _db;
|
||||||
@ -41,6 +44,8 @@ namespace NadekoBot.Modules.Searches.Services
|
|||||||
|
|
||||||
public SearchesService(DiscordSocketClient client, IGoogleApiService google, DbService db, IEnumerable<GuildConfig> gcs)
|
public SearchesService(DiscordSocketClient client, IGoogleApiService google, DbService db, IEnumerable<GuildConfig> gcs)
|
||||||
{
|
{
|
||||||
|
Http = new HttpClient();
|
||||||
|
Http.AddFakeHeaders();
|
||||||
_client = client;
|
_client = client;
|
||||||
_google = google;
|
_google = google;
|
||||||
_db = db;
|
_db = db;
|
||||||
@ -128,14 +133,26 @@ namespace NadekoBot.Modules.Searches.Services
|
|||||||
|
|
||||||
public Task<ImageCacherObject> DapiSearch(string tag, DapiSearchType type, ulong? guild, bool isExplicit = false)
|
public Task<ImageCacherObject> DapiSearch(string tag, DapiSearchType type, ulong? guild, bool isExplicit = false)
|
||||||
{
|
{
|
||||||
if (guild.HasValue && GetBlacklistedTags(guild.Value)
|
if (guild.HasValue)
|
||||||
.Any(x => tag.ToLowerInvariant().Contains(x)))
|
|
||||||
{
|
{
|
||||||
throw new TagBlacklistedException();
|
var blacklistedTags = GetBlacklistedTags(guild.Value);
|
||||||
|
|
||||||
|
if (blacklistedTags
|
||||||
|
.Any(x => tag.ToLowerInvariant().Contains(x)))
|
||||||
|
{
|
||||||
|
throw new TagBlacklistedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
var cacher = _imageCacher.GetOrAdd(guild.Value, (key) => new SearchImageCacher());
|
||||||
|
|
||||||
|
return cacher.GetImage(tag, isExplicit, type, blacklistedTags);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var cacher = _imageCacher.GetOrAdd(guild ?? 0, (key) => new SearchImageCacher());
|
||||||
|
|
||||||
|
return cacher.GetImage(tag, isExplicit, type);
|
||||||
}
|
}
|
||||||
var cacher = _imageCacher.GetOrAdd(guild ?? 0, (key) => new SearchImageCacher());
|
|
||||||
|
|
||||||
return cacher.GetImage(tag, isExplicit, type);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public HashSet<string> GetBlacklistedTags(ulong guildId)
|
public HashSet<string> GetBlacklistedTags(ulong guildId)
|
||||||
|
Loading…
Reference in New Issue
Block a user