Skip to content

Commit 37a2825

Browse files
committed
Organize search result classes, refactoring
1 parent b2cb42b commit 37a2825

File tree

13 files changed

+212
-229
lines changed

13 files changed

+212
-229
lines changed

SmartImage/Core/Info.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ internal static void ShowInfo()
173173
var dependencies = RuntimeInfo.DumpDependencies();
174174

175175
foreach (var name in dependencies) {
176-
NConsole.WriteInfo("{0} ({1})", name.Name, name.Version);
176+
NConsole.WriteInfo("{0} ({1})", name.Name!, name.Version!);
177177
}
178178
}
179179
}

SmartImage/Core/Interface.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ private static SearchEngineOptions ReadSearchEngineOptions()
207207

208208
private static string GetAutoFilterString()
209209
{
210-
var x = SearchConfig.Config.FilterResults;
210+
bool x = SearchConfig.Config.FilterResults;
211211
return $"Filter results: {x}";
212212
}
213213

@@ -347,16 +347,16 @@ private static string GetContextMenuString(bool added) =>
347347
//var cd2 = cd.Parent.Parent.Parent.Parent.ToString();
348348
//var cd2 = cd.GetParentLevel(4).ToString();
349349

350-
var cd2 = FileSystem.GetParentLevel(Environment.CurrentDirectory, 4);
350+
string? cd2 = FileSystem.GetParentLevel(Environment.CurrentDirectory, 4);
351351

352352
var rgOption = NConsoleOption.FromArray(TestImages, s => s);
353353

354-
var testImg = (string) NConsole.ReadOptions(rgOption).First();
354+
string? testImg = (string) NConsole.ReadOptions(rgOption).First();
355355

356-
var img = Path.Combine(cd2, testImg);
356+
string? img = Path.Combine(cd2, testImg);
357357

358358
SearchConfig.Config.Image = img;
359-
//SearchConfig.Config.PriorityEngines = SearchEngineOptions.None;
359+
SearchConfig.Config.PriorityEngines = SearchEngineOptions.None;
360360

361361
return true;
362362
}

SmartImage/Core/SearchConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private SearchConfig()
7878
/// <summary>
7979
/// Does not open results from priority engines if the result similarity (if available) is below a certain threshold,
8080
/// or there are no relevant results.
81-
/// <see cref="ISearchResult.Filter"/> is <c>true</c> if <see cref="ISearchEngine.FilterThreshold"/> is less than <see cref="ISearchResult.Similarity"/>
81+
/// <see cref="BasicSearchResult.Filter"/> is <c>true</c> if <see cref="ISearchEngine.FilterThreshold"/> is less than <see cref="BasicSearchResult.Similarity"/>
8282
/// </summary>
8383
[field: ConfigComponent("filter_results", "--filter-results", true, true)]
8484
public bool FilterResults { get; set; }

SmartImage/Engines/Other/IqdbEngine.cs

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,45 +24,9 @@ public IqdbEngine() : base("https://iqdb.org/?url=") { }
2424

2525
public override float? FilterThreshold => 70.00F;
2626

27-
private struct IqdbResult : ISearchResult
28-
{
29-
public bool Filter { get; set; }
30-
31-
public string? Caption { get; set; }
32-
33-
public string? Source { get; set; }
34-
35-
public int? Width { get; set; }
36-
37-
public int? Height { get; set; }
38-
39-
public string Url { get; set; }
40-
41-
public float? Similarity { get; set; }
42-
43-
public string? Artist { get; set; }
27+
4428

45-
public string? Characters { get; set; }
46-
47-
public string? SiteName { get; set; }
48-
49-
public IqdbResult(string siteName, string source, string url, int width, int height, float? similarity,
50-
string? caption)
51-
{
52-
SiteName = siteName;
53-
Url = url;
54-
Source = source;
55-
Width = width;
56-
Height = height;
57-
Similarity = similarity;
58-
Filter = false; // set later
59-
Artist = null;
60-
Characters = null;
61-
Caption = caption;
62-
}
63-
}
64-
65-
private IqdbResult ParseResult(HtmlNodeCollection tr)
29+
private BasicSearchResult ParseResult(HtmlNodeCollection tr)
6630
{
6731
var caption = tr[0];
6832
var img = tr[1];
@@ -115,7 +79,7 @@ private IqdbResult ParseResult(HtmlNodeCollection tr)
11579
}
11680

11781

118-
var i = new IqdbResult(src.InnerText, null, url, w, h, sim, caption.InnerText);
82+
var i = new BasicSearchResult(url, sim, w, h, src.InnerText, null, caption.InnerText);
11983
i.Filter = i.Similarity < FilterThreshold;
12084
return i;
12185
}
@@ -160,14 +124,8 @@ public override FullSearchResult GetResult(string url)
160124
images.RemoveAt(0);
161125

162126
var best = images[0];
163-
sr.Url = best.Url;
164-
sr.Similarity = best.Similarity;
165-
sr.Filter = best.Filter;
166-
sr.Source = best.Source;
167-
sr.SiteName = best.SiteName;
127+
sr.UpdateFrom(best);
168128
sr.AddExtendedResults(images.ToArray());
169-
170-
171129
}
172130
catch (Exception) {
173131
// ...

SmartImage/Engines/Other/KarmaDecayEngine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public sealed class KarmaDecayEngine : BasicSearchEngine
77
public KarmaDecayEngine() : base("http://karmadecay.com/search/?q=") { }
88

99
public override string Name => "KarmaDecay";
10+
1011
public override Color Color => Color.Orange;
1112

1213
public override SearchEngineOptions Engine => SearchEngineOptions.KarmaDecay;

SmartImage/Engines/Other/YandexEngine.cs

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,47 +21,11 @@ public YandexEngine() : base("https://yandex.com/images/search?rpt=imageview&url
2121

2222
public override string Name => "Yandex";
2323

24-
private struct YandexResult : ISearchResult
25-
{
26-
public bool Filter { get; set; }
27-
public float? Similarity { get; set; }
28-
29-
public int? Width { get; set; }
30-
31-
public int? Height { get; set; }
32-
33-
public string? Caption { get; set; }
34-
35-
public string Url { get; set; }
36-
37-
public string? Artist { get; set; }
38-
public string? Source { get; set; }
39-
public string? Characters { get; set; }
40-
public string? SiteName { get; set; }
41-
42-
internal YandexResult(int width, int height, string url)
43-
{
44-
Width = width;
45-
Height = height;
46-
Url = url;
47-
Caption = null;
48-
Similarity = null;
49-
Filter = false;
50-
Artist = null;
51-
Source = null;
52-
Characters = null;
53-
SiteName=null;
54-
}
55-
56-
public override string ToString()
57-
{
58-
return $"{Width}x{Height} {Url} [{((ISearchResult) this).FullResolution:N}]";
59-
}
60-
}
24+
6125

6226
private const int TOTAL_RES_MIN = 500_000;
6327

64-
private static ISearchResult[] FilterAndSelectBestImages(List<ISearchResult> rg)
28+
private static BasicSearchResult[] FilterAndSelectBestImages(List<BasicSearchResult> rg)
6529
{
6630
const int TAKE_N = 5;
6731

@@ -84,7 +48,7 @@ private static string GetYandexAnalysis(HtmlDocument doc)
8448
}
8549

8650

87-
private static List<ISearchResult> GetYandexImages(HtmlDocument doc)
51+
private static List<BasicSearchResult> GetYandexImages(HtmlDocument doc)
8852
{
8953
const string TAGS_ITEM_XP = "//a[contains(@class, 'Tags-Item')]";
9054

@@ -96,26 +60,25 @@ private static List<ISearchResult> GetYandexImages(HtmlDocument doc)
9660
var sizeTags = tagsItem.Where(sx =>
9761
!sx.ParentNode.ParentNode.Attributes["class"].Value.Contains(CBIR_ITEM));
9862

99-
10063

101-
var images = new List<ISearchResult>();
64+
var images = new List<BasicSearchResult>();
10265

10366
foreach (var siz in sizeTags) {
10467
string? link = siz.Attributes["href"].Value;
10568

10669
string? resText = siz.FirstChild.InnerText;
10770

108-
string[]? resFull = resText.Split(Formatting.MUL_SIGN);
109-
110-
int w = Int32.Parse(resFull[0]);
111-
int h = Int32.Parse(resFull[1]);
112-
int totalRes = w * h;
71+
string[]? resFull = resText.Split(Formatting.MUL_SIGN);
72+
73+
int w = Int32.Parse(resFull[0]);
74+
int h = Int32.Parse(resFull[1]);
75+
int totalRes = w * h;
11376

11477
if (totalRes >= TOTAL_RES_MIN) {
11578
var restRes = Network.GetSimpleResponse(link);
11679

11780
if (restRes.StatusCode != HttpStatusCode.NotFound) {
118-
var yi = new YandexResult(w, h, link);
81+
var yi = new BasicSearchResult(link, w, h);
11982

12083
images.Add(yi);
12184
}
@@ -144,7 +107,6 @@ public override FullSearchResult GetResult(string url)
144107
*/
145108

146109
string? looksLike = GetYandexAnalysis(doc);
147-
sr.Caption = looksLike;
148110

149111

150112
/*
@@ -153,14 +115,14 @@ public override FullSearchResult GetResult(string url)
153115

154116
var images = GetYandexImages(doc);
155117

156-
ISearchResult[] bestImages = FilterAndSelectBestImages(images);
118+
BasicSearchResult[] bestImages = FilterAndSelectBestImages(images);
157119

158120
//
159121
var best = images[0];
160-
sr.Width = best.Width;
161-
sr.Height = best.Height;
162-
sr.Url = best.Url;
122+
sr.UpdateFrom(best);
123+
163124

125+
sr.Description = looksLike;
164126

165127
sr.AddExtendedResults(bestImages);
166128
}

SmartImage/Engines/SauceNao/SauceNaoEngine.cs

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ public SauceNaoEngine() : this(SearchConfig.Config.SauceNaoAuth) { }
5555

5656
public override float? FilterThreshold => 70.00F;
5757

58-
private ISearchResult[] ConvertResults(SauceNaoDataResult[] results)
58+
private BasicSearchResult[] ConvertResults(SauceNaoDataResult[] results)
5959
{
60-
var rg = new List<ISearchResult>();
60+
var rg = new List<BasicSearchResult>();
6161

6262
foreach (var sn in results) {
6363
if (sn.Urls != null) {
6464
string? url = sn.Urls.FirstOrDefault(u => u != null)!;
6565
string? name = sn.Index.ToString();
6666

67-
var x = new SauceNaoSimpleResult(sn.WebsiteTitle, url,
68-
sn.Similarity, sn.Creator, sn.Material, sn.Character, name);
67+
var x = new BasicSearchResult(url, sn.Similarity,
68+
sn.WebsiteTitle, sn.Creator, sn.Material, sn.Character, name);
6969

7070
x.Filter = x.Similarity < FilterThreshold;
7171

@@ -95,9 +95,6 @@ public override FullSearchResult GetResult(string url)
9595
string? creator = orig.FirstOrDefault(o => o.Creator != null)?.Creator;
9696
string? material = orig.FirstOrDefault(o => o.Material != null)?.Material;
9797

98-
result.Characters = character;
99-
result.Artist = creator;
100-
result.Source = material;
10198

10299
var extended = ConvertResults(orig);
103100

@@ -108,15 +105,15 @@ public override FullSearchResult GetResult(string url)
108105
var best = ordered.First();
109106

110107
// Copy
111-
result.Url = best.Url;
112-
result.Similarity = best.Similarity;
113-
result.Filter = best.Filter;
114-
result.SiteName = best.SiteName;
108+
result.UpdateFrom(best);
109+
110+
result.Characters = character;
111+
result.Artist = creator;
112+
result.Source = material;
115113

116114
result.AddExtendedResults(extended);
117115

118116
if (!String.IsNullOrWhiteSpace(m_apiKey)) {
119-
120117
result.ExtendedInfo.Add("Using API");
121118
}
122119

@@ -126,7 +123,6 @@ public override FullSearchResult GetResult(string url)
126123
result.ExtendedInfo.Add("Error parsing");
127124
}
128125

129-
130126
return result;
131127
}
132128

@@ -249,39 +245,5 @@ public override string ToString()
249245
return $"{firstUrl} ({Similarity}, {Index})";
250246
}
251247
}
252-
253-
private struct SauceNaoSimpleResult : ISearchResult
254-
{
255-
public string? Caption { get; set; }
256-
public bool Filter { get; set; }
257-
public string Url { get; set; }
258-
public float? Similarity { get; set; }
259-
public int? Width { get; set; }
260-
public int? Height { get; set; }
261-
public string? Artist { get; set; }
262-
public string? Source { get; set; }
263-
public string? Characters { get; set; }
264-
public string? SiteName { get; set; }
265-
266-
public SauceNaoSimpleResult(string? title, string url, float? similarity, string? artist, string? source,
267-
string? characters, string? siteName)
268-
{
269-
Caption = title;
270-
Url = url;
271-
Similarity = similarity;
272-
Width = null;
273-
Height = null;
274-
Filter = false; //set later
275-
Artist = artist;
276-
Source = source;
277-
Characters = characters;
278-
SiteName = siteName;
279-
}
280-
281-
public override string ToString()
282-
{
283-
return $"{Url} {Similarity}";
284-
}
285-
}
286248
}
287249
}

SmartImage/Engines/TraceMoe/TraceMoeEngine.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public TraceMoeEngine() : base("https://trace.moe/?url=") { }
1414

1515
public override SearchEngineOptions Engine => SearchEngineOptions.TraceMoe;
1616

17-
private static TraceMoeRootObject GetApiResults(string url, out HttpStatusCode code, out ResponseStatus status,
18-
out string msg)
17+
private static TraceMoeRootObject GetApiResults(string url,
18+
out HttpStatusCode code, out ResponseStatus status, out string msg)
1919
{
2020
// https://soruly.github.io/trace.moe/#/
2121

@@ -26,7 +26,7 @@ private static TraceMoeRootObject GetApiResults(string url, out HttpStatusCode c
2626
rq.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
2727
rq.RequestFormat = DataFormat.Json;
2828

29-
IRestResponse<TraceMoeRootObject> re = rc.Execute<TraceMoeRootObject>(rq, Method.GET);
29+
var re = rc.Execute<TraceMoeRootObject>(rq, Method.GET);
3030

3131
code = re.StatusCode;
3232
status = re.ResponseStatus;
@@ -49,7 +49,7 @@ private ISearchResult[] ConvertResults(TraceMoeRootObject obj)
4949
results[i] = new FullSearchResult(this, malUrl, sim)
5050
{
5151
Source = doc.title_english,
52-
Caption = $"Episode #{doc.episode} @ {TimeSpan.FromSeconds(doc.at)}"
52+
Description = $"Episode #{doc.episode} @ {TimeSpan.FromSeconds(doc.at)}"
5353
};
5454
}
5555

@@ -82,7 +82,7 @@ public override FullSearchResult GetResult(string url)
8282
r = new FullSearchResult(this, best.Url, best.Similarity)
8383
{
8484
Source = best.Source,
85-
Caption = best.Caption,
85+
Description = best.Description,
8686
};
8787
r.Filter = r.Similarity < FilterThreshold;
8888

@@ -99,8 +99,7 @@ public override FullSearchResult GetResult(string url)
9999
else {
100100
r = base.GetResult(url);
101101

102-
r.ExtendedInfo.Add(string.Format("API: Returned null (possible timeout) [{0} {1} {2}]", code, res,
103-
msg));
102+
r.ExtendedInfo.Add($"API: Returned null (possible timeout) [{code} {res} {msg}]");
104103
}
105104

106105

0 commit comments

Comments
 (0)