Skip to content

Commit 777dfd5

Browse files
committed
Refactoring
1 parent 3c5681f commit 777dfd5

File tree

11 files changed

+57
-104
lines changed

11 files changed

+57
-104
lines changed

SmartImage.Lib 3/Engines/WebSearchEngine.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace SmartImage.Lib.Engines;
1313

1414
public abstract class WebSearchEngine : BaseSearchEngine
1515
{
16+
1617
protected WebSearchEngine([NN] string baseUrl) : base(baseUrl) { }
1718

1819
public override async Task<SearchResult> GetResultAsync(SearchQuery query, CancellationToken token = default)
@@ -47,15 +48,15 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
4748

4849
var sri = await ParseResultItem(node, res);
4950

50-
if (SearchResultItem.IsValid(sri)) {
51+
if (sri is { }) {
5152
res.Results.Add(sri);
5253
}
5354
}
5455

5556
Debug.WriteLine($"{Name} :: {res.RawUrl} {doc.TextContent?.Length} {nodes.Length}",
5657
nameof(GetResultAsync));
5758

58-
ret:
59+
ret:
5960
res.Update();
6061
return res;
6162
}
@@ -124,4 +125,5 @@ protected bool Validate([CBN] IDocument doc, SearchResult sr)
124125
return true;
125126

126127
}
128+
127129
}

SmartImage.Lib 3/Model/BaseImageHost.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,4 @@ await Parallel.ForEachAsync(s2, ct, async (s1, token) =>
219219

220220
internal static readonly string GalleryDLPath = FileSystem.FindInPath(GALLERY_DL_EXE);
221221

222-
}
223-
224-
public class DanbooruImageHost : GenericImageHost
225-
{
226-
227-
public override Url Host => "danbooru.donmai.us";
228-
229-
public override bool Refine(string b)
230-
{
231-
return base.Refine(b);
232-
}
233-
234222
}

SmartImage.Lib 3/Model/IConfig.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ namespace SmartImage.Lib.Model;
55

66
public interface IConfig
77
{
8-
public ValueTask ApplyAsync(SearchConfig cfg);
8+
9+
public ValueTask ApplyAsync(SearchConfig cfg);
10+
911
}

SmartImage.Lib 3/Model/IValidator.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

SmartImage.Lib 3/Model/IVerify.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

SmartImage.Lib 3/Results/SearchResult.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,6 @@ public sealed class SearchResult : IDisposable, INotifyPropertyChanged
6969

7070
public List<SearchResultItem> Results { get; internal set; }
7171

72-
public IEnumerable<SearchResultItem> GetAllResults()
73-
{
74-
return Results;
75-
// return Results.Union(Results.SelectMany(r => r.Children));
76-
}
77-
7872
[CBN]
7973
public string ErrorMessage { get; internal set; }
8074

@@ -94,8 +88,6 @@ public SearchResultItem GetBestResult()
9488
.FirstOrDefault(r => Url.IsValid(r.Url));
9589
}
9690

97-
public bool IsStatusSuccessful => Status.IsSuccessful();
98-
9991
internal SearchResult(BaseSearchEngine bse)
10092
{
10193
Engine = bse;
@@ -157,11 +149,13 @@ private bool SetField<T>(ref T field, T value, [CallerMemberName] string propert
157149
return true;
158150
}
159151

160-
public SearchResultItem GetRaw()
161-
=> new SearchResultItem(this)
152+
public SearchResultItem AsRawResultItem()
153+
{
154+
return new SearchResultItem(this)
162155
{
163156
IsRaw = true,
164157
Url = RawUrl
165158
};
159+
}
166160

167161
}

SmartImage.Lib 3/Results/SearchResultItem.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111

1212
namespace SmartImage.Lib.Results;
1313

14-
public sealed record SearchResultItem : IDisposable,
15-
IComparable<SearchResultItem>, IComparable,
16-
IValidator<SearchResultItem>
14+
public sealed record SearchResultItem : IDisposable, IComparable<SearchResultItem>, IComparable
1715
{
1816

1917
private bool m_isScored;
@@ -136,6 +134,7 @@ internal SearchResultItem(SearchResult r)
136134
Uni = null;
137135
Parent = null;
138136
IsRaw = false;
137+
139138
// Children = [];
140139
}
141140

@@ -160,15 +159,6 @@ internal SearchResultItem[] FromUni()
160159
}
161160
*/
162161

163-
public static bool IsValid([CBN] SearchResultItem r)
164-
{
165-
return r switch
166-
{
167-
not { } => false,
168-
_ => true
169-
};
170-
}
171-
172162
public void UpdateScore()
173163
{
174164
if (m_isScored) {
@@ -229,9 +219,10 @@ public SearchResultItem[] AddChildren(string[] rg)
229219

230220
rg2[i] = new SearchResultItem(this)
231221
{
232-
Url = rg[i],
222+
Url = rg[i],
223+
233224
// Children = [],
234-
Parent = this,
225+
Parent = this,
235226
};
236227

237228
// Children.Add(sri);
@@ -298,8 +289,8 @@ public int CompareTo(object obj)
298289
if (ReferenceEquals(this, obj)) return 0;
299290

300291
return obj is SearchResultItem other
301-
? CompareTo(other)
302-
: throw new ArgumentException($"Object must be of type {nameof(SearchResultItem)}");
292+
? CompareTo(other)
293+
: throw new ArgumentException($"Object must be of type {nameof(SearchResultItem)}");
303294
}
304295

305296
public static bool operator <(SearchResultItem left, SearchResultItem right)

SmartImage.Lib 3/SearchClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query, bool reload
168168
// var result = Optimize(sri).FirstOrDefault() ?? sri.FirstOrDefault();
169169
//todo
170170
try {
171-
IOrderedEnumerable<SearchResultItem> rr = results.SelectMany(rr => rr.GetAllResults())
171+
IOrderedEnumerable<SearchResultItem> rr = results.SelectMany(rr => rr.Results)
172172
.OrderByDescending(rr => rr.Score);
173173

174174
if (Config.OpenRaw) {
175-
OpenResult(results.MaxBy(x => x.GetAllResults().Sum(xy => xy.Score)));
175+
OpenResult(results.MaxBy(x => x.Results.Sum(xy => xy.Score)));
176176
}
177177
else {
178178
OpenResult(rr.OrderByDescending(x => x.Similarity)
@@ -182,7 +182,7 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query, bool reload
182182
catch (Exception e) {
183183
Debug.WriteLine($"{e.Message}");
184184

185-
SearchResult result = results.FirstOrDefault(f => f.IsStatusSuccessful) ?? results.First();
185+
SearchResult result = results.FirstOrDefault(f => f.Status.IsSuccessful()) ?? results.First();
186186
OpenResult(result);
187187
}
188188
}

SmartImage.Rdx/ResultModel.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ internal Grid UpdateGrid(int ix = -1, bool clear = false)
9797
Grid = CreateGrid();
9898
}
9999

100-
var allRes = Result.GetAllResults();
100+
var allRes = Result.Results;
101101
int i = 0;
102102

103103
var gr2 = new Grid();
@@ -117,20 +117,6 @@ internal Grid UpdateGrid(int ix = -1, bool clear = false)
117117
return gr2;
118118
}
119119

120-
internal IRenderable[][] GetRowsForFormat2(OutputFields format)
121-
{
122-
var allRes = Result.GetAllResults();
123-
var ls = new List<IRenderable[]>();
124-
int j = 0;
125-
126-
foreach (SearchResultItem item in allRes) {
127-
var rg = GetRowsForFormat(item, j++, format);
128-
ls.Add(rg);
129-
}
130-
131-
return ls.ToArray();
132-
}
133-
134120
internal static IRenderable[] GetRowsForFormat(SearchResultItem s, int i, OutputFields format)
135121
{
136122
var ls = new List<IRenderable>();

SmartImage.Rdx/SearchCommand.cs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,6 @@ public SearchCommand()
7676
Query = SearchQuery.Null;
7777
}
7878

79-
public override ValidationResult Validate(CommandContext context, SearchCommandSettings settings)
80-
{
81-
var r = base.Validate(context, settings);
82-
return r;
83-
84-
}
85-
8679
private async void OnComplete(object sender, SearchResult[] searchResults)
8780
{
8881
// pt1.Increment(COMPLETE);
@@ -219,25 +212,8 @@ public override async Task<int> ExecuteAsync(CommandContext context, SearchComma
219212

220213
await task;
221214

222-
var dt = new Grid();
223-
dt.AddColumns(2);
224-
225-
var kv = new Dictionary<string, object>()
226-
{
227-
[R1.S_SearchEngines] = Config.SearchEngines,
228-
[R1.S_PriorityEngines] = Config.PriorityEngines,
229-
[R1.S_AutoSearch] = Config.AutoSearch,
230-
[R1.S_ReadCookies] = Config.ReadCookies,
231-
232-
["Input"] = Query
233-
};
234-
235-
foreach (var o in kv) {
236-
dt.AddRow(new Text(o.Key, CliFormat.Sty_Grid1),
237-
new Text(o.Value.ToString()));
238-
}
239-
240-
AConsole.Write(dt);
215+
var gr = CreateInfoGrid();
216+
AConsole.Write(gr);
241217

242218
// AConsole.WriteLine($"Input: {Query}");
243219

@@ -260,6 +236,30 @@ public override async Task<int> ExecuteAsync(CommandContext context, SearchComma
260236
act = await RunSimpleAsync();
261237

262238
return (int) act;
239+
240+
}
241+
242+
private Grid CreateInfoGrid()
243+
{
244+
var dt = new Grid();
245+
dt.AddColumns(2);
246+
247+
var kv = new Dictionary<string, object>()
248+
{
249+
[R1.S_SearchEngines] = Config.SearchEngines,
250+
[R1.S_PriorityEngines] = Config.PriorityEngines,
251+
[R1.S_AutoSearch] = Config.AutoSearch,
252+
[R1.S_ReadCookies] = Config.ReadCookies,
253+
254+
["Input"] = Query
255+
};
256+
257+
foreach (var o in kv) {
258+
dt.AddRow(new Text(o.Key, CliFormat.Sty_Grid1),
259+
new Text(o.Value.ToString()));
260+
}
261+
262+
return dt;
263263
}
264264

265265
private async Task<int> RunSimpleAsync()
@@ -271,7 +271,7 @@ private async Task<int> RunSimpleAsync()
271271
var cnt = (double) Client.Engines.Length;
272272
var pt = c.AddTask("Running search", maxValue: cnt);
273273
pt.IsIndeterminate = true;
274-
274+
275275
// var p2 = c.AddTask("Engines", maxValue: cnt);
276276

277277
// Client.OnResult += OnResultComplete;
@@ -301,6 +301,13 @@ private async Task<int> RunSimpleAsync()
301301
return EC_OK;
302302
}
303303

304+
public override ValidationResult Validate(CommandContext context, SearchCommandSettings settings)
305+
{
306+
var r = base.Validate(context, settings);
307+
return r;
308+
309+
}
310+
304311
public void Dispose()
305312
{
306313
foreach (var sr in m_results) {

0 commit comments

Comments
 (0)