Skip to content

Commit 5ebd869

Browse files
committed
Direct image scraping algorithm updates
1 parent 76651e3 commit 5ebd869

File tree

4 files changed

+98
-57
lines changed

4 files changed

+98
-57
lines changed

SmartImage.Lib/SearchClient.cs

Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
1-
using JetBrains.Annotations;
2-
using Novus.Utilities;
3-
using SmartImage.Lib.Engines;
4-
using SmartImage.Lib.Searching;
5-
using SmartImage.Lib.Utilities;
6-
using System;
7-
using System.Collections.Concurrent;
1+
using System;
82
using System.Collections.Generic;
9-
using System.ComponentModel;
103
using System.Diagnostics;
11-
using System.IO;
124
using System.Linq;
13-
using System.Runtime.CompilerServices;
14-
using System.Threading;
155
using System.Threading.Tasks;
16-
using Kantan.Diagnostics;
17-
using Kantan.Net;
18-
using Kantan.Utilities;
6+
using JetBrains.Annotations;
7+
using Novus.Utilities;
8+
using SmartImage.Lib.Engines;
199
using SmartImage.Lib.Engines.Model;
10+
using SmartImage.Lib.Searching;
2011
using SmartImage.Lib.Upload;
12+
using SmartImage.Lib.Utilities;
2113
using static Kantan.Diagnostics.LogCategories;
2214

2315
// ReSharper disable CognitiveComplexity
@@ -166,25 +158,21 @@ public async Task RunSearchAsync()
166158
Trace.WriteLine($"{nameof(SearchClient)}: Search complete", C_SUCCESS);
167159

168160

169-
var args2 = new SearchCompletedEventArgs()
161+
var args = new SearchCompletedEventArgs
170162
{
171-
Results = Results,
172-
Best = new Lazy<ImageResult>(() => GetDetailedResults().FirstOrDefault()),
163+
Results = Results,
164+
Detailed = new Lazy<ImageResult>(() => GetDetailedResults().FirstOrDefault()),
173165
Direct = new Lazy<ImageResult[]>(() =>
174166
{
175-
if (Config.Notification && Config.NotificationImage) {
176-
177-
Debug.WriteLine($"Finding direct results");
178-
var direct = GetDirectResults();
179-
180-
return direct;
181-
}
167+
Debug.WriteLine("Finding direct results");
168+
var direct = GetDirectResults();
182169

183-
return null;
184-
})
170+
return direct;
171+
}),
172+
FirstDirect = new Lazy<ImageResult>(GetDirectResult)
185173
};
186174

187-
SearchCompleted?.Invoke(null, args2);
175+
SearchCompleted?.Invoke(null, args);
188176
}
189177

190178
#endregion
@@ -202,8 +190,7 @@ public async Task RefineSearchAsync()
202190

203191
Debug.WriteLine("Finding best result");
204192

205-
var best = GetDirectResults()
206-
.FirstOrDefault(f => ImageHelper.IsDirect(f.Direct.ToString(), DirectImageCriterion.Binary));
193+
var best = GetDirectResult();
207194

208195
if (best == null) {
209196
throw new SmartImageException(ERR_NO_BEST_RESULT);
@@ -237,6 +224,37 @@ public List<SearchResult> MaximizeResults<T>(Func<SearchResult, T> property)
237224
return res;
238225
}
239226

227+
[CanBeNull]
228+
public ImageResult GetDirectResult()
229+
{
230+
231+
// var best = FindBestResults().ToList();
232+
/*var best = Results.Where(r => r.IsNonPrimitive)
233+
.Where(r => r.Engine.SearchType.HasFlag(EngineSearchType.Image))
234+
.AsParallel()
235+
.OrderByDescending(r => r.PrimaryResult.Similarity)
236+
.ThenByDescending(r => r.PrimaryResult.PixelResolution)
237+
.ThenByDescending(r => r.PrimaryResult.DetailScore)
238+
.SelectMany(r =>
239+
{
240+
var x = r.OtherResults;
241+
x.Insert(0, r.PrimaryResult);
242+
return x;
243+
})
244+
.ToList();*/
245+
246+
var best = RefineFilter(r => r.IsNonPrimitive
247+
&& r.Engine.ResultType.HasFlag(EngineResultType.Image)).ToList();
248+
249+
var images = best.Where(x => x.CheckDirect(DirectImageCriterion.Regex))
250+
.Take(10)
251+
.AsParallel()
252+
.FirstOrDefault(x => x.CheckDirect(DirectImageCriterion.Binary));
253+
254+
255+
return images;
256+
}
257+
240258
public ImageResult[] GetDirectResults(int count = 5)
241259
{
242260

@@ -260,9 +278,12 @@ public ImageResult[] GetDirectResults(int count = 5)
260278

261279
Debug.WriteLine($"{nameof(SearchClient)}: Found {best.Count} best results", C_DEBUG);
262280

263-
var images1 = best.Where(x => x.CheckDirect(DirectImageCriterion.Regex)).Take(count*2);
264-
265-
var images = images1.Where(x => x.CheckDirect(DirectImageCriterion.Binary)).Take(count).ToList();
281+
var images = best.Where(x => x.CheckDirect(DirectImageCriterion.Regex))
282+
.Take(count * 2)
283+
.AsParallel()
284+
.Where(x => x.CheckDirect(DirectImageCriterion.Binary))
285+
.Take(count)
286+
.ToList();
266287

267288
Debug.WriteLine($"{nameof(SearchClient)}: Found {images.Count} direct results", C_DEBUG);
268289

@@ -334,7 +355,16 @@ public sealed class SearchCompletedEventArgs : EventArgs
334355
public Lazy<ImageResult[]> Direct { get; internal set; }
335356

336357
[CanBeNull]
337-
public Lazy<ImageResult> Best { get; internal set; }
358+
public Lazy<ImageResult> FirstDirect { get; internal set; }
359+
360+
[CanBeNull]
361+
public List<Lazy<ImageResult>> xDirect { get; internal set; }
362+
363+
364+
[CanBeNull]
365+
public Lazy<ImageResult> Detailed { get; internal set; }
366+
367+
// todo: maybe lazy list? i.e., each item is a lazy load
338368
}
339369

340370
public sealed class ResultCompletedEventArgs : EventArgs

SmartImage.Lib/Searching/ImageResult.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,14 @@ public void UpdateImageData()
239239
}
240240
}
241241

242-
public bool CheckDirect(DirectImageCriterion d = DirectImageCriterion.Regex)
242+
public bool CheckDirect(DirectImageCriterion d)
243243
{
244-
var s = Url?.ToString();
245-
246-
if (s is not { }) {
244+
if (Url is not {}) {
247245
return false;
248246
}
249247

248+
var s = Url.ToString();
249+
250250
var b = ImageHelper.IsDirect(s, d);
251251

252252
if (b) {

SmartImage.Lib/Utilities/ImageHelper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public static Dictionary<string, string> UtilitiesMap
8888

8989
private const int TimeoutMS = 1000;
9090

91+
[CanBeNull]
9192
public static string Download(Uri src, string path)
9293
{
9394
var filename = NormalizeFilename(src);
@@ -96,14 +97,14 @@ public static string Download(Uri src, string path)
9697

9798
using var wc = new WebClient();
9899

99-
Debug.WriteLine($"Downloading {src} to {combine} ...");
100+
Debug.WriteLine($"{nameof(ImageHelper)}: Downloading {src} to {combine} ...");
100101

101102
try {
102103
wc.DownloadFile(src.ToString(), combine);
103104
return combine;
104105
}
105106
catch (Exception e) {
106-
Debug.WriteLine($"{e.Message}", LogCategories.C_ERROR);
107+
Debug.WriteLine($"{nameof(ImageHelper)}: {e.Message}", LogCategories.C_ERROR);
107108
return null;
108109
}
109110
}
@@ -127,8 +128,7 @@ private static string NormalizeFilename(Uri src)
127128
}
128129

129130
filename += ext;
130-
131-
Debug.WriteLine("Fixed file", C_DEBUG);
131+
132132
}
133133

134134
// Stupid URI parameter Twitter appends to filenames
@@ -159,7 +159,7 @@ public static async Task<List<string>> FindDirectImages(string url, int count =
159159
document = WebUtilities.GetHtmlDocument(url);
160160
}
161161
catch (Exception e) {
162-
Debug.WriteLine($"{e.Message}", C_ERROR);
162+
Debug.WriteLine($"{nameof(ImageHelper)}: {e.Message}", C_ERROR);
163163

164164
return null;
165165
}

SmartImage/UI/AppInterface.cs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ static AppInterface()
192192

193193
public static void ShowToast(object sender, SearchCompletedEventArgs args)
194194
{
195-
var bestResult = args.Best;
195+
var bestResult = args.Detailed;
196196

197197
var builder = new ToastContentBuilder();
198198
var button = new ToastButton();
@@ -210,28 +210,39 @@ public static void ShowToast(object sender, SearchCompletedEventArgs args)
210210
.AddText($"{bestResult}")
211211
.AddText($"Results: {Client.Results.Count}");
212212

213-
var direct = args.Direct?.Value;
213+
if (Config.Notification && Config.NotificationImage) {
214+
214215

215-
if (direct != null) {
216-
var path = Path.GetTempPath();
216+
var imageResult = args.FirstDirect.Value;
217217

218-
string file;
218+
if (imageResult != null) {
219+
var path = Path.GetTempPath();
219220

220-
int i = 0;
221+
string file = ImageHelper.Download(imageResult.Direct, path);
221222

222-
do {
223-
file = ImageHelper.Download(direct[i++].Direct, path);
223+
if (file == null) {
224+
int i = 0;
224225

225-
} while (string.IsNullOrWhiteSpace(file) && i < direct.Length);
226+
var imageResults = args.Direct.Value;
226227

227-
Debug.WriteLine($"Downloaded {file}", C_INFO);
228+
do {
229+
file = ImageHelper.Download(imageResults[i++].Direct, path);
230+
231+
} while (string.IsNullOrWhiteSpace(file) && i < imageResults.Length);
232+
233+
}
234+
235+
Debug.WriteLine($"{nameof(AppInterface)}: Downloaded {file}", C_INFO);
236+
237+
builder.AddHeroImage(new Uri(file));
238+
239+
AppDomain.CurrentDomain.ProcessExit += (sender2, args2) =>
240+
{
241+
File.Delete(file);
242+
};
243+
}
228244

229-
builder.AddHeroImage(new Uri(file));
230245

231-
AppDomain.CurrentDomain.ProcessExit += (sender2, args2) =>
232-
{
233-
File.Delete(file);
234-
};
235246
}
236247

237248
builder.SetBackgroundActivation();

0 commit comments

Comments
 (0)