2017-07-17 19:42:36 +00:00
|
|
|
|
using System;
|
2017-06-29 15:58:16 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common;
|
|
|
|
|
using NadekoBot.Extensions;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using NLog;
|
2017-06-29 15:58:16 +00:00
|
|
|
|
|
2017-07-17 19:42:36 +00:00
|
|
|
|
namespace NadekoBot.Modules.Searches.Common
|
2017-06-29 15:58:16 +00:00
|
|
|
|
{
|
|
|
|
|
public class SearchImageCacher
|
|
|
|
|
{
|
|
|
|
|
private readonly NadekoRandom _rng;
|
|
|
|
|
private readonly ConcurrentDictionary<DapiSearchType, SemaphoreSlim> _locks = new ConcurrentDictionary<DapiSearchType, SemaphoreSlim>();
|
|
|
|
|
|
|
|
|
|
private readonly SortedSet<ImageCacherObject> _cache;
|
2017-07-02 11:53:09 +00:00
|
|
|
|
private readonly Logger _log;
|
2017-09-10 21:44:24 +00:00
|
|
|
|
private readonly HttpClient _http;
|
2017-06-29 15:58:16 +00:00
|
|
|
|
|
|
|
|
|
public SearchImageCacher()
|
|
|
|
|
{
|
2017-09-10 21:44:24 +00:00
|
|
|
|
_http = new HttpClient();
|
|
|
|
|
_http.AddFakeHeaders();
|
|
|
|
|
|
2017-07-02 11:53:09 +00:00
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
2017-06-29 15:58:16 +00:00
|
|
|
|
_rng = new NadekoRandom();
|
|
|
|
|
_cache = new SortedSet<ImageCacherObject>();
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-10 21:44:24 +00:00
|
|
|
|
public async Task<ImageCacherObject> GetImage(string tag, bool forceExplicit, DapiSearchType type,
|
|
|
|
|
HashSet<string> blacklistedTags = null)
|
2017-06-29 15:58:16 +00:00
|
|
|
|
{
|
|
|
|
|
tag = tag?.ToLowerInvariant();
|
|
|
|
|
|
2017-09-10 21:44:24 +00:00
|
|
|
|
blacklistedTags = blacklistedTags ?? new HashSet<string>();
|
|
|
|
|
|
2017-06-29 15:58:16 +00:00
|
|
|
|
if (type == DapiSearchType.E621)
|
|
|
|
|
tag = tag?.Replace("yuri", "female/female");
|
|
|
|
|
|
|
|
|
|
var _lock = GetLock(type);
|
|
|
|
|
await _lock.WaitAsync();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ImageCacherObject[] imgs;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(tag))
|
|
|
|
|
{
|
|
|
|
|
imgs = _cache.Where(x => x.Tags.IsSupersetOf(tag.Split('+')) && x.SearchType == type && (!forceExplicit || x.Rating == "e")).ToArray();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tag = null;
|
|
|
|
|
imgs = _cache.Where(x => x.SearchType == type).ToArray();
|
|
|
|
|
}
|
|
|
|
|
ImageCacherObject img;
|
|
|
|
|
if (imgs.Length == 0)
|
|
|
|
|
img = null;
|
|
|
|
|
else
|
|
|
|
|
img = imgs[_rng.Next(imgs.Length)];
|
|
|
|
|
|
|
|
|
|
if (img != null)
|
|
|
|
|
{
|
|
|
|
|
_cache.Remove(img);
|
|
|
|
|
return img;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var images = await DownloadImages(tag, forceExplicit, type).ConfigureAwait(false);
|
2017-09-10 21:44:24 +00:00
|
|
|
|
images = images
|
|
|
|
|
.Where(x => x.Tags.All(t => !blacklistedTags.Contains(t)))
|
|
|
|
|
.ToArray();
|
2017-06-29 15:58:16 +00:00
|
|
|
|
if (images.Length == 0)
|
|
|
|
|
return null;
|
|
|
|
|
var toReturn = images[_rng.Next(images.Length)];
|
2017-09-12 21:49:37 +00:00
|
|
|
|
#if !GLOBAL_NADEKO
|
2017-06-29 15:58:16 +00:00
|
|
|
|
foreach (var dledImg in images)
|
|
|
|
|
{
|
|
|
|
|
if(dledImg != toReturn)
|
|
|
|
|
_cache.Add(dledImg);
|
|
|
|
|
}
|
2017-09-12 21:49:37 +00:00
|
|
|
|
#endif
|
2017-06-29 15:58:16 +00:00
|
|
|
|
return toReturn;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_lock.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private SemaphoreSlim GetLock(DapiSearchType type)
|
|
|
|
|
{
|
|
|
|
|
return _locks.GetOrAdd(type, _ => new SemaphoreSlim(1, 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<ImageCacherObject[]> DownloadImages(string tag, bool isExplicit, DapiSearchType type)
|
|
|
|
|
{
|
|
|
|
|
tag = tag?.Replace(" ", "_").ToLowerInvariant();
|
|
|
|
|
if (isExplicit)
|
|
|
|
|
tag = "rating%3Aexplicit+" + tag;
|
|
|
|
|
var website = "";
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case DapiSearchType.Safebooru:
|
|
|
|
|
website = $"https://safebooru.org/index.php?page=dapi&s=post&q=index&limit=1000&tags={tag}";
|
|
|
|
|
break;
|
|
|
|
|
case DapiSearchType.E621:
|
|
|
|
|
website = $"https://e621.net/post/index.json?limit=1000&tags={tag}";
|
|
|
|
|
break;
|
|
|
|
|
case DapiSearchType.Danbooru:
|
2017-08-04 12:36:07 +00:00
|
|
|
|
website = $"http://danbooru.donmai.us/posts.json?limit=100&tags={tag}";
|
2017-06-29 15:58:16 +00:00
|
|
|
|
break;
|
|
|
|
|
case DapiSearchType.Gelbooru:
|
2017-08-04 12:36:07 +00:00
|
|
|
|
website = $"http://gelbooru.com/index.php?page=dapi&s=post&q=index&limit=100&tags={tag}";
|
2017-06-29 15:58:16 +00:00
|
|
|
|
break;
|
|
|
|
|
case DapiSearchType.Rule34:
|
|
|
|
|
website = $"https://rule34.xxx/index.php?page=dapi&s=post&q=index&limit=100&tags={tag}";
|
|
|
|
|
break;
|
|
|
|
|
case DapiSearchType.Konachan:
|
2017-08-04 12:36:07 +00:00
|
|
|
|
website = $"https://konachan.com/post.json?s=post&q=index&limit=100&tags={tag}";
|
2017-06-29 15:58:16 +00:00
|
|
|
|
break;
|
|
|
|
|
case DapiSearchType.Yandere:
|
2017-08-04 12:36:07 +00:00
|
|
|
|
website = $"https://yande.re/post.json?limit=100&tags={tag}";
|
2017-06-29 15:58:16 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-10 21:44:24 +00:00
|
|
|
|
if (type == DapiSearchType.Konachan || type == DapiSearchType.Yandere ||
|
|
|
|
|
type == DapiSearchType.E621 || type == DapiSearchType.Danbooru)
|
|
|
|
|
{
|
|
|
|
|
var data = await _http.GetStringAsync(website).ConfigureAwait(false);
|
|
|
|
|
return JsonConvert.DeserializeObject<DapiImageObject[]>(data)
|
|
|
|
|
.Where(x => x.File_Url != null)
|
|
|
|
|
.Select(x => new ImageCacherObject(x, type))
|
|
|
|
|
.ToArray();
|
2017-06-29 15:58:16 +00:00
|
|
|
|
}
|
2017-09-10 21:44:24 +00:00
|
|
|
|
|
|
|
|
|
return (await LoadXmlAsync(website, type)).ToArray();
|
2017-06-29 15:58:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<ImageCacherObject[]> LoadXmlAsync(string website, DapiSearchType type)
|
|
|
|
|
{
|
2017-08-04 12:36:07 +00:00
|
|
|
|
var list = new List<ImageCacherObject>();
|
2017-09-10 21:44:24 +00:00
|
|
|
|
using (var reader = XmlReader.Create(await _http.GetStreamAsync(website), new XmlReaderSettings()
|
2017-06-29 15:58:16 +00:00
|
|
|
|
{
|
2017-09-10 21:44:24 +00:00
|
|
|
|
Async = true,
|
|
|
|
|
}))
|
|
|
|
|
{
|
|
|
|
|
while (await reader.ReadAsync())
|
2017-06-29 15:58:16 +00:00
|
|
|
|
{
|
2017-09-10 21:44:24 +00:00
|
|
|
|
if (reader.NodeType == XmlNodeType.Element &&
|
|
|
|
|
reader.Name == "post")
|
2017-06-29 15:58:16 +00:00
|
|
|
|
{
|
2017-09-10 21:44:24 +00:00
|
|
|
|
list.Add(new ImageCacherObject(new DapiImageObject()
|
2017-06-29 15:58:16 +00:00
|
|
|
|
{
|
2017-09-10 21:44:24 +00:00
|
|
|
|
File_Url = reader["file_url"],
|
|
|
|
|
Tags = reader["tags"],
|
|
|
|
|
Rating = reader["rating"] ?? "e"
|
2017-06-29 15:58:16 +00:00
|
|
|
|
|
2017-09-10 21:44:24 +00:00
|
|
|
|
}, type));
|
2017-06-29 15:58:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list.ToArray();
|
|
|
|
|
}
|
2017-09-11 17:30:02 +00:00
|
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
_cache.Clear();
|
|
|
|
|
}
|
2017-06-29 15:58:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ImageCacherObject : IComparable<ImageCacherObject>
|
|
|
|
|
{
|
|
|
|
|
public DapiSearchType SearchType { get; }
|
|
|
|
|
public string FileUrl { get; }
|
|
|
|
|
public HashSet<string> Tags { get; }
|
|
|
|
|
public string Rating { get; }
|
|
|
|
|
|
|
|
|
|
public ImageCacherObject(DapiImageObject obj, DapiSearchType type)
|
|
|
|
|
{
|
|
|
|
|
if (type == DapiSearchType.Danbooru)
|
|
|
|
|
this.FileUrl = "https://danbooru.donmai.us" + obj.File_Url;
|
|
|
|
|
else
|
|
|
|
|
this.FileUrl = obj.File_Url.StartsWith("http") ? obj.File_Url : "https:" + obj.File_Url;
|
|
|
|
|
this.SearchType = type;
|
|
|
|
|
this.Rating = obj.Rating;
|
|
|
|
|
this.Tags = new HashSet<string>((obj.Tags ?? obj.Tag_String).Split(' '));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return FileUrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int CompareTo(ImageCacherObject other)
|
|
|
|
|
{
|
|
|
|
|
return FileUrl.CompareTo(other.FileUrl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DapiImageObject
|
|
|
|
|
{
|
|
|
|
|
public string File_Url { get; set; }
|
|
|
|
|
public string Tags { get; set; }
|
|
|
|
|
public string Tag_String { get; set; }
|
|
|
|
|
public string Rating { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum DapiSearchType
|
|
|
|
|
{
|
|
|
|
|
Safebooru,
|
|
|
|
|
E621,
|
|
|
|
|
Gelbooru,
|
|
|
|
|
Konachan,
|
|
|
|
|
Rule34,
|
|
|
|
|
Yandere,
|
|
|
|
|
Danbooru
|
|
|
|
|
}
|
|
|
|
|
}
|