Skip to content

Commit 10ee92d

Browse files
committed
Refactoring; misc
1 parent ca610f8 commit 10ee92d

File tree

14 files changed

+273
-112
lines changed

14 files changed

+273
-112
lines changed

SmartImage.Lib 3/Engines/Impl/Search/ArchiveMoeEngine.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace SmartImage.Lib.Engines.Impl.Search;
1616

1717
public class ArchiveMoeEngine : WebSearchEngine
1818
{
19+
1920
public ArchiveMoeEngine() : this("https://archived.moe/_/search/") { }
2021

2122
protected ArchiveMoeEngine(string baseUrl) : base(baseUrl) { }
@@ -33,7 +34,7 @@ protected static string GetHash(SearchQuery q)
3334
{
3435
//var digestBase64URL = digestBase64.replace('==', '').replace(/\//g, '_').replace(/\+/g, '-');
3536
var data = MD5.HashData(q.Uni.Stream);
36-
var b64 = Convert.ToBase64String(data).Replace("==", "");
37+
var b64 = Convert.ToBase64String(data).Replace("==", "");
3738
b64 = Regex.Replace(b64, @"\//", "_");
3839
b64 = Regex.Replace(b64, @"\+", "-");
3940

@@ -83,17 +84,19 @@ protected override ValueTask<SearchResultItem> ParseResultItem(INode n, SearchRe
8384
Text = text
8485
};
8586

86-
return ValueTask.FromResult(p.Convert(r));
87+
return ValueTask.FromResult(p.Convert(r, out _));
8788

8889
// ReSharper restore PossibleNullReferenceException
8990

9091
}
9192

9293
protected override string NodesSelector => "//article[contains(@class,'post')]";
94+
9395
}
9496

9597
internal record ChanPost : IResultConvertable
9698
{
99+
97100
public long Id;
98101
public string Board;
99102
public string Filename;
@@ -108,8 +111,10 @@ internal record ChanPost : IResultConvertable
108111
public DateTime Time2;
109112
public string Text;
110113

111-
public SearchResultItem Convert(SearchResult sr)
114+
public SearchResultItem Convert(SearchResult sr, out SearchResultItem[] ch)
112115
{
116+
ch = [];
117+
113118
var sri = new SearchResultItem(sr)
114119
{
115120
Url = File,
@@ -118,10 +123,11 @@ public SearchResultItem Convert(SearchResult sr)
118123
Artist = Author,
119124
Description = Title,
120125
Time = Time2,
121-
Site = File.Host,
126+
Site = File.Host,
122127
Metadata = this
123128
};
124129

125130
return sri;
126131
}
132+
127133
}

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,15 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
108108
var imageResults = dataResults.Where(o => o != null)
109109

110110
// .AsParallel()
111-
.Select(x => x.Convert(result))
111+
.SelectMany(x =>
112+
{
113+
var i = x.Convert(result, out var rg);
114+
115+
Array.Resize(ref rg, rg.Length + 1);
116+
rg[^1] = i;
117+
118+
return rg;
119+
})
112120
.Where(o => o != null)
113121

114122
// .OrderByDescending(e => e.Similarity)
@@ -127,7 +135,7 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
127135

128136
// TODO: HACK
129137

130-
var allSisters = imageResults
138+
/*var allSisters = imageResults
131139
.SelectMany(ir => ir.Children)
132140
.DistinctBy(s => s.Url)
133141
.ToList(); // note: need ToList()
@@ -136,7 +144,7 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
136144
var ir = imageResults[i];
137145
ir.Children.Clear();
138146
ir.Children.AddRange(allSisters.Where(irs => irs.Parent == ir));
139-
}
147+
}*/
140148

141149
}
142150

@@ -165,7 +173,7 @@ private async Task<IEnumerable<SauceNaoDataResult>> GetWebResultsAsync(SearchQue
165173
IFlurlResponse response = null;
166174

167175
response = await EndpointUrl.AllowHttpStatus()
168-
.OnError( x =>
176+
.OnError(x =>
169177
{
170178

171179
x.ExceptionHandled = true;
@@ -501,7 +509,7 @@ private sealed class SauceNaoDataResult : IResultConvertable
501509

502510
public string ThumbnailTitle { get; internal set; }
503511

504-
public SearchResultItem Convert(SearchResult r)
512+
public SearchResultItem Convert(SearchResult r, out SearchResultItem[] children)
505513
{
506514
var idxStr = Index.ToString();
507515
string siteName = Index != 0 ? idxStr : null;
@@ -555,7 +563,7 @@ public SearchResultItem Convert(SearchResult r)
555563

556564
};
557565

558-
imageResult.AddChildren(meta);
566+
children = imageResult.AddChildren(meta);
559567

560568
return imageResult;
561569

SmartImage.Lib 3/Engines/Impl/Search/TraceMoeEngine.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private async Task<IEnumerable<SearchResultItem>> ConvertResultsAsync(TraceMoeRo
119119

120120
for (int i = 0; i < items.Length; i++) {
121121
var doc = results[i];
122-
var result = doc.Convert(sr);
122+
var result = doc.Convert(sr, out var ch);
123123

124124
try {
125125
string anilistUrl = ANILIST_URL.AppendPathSegment(doc.anilist);
@@ -203,8 +203,9 @@ public string EpisodeString
203203
}
204204
}
205205

206-
public SearchResultItem Convert(SearchResult sr)
206+
public SearchResultItem Convert(SearchResult sr, out SearchResultItem[] children)
207207
{
208+
children = [];
208209
var sim = Math.Round(similarity * 100.0f, 2);
209210

210211
string epStr = EpisodeString;

SmartImage.Lib 3/Model/IResultConvertable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ namespace SmartImage.Lib.Model;
77

88
public interface IResultConvertable
99
{
10-
public SearchResultItem Convert(SearchResult sr);
10+
public SearchResultItem Convert(SearchResult sr, out SearchResultItem[] children);
1111

1212
}

SmartImage.Lib 3/Results/SearchResult.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public sealed class SearchResult : IDisposable, INotifyPropertyChanged
7171

7272
public IEnumerable<SearchResultItem> GetAllResults()
7373
{
74-
return Results.Union(Results.SelectMany(r => r.Children));
74+
return Results;
75+
// return Results.Union(Results.SelectMany(r => r.Children));
7576
}
7677

7778
[CBN]
@@ -82,6 +83,7 @@ public IEnumerable<SearchResultItem> GetAllResults()
8283
[CBN]
8384
public string Overview { get; internal set; }
8485

86+
/*
8587
[CBN]
8688
public SearchResultItem GetBestResult()
8789
{
@@ -92,6 +94,7 @@ public SearchResultItem GetBestResult()
9294
return Results.OrderByDescending(r => r.Similarity)
9395
.FirstOrDefault(r => Url.IsValid(r.Url));
9496
}
97+
*/
9598

9699
public bool IsStatusSuccessful => Status.IsSuccessful();
97100

@@ -156,4 +159,11 @@ private bool SetField<T>(ref T field, T value, [CallerMemberName] string propert
156159
return true;
157160
}
158161

162+
public SearchResultItem GetRaw()
163+
=> new SearchResultItem(this)
164+
{
165+
IsRaw = true,
166+
Url = RawUrl
167+
};
168+
159169
}

SmartImage.Lib 3/Results/SearchResultItem.cs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
namespace SmartImage.Lib.Results;
1414

1515
public sealed record SearchResultItem : IDisposable,
16-
IComparable<SearchResultItem>, IComparable,
17-
IValidity<SearchResultItem>
16+
IComparable<SearchResultItem>, IComparable,
17+
IValidity<SearchResultItem>
1818
{
19+
1920
private bool m_isScored;
2021

2122
public const int MAX_SCORE = 13;
@@ -109,12 +110,25 @@ public sealed record SearchResultItem : IDisposable,
109110
[CBN]
110111
public SearchResultItem Parent { get; internal set; }
111112

112-
public List<SearchResultItem> Children { get; internal set; }
113+
// public List<SearchResultItem> Children { get; internal set; }
113114

114115
// public bool IsUniType { get; internal set; }
115116

116117
public bool IsRaw { get; internal set; }
117118

119+
public bool Equals(SearchResultItem other)
120+
{
121+
if (ReferenceEquals(null, other)) return false;
122+
if (ReferenceEquals(this, other)) return true;
123+
124+
return Root.Equals(other.Root) && Uni == other.Uni;
125+
}
126+
127+
public override int GetHashCode()
128+
{
129+
return HashCode.Combine(Root, Uni);
130+
}
131+
118132
internal SearchResultItem(SearchResult r)
119133
{
120134
Root = r;
@@ -123,7 +137,7 @@ internal SearchResultItem(SearchResult r)
123137
Uni = null;
124138
Parent = null;
125139
IsRaw = false;
126-
Children = [];
140+
// Children = [];
127141
}
128142

129143
/*
@@ -146,12 +160,6 @@ internal SearchResultItem[] FromUni()
146160
return u;
147161
}
148162
*/
149-
internal static SearchResultItem GetRaw(SearchResult r)
150-
=> new SearchResultItem(r)
151-
{
152-
IsRaw = true,
153-
Url = r.RawUrl
154-
};
155163

156164
public static bool IsValid([CBN] SearchResultItem r)
157165
{
@@ -197,6 +205,7 @@ public void UpdateScore()
197205
m_isScored = true;
198206
}
199207

208+
/*
200209
public void AddChildren(string[] rg)
201210
{
202211
for (int i = 0; i < rg.Length; i++) {
@@ -212,6 +221,25 @@ public void AddChildren(string[] rg)
212221
}
213222
214223
}
224+
*/
225+
public SearchResultItem[] AddChildren(string[] rg)
226+
{
227+
var rg2 = new SearchResultItem[rg.Length];
228+
229+
for (int i = 0; i < rg.Length; i++) {
230+
231+
rg2[i] = new SearchResultItem(this)
232+
{
233+
Url = rg[i],
234+
// Children = [],
235+
Parent = this,
236+
};
237+
238+
// Children.Add(sri);
239+
}
240+
241+
return rg2;
242+
}
215243

216244
// [MustUseReturnValue]
217245
public async Task<bool> LoadUniAsync(CancellationToken ct = default)
@@ -271,8 +299,8 @@ public int CompareTo(object obj)
271299
if (ReferenceEquals(this, obj)) return 0;
272300

273301
return obj is SearchResultItem other
274-
? CompareTo(other)
275-
: throw new ArgumentException($"Object must be of type {nameof(SearchResultItem)}");
302+
? CompareTo(other)
303+
: throw new ArgumentException($"Object must be of type {nameof(SearchResultItem)}");
276304
}
277305

278306
public static bool operator <(SearchResultItem left, SearchResultItem right)
@@ -296,4 +324,5 @@ public int CompareTo(object obj)
296324
}
297325

298326
#endregion
327+
299328
}

SmartImage.Rdx/Cli/CliFormat.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal enum ResultTableFormat
2323
Similarity = 1 << 1,
2424
Url = 1 << 2,
2525

26-
Default = Name | Similarity | Url
26+
Full = Name | Similarity | Url
2727

2828
}
2929

SmartImage.Rdx/ResultModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public ResultModel(SearchResult result, int id)
2727
{
2828
Result = result;
2929
Id = id;
30-
Grid = Create();
30+
Grid = CreateGrid();
3131

3232
// Table = Create();
3333
}
@@ -39,7 +39,7 @@ public void Dispose()
3939
Result.Dispose();
4040
}
4141

42-
private static Grid Create()
42+
private static Grid CreateGrid()
4343
{
4444
var gr = new Grid();
4545
gr.AddColumns(2);
@@ -94,7 +94,7 @@ private static STable CreateTable()
9494
internal Grid UpdateGrid(int ix = -1, bool clear = false)
9595
{
9696
if (clear) {
97-
Grid = Create();
97+
Grid = CreateGrid();
9898
}
9999

100100
var allRes = Result.GetAllResults();

0 commit comments

Comments
 (0)