66using System . Threading ;
77using System . Threading . Tasks ;
88using JetBrains . Annotations ;
9+ using Kantan . Net ;
910using Kantan . Threading ;
1011using Novus . Utilities ;
1112using SmartImage . Lib . Engines ;
@@ -176,7 +177,10 @@ public async Task RunSearchAsync()
176177 DetailedResults . Add ( value . PrimaryResult ) ;
177178 }
178179
179- ThreadPool . QueueUserWorkItem ( c => FindDirectResults ( c , value ) ) ;
180+ /* 1st pass */
181+ if ( value . IsSuccessful && value . IsNonPrimitive ) {
182+ ThreadPool . QueueUserWorkItem ( c => FindDirectResults ( c , value ) ) ;
183+ }
180184
181185 // Call event
182186 ResultCompleted ? . Invoke ( null , new ResultCompletedEventArgs ( value )
@@ -190,6 +194,10 @@ public async Task RunSearchAsync()
190194
191195 Trace . WriteLine ( $ "{ nameof ( SearchClient ) } : Search complete", C_SUCCESS ) ;
192196
197+
198+ /* 2nd pass */
199+
200+
193201 var args = new SearchCompletedEventArgs
194202 {
195203 Results = Results ,
@@ -198,9 +206,65 @@ public async Task RunSearchAsync()
198206 Filtered = FilteredResults
199207 } ;
200208
209+
201210 SearchCompleted ? . Invoke ( null , args ) ;
202211 }
203212
213+ public async void FindDirectResults ( SearchResult result )
214+ {
215+ Debug . WriteLine ( $ "searching within { result . Engine . Name } ") ;
216+
217+ for ( int i = 0 ; i < result . AllResults . Count ; i ++ ) {
218+ var ir = result . AllResults [ i ] ;
219+
220+ var b = await ir . FindDirectImages ( ) ;
221+
222+ if ( b && ! DirectResults . Contains ( ir ) ) {
223+ ir . Direct = new Uri ( UriUtilities . NormalizeUrl ( ir . Direct ) ) ;
224+
225+ Debug . WriteLine ( $ "{ ir . Direct } ") ;
226+
227+ DirectResults . Add ( ir ) ;
228+
229+ result . PrimaryResult . Direct ??= ir . Direct ;
230+
231+ DirectFound ? . Invoke ( null , new DirectResultsFoundEventArgs
232+ {
233+ DirectResultsSubset = new ( ) { ir } ,
234+ } ) ;
235+ ResultUpdated ? . Invoke ( null , EventArgs . Empty ) ;
236+ }
237+ }
238+ }
239+
240+
241+ private void FindDirectResults ( object state , SearchResult value , int take1 = 10 , int take2 = 5 )
242+ {
243+
244+ var imageResults = value . AllResults ;
245+
246+ var images = imageResults . AsParallel ( )
247+ . Where ( x => x . CheckDirect ( DirectImageCriterion . Regex ) )
248+ . Take ( take1 )
249+ . Where ( x => x . CheckDirect ( DirectImageCriterion . Binary ) )
250+ . Take ( take2 )
251+ . ToList ( ) ;
252+
253+ if ( images . Any ( ) ) {
254+ Debug . WriteLine ( $ "*{ nameof ( SearchClient ) } : Found { images . Count } direct results", C_DEBUG ) ;
255+ DirectResults . AddRange ( images ) ;
256+
257+ DirectFound ? . Invoke ( null , new DirectResultsFoundEventArgs
258+ {
259+ DirectResultsSubset = images ,
260+ } ) ;
261+ }
262+ else {
263+ var t = Task . Factory . StartNew ( ( ) => FindDirectResults ( value ) ) ;
264+
265+ }
266+ }
267+
204268 /// <summary>
205269 /// Waits until <see cref="DirectResults"/> contains any elements
206270 /// </summary>
@@ -211,8 +275,10 @@ public Task<List<ImageResult>> WaitForDirectResults()
211275 {
212276 while ( Results . Any ( ) && ! DirectResults . Any ( ) ) {
213277 if ( IsComplete ) {
214- List < ImageResult > rescan = RescanImageResults ( Results ) ;
215- DirectResults . AddRange ( rescan ) ;
278+ var b = Results . SelectMany ( x => x . AllResults . Where ( x2 => x2 . Direct != null ) )
279+ . OrderByDescending ( x => x . PixelResolution )
280+ . ToList ( ) ;
281+ DirectResults . AddRange ( b ) ;
216282
217283 break ;
218284 }
@@ -222,13 +288,7 @@ public Task<List<ImageResult>> WaitForDirectResults()
222288 } ) ;
223289 }
224290
225- private static List < ImageResult > RescanImageResults ( List < SearchResult > results )
226- {
227- var b = results . SelectMany ( x => x . AllResults . Where ( x2 => x2 . Direct != null ) )
228- . OrderByDescending ( x => x . PixelResolution )
229- . ToList ( ) ;
230- return b ;
231- }
291+ #endregion
232292
233293 /// <summary>
234294 /// Refines search results by searching with the most-detailed result (<see cref="FindDirectResults" />).
@@ -258,29 +318,6 @@ public async Task RefineSearchAsync()
258318 await RunSearchAsync ( ) ;
259319 }
260320
261- private void FindDirectResults ( object state , SearchResult value , int take1 = 10 , int take2 = 5 )
262- {
263- var imageResults = value . AllResults ;
264-
265- var images = imageResults . AsParallel ( )
266- . Where ( x => x . CheckDirect ( DirectImageCriterion . Regex ) )
267- . Take ( take1 )
268- . Where ( x => x . CheckDirect ( DirectImageCriterion . Binary ) )
269- . Take ( take2 )
270- . ToList ( ) ;
271-
272- if ( images . Any ( ) ) {
273- Debug . WriteLine ( $ "*{ nameof ( SearchClient ) } : Found { images . Count } direct results", C_DEBUG ) ;
274- DirectResults . AddRange ( images ) ;
275-
276- DirectFound ? . Invoke ( null , new DirectResultsFoundEventArgs
277- {
278- DirectResultsSubset = images ,
279- } ) ;
280- }
281- }
282-
283-
284321 public static List < ImageResult > ApplyPredicateFilter ( List < SearchResult > results , Predicate < SearchResult > predicate )
285322 {
286323 var query = results . Where ( r => predicate ( r ) )
@@ -309,8 +346,6 @@ public List<SearchResult> MaximizeResults<T>(Func<SearchResult, T> property)
309346 return res ;
310347 }
311348
312- #endregion
313-
314349 public static BaseUploadEngine [ ] GetAllUploadEngines ( )
315350 {
316351 return typeof ( BaseUploadEngine ) . GetAllSubclasses ( )
@@ -327,6 +362,11 @@ public static BaseSearchEngine[] GetAllSearchEngines()
327362 . ToArray ( ) ;
328363 }
329364
365+ /// <summary>
366+ /// Fires when a result has been updated with new information
367+ /// </summary>
368+ public event EventHandler ResultUpdated ;
369+
330370 /// <summary>
331371 /// Fires when a result is returned (<see cref="RunSearchAsync" />).
332372 /// </summary>
0 commit comments