Skip to content

Commit 0e8bdb4

Browse files
committed
Add Ascii2D alternative domain
1 parent 5737aab commit 0e8bdb4

File tree

15 files changed

+176
-156
lines changed

15 files changed

+176
-156
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,8 @@ SmartImage.UI/Properties/*
473473
/SmartImage.Server/appsettings.json
474474
/SmartImage.Server/hostsettings.json
475475
/Profiling
476+
477+
*.jpg
478+
*.jpeg
479+
*.png
480+
*.gif

SmartImage.Lib/Engines/Impl/Search/Ascii2DEngine.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Date: 2024/06/06 @ 14:06:00
33

44
using System.Diagnostics;
5+
using System.Net;
6+
using System.Xml.Linq;
57
using AngleSharp.Dom;
68
using AngleSharp.Html.Dom;
79
using AngleSharp.Html.Parser;
@@ -39,7 +41,11 @@ protected override string[] ErrorBodyMessages
3941
"ごく最近、このURLからのダウンロードに失敗しています。少し時間を置いてください。"
4042
];
4143

42-
public Ascii2DEngine() : base("https://ascii2d.net/search/url/")
44+
public const string ALT_URL = "https://ascii2d.obfs.dev/search/url/";
45+
46+
public const string MAIN_URL = "https://ascii2d.net/search/url/";
47+
48+
public Ascii2DEngine() : base(ALT_URL)
4349
{
4450
Timeout = TimeSpan.FromSeconds(30);
4551
MaxSize = 10_000_000;
@@ -175,6 +181,11 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
175181
}
176182
else {
177183
using var res = await GetResponseByUrlAsync(origin, token);
184+
185+
if (res.StatusCode == (int) HttpStatusCode.BadGateway) {
186+
return null;
187+
}
188+
178189
str = await res.GetStringAsync();
179190
}
180191

@@ -220,6 +231,26 @@ private async Task<IFlurlResponse> GetResponseByUrlAsync(Url origin, Cancellatio
220231
return res;
221232
}
222233

234+
#region Overrides of WebSearchEngine
235+
236+
protected override async ValueTask<List<INode>> GetNodes(IDocument d)
237+
{
238+
var nodes = await base.GetNodes(d);
239+
240+
var cnt = nodes.RemoveAll(x =>
241+
{
242+
var e = x as IHtmlElement;
243+
244+
var b = e.Children is { Length: 1 } && e.Children[0].ClassName.Contains("hidden-md");
245+
246+
return b;
247+
});
248+
249+
return nodes;
250+
}
251+
252+
#endregion
253+
223254
protected override ValueTask<SearchResultItem> ParseResultItem(INode nx, SearchResult r)
224255
{
225256
var sri = new SearchResultItem(r);

SmartImage.Lib/Engines/Impl/Search/EHentaiEngine.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
149149
return await parser.ParseDocumentAsync(content, token);
150150
}
151151

152-
protected override ValueTask<INode[]> GetNodes(IDocument d)
152+
protected override ValueTask<List<INode>> GetNodes(IDocument d)
153153
{
154154
// Index 0 is table header
155-
var array = d.Body.SelectNodes(NodesSelector).ToArray();
155+
var array = d.Body.SelectNodes(NodesSelector);
156156

157-
if (array.Length != 0) {
157+
if (array.Count != 0) {
158158
array = array[1..];
159159

160160
}
@@ -186,7 +186,12 @@ public async ValueTask<bool> ApplyCookiesAsync(CancellationToken ct = default)
186186
var cookies = await Provider.GetOrLoadCookiesAsync(ct);
187187

188188
foreach (var bck in cookies) {
189+
189190
var cookie = bck.AsCookie();
191+
192+
if (cookie == null) {
193+
continue;
194+
}
190195
bool c = false;
191196

192197
if (UseExHentai) {

SmartImage.Lib/Engines/Impl/Search/SauceNaoEngine.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,19 @@ private async ValueTask GetWebResultsAsync(SearchQuery query, SearchResult sr)
143143
.PostMultipartAsync(m =>
144144
{
145145
m.AddString("url", query.Source.IsUri ? query.Source.ValueString : string.Empty);
146+
string s;
146147

147148
if (query.Source.IsUri) { }
148149
else if (query.Source.IsFile) {
149-
m.AddFile("file", query.Source.Value.ToString(), fileName: "image.png");
150-
}else {
151-
m.AddFile("file", query.Source.FilePath, fileName: "image.png");
150+
s = query.Source.ValueString;
151+
m.AddFile("file", s, fileName: "image.png");
152+
}
153+
else {
154+
s = query.Source.FilePath;
155+
m.AddFile("file", s, fileName: "image.png");
152156

153157
}
158+
154159
});
155160

156161
html = await response.GetStringAsync();
@@ -335,7 +340,7 @@ private static void Parse(INode result, SearchResult sr)
335340

336341
// var resultcontent = resulttablecontent.ChildNodes[1];
337342
// var resultcontentcolumn = resultcontent.ChildNodes[1];
338-
var resultcontent = ((IElement) result).GetElementsByClassName("resultcontent")[0];
343+
var resultcontent = ((IElement) result).GetElementsByClassName("resultcontent")[0];
339344
IHtmlCollection<IElement> resultcontentcolumn_rg = null;
340345

341346
if (result is IElement { } elem) {

SmartImage.Lib/Engines/Impl/Search/YandexEngine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,20 +266,20 @@ private static List<SearchResultItem> ParseExternalInfo(IDocument doc, SearchRes
266266

267267
public override void Dispose() { }
268268

269-
protected override async ValueTask<INode[]> GetNodes(IDocument doc)
269+
protected override async ValueTask<List<INode>> GetNodes(IDocument doc)
270270
{
271271
var tagsItem = doc.Body.SelectNodes(NodesSelector);
272272

273273
if (tagsItem.Count == 0) {
274274
// return await Task.FromResult(Enumerable.Empty<INode>());
275-
return await Task.FromResult(tagsItem.ToArray()).ConfigureAwait(false);
275+
return await Task.FromResult(tagsItem).ConfigureAwait(false);
276276

277277
// return tagsItem;
278278
}
279279

280280
var sizeTags = tagsItem.Where(sx => !sx.Parent.Parent.TryGetAttribute("class").Contains("CbirItem")).ToList();
281281

282-
return await Task.FromResult(sizeTags.ToArray()).ConfigureAwait(false);
282+
return await Task.FromResult(sizeTags).ConfigureAwait(false);
283283

284284
// return sizeTags;
285285
}

SmartImage.Lib/Engines/SearchEngineOptions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ public enum SearchEngineOptions
9797
/// </summary>
9898
Fluffle = 1 << 15,
9999

100-
#region
100+
101+
#region
101102

102103
/// <summary>
103104
/// All engines
@@ -109,6 +110,6 @@ public enum SearchEngineOptions
109110

110111
Advanced = All & ~ (Bing | GoogleImages | ImgOps | KarmaDecay)
111112

112-
#endregion
113+
#endregion
113114

114115
}

SmartImage.Lib/Engines/WebSearchEngine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
5858
}
5959
}
6060

61-
Debug.WriteLine($"{Name} :: {res.RawUrl} {doc.TextContent?.Length} {nodes.Length}",
61+
Debug.WriteLine($"{Name} :: {res.RawUrl} {doc.TextContent?.Length} {nodes.Count}",
6262
nameof(GetResultAsync));
6363

6464
res.Status = SearchResultStatus.Success;
@@ -106,9 +106,9 @@ protected virtual async Task<IDocument> GetDocumentAsync(SearchResult sr, Search
106106

107107
protected abstract ValueTask<SearchResultItem> ParseResultItem(INode n, SearchResult r);
108108

109-
protected virtual ValueTask<INode[]> GetNodes(IDocument d)
109+
protected virtual ValueTask<List<INode>> GetNodes(IDocument d)
110110
{
111-
return ValueTask.FromResult(d.Body.SelectNodes(NodesSelector).ToArray());
111+
return ValueTask.FromResult(d.Body.SelectNodes(NodesSelector));
112112
}
113113

114114
protected bool Validate([CBN] IDocument doc, SearchResult sr)

SmartImage.Lib/Images/Uni/UniImage.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,7 @@ public bool TryCalculateSimilarity(IHashable comparand)
264264
throw new InvalidOperationException();
265265
}
266266

267-
if (!Similarity.HasValue) {
268-
Similarity = CompareHash.Similarity(comparand.Hash.Value, Hash.Value);
269-
}
267+
Similarity ??= CompareHash.Similarity(comparand.Hash.Value, Hash.Value);
270268

271269
return Similarity.HasValue;
272270
}

SmartImage.Lib/Images/Uni/UniImageUri.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Author: Deci | Project: SmartImage.Lib | Name: UniImageUri.cs
22
// Date: 2024/07/17 @ 02:07:26
33

4+
using System.Collections.Immutable;
45
using System.Net;
56
using Flurl.Http;
67

@@ -64,8 +65,9 @@ public async ValueTask<bool> AllocResponseAsync(CancellationToken ct = default)
6465
return HasResponse;
6566
}
6667

67-
public static readonly List<string> RestrictedSchemes = ["file", "javascript", "cpu"];
68-
public static readonly List<string> LegalSchemes = ["http", "https"];
68+
public static readonly ImmutableArray<string> RestrictedSchemes = ["file", "javascript", "cpu"];
69+
70+
public static readonly ImmutableArray<string> LegalSchemes = ["http", "https"];
6971

7072
public static async ValueTask<IFlurlResponse> GetResponseAsync(Url value, CancellationToken ct)
7173
{

SmartImage.Lib/SearchClient.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
global using INN = JetBrains.Annotations.ItemNotNullAttribute;
55
using System.Collections;
66
using System.Collections.Concurrent;
7+
using System.Collections.Immutable;
78
using System.Diagnostics;
89
using System.Net;
910
using System.Net.NetworkInformation;
@@ -45,7 +46,7 @@ public sealed class SearchClient : IDisposable
4546

4647
public bool IsComplete { get; private set; }
4748

48-
public BaseSearchEngine[] Engines { get; private set; }
49+
public IEnumerable<BaseSearchEngine> Engines { get; private set; }
4950

5051
public bool ConfigApplied { get; private set; }
5152

@@ -99,12 +100,14 @@ public void OpenChannel()
99100
{
100101
var ok = ResultChannel?.Writer.TryComplete(new ChannelClosedException("Reopened channel"));
101102

102-
if (ok.HasValue && ok.Value) { }
103-
104103
ResultChannel = Channel.CreateUnbounded<SearchResult>(new UnboundedChannelOptions()
105104
{
106105
SingleWriter = true,
107106
});
107+
108+
if (ok.HasValue && ok.Value) { }
109+
110+
// throw new InvalidOperationException();
108111
}
109112

110113
/// <summary>
@@ -114,7 +117,7 @@ public void OpenChannel()
114117
/// <param name="scheduler"></param>
115118
/// <param name="token">Cancellation token passed to <see cref="WebSearchEngine.GetResultAsync(SearchQuery,CancellationToken)"/></param>
116119
public async Task<SearchResult[]> RunSearchAsync(SearchQuery query,
117-
TaskScheduler scheduler = default,
120+
TaskScheduler scheduler = null,
118121
CancellationToken token = default)
119122
{
120123
scheduler ??= TaskScheduler.Default;
@@ -140,7 +143,7 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query,
140143

141144
Debug.WriteLine($"Config: {Config} | {Engines.QuickJoin()}");
142145

143-
List<Task<SearchResult>> tasks = GetSearchTasks(query, scheduler, token).ToList();
146+
var tasks = GetSearchTasks(query, scheduler, token).ToHashSet();
144147

145148
var results = new SearchResult[tasks.Count];
146149
int i = 0;
@@ -186,6 +189,8 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query,
186189
results[i] = result;
187190
i++;
188191
}*/
192+
193+
189194
ret:
190195
CompleteSearchAsync();
191196
OnSearchComplete?.Invoke(this, results);
@@ -213,6 +218,7 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query,
213218
return results;
214219
}
215220

221+
[return: MN]
216222
public static SearchResultItem GetBest(SearchResult[] results)
217223
{
218224
var ordered = results.Select(x => x.GetBestResult())
@@ -278,7 +284,8 @@ public static void OpenResult([MN] Url url1)
278284
public IEnumerable<Task<SearchResult>> GetSearchTasks(SearchQuery query, TaskScheduler scheduler,
279285
CancellationToken token)
280286
{
281-
var tasks = Engines.Select(e =>
287+
288+
return Engines.Select(e =>
282289
{
283290
try {
284291
Debug.WriteLine($"Starting {e} for {query}");
@@ -300,10 +307,8 @@ public IEnumerable<Task<SearchResult>> GetSearchTasks(SearchQuery query, TaskSch
300307
// return Task.FromException(exception);
301308
}
302309

303-
return default;
310+
return null;
304311
});
305-
306-
return tasks;
307312
}
308313

309314
public async ValueTask LoadEnginesAsync(CancellationToken token = default)
@@ -312,7 +317,7 @@ public async ValueTask LoadEnginesAsync(CancellationToken token = default)
312317

313318
Trace.WriteLine("Loading engines");
314319

315-
Engines = BaseSearchEngine.GetSelectedEngines(Config.SearchEngines).ToArray();
320+
Engines = BaseSearchEngine.GetSelectedEngines(Config.SearchEngines);
316321

317322
if (Config.ReadCookies) {
318323

@@ -346,7 +351,7 @@ public async ValueTask LoadEnginesAsync(CancellationToken token = default)
346351

347352
if (Config.FlareSolverr && !FlareSolverrClient.Value.IsInitialized) {
348353

349-
var ok = FlareSolverrClient.Value.Configure(Config.FlareSolverrApiUrl);
354+
var ok = await FlareSolverrClient.Value.ApplyConfigAsync(Config,token);
350355

351356
if (!ok) {
352357
Debugger.Break();

0 commit comments

Comments
 (0)