Skip to content

Commit b844f6c

Browse files
committed
Refactoring
1 parent 07650cc commit b844f6c

21 files changed

+369
-338
lines changed

SmartImage.Lib/Engines/BaseSearchEngine.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
using Microsoft.Net.Http.Headers;
1515
using SmartImage.Lib.Clients;
1616
using SmartImage.Lib.Cookies;
17-
using SmartImage.Lib.Engines.Impl.Search;
18-
using SmartImage.Lib.Engines.Impl.Search.Other;
17+
using SmartImage.Lib.Engines.Search;
18+
using SmartImage.Lib.Engines.Search.Other;
1919
using SmartImage.Lib.Engines.Results;
2020
using SmartImage.Lib.Engines.Results.Model;
2121
using SmartImage.Lib.Engines.Search;
@@ -104,7 +104,7 @@ public virtual async Task<SearchResult> GetResultAsync(SearchQuery query, Cancel
104104
{
105105
var b = await VerifyQueryAsync(query).ConfigureAwait(false);
106106

107-
SmartImageException.Assert(b, nameof(query));
107+
// SmartImageException.Assert(b, nameof(query));
108108

109109
var srs = b ? SearchResultStatus.None : SearchResultStatus.IllegalInput;
110110

@@ -218,7 +218,7 @@ public static IEnumerable<BaseSearchEngine> GetSelectedEngines(SearchEngineOptio
218218
yield return new TinEyeEngine();
219219

220220
if (options.HasFlag(SearchEngineOptions.Iqdb))
221-
yield return new IqdbEngine<IqdbItem>();
221+
yield return new IqdbEngine();
222222

223223
if (options.HasFlag(SearchEngineOptions.TraceMoe))
224224
yield return new TraceMoeEngine();

SmartImage.Lib/Engines/Results/Model/ISimilarity.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
namespace SmartImage.Lib.Engines.Results.Model;
55

66
#pragma warning disable CS0168
7+
78
public interface ISimilarity
89
{
9-
10-
// todo
1110
public double? Similarity { get; }
12-
1311
}

SmartImage.Lib/Engines/Results/Model/ISourceItemParseable.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@
33

44
namespace SmartImage.Lib.Engines.Results.Model;
55

6-
public interface ISourceItemParseable<in TSource, out TItem>
6+
public interface ISearchResultItems<T> where T : SearchResultItem
7+
{
8+
9+
public static abstract T Parse(T[] t, SearchResult r);
10+
11+
}
12+
13+
public interface ISourceItemParseable<in TSource, out TItem>
714
where TItem : SearchResultItem
815
{
16+
917
public static abstract TItem ParseResultItem(TSource n, SearchResult r);
18+
1019
}

SmartImage.Lib/Engines/Results/SearchResult.cs

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,6 @@ namespace SmartImage.Lib.Engines.Results;
77

88
#nullable disable
99

10-
public enum SearchResultStatus
11-
{
12-
13-
/// <summary>
14-
/// N/A
15-
/// </summary>
16-
None = 0,
17-
18-
/// <summary>
19-
/// Result obtained successfully
20-
/// </summary>
21-
Success,
22-
23-
/// <summary>
24-
/// Engine is on cooldown due to too many requests
25-
/// </summary>
26-
Cooldown,
27-
28-
/// <summary>
29-
/// Obtaining results failed due to an engine error
30-
/// </summary>
31-
UnknownError,
32-
33-
IllegalInput,
34-
35-
/// <summary>
36-
/// Engine is unavailable
37-
/// </summary>
38-
Unavailable
39-
40-
}
41-
42-
[Flags]
43-
public enum SearchResultFlags
44-
{
45-
46-
None = 0,
47-
48-
/// <summary>
49-
/// Engine returned no results
50-
/// </summary>
51-
NoResults = 1 << 0,
52-
53-
/// <summary>
54-
/// Result is extraneous
55-
/// </summary>
56-
Extraneous = 1 << 1,
57-
58-
}
59-
6010
/// <summary>
6111
/// Root search result returned by a <see cref="BaseSearchEngine"/>
6212
/// </summary>
@@ -104,7 +54,7 @@ public Url RawUrl
10454
// private Lazy<SearchResultItem> m_rawResultItem;
10555

10656
[JI]
107-
public SearchResultItem RawResultItem { get; }
57+
public SearchResultItem RawResultItem { get; }
10858

10959
internal SearchResult(BaseSearchEngine bse, Url rawUrl)
11060
{
@@ -134,11 +84,12 @@ private SearchResultItem GetRawResultItem(Url rawUrl)
13484
};
13585
}
13686

137-
public void Update()
87+
public virtual void Update()
13888
{
13989
if (Status.IsUnknown()) { }
14090

141-
if (Status.IsError()) {
91+
if (Status.IsError())
92+
{
14293
return;
14394
}
14495

@@ -165,14 +116,15 @@ private bool SetField<T>(ref T field, T value, [CMN] string propertyName = null)
165116
[CBN]
166117
public SearchResultItem GetBestResult()
167118
{
168-
if (Results.Count == 0) {
169-
// This should never happen so long as results contains the raw item
170-
Debugger.Break();
171-
return null;
172-
}
119+
// This should never happen so long as results contains the raw item
120+
Debug.Assert(Results.Count == 0);
173121

174-
return Results.OrderByDescending(static r => r.Similarity)
175-
.FirstOrDefault(static r => Url.IsValid(r.Url));
122+
// *? IMPROVE
123+
124+
return Results.Where(static r => Url.IsValid(r.Url))
125+
.OrderByDescending(static r => r.Similarity)
126+
.ThenByDescending(static r=>r.Score)
127+
.FirstOrDefault();
176128
}
177129

178130
public override string ToString()
@@ -184,9 +136,60 @@ public void Dispose()
184136
{
185137
Debug.WriteLine($"Disposing {Engine.Name} with {Results.Count}", LogCategories.C_VERBOSE);
186138

187-
foreach (SearchResultItem item in Results) {
139+
foreach (SearchResultItem item in Results)
140+
{
188141
item.Dispose();
189142
}
190143
}
191144

145+
}
146+
147+
[Flags]
148+
public enum SearchResultFlags
149+
{
150+
151+
None = 0,
152+
153+
/// <summary>
154+
/// Engine returned no results
155+
/// </summary>
156+
NoResults = 1 << 0,
157+
158+
/// <summary>
159+
/// Result is extraneous
160+
/// </summary>
161+
Extraneous = 1 << 1,
162+
163+
}
164+
165+
public enum SearchResultStatus
166+
{
167+
168+
/// <summary>
169+
/// N/A
170+
/// </summary>
171+
None = 0,
172+
173+
/// <summary>
174+
/// Result obtained successfully
175+
/// </summary>
176+
Success,
177+
178+
/// <summary>
179+
/// Engine is on cooldown due to too many requests
180+
/// </summary>
181+
Cooldown,
182+
183+
/// <summary>
184+
/// Obtaining results failed due to an engine error
185+
/// </summary>
186+
UnknownError,
187+
188+
IllegalInput,
189+
190+
/// <summary>
191+
/// Engine is unavailable
192+
/// </summary>
193+
Unavailable
194+
192195
}

SmartImage.Lib/Engines/Results/SearchResultItem.cs

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#nullable disable
22
using System.Collections.Concurrent;
3+
using System.Data.SqlTypes;
34
using System.Diagnostics;
45
using System.Text.Json.Serialization;
56
using System.Threading.Channels;
@@ -115,6 +116,43 @@ public record SearchResultItem : IDisposable, IComparable<SearchResultItem>, ICo
115116
/// </summary>
116117
public bool IsRaw { get; }
117118

119+
public double Score
120+
{
121+
get
122+
{
123+
if (IsRaw)
124+
return 0;
125+
126+
var s = 0d;
127+
128+
if (HasUni)
129+
s += Uni.Count;
130+
131+
if (Similarity.HasValue)
132+
s += Similarity.Value * 0.66d;
133+
134+
if (Url.IsValid(Url))
135+
s++;
136+
137+
if (Url.IsValid(Thumbnail))
138+
s++;
139+
140+
int?[] ir = [Width, Height];
141+
s += ir.Count(static c => c.HasValue);
142+
143+
string[] p = [Title, Source, Artist, Description, Character, Site, ThumbnailTitle];
144+
s += p.Count(static c => !String.IsNullOrWhiteSpace(c));
145+
146+
if (Time.HasValue)
147+
s++;
148+
149+
if (Metadata is not null)
150+
s++;
151+
152+
return s;
153+
}
154+
}
155+
118156
internal SearchResultItem(SearchResult r, bool isRaw = false)
119157
{
120158
Root = r;
@@ -132,7 +170,8 @@ public SearchResultItem[] CreateChildren(string[] rg)
132170
{
133171
var rg2 = new SearchResultItem[rg.Length];
134172

135-
for (int i = 0; i < rg.Length; i++) {
173+
for (int i = 0; i < rg.Length; i++)
174+
{
136175

137176
rg2[i] = new SearchResultItem(this)
138177
{
@@ -174,11 +213,13 @@ public SearchResultItem[] CreateChildren(string[] rg)
174213

175214
public async ValueTask<bool> LoadThumbnail(CancellationToken ct = default)
176215
{
177-
if (Url.IsValid(Thumbnail) && !(HasUni && Uni.Any(u => u.ValueString == Thumbnail))) {
216+
if (Url.IsValid(Thumbnail) && !(HasUni && Uni.Any(u => u.ValueString == Thumbnail)))
217+
{
178218

179219
var uni = await UniImage.TryCreateAsync(Thumbnail, ct: ct);
180220

181-
if (uni == null) {
221+
if (uni == null)
222+
{
182223
return false;
183224
}
184225

@@ -193,11 +234,13 @@ public async Task<bool> ScanAsync(CancellationToken ct = default)
193234
// TODO: USE CHANNELS
194235
// TODO: REFACTOR TO USE THIS FUNCTION
195236

196-
if (HasUni) {
237+
if (HasUni)
238+
{
197239
return true;
198240
}
199241

200-
if (Url == null) {
242+
if (Url == null)
243+
{
201244
return false;
202245
}
203246

@@ -206,16 +249,19 @@ public async Task<bool> ScanAsync(CancellationToken ct = default)
206249

207250
var ch = Channel.CreateUnbounded<UniImage>(new UnboundedChannelOptions() { SingleReader = true });
208251

209-
var tasks = ImageScanner.ScanImagesAsync(Url, ch.Writer, ct: ct);
252+
var tasks = ImageScanner.ScanImagesAsync(Url, ch.Writer, ct: ct);
210253

211-
while (await ch.Reader.WaitToReadAsync(ct)) {
254+
while (await ch.Reader.WaitToReadAsync(ct))
255+
{
212256
var v = await ch.Reader.ReadAsync(ct);
213257

214-
if (v != UniImage.Null && v.HasImageFormat) {
258+
if (v != UniImage.Null && v.HasImageFormat)
259+
{
215260
buf.Add(v);
216261
}
217262

218-
if (ct.IsCancellationRequested) {
263+
if (ct.IsCancellationRequested)
264+
{
219265
break;
220266
}
221267
}
@@ -229,8 +275,6 @@ public async Task<bool> ScanAsync(CancellationToken ct = default)
229275
}
230276

231277

232-
233-
234278
// public IFlurlResponse Response { get; private set; }
235279

236280
public override string ToString()
@@ -259,8 +303,10 @@ public void Dispose()
259303
{
260304
Debug.WriteLine($"Disposing {Url} of {Root.Engine.Name}", LogCategories.C_VERBOSE);
261305

262-
if (Uni != null && Uni.Any()) {
263-
foreach (var us in Uni) {
306+
if (Uni != null && Uni.Any())
307+
{
308+
foreach (var us in Uni)
309+
{
264310

265311
us?.Dispose();
266312
}

0 commit comments

Comments
 (0)