Skip to content

Commit 23910db

Browse files
committed
Fixes; UI improvements
1 parent b5c079a commit 23910db

File tree

8 files changed

+92
-46
lines changed

8 files changed

+92
-46
lines changed

SmartImage.Lib/Engines/Impl/IqdbEngine.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ private static ImageResult ParseResult(IHtmlCollection<IElement> tr)
4242
url = img.ChildNodes[0].ChildNodes[0].TryGetAttribute("href");
4343

4444
// Links must begin with http:// in order to work with "start"
45-
//if (url.StartsWith("//")) {
46-
// url = "http:" + url;
47-
//}
45+
4846
}
4947
catch {
5048
// ignored
@@ -79,7 +77,18 @@ private static ImageResult ParseResult(IHtmlCollection<IElement> tr)
7977
sim = null;
8078
}
8179

82-
var uri = url != null ? new Uri(url) : null;
80+
Uri uri;
81+
82+
if (url != null) {
83+
if (url.StartsWith("//")) {
84+
url = "http:" + url;
85+
}
86+
87+
uri = new Uri(url);
88+
}
89+
else {
90+
uri = null;
91+
}
8392

8493

8594
var result = new ImageResult
@@ -93,9 +102,6 @@ private static ImageResult ParseResult(IHtmlCollection<IElement> tr)
93102
};
94103

95104

96-
97-
98-
99105
return result;
100106
}
101107

SmartImage.Lib/SearchClient.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
using SmartImage.Lib.Utilities;
66
using System;
77
using System.Collections.Generic;
8+
using System.ComponentModel;
89
using System.Diagnostics;
910
using System.Linq;
11+
using System.Runtime.CompilerServices;
1012
using System.Threading.Tasks;
1113
using SimpleCore.Net;
14+
using SimpleCore.Utilities;
1215
using SmartImage.Lib.Upload;
1316
using static SimpleCore.Diagnostics.LogCategories;
1417

@@ -24,16 +27,28 @@ public SearchClient(SearchConfig config)
2427

2528
Results = new List<SearchResult>();
2629

30+
Update();
31+
}
32+
33+
public void Update()
34+
{
35+
if (Config.SearchEngines == SearchEngineOptions.None) {
36+
Config.SearchEngines = SearchEngineOptions.All;
37+
}
38+
2739
Engines = GetAllSearchEngines()
2840
.Where(e => Config.SearchEngines.HasFlag(e.Engine))
2941
.ToArray();
42+
43+
Trace.WriteLine($"Engines: {Config.SearchEngines} | {Engines.QuickJoin()}");
3044
}
3145

46+
3247
public SearchConfig Config { get; init; }
3348

3449
public bool IsComplete { get; private set; }
3550

36-
public BaseSearchEngine[] Engines { get; init; }
51+
public BaseSearchEngine[] Engines { get; private set; }
3752

3853
public List<SearchResult> Results { get; }
3954

@@ -170,7 +185,8 @@ public static BaseSearchEngine[] GetAllSearchEngines()
170185

171186
public event EventHandler<SearchResultEventArgs> ResultCompleted;
172187

173-
public event EventHandler SearchCompleted;
188+
public event EventHandler SearchCompleted;
189+
public event PropertyChangedEventHandler PropertyChanged;
174190
}
175191

176192
public sealed class SearchResultEventArgs : EventArgs

SmartImage.Lib/SearchConfig.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Text;
55
using System.Threading;
66
using System.Threading.Tasks;
7+
using JetBrains.Annotations;
78
using SimpleCore.Utilities;
89
using SmartImage.Lib.Engines;
910
using SmartImage.Lib.Searching;
@@ -24,7 +25,7 @@ public sealed class SearchConfig
2425
/// <summary>
2526
/// Search engines to use
2627
/// </summary>
27-
public SearchEngineOptions SearchEngines { get; set; } = SearchEngineOptions.All;
28+
public SearchEngineOptions SearchEngines { get; set; }
2829

2930
/// <summary>
3031
/// Priority engines

SmartImage.Lib/Searching/ImageResult.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,24 @@ public void UpdateFrom(ImageResult result)
190190

191191
public void FindDirectImages()
192192
{
193-
194-
if (Url is not null) {
195-
string? images = ImageHelper.FindDirectImages(Url?.ToString()).FirstOrDefault();
193+
194+
if (Url is not null) {
195+
var directImages = ImageHelper.FindDirectImages(Url?.ToString());
196+
197+
if (directImages is { }) {
198+
string? images = directImages.FirstOrDefault();
196199

197200
if (images is { }) {
198-
var uri = new Uri(images);
199201

202+
var uri = new Uri(images);
200203
Direct = uri;
204+
Debug.WriteLine($"{Url} -> {Direct}");
201205
}
202-
203206
}
204-
207+
208+
209+
}
210+
205211
}
206212

207213
public string ToString(bool indent)

SmartImage.Lib/Utilities/ImageHelper.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public static string[] FindDirectImages(string url)
114114
Debug.WriteLine($"{e.Message}", C_ERROR);
115115
return null;
116116
}
117+
117118
const string HREF_PATTERN = "<a\\s+(?:[^>]*?\\s+)?href=\"([^\"]*)\"";
118119

119120
var m2 = Regex.Matches(html, HREF_PATTERN);
@@ -141,20 +142,20 @@ public static string[] FindDirectImages(string url)
141142
// todo: is running PLINQ within a task thread-safe?
142143

143144
results = rg.AsParallel()
144-
.Where(e => Network.IsUri(e, out var u) && Network.IsUriAlive(new Uri(e)) && IsDirect(e))
145+
.Where(e => Network.IsUri(e, out var u) && IsDirect(u == null ? e : u.ToString()))
145146
.ToArray();
146147

147-
Debug.WriteLine($"{nameof(FindDirectImages)}: {rg.Count} -> {results.Length}", C_DEBUG);
148+
//Debug.WriteLine($"{nameof(FindDirectImages)}: {rg.Count} -> {results.Length}", C_DEBUG);
148149
});
149150

150151

151-
var timeout = TimeSpan.FromSeconds(3);
152+
var timeout = TimeSpan.FromSeconds(5);
152153

153154
if (t.Wait(timeout)) {
154155
//
155156
}
156157
else {
157-
Debug.WriteLine($"{nameof(FindDirectImages)}: timeout!", C_WARN);
158+
//Debug.WriteLine($"{nameof(FindDirectImages)}: timeout!", C_WARN);
158159
}
159160

160161

SmartImage/Core/DialogUtilities.cs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
using System.Collections.Generic;
44
using System.Diagnostics;
55
using System.Linq;
6+
using System.Net;
67
using System.Text;
78
using System.Threading.Tasks;
9+
using Novus.Win32;
810
using SimpleCore.Cli;
911
using SimpleCore.Net;
1012
using SimpleCore.Utilities;
@@ -35,39 +37,35 @@ internal static NConsoleOption Convert(SearchResult result)
3537

3638
return null;
3739
},
38-
/*CtrlFunction = () =>
39-
{
40-
var flatten = new List<ImageResult>()
41-
{
42-
result.PrimaryResult,
43-
};
44-
flatten.AddRange(result.OtherResults);
45-
46-
flatten = flatten.Where(f => f.Url != null).ToList();
47-
48-
//var direct = flatten.AsParallel().SelectMany(x => ImageHelper.FindDirectImages(x?.Url?.ToString()));
49-
50-
51-
52-
Parallel.ForEach(flatten, f =>
53-
{
54-
f.FindDirectImages();
55-
});
5640

41+
ComboFunction = () =>
42+
{
43+
var direct = result.PrimaryResult.Direct;
5744

45+
var ok = direct != null;
5846

59-
foreach (var s in flatten) {
60-
Debug.WriteLine($"{s.Direct}");
47+
if (ok) {
48+
var p = WebUtilities.Download(direct!.ToString());
49+
FileSystem.ExploreFile(p);
6150
}
6251

63-
64-
6552
return null;
66-
},*/
53+
},
6754
//Name = result.Engine.Name,
6855
Data = result.ToString()
6956
};
7057

58+
option.CtrlFunction = () =>
59+
{
60+
result.OtherResults.AsParallel().ForAll(x => x.FindDirectImages());
61+
62+
result.PrimaryResult.UpdateFrom(result.OtherResults.First());
63+
64+
option.Data = result.ToString();
65+
66+
return null;
67+
};
68+
7169
return option;
7270
}
7371

SmartImage/Core/MainDialog.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public static class MainDialog
6161

6262
Console.WriteLine(Program.Config.SearchEngines);
6363
NConsole.WaitForSecond();
64+
Program.Client.Update();
6465
return null;
6566
}
6667
},
@@ -74,6 +75,7 @@ public static class MainDialog
7475

7576
Console.WriteLine(Program.Config.PriorityEngines);
7677
NConsole.WaitForSecond();
78+
Program.Client.Update();
7779
return null;
7880
}
7981
},
@@ -86,7 +88,7 @@ public static class MainDialog
8688

8789
//hack: hacky
8890
MainMenuOptions[3].Name = GetFilterName(Program.Config.Filter);
89-
91+
Program.Client.Update();
9092
return null;
9193
}
9294
},
@@ -99,7 +101,7 @@ public static class MainDialog
99101

100102
//hack: hacky
101103
MainMenuOptions[4].Name = GetDirectName(Program.Config.DirectUri);
102-
104+
Program.Client.Update();
103105
return null;
104106
}
105107
},

SmartImage/Program.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.IO;
1111
using System.Linq;
1212
using System.Media;
13+
using System.Threading;
1314
using System.Threading.Tasks;
1415
using Novus.Win32;
1516
using SimpleCore.Net;
@@ -91,6 +92,7 @@ private static async Task Main(string[] args)
9192
var directImages = ImageHelper.FindDirectImages((string) arg);
9293

9394
Console.WriteLine("Links:");
95+
9496
foreach (string s in directImages) {
9597
Console.WriteLine(s);
9698
}
@@ -112,10 +114,13 @@ private static async Task Main(string[] args)
112114
Client.ResultCompleted += ResultCompleted;
113115
Client.SearchCompleted += SearchCompleted;
114116

115-
// Show results
116117

118+
StartProgressBar();
119+
120+
// Show results
117121
var searchTask = Client.RunSearchAsync();
118122

123+
119124
NConsole.ReadOptions(ResultDialog);
120125

121126
await searchTask;
@@ -129,10 +134,21 @@ private static async Task Main(string[] args)
129134
}
130135

131136

137+
private static void StartProgressBar()
138+
{
139+
// Pass the token to the cancelable operation.
140+
ThreadPool.QueueUserWorkItem(NConsoleProgress.Show, ProgressCancellationToken.Token);
141+
}
142+
143+
144+
private static readonly CancellationTokenSource ProgressCancellationToken = new();
145+
132146
private static void SearchCompleted(object? sender, EventArgs eventArgs)
133147
{
134148
NativeImports.FlashConsoleWindow();
135149
SystemSounds.Exclamation.Play();
150+
151+
ProgressCancellationToken.Cancel();
136152
}
137153

138154
private static void ResultCompleted(object? sender, SearchResultEventArgs eventArgs)

0 commit comments

Comments
 (0)