Skip to content

Commit b21f570

Browse files
committed
...
1 parent d2cd43c commit b21f570

File tree

12 files changed

+128
-74
lines changed

12 files changed

+128
-74
lines changed

SmartImage 3/Mode/Shell/Assets/UI.Styles.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,35 @@ static UI() { }
161161

162162
internal static ColorScheme Make(Attribute norm, Attribute? focus = null, Attribute? disabled = null)
163163
{
164-
focus??= Attribute.Get();
164+
focus ??= Attribute.Get();
165165
disabled ??= Attribute.Get();
166-
166+
167167
return new ColorScheme()
168168
{
169169
Normal = norm,
170170
Focus = focus.Value,
171171
Disabled = disabled.Value
172172
}.NormalizeHot();
173173
}
174-
174+
175+
public static ColorScheme NormalizeHot(this ColorScheme cs)
176+
{
177+
cs.HotFocus = cs.Focus;
178+
cs.HotNormal = cs.Normal;
179+
return cs;
180+
}
181+
182+
private const int BRIGHT_DELTA = 0b1000;
183+
184+
public static Color ToBrightVariant(this Color c)
185+
{
186+
// +8
187+
return (Color) ((int) c + BRIGHT_DELTA);
188+
}
189+
190+
public static Color ToDarkVariant(this Color c)
191+
{
192+
// +8
193+
return (Color) ((int) c - BRIGHT_DELTA);
194+
}
175195
}

SmartImage 3/Mode/Shell/Assets/UI.cs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
using SmartImage.Lib.Engines;
1414
using Terminal.Gui;
1515
using Attribute = Terminal.Gui.Attribute;
16-
using Color=Terminal.Gui.Color;
16+
using Color = Terminal.Gui.Color;
17+
1718
#endregion
1819

1920
// ReSharper disable InconsistentNaming
@@ -29,14 +30,14 @@ internal static bool QueueProgress(CancellationTokenSource cts, ProgressBar pbr,
2930
while (state is CancellationToken { IsCancellationRequested: false }) {
3031
pbr.Pulse();
3132
f?.Invoke(state);
32-
33+
3334
// Task.Delay(TimeSpan.FromSeconds(0.5));
3435
// Thread.Sleep(TimeSpan.FromMilliseconds(100));
35-
36+
3637
}
3738

3839
}, cts.Token);
39-
40+
4041
}
4142

4243
internal static Button CreateLinkButton(this Dialog d, string text, string? url = null, Action? urlAction = null)
@@ -87,7 +88,7 @@ public static void FromEnum<TEnum>(this ListView lv, TEnum e) where TEnum : stru
8788
TEnum e2 = list[i];
8889
var mark = e.HasFlag(e2);
8990

90-
if (e2.Equals(default(TEnum))) {
91+
if (EqualityComparer<TEnum>.Default.Equals(e2, default)) {
9192
// Debug.WriteLine($"Skipping {default(TEnum)}");
9293
continue;
9394
}
@@ -200,25 +201,4 @@ internal static void OnEngineSelected(ListView lv, ListViewItemEventArgs lvie, r
200201
lv.SetNeedsDisplay();
201202
Debug.WriteLine($"{val} {args.Item} -> {e} {isMarked}", nameof(OnEngineSelected));
202203
}*/
203-
204-
public static ColorScheme NormalizeHot(this ColorScheme cs)
205-
{
206-
cs.HotFocus = cs.Focus;
207-
cs.HotNormal = cs.Normal;
208-
return cs;
209-
}
210-
211-
private const int BRIGHT_DELTA = 0b1000;
212-
213-
public static Color ToBrightVariant(this Color c)
214-
{
215-
// +8
216-
return (Color) ((int) c + BRIGHT_DELTA);
217-
}
218-
219-
public static Color ToDarkVariant(this Color c)
220-
{
221-
// +8
222-
return (Color) ((int) c - BRIGHT_DELTA);
223-
}
224204
}

SmartImage 3/Mode/Shell/ShellMode.Dialog.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,8 @@ void OnAction(ListView lv, Action<SearchEngineOptions> f)
432432
cbContextMenu, cbOnTop, lbConfig, lbSearchEngines, lbPriorityEngines,
433433
lbHelp, cbAutoSearch, lbEhUsername, tfEhUsername, lbEhPassword, tfEhPassword,
434434
cbOpenRaw, cbSilent, btnClear, btnClear2, cbCb
435-
436-
/*cbSendTo*/
437-
438-
);
435+
/*cbSendTo*/
436+
);
439437

440438
var btnHelp = dlCfg.CreateLinkButton("?", R2.Wiki_Url);
441439

@@ -504,19 +502,20 @@ private void Queue_Dialog()
504502
var cpy2 = lv.Source.ToList();
505503

506504
if (lv.SelectedItem < cpy2.Count && lv.SelectedItem >= 0) {
507-
var i = (string) cpy2[lv.SelectedItem];
508-
// Debug.WriteLine($"{i}");
509-
cpy.Remove(i);
510-
// Queue.Clear();
511-
Queue.Clear();
512-
513-
foreach (var c in cpy) {
514-
Queue.Enqueue(c);
505+
if (cpy2[lv.SelectedItem] is string i) {
506+
// Debug.WriteLine($"{i}");
507+
cpy.Remove(i);
508+
// Queue.Clear();
509+
Queue.Clear();
510+
511+
foreach (var c in cpy) {
512+
Queue.Enqueue(c);
513+
}
514+
515+
// Queue = new ConcurrentQueue<string>(cpy);
516+
lv.SetFocus();
515517
}
516518

517-
// Queue = new ConcurrentQueue<string>(cpy);
518-
lv.SetFocus();
519-
520519
}
521520
};
522521

SmartImage 3/Mode/Shell/ShellMode.Handlers.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,10 @@ private async void ResultTable_KeyPress(View.KeyEventEventArgs eventArgs)
442442
(r, c) = (Norm(r), Norm(c));
443443

444444
// NOTE: Column 2 contains the URL
445+
//index >= 0 && index < array.Length
446+
if (!(r >= 0 && r < Tv_Results.Table.Rows.Count)) {
447+
return;
448+
}
445449

446450
Url v = (Tv_Results.Table.Rows[r][COL_URL]).ToString();
447451

SmartImage.Lib 3/Engines/BaseSearchEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public abstract class BaseSearchEngine : IDisposable
3131

3232
protected long MaxSize { get; set; } = NA_SIZE;
3333

34-
protected virtual string[] Illegal { get; } = Array.Empty<string>();
34+
protected virtual string[] ErrorBodyMessages { get; } = Array.Empty<string>();
3535

3636
protected BaseSearchEngine(string baseUrl)
3737
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ protected override async Task<IDocument> GetDocumentAsync(object sender, SearchQ
118118
}
119119
}
120120

121-
protected override string[] Illegal => new[] { "検索できるのは 縦 10000px での画像です。" };
121+
protected override string[] ErrorBodyMessages => new[] { "検索できるのは 縦 10000px での画像です。" };
122122

123123
protected override ValueTask<SearchResultItem> ParseResultItem(INode n, SearchResult r)
124124
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private async Task<IDocument> GetDocumentAsync(SearchQuery query, CancellationTo
141141
}
142142
}
143143

144-
protected override string[] Illegal => new[] { "Can't read query result!","too large" };
144+
protected override string[] ErrorBodyMessages => new[] { "Can't read query result!","too large" };
145145

146146
public override async Task<SearchResult> GetResultAsync(SearchQuery query, CancellationToken token = default)
147147
{
@@ -160,7 +160,7 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
160160
sr.Status = SearchResultStatus.Failure;
161161
goto ret;
162162
}
163-
foreach (string s in Illegal) {
163+
foreach (string s in ErrorBodyMessages) {
164164
if (doc.Body.TextContent.Contains(s)) {
165165

166166
sr.Status = SearchResultStatus.IllegalInput;

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

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,9 @@ static SauceNaoDataResult Parse(INode result)
182182

183183
// Contains links
184184
var resultmiscinfo = resultmatchinfo.ChildNodes[1];
185-
var resultcontent = resulttablecontent.ChildNodes[1];
185+
// var resultcontent = resulttablecontent.ChildNodes[1];
186186
// var resultcontentcolumn = resultcontent.ChildNodes[1];
187+
var resultcontent = ((IElement) result).GetElementsByClassName("resultcontent")[0];
187188

188189
IHtmlCollection<IElement> resultcontentcolumn_rg = null;
189190

@@ -196,7 +197,8 @@ static SauceNaoDataResult Parse(INode result)
196197
var links = new List<string>();
197198

198199
if (resulttablecontent is IElement { } e) {
199-
var links1 = e.QuerySelectorAll("a").Select(x => x.GetAttribute(Serialization.Atr_href));
200+
var links1 = e.QuerySelectorAll(Serialization.Tag_a)
201+
.Select(x => x.GetAttribute(Serialization.Atr_href));
200202
links.AddRange(links1);
201203
}
202204

@@ -258,15 +260,15 @@ static SauceNaoDataResult Parse(INode result)
258260
var nStr = n.TextContent;
259261
var n2Str = n2.TextContent;
260262

261-
if (synonyms.Any(s => nStr.StartsWith(s))) {
263+
if (synonyms.Any(nStr.StartsWith)) {
262264
creator1 = n2Str;
263265
}
264266

265-
if (material.Any(s => nStr.StartsWith(s))) {
267+
if (material.Any(nStr.StartsWith)) {
266268
material1 = n2Str;
267269
}
268270

269-
if (characters.Any(s => nStr.StartsWith(s))) {
271+
if (characters.Any(nStr.StartsWith)) {
270272
characters1 = n2Str;
271273
}
272274
}
@@ -310,6 +312,48 @@ static SauceNaoDataResult Parse(INode result)
310312

311313
};
312314

315+
/*foreach (INode rccn in resultcontent.ChildNodes) {
316+
foreach (string s in material) {
317+
if (rccn.TextContent.StartsWith(s)) {
318+
dataResult.Material = rccn.TextContent.Split(s)[1];
319+
320+
}
321+
}
322+
}*/
323+
foreach (IElement element1 in resultcontent.QuerySelectorAll("strong")) {
324+
for (int i = 0; i < element1.Children.Length - 1; i += 2) {
325+
IElement ec1 = element1.Children[i];
326+
327+
if (ec1.TagName == "BR") {
328+
continue;
329+
}
330+
331+
foreach (string m in material) {
332+
if (ec1.TextContent.StartsWith(m)) {
333+
dataResult.Material = element1.Children[++i].TextContent;
334+
break;
335+
}
336+
337+
}
338+
339+
foreach (string m in characters) {
340+
if (ec1.TextContent.StartsWith(m)) {
341+
dataResult.Character = element1.Children[++i].TextContent;
342+
break;
343+
}
344+
345+
}
346+
347+
foreach (string m in synonyms) {
348+
if (ec1.TextContent.StartsWith(m)) {
349+
dataResult.Creator = element1.Children[++i].TextContent;
350+
break;
351+
}
352+
353+
}
354+
}
355+
}
356+
313357
return dataResult;
314358

315359
}
@@ -410,7 +454,8 @@ private async Task<IEnumerable<SauceNaoDataResult>> GetAPIResultsAsync(SearchQue
410454

411455
private static SearchResultItem ConvertToImageResult(SauceNaoDataResult sn, SearchResult r)
412456
{
413-
string siteName = sn.Index != 0 ? sn.Index.ToString() : null;
457+
var idxStr = sn.Index.ToString();
458+
string siteName = sn.Index != 0 ? idxStr : null;
414459

415460
var site = Strings.NormalizeNull(siteName);
416461
var title = Strings.NormalizeNull(sn.WebsiteTitle);
@@ -439,15 +484,13 @@ private static SearchResultItem ConvertToImageResult(SauceNaoDataResult sn, Sear
439484
if ((urls.Length >= 2)) {
440485
meta = urls[1..];
441486
}
442-
else {
443-
444-
}
487+
else { }
445488

446489
var imageResult = new SearchResultItem(r)
447490
{
448491
Url = urls.FirstOrDefault(),
449492
Similarity = Math.Round(sn.Similarity, 2),
450-
Description = Strings.NormalizeNull(sn.Index.ToString()),
493+
Description = Strings.NormalizeNull(idxStr),
451494
Artist = Strings.NormalizeNull(sn.Creator),
452495
Source = Strings.NormalizeNull(sn.Material),
453496
Character = Strings.NormalizeNull(sn.Character),

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections;
22
using System.Diagnostics;
3+
using Flurl;
34
using Flurl.Http;
45
using JetBrains.Annotations;
56
using Kantan.Collections;
@@ -40,7 +41,7 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
4041
var r = await base.GetResultAsync(query, token);
4142

4243
try {
43-
IFlurlRequest request = (EndpointUrl + "/search")
44+
IFlurlRequest request = (EndpointUrl.AppendPathSegment("/search"))
4445
.AllowAnyHttpStatus()
4546
.SetQueryParam("url", query.Upload, true);
4647

@@ -72,7 +73,7 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
7273
// Most similar to least similar
7374

7475
try {
75-
var results = await ConvertResults(tm, r);
76+
var results = await ConvertResultsAsync(tm, r);
7677

7778
r.RawUrl = new Url(BaseUrl + query.Upload);
7879
r.Results.AddRange(results);
@@ -99,7 +100,7 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
99100
return r;
100101
}
101102

102-
private async Task<IEnumerable<SearchResultItem>> ConvertResults(TraceMoeRootObject obj, SearchResult sr)
103+
private async Task<IEnumerable<SearchResultItem>> ConvertResultsAsync(TraceMoeRootObject obj, SearchResult sr)
103104
{
104105
var results = obj.result;
105106
var items = new SearchResultItem[results.Count];
@@ -124,13 +125,13 @@ private async Task<IEnumerable<SearchResultItem>> ConvertResults(TraceMoeRootObj
124125
result.Metadata.image = doc.image;
125126

126127
try {
127-
string anilistUrl = ANILIST_URL + doc.anilist;
128+
string anilistUrl = ANILIST_URL.AppendPathSegment(doc.anilist);
128129
string name = await m_anilistClient.GetTitleAsync((int) doc.anilist);
129130
result.Source = name;
130131
result.Url = new Url(anilistUrl);
131132
}
132133
catch (Exception e) {
133-
Debug.WriteLine($"{Name} :: {e.Message}", nameof(ConvertResults));
134+
Debug.WriteLine($"{Name} :: {e.Message}", nameof(ConvertResultsAsync));
134135
}
135136

136137
if (result.Similarity < FILTER_THRESHOLD) {

0 commit comments

Comments
 (0)