1- #region
2-
3- #nullable enable
4- using System ;
1+ using System ;
52using System . Collections . Generic ;
6- using System . Drawing ;
73using System . IO ;
84using System . Linq ;
95using System . Threading ;
10- using System . Threading . Tasks ;
11- using SimpleCore . Utilities ;
126using SimpleCore . Win32 . Cli ;
137using SmartImage . Searching . Engines . Imgur ;
148using SmartImage . Searching . Engines . SauceNao ;
1812using SmartImage . Shell ;
1913using SmartImage . Utilities ;
2014
21- // ReSharper disable ReturnTypeCanBeEnumerable.Local
22-
23- #endregion
24-
25- // ReSharper disable ReturnTypeCanBeEnumerable.Global
26-
2715namespace SmartImage . Searching
2816{
29- /// <summary>
30- /// Runs image searches
31- /// </summary>
32- public static class Search
17+ public class SearchClient : IDisposable
3318 {
3419 /// <summary>
35- /// Common image extensions
20+ /// Common image extensions
3621 /// </summary>
3722 private static readonly string [ ] ImageExtensions =
3823 {
3924 ".jpg" , ".jpeg" , ".png" , ".gif" , ".tga" , ".jfif" , ".bmp"
4025 } ;
4126
27+ private readonly SearchEngines m_engines ;
4228
43- private static ISearchEngine [ ] GetAllEngines ( )
44- {
45- var engines = new ISearchEngine [ ]
46- {
47- new SauceNaoClient ( ) ,
48- new ImgOpsClient ( ) ,
49- new GoogleImagesClient ( ) ,
50- new TinEyeClient ( ) ,
51- new IqdbClient ( ) ,
52- new BingClient ( ) ,
53- new YandexClient ( ) ,
54- new KarmaDecayClient ( ) ,
55- new TraceMoeClient ( )
56- } ;
57-
58- return engines ;
59- }
29+ private readonly string m_img ;
6030
31+ private readonly string m_imgUrl ;
6132
62- public static bool RunSearch ( string img , ref ConsoleOption [ ] res )
63- {
64- /*
65- * Run
66- */
33+ private ConsoleOption [ ] m_results ;
6734
68- // Run checks
69- if ( ! IsFileValid ( img ) ) {
70- SearchConfig . UpdateFile ( ) ;
35+ private readonly Thread [ ] m_threads ;
7136
72- return false ;
73- }
37+ public SearchClient ( string img )
38+ {
39+
7440
7541 string auth = SearchConfig . Config . ImgurAuth ;
7642 bool useImgur = ! String . IsNullOrWhiteSpace ( auth ) ;
@@ -84,76 +50,81 @@ public static bool RunSearch(string img, ref ConsoleOption[] res)
8450 engines = SearchConfig . ENGINES_DEFAULT ;
8551 }
8652
53+ m_engines = engines ;
8754
88- // Display config
89- CliOutput . WriteInfo ( SearchConfig . Config ) ;
9055
56+ m_imgUrl = Upload ( img , useImgur ) ;
9157
92- string imgUrl = Upload ( img , useImgur ) ;
58+
59+ m_threads = CreateSearchThreads ( ) ;
60+ }
9361
94- CliOutput . WriteInfo ( "Temporary image url: {0}" , imgUrl ) ;
62+ public ref ConsoleOption [ ] Results => ref m_results ;
9563
96- Console . WriteLine ( ) ;
64+ public void Dispose ( )
65+ {
66+ foreach ( var thread in m_threads ) {
67+ thread . Join ( ) ;
68+ }
69+ }
9770
71+ public void Start ( )
72+ {
73+ // Display config
74+ CliOutput . WriteInfo ( SearchConfig . Config ) ;
9875
99- // Where the actual searching occurs
76+ CliOutput . WriteInfo ( "Temporary image url: {0}" , m_imgUrl ) ;
10077
101- //StartSearches(imgUrl, engines, ref res);
102- var threads = StartSearchesMultithread ( imgUrl , engines , ref res ) ;
78+ Console . WriteLine ( ) ;
10379
104- foreach ( var thread in threads ) {
80+ foreach ( var thread in m_threads ) {
10581 thread . Start ( ) ;
10682 }
107-
108- return true ;
10983 }
11084
111-
11285
113- private static Thread [ ] StartSearchesMultithread ( string imgUrl , SearchEngines engines , ref ConsoleOption [ ] res )
86+ private Thread [ ] CreateSearchThreads ( )
11487 {
11588 // todo: improve
116- // todo: use tasks
89+
11790
11891 var availableEngines = GetAllEngines ( )
119- . Where ( e => engines . HasFlag ( e . Engine ) )
92+ . Where ( e => m_engines . HasFlag ( e . Engine ) )
12093 . ToArray ( ) ;
12194
12295 int i = 0 ;
12396
124- res = new SearchResult [ availableEngines . Length + 1 ] ;
125- res [ i ] = new SearchResult ( ConsoleColor . Gray , "(Original image)" , imgUrl , null ) ;
97+ m_results = new ConsoleOption [ availableEngines . Length + 1 ] ;
98+ m_results [ i ] = new SearchResult ( ConsoleColor . Gray , "(Original image)" , m_imgUrl ) ;
12699
127100 i ++ ;
128101
129102
130103 var threads = new List < Thread > ( ) ;
131104
132- foreach ( var currentEngine in availableEngines )
133- {
134- var options = res ;
105+ foreach ( var currentEngine in availableEngines ) {
135106
136- int i1 = i ;
107+ var resultsCopy = m_results ;
108+ int iCopy = i ;
137109
138- ThreadStart ts = ( ) =>
110+ ThreadStart threadFunction = ( ) =>
139111 {
140- var result = currentEngine . GetResult ( imgUrl ) ;
141- options [ i1 ] = result ;
142-
112+ var result = currentEngine . GetResult ( m_imgUrl ) ;
113+ resultsCopy [ iCopy ] = result ;
143114
144115 // If the engine is priority, open its result in the browser
145- if ( SearchConfig . Config . PriorityEngines . HasFlag ( currentEngine . Engine ) )
146- {
116+ if ( SearchConfig . Config . PriorityEngines . HasFlag ( currentEngine . Engine ) ) {
147117 Network . OpenUrl ( result . Url ) ;
148118 }
149119
150120 ConsoleIO . Status = ConsoleIO . STATUS_REFRESH ;
121+ } ;
151122
152-
123+ var t = new Thread ( threadFunction )
124+ {
125+ Priority = ThreadPriority . Highest
153126 } ;
154- var t = new Thread ( ts ) ;
155- t . Priority = ThreadPriority . Highest ;
156- t . Name = string . Format ( "thread - {0}" , currentEngine . Name ) ;
127+
157128 threads . Add ( t ) ;
158129
159130 i ++ ;
@@ -164,9 +135,27 @@ private static Thread[] StartSearchesMultithread(string imgUrl, SearchEngines en
164135
165136 }
166137
167- private static bool IsFileValid ( string img )
138+ private static ISearchEngine [ ] GetAllEngines ( )
139+ {
140+ var engines = new ISearchEngine [ ]
141+ {
142+ new SauceNaoClient ( ) ,
143+ new ImgOpsClient ( ) ,
144+ new GoogleImagesClient ( ) ,
145+ new TinEyeClient ( ) ,
146+ new IqdbClient ( ) ,
147+ new BingClient ( ) ,
148+ new YandexClient ( ) ,
149+ new KarmaDecayClient ( ) ,
150+ new TraceMoeClient ( )
151+ } ;
152+
153+ return engines ;
154+ }
155+
156+ internal static bool IsFileValid ( string img )
168157 {
169- if ( string . IsNullOrWhiteSpace ( img ) ) {
158+ if ( String . IsNullOrWhiteSpace ( img ) ) {
170159 return false ;
171160 }
172161
0 commit comments