Skip to content

Commit 3c450f4

Browse files
committed
Algorithm refactoring
1 parent 3027346 commit 3c450f4

File tree

9 files changed

+216
-294
lines changed

9 files changed

+216
-294
lines changed

SmartImage.Lib/SearchClient.cs

Lines changed: 14 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,12 @@ public async Task RunSearchAsync()
174174

175175
var direct = FindDirectResult();
176176

177-
Debug.WriteLine(direct);
177+
if (direct?.Direct != null) {
178+
Debug.WriteLine(direct);
179+
Debug.WriteLine(direct.Direct.ToString());
180+
args.Direct = direct;
178181

179-
Debug.WriteLine(direct.Direct.ToString());
180-
181-
args.Direct = direct;
182+
}
182183
}
183184

184185
ExtraResults?.Invoke(null, args);
@@ -241,98 +242,32 @@ public List<SearchResult> MaximizeResults<T>(Func<SearchResult, T> property)
241242

242243
public ImageResult[] FindDirectResults(int count = 5)
243244
{
244-
var best = FindBestResults().ToList();
245245

246-
var options = new ParallelOptions()
247-
{
248-
MaxDegreeOfParallelism = Int32.MaxValue,
249-
TaskScheduler = TaskScheduler.Default,
250-
};
246+
var best = FindBestResults().ToList();
251247

252248
Debug.WriteLine($"Found {best.Count} best results");
253249

254250
var images = new ConcurrentBag<ImageResult>();
255251

256-
//Parallel.For(0, best.Count, options, (i, s) =>
257-
//{
258-
// //if (images.Count >= count) {
259-
// // s.Stop();
260-
// // return;
261-
// //}
262-
263-
// //if (s.IsStopped) {
264-
// // return;
265-
// //}
266-
267-
// var item = best[i];
268-
269-
// item.FindDirectImages();
270-
271-
// if (item.Direct == null) {
272-
// return;
273-
// }
274-
275-
// if (ImageHelper.IsDirect(item.Direct.ToString(), DirectImageType.Binary)) {
276-
// //if (images.Count >= count) {
277-
// // s.Stop();
278-
// // return;
279-
// //}
280-
281-
// Debug.WriteLine($"Adding {item.Direct}");
252+
// todo: this is just a stopgap
282253

283-
// images.Add(item);
284-
285-
// }
286-
//});
287-
Parallel.For(0, count, options, (i, s) =>
288-
{
289-
//if (images.Count >= count) {
290-
// s.Stop();
291-
// return;
292-
//}
293-
294-
//if (s.IsStopped) {
295-
// return;
296-
//}
254+
int i = 0;
297255

256+
do {
298257
var item = best[i];
299258

300259
item.FindDirectImages();
301260

302261
if (item.Direct == null) {
303-
return;
262+
continue;
304263
}
305264

306-
if (ImageHelper.IsDirect(item.Direct.ToString(), DirectImageType.Binary)) {
307-
//if (images.Count >= count) {
308-
// s.Stop();
309-
// return;
310-
//}
311-
312-
Debug.WriteLine($"Adding {item.Direct}");
313-
314-
images.Add(item);
315-
316-
}
317-
});
318-
//int i = 0;
319-
//do {
320-
// var item = best[i++];
321-
// item.FindDirectImages();
322-
323-
// var c = item.Direct != null;
324-
325-
// if (c) {
326-
// var c2 = ImageHelper.IsDirect(item.Direct.ToString(), DirectImageType.Binary);
327-
328-
// if (c2) {
329-
// images.Add(item);
330-
// Debug.WriteLine($"{item.Direct} | {images.Count}");
265+
Debug.WriteLine($"{nameof(FindDirectResult)}: Adding {item.Direct}");
331266

332-
// }
333-
// }
267+
images.Add(item);
334268

335-
//} while (images.Count < count);
269+
270+
} while (++i != best.Count && i < count /*!images.Any(x=>x.Direct!=null)*/);
336271

337272

338273
Debug.WriteLine($"Found {images.Count} direct results");

SmartImage.Lib/Searching/ImageResult.cs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -213,36 +213,41 @@ public void FindDirectImages()
213213
if (Url == null || Direct != null)
214214
return;
215215

216-
if (ImageHelper.IsDirect(Url.ToString(), DirectImageType.Binary)) {
217-
Direct = Url;
218-
}
216+
// if (ImageHelper.IsDirect(Url.ToString(), DirectImageType.Binary)) {
217+
// Direct = Url;
218+
// }
219+
// else {
220+
//
221+
// }
219222

220-
else {
221-
try {
223+
try {
222224

223-
var directImages = ImageHelper.FindDirectImages(Url.ToString());
225+
var directImages = ImageHelper.FindDirectImages(Url.ToString());
224226

225-
/*directImages = directImages.AsParallel().Where(x => ImageHelper.IsDirect(x, DirectImageType.Binary))
226-
.ToList();*/
227+
//Debug.WriteLine($"{Url}: {directImages?.QuickJoin()}");
227228

228-
//var direct = directImages?.FirstOrDefault(x=>ImageHelper.IsDirect(x, DirectImageType.Binary));
229-
230-
var direct = directImages?.FirstOrDefault();
231229

232-
if (direct != null) {
233-
Direct = new Uri((direct));
230+
/*directImages = directImages.AsParallel().Where(x => ImageHelper.IsDirect(x, DirectImageType.Binary))
231+
.ToList();*/
234232

235-
// todo
236-
//Image = ImageHelper.GetImage(direct);
237-
//Debug.WriteLine($"{Url} -> {Direct}");
233+
//var direct = directImages?.FirstOrDefault(x=>ImageHelper.IsDirect(x, DirectImageType.Binary));
234+
235+
var direct = directImages?.FirstOrDefault();
236+
237+
//var direct = directImages?.AsParallel().FirstOrDefault(f => ImageHelper.IsDirect(f, DirectImageType.Binary));
238+
239+
if (direct != null) {
240+
Direct = new Uri((direct));
241+
242+
// todo
243+
//Image = ImageHelper.GetImage(direct);
244+
//Debug.WriteLine($"{Url} -> {Direct}");
238245

239-
}
240-
}
241-
catch {
242-
//
243246
}
244247
}
245-
248+
catch {
249+
//
250+
}
246251

247252
UpdateImageData();
248253
}

SmartImage.Lib/Utilities/ImageHelper.cs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Drawing;
5+
using System.Drawing.Imaging;
56
using System.IO;
67
using System.Linq;
78
using System.Net;
@@ -145,7 +146,7 @@ public static Dictionary<string, string> UtilitiesMap
145146

146147
#endregion
147148

148-
public static string Download(Uri direct)
149+
public static string Download(Uri direct, string path)
149150
{
150151
string filename = Path.GetFileName(direct.AbsolutePath);
151152

@@ -168,7 +169,6 @@ public static string Download(Uri direct)
168169
Debug.WriteLine("Fixed file");
169170
}
170171

171-
var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
172172

173173
string combine = Path.Combine(path, filename);
174174

@@ -179,6 +179,8 @@ public static string Download(Uri direct)
179179
return combine;
180180
}
181181

182+
public static bool IsImage(string s, double d) => Network.IsType(s, "image",(long) TimeSpan.FromSeconds(d).TotalMilliseconds);
183+
182184
public static Image GetImage(string s)
183185
{
184186
using var wc = new WebClient();
@@ -201,16 +203,18 @@ public static Image GetImage(string s)
201203
/// Scans for direct images within a webpage.
202204
/// </summary>
203205
/// <param name="url">Url to search</param>
204-
/// <param name="directType">Which criterion to use to determine whether a URI is a direct image </param>
205206
/// <param name="count">Number of direct images to return</param>
206207
/// <param name="pingTimeSec"></param>
207-
public static List<string> FindDirectImages(string url, DirectImageType directType = DirectImageType.Regex,
208-
int count = 10, double pingTimeSec = 1)
208+
public static List<string> FindDirectImages(string url, int count = 10, double pingTimeSec = 1)
209209
{
210210

211-
var images = new List<string>();
212211

213-
var pingTime = TimeSpan.FromSeconds(pingTimeSec);
212+
/*
213+
* TODO: WIP
214+
*/
215+
216+
var images = new List<string>();
217+
214218

215219
string gallerydl = UtilitiesMap[GALLERY_DL_EXE];
216220

@@ -241,8 +245,7 @@ public static List<string> FindDirectImages(string url, DirectImageType directTy
241245
.Split('|')
242246
.First();
243247

244-
if (!string.IsNullOrWhiteSpace(str) &&
245-
Network.IsAlive(new Uri(str), (long) pingTime.TotalMilliseconds)) {
248+
if (!string.IsNullOrWhiteSpace(str) && IsImage(str, pingTimeSec)) {
246249
images.Add(str);
247250

248251
}
@@ -301,30 +304,14 @@ public static List<string> FindDirectImages(string url, DirectImageType directTy
301304
Parallel.For(0, flat.Count, options, (i, s) =>
302305
{
303306
string currentUrl = flat[i];
304-
305-
if (imagesCopy.Count >= count) {
306-
s.Stop();
307+
308+
if (!IsImage(currentUrl, pingTimeSec)) {
307309
return;
308310
}
309311

310-
if (!Network.IsUri(currentUrl, out var uri))
311-
return;
312-
313-
if (!Network.IsAlive(uri, (long) pingTime.TotalMilliseconds)) {
314-
//Debug.WriteLine($"{uri} isn't alive");
315-
return;
316-
}
317-
318-
if (!IsDirect(currentUrl, directType))
319-
return;
320-
321-
if (imagesCopy.Count >= count) {
322-
s.Stop();
323-
return;
324-
}
312+
Debug.WriteLine($"{nameof(FindDirectImages)}: Adding {currentUrl}");
325313

326314
imagesCopy.Add(currentUrl);
327-
328315
});
329316

330317

SmartImage/Core/AppInfo.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
using System.Threading;
1717
using Kantan.Cli;
1818
using Kantan.Diagnostics;
19+
using Kantan.Utilities;
20+
using SmartImage.Lib.Utilities;
21+
using static Kantan.Diagnostics.LogCategories;
1922

2023
// ReSharper disable CognitiveComplexity
2124

@@ -109,6 +112,8 @@ public static void Setup()
109112
AppIntegration.HandlePath(IntegrationOption.Add);
110113
}
111114

115+
Debug.WriteLine($"Cli utilities: {ImageHelper.Utilities.QuickJoin()}", C_INFO);
116+
112117
var languages = Windows.System.UserProfile.GlobalizationPreferences.Languages;
113118

114119
bool zh = languages.Any(l => l.Contains("zh"));

SmartImage/Program.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
using SmartImage.Lib.Engines;
3838
using SmartImage.Lib.Searching;
3939
using SmartImage.Lib.Utilities;
40+
using SmartImage.UI;
4041
using SmartImage.Utilities;
41-
using SmartImage.UX;
4242

4343
// ReSharper disable CognitiveComplexity
4444

@@ -62,7 +62,7 @@ public static class Program
6262
public static readonly NConsoleDialog ResultDialog = new()
6363
{
6464
Options = new List<NConsoleOption>(),
65-
Description = InterfaceElements.Description
65+
Description = Elements.Description
6666
};
6767

6868
#endregion
@@ -131,11 +131,11 @@ private static async Task Main(string[] args)
131131

132132
// Show results
133133
var searchTask = Client.RunSearchAsync();
134-
134+
135135
// Add original image
136-
ResultDialog.Options.Add(InterfaceElements.CreateResultOption(
136+
ResultDialog.Options.Add(NConsoleFactory.CreateResultOption(
137137
Config.Query.GetImageResult(), "(Original image)",
138-
InterfaceElements.ColorMain, -0.1f));
138+
Elements.ColorMain, -0.1f));
139139

140140

141141
NConsole.ReadOptions(ResultDialog);
@@ -181,13 +181,13 @@ private static bool HandleArguments(string[] args)
181181

182182
var imageResults = directImages.Select(ImageResult.FromDirectImage);
183183

184-
var directOptions = InterfaceElements.CreateResultOptions(imageResults, "Image");
184+
var directOptions = NConsoleFactory.CreateResultOptions(imageResults, "Image");
185185

186186

187187
NConsole.ReadOptions(new NConsoleDialog
188188
{
189189
Options = directOptions,
190-
Description = InterfaceElements.Description
190+
Description = Elements.Description
191191
});
192192

193193
return true;
@@ -227,7 +227,7 @@ private static void OnResultCompleted(object? sender, SearchResultEventArgs even
227227
{
228228
var result = eventArgs.Result;
229229

230-
var option = InterfaceElements.CreateResultOption(result);
230+
var option = NConsoleFactory.CreateResultOption(result);
231231

232232
bool? isFiltered = eventArgs.IsFiltered;
233233

0 commit comments

Comments
 (0)