Skip to content

Commit 90fc74d

Browse files
committed
More refactoring & cleanup
1 parent 1d57b45 commit 90fc74d

33 files changed

+414
-476
lines changed
+125-164
Original file line numberDiff line numberDiff line change
@@ -1,191 +1,75 @@
1-
global using R1 = SmartImage.Lib.Resources;
1+
// Author: Deci | Project: SmartImage.Lib | Name: BaseSearchEngine.cs
2+
// Date: 2024/06/06 @ 14:06:00
3+
4+
global using R1 = SmartImage.Lib.Resources;
25
global using Url = Flurl.Url;
3-
using System.Collections.Concurrent;
46
using System.Diagnostics;
5-
using System.Drawing;
6-
using System.Text.Json;
7-
using System.Resources;
8-
using Novus.Utilities;
9-
using SmartImage.Lib.Results;
10-
using AngleSharp.Dom;
11-
using AngleSharp.Html.Parser;
127
using Flurl.Http;
13-
using Flurl.Http.Configuration;
148
using Kantan.Diagnostics;
159
using Kantan.Net.Utilities;
16-
using Kantan.Net.Web;
17-
using Microsoft.Extensions.Http.Logging;
1810
using Microsoft.Extensions.Logging;
1911
using SmartImage.Lib.Engines.Impl.Search;
2012
using SmartImage.Lib.Engines.Impl.Search.Other;
21-
using SmartImage.Lib.Images;
22-
using SmartImage.Lib.Utilities;
23-
using SmartImage.Lib.Results.Data;
24-
using SmartImage.Lib.Diagnostics;
13+
using SmartImage.Lib.Results;
2514
using SmartImage.Lib.Utilities.Diagnostics;
2615

2716
namespace SmartImage.Lib.Engines;
2817
#nullable enable
2918
public abstract class BaseSearchEngine : IDisposable, IEquatable<BaseSearchEngine>
3019
{
3120

32-
protected static FlurlClient Client { get; }
33-
34-
/// <summary>
35-
/// The corresponding <see cref="SearchEngineOptions"/> of this engine
36-
/// </summary>
37-
public abstract SearchEngineOptions EngineOption { get; }
38-
39-
/// <summary>
40-
/// Name of this engine
41-
/// </summary>
42-
public virtual string Name => EngineOption.ToString();
43-
44-
public virtual Url BaseUrl { get; }
45-
46-
public bool IsAdvanced { get; protected init; }
47-
48-
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(15);
49-
50-
public string? EndpointUrl { get; }
51-
52-
protected long? MaxSize { get; set; }
53-
54-
protected virtual string[] ErrorBodyMessages { get; } = [];
55-
56-
protected BaseSearchEngine(string baseUrl, string? endpoint = null)
57-
{
58-
BaseUrl = baseUrl;
59-
IsAdvanced = true;
60-
EndpointUrl = endpoint;
61-
MaxSize = null;
62-
}
63-
64-
protected static readonly ILogger Logger = AppSupport.Factory.CreateLogger(nameof(BaseSearchEngine));
65-
66-
/*protected IFlurlRequest Build(IFlurlRequest request)
67-
{
68-
return request.WithTimeout(Timeout);
69-
}*/
70-
71-
7221
static BaseSearchEngine()
7322
{
74-
/*var handler = new LoggingHttpMessageHandler(Logger)
75-
{
76-
InnerHandler = new HttpLoggingHandler(Logger)
77-
{
78-
InnerHandler = new HttpClientHandler()
79-
}
80-
};
81-
82-
Client = new FlurlClient(new HttpClient(handler))
83-
{
84-
Settings =
85-
{
86-
Redirects =
87-
{
88-
Enabled = true,
89-
AllowSecureToInsecure = true,
90-
ForwardAuthorizationHeader = true,
91-
MaxAutoRedirects = 20,
92-
},
93-
}
94-
};*/
95-
96-
9723
Client = (FlurlClient) FlurlHttp.Clients.GetOrAdd(nameof(BaseSearchEngine), null, builder =>
9824
{
9925
builder.Headers.AddOrReplace("User-Agent", HttpUtilities.UserAgent);
100-
26+
10127
// builder.Settings.JsonSerializer = new DefaultJsonSerializer();
10228

10329
builder.Settings.AllowedHttpStatusRange = "*";
104-
builder.OnError(f=>
30+
31+
builder.OnError(f =>
10532
{
33+
Debugger.Break();
10634
Logger.LogError(f.Exception, $"from {f.Request}");
107-
return;
10835
});
10936

11037
builder.AddMiddleware(() => new HttpLoggingHandler(Logger));
11138

112-
});;
113-
}
114-
115-
public override string ToString()
116-
{
117-
return $"{Name}: {BaseUrl} {Timeout}";
118-
}
119-
120-
public virtual async Task<SearchResult> GetResultAsync(SearchQuery query, CancellationToken token = default)
121-
{
122-
var b = await VerifyQueryAsync(query);
123-
124-
/*
125-
if (!b) {
126-
// throw new SmartImageException($"{query}");
127-
Debug.WriteLine($"{query} : Verification error", LogCategories.C_ERROR);
128-
}
129-
*/
130-
131-
var srs = b ? SearchResultStatus.None : SearchResultStatus.IllegalInput;
132-
133-
var res = new SearchResult(this)
134-
{
135-
RawUrl = GetRawUrl(query),
136-
ErrorMessage = null,
137-
Status = srs
138-
};
139-
140-
lock (res.Results) {
141-
res.Results.Add(res.GetRawResultItem());
142-
}
143-
144-
Debug.WriteLine($"{Name} | {query} - {res.Status}", LogCategories.C_INFO);
145-
146-
return res;
39+
});
40+
;
14741
}
14842

149-
protected virtual Url GetRawUrl(SearchQuery query)
43+
protected BaseSearchEngine(string baseUrl, string? endpoint = null)
15044
{
151-
//
152-
Url u = ((BaseUrl + query.Upload));
153-
154-
return u;
45+
BaseUrl = baseUrl;
46+
EndpointUrl = endpoint;
47+
MaxSize = null;
15548
}
15649

157-
public virtual ValueTask<bool> VerifyQueryAsync(SearchQuery q)
158-
{
159-
/*if (q.Upload is not { }) {
160-
return false;
161-
}*/
162-
163-
bool b = true;
50+
protected static readonly ILogger Logger = AppSupport.Factory.CreateLogger(nameof(BaseSearchEngine));
16451

165-
if (MaxSize.HasValue) {
166-
b = q.Source.Size <= MaxSize;
167-
}
52+
/// <summary>
53+
/// The corresponding <see cref="SearchEngineOptions" /> of this engine
54+
/// </summary>
55+
public abstract SearchEngineOptions EngineOption { get; }
16856

169-
/*if (MaxSize == NA_SIZE || q.Size == NA_SIZE) {
170-
b = true;
171-
}
57+
/// <summary>
58+
/// Name of this engine
59+
/// </summary>
60+
public virtual string Name => EngineOption.ToString();
17261

173-
else {
174-
b = q.Size <= MaxSize;
175-
}*/
62+
public virtual Url BaseUrl { get; }
17663

177-
return ValueTask.FromResult(b);
178-
}
64+
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(15);
17965

66+
public string? EndpointUrl { get; }
18067

181-
// TODO: move config application to ctors?
68+
protected long? MaxSize { get; set; }
18269

183-
public abstract void Dispose();
70+
protected virtual string[] ErrorBodyMessages { get; } = [];
18471

185-
/*
186-
public static readonly BaseSearchEngine[] All =
187-
ReflectionHelper.CreateAllInAssembly<BaseSearchEngine>(InheritanceProperties.Subclass).ToArray();
188-
*/
72+
protected static FlurlClient Client { get; }
18973

19074
public static IEnumerable<BaseSearchEngine> GetSelectedEngines(SearchEngineOptions options)
19175
{
@@ -242,21 +126,87 @@ public static IEnumerable<BaseSearchEngine> GetSelectedEngines(SearchEngineOptio
242126

243127
}
244128

245-
#region
129+
public static bool operator ==(BaseSearchEngine? left, BaseSearchEngine? right)
130+
{
131+
return Equals(left, right);
132+
}
133+
134+
public static bool operator !=(BaseSearchEngine? left, BaseSearchEngine? right)
135+
{
136+
return !Equals(left, right);
137+
}
246138

247-
public bool Equals(BaseSearchEngine? other)
139+
public virtual async Task<SearchResult> GetResultAsync(SearchQuery query, CancellationToken token = default)
248140
{
249-
if (ReferenceEquals(null, other)) return false;
250-
if (ReferenceEquals(this, other)) return true;
141+
var b = await VerifyQueryAsync(query);
251142

252-
return EngineOption == other.EngineOption;
143+
/*
144+
if (!b) {
145+
// throw new SmartImageException($"{query}");
146+
Debug.WriteLine($"{query} : Verification error", LogCategories.C_ERROR);
147+
}
148+
*/
149+
150+
var srs = b ? SearchResultStatus.None : SearchResultStatus.IllegalInput;
151+
152+
var res = new SearchResult(this)
153+
{
154+
RawUrl = GetRawUrl(query),
155+
ErrorMessage = null,
156+
Status = srs
157+
};
158+
159+
lock (res.Results) {
160+
res.Results.Add(res.GetRawResultItem());
161+
}
162+
163+
Debug.WriteLine($"{Name} | {query} - {res.Status}", LogCategories.C_INFO);
164+
165+
return res;
166+
}
167+
168+
protected virtual Url GetRawUrl(SearchQuery query)
169+
{
170+
//
171+
Url u = ((BaseUrl + query.Upload));
172+
173+
return u;
174+
}
175+
176+
public virtual ValueTask<bool> VerifyQueryAsync(SearchQuery q)
177+
{
178+
bool b = true;
179+
180+
if (MaxSize.HasValue) {
181+
b = q.Source.Size <= MaxSize;
182+
}
183+
184+
return ValueTask.FromResult(b);
185+
}
186+
187+
public int GetHashCode(BaseSearchEngine obj)
188+
{
189+
return (int) obj.EngineOption;
190+
}
191+
192+
public override string ToString()
193+
{
194+
return $"{Name}: {BaseUrl} {Timeout}";
253195
}
254196

255197
public override bool Equals(object? obj)
256198
{
257-
if (ReferenceEquals(null, obj)) return false;
258-
if (ReferenceEquals(this, obj)) return true;
259-
if (obj.GetType() != this.GetType()) return false;
199+
if (obj is null) {
200+
return false;
201+
}
202+
203+
if (ReferenceEquals(this, obj)) {
204+
return true;
205+
}
206+
207+
if (obj.GetType() != GetType()) {
208+
return false;
209+
}
260210

261211
return Equals((BaseSearchEngine) obj);
262212
}
@@ -266,21 +216,32 @@ public override int GetHashCode()
266216
return (int) EngineOption;
267217
}
268218

269-
public static bool operator ==(BaseSearchEngine? left, BaseSearchEngine? right)
270-
{
271-
return Equals(left, right);
272-
}
219+
public abstract void Dispose();
273220

274-
public static bool operator !=(BaseSearchEngine? left, BaseSearchEngine? right)
221+
public bool Equals(BaseSearchEngine? other)
275222
{
276-
return !Equals(left, right);
223+
if (other is null) {
224+
return false;
225+
}
226+
227+
if (ReferenceEquals(this, other)) {
228+
return true;
229+
}
230+
231+
return EngineOption == other.EngineOption;
277232
}
278233

279-
public int GetHashCode(BaseSearchEngine obj)
234+
/*protected IFlurlRequest Build(IFlurlRequest request)
280235
{
281-
return (int) obj.EngineOption;
282-
}
236+
return request.WithTimeout(Timeout);
237+
}*/
238+
283239

284-
#endregion
240+
// TODO: move config application to ctors?
241+
242+
/*
243+
public static readonly BaseSearchEngine[] All =
244+
ReflectionHelper.CreateAllInAssembly<BaseSearchEngine>(InheritanceProperties.Subclass).ToArray();
245+
*/
285246

286247
}

0 commit comments

Comments
 (0)