1919// ReSharper disable IdentifierTypo
2020// ReSharper disable StringLiteralTypo
2121
22- namespace SmartImage . Lib . Engines . Impl
22+ namespace SmartImage . Lib . Engines . Impl ;
23+
24+ public sealed class Ascii2DEngine : WebSearchEngine
2325{
24- public sealed class Ascii2DEngine : WebSearchEngine
25- {
26- public Ascii2DEngine ( ) : base ( "https://ascii2d.net/search/url/" ) { }
26+ public Ascii2DEngine ( ) : base ( "https://ascii2d.net/search/url/" ) { }
2727
28- public override TimeSpan Timeout => TimeSpan . FromSeconds ( 5 ) ;
28+ public override TimeSpan Timeout => TimeSpan . FromSeconds ( 5 ) ;
2929
30- public override SearchEngineOptions EngineOption => SearchEngineOptions . Ascii2D ;
30+ public override SearchEngineOptions EngineOption => SearchEngineOptions . Ascii2D ;
3131
32- public override string Name => EngineOption . ToString ( ) ;
32+ public override string Name => EngineOption . ToString ( ) ;
3333
34- /// <inheritdoc />
35- public override EngineSearchType SearchType => EngineSearchType . Image | EngineSearchType . Metadata ;
34+ /// <inheritdoc />
35+ public override EngineSearchType SearchType => EngineSearchType . Image | EngineSearchType . Metadata ;
3636
37- protected override Uri GetRawUri ( ImageQuery query )
38- {
39- var uri = base . GetRawUri ( query ) ;
37+ protected override Uri GetRawUri ( ImageQuery query )
38+ {
39+ var uri = base . GetRawUri ( query ) ;
4040
41- // var request = WebRequest.Create(uri);
42- // using var response = request.GetResponse();
41+ // var request = WebRequest.Create(uri);
42+ // using var response = request.GetResponse();
4343
44- var request = new HttpRequestMessage ( )
45- {
46- RequestUri = uri
47- } ;
44+ var request = new HttpRequestMessage ( )
45+ {
46+ RequestUri = uri
47+ } ;
4848
49- var response = new HttpClient ( ) . Send ( request ) ;
49+ var response = new HttpClient ( ) . Send ( request ) ;
5050
5151
52- /*
53- * URL parameters
54- *
55- * color https://ascii2d.net/search/color/<hash>
56- * detail https://ascii2d.net/search/bovw/<hash>
57- *
58- */
52+ /*
53+ * URL parameters
54+ *
55+ * color https://ascii2d.net/search/color/<hash>
56+ * detail https://ascii2d.net/search/bovw/<hash>
57+ *
58+ */
5959
60- /*
61- * With Ascii2D, two requests need to be made in order to get the detail results
62- * as the color results are returned by default
63- *
64- */
60+ /*
61+ * With Ascii2D, two requests need to be made in order to get the detail results
62+ * as the color results are returned by default
63+ *
64+ */
6565
66- // var responseUri = response.ResponseUri;
66+ // var responseUri = response.ResponseUri;
6767
68- Debug . Assert ( response . RequestMessage ? . RequestUri != null ) ;
68+ Debug . Assert ( response . RequestMessage ? . RequestUri != null ) ;
6969
70- var responseUri = response . RequestMessage . RequestUri ;
70+ var responseUri = response . RequestMessage . RequestUri ;
7171
72- string detailUrl = responseUri . ToString ( ) . Replace ( "/color/" , "/bovw/" ) ;
72+ string detailUrl = responseUri . ToString ( ) . Replace ( "/color/" , "/bovw/" ) ;
7373
74- return new Uri ( detailUrl ) ;
74+ return new Uri ( detailUrl ) ;
7575
76- }
76+ }
7777
78- protected override SearchResultOrigin GetResultOrigin ( ImageQuery query )
79- {
80- var now = Stopwatch . GetTimestamp ( ) ;
78+ protected override SearchResultOrigin GetResultOrigin ( ImageQuery query )
79+ {
80+ var now = Stopwatch . GetTimestamp ( ) ;
8181
82- var rawUri = GetRawUri ( query ) ;
82+ var rawUri = GetRawUri ( query ) ;
8383
84- var content = WebUtilities . GetString ( rawUri . ToString ( ) ) ;
85- var diff = TimeSpan . FromTicks ( Stopwatch . GetTimestamp ( ) - now ) ;
84+ var content = WebUtilities . GetString ( rawUri . ToString ( ) ) ;
85+ var diff = TimeSpan . FromTicks ( Stopwatch . GetTimestamp ( ) - now ) ;
8686
87- var stub = new SearchResultOrigin ( )
87+ var stub = new SearchResultOrigin ( )
88+ {
89+ InitialResponse = new RestResponse
8890 {
89- InitialResponse = new RestResponse
90- {
91- Content = content
92- } ,
93- Retrieval = diff ,
94- InitialSuccess = true ,
95- RawUri = rawUri
96- } ;
91+ Content = content
92+ } ,
93+ Retrieval = diff ,
94+ InitialSuccess = true ,
95+ RawUri = rawUri
96+ } ;
9797
9898
99- return stub ;
100- }
99+ return stub ;
100+ }
101101
102102
103- protected override SearchResult Process ( object obj , SearchResult sr )
104- {
105- var doc = ( IDocument ) obj ;
103+ protected override SearchResult Process ( object obj , SearchResult sr )
104+ {
105+ var doc = ( IDocument ) obj ;
106106
107- var nodes = doc . Body . SelectNodes ( "//*[contains(@class, 'info-box')]" ) ;
107+ var nodes = doc . Body . SelectNodes ( "//*[contains(@class, 'info-box')]" ) ;
108108
109- var rg = new List < ImageResult > ( ) ;
109+ var rg = new List < ImageResult > ( ) ;
110110
111- foreach ( var node in nodes ) {
112- var ir = new ImageResult ( ) ;
111+ foreach ( var node in nodes ) {
112+ var ir = new ImageResult ( ) ;
113113
114- var info = node . ChildNodes . Where ( n => ! String . IsNullOrWhiteSpace ( n . TextContent ) ) . ToArray ( ) ;
114+ var info = node . ChildNodes . Where ( n => ! String . IsNullOrWhiteSpace ( n . TextContent ) ) . ToArray ( ) ;
115115
116- string hash = info . First ( ) . TextContent ;
116+ string hash = info . First ( ) . TextContent ;
117117
118- ir . OtherMetadata . Add ( "Hash" , hash ) ;
118+ ir . OtherMetadata . Add ( "Hash" , hash ) ;
119119
120- string [ ] data = info [ 1 ] . TextContent . Split ( ' ' ) ;
120+ string [ ] data = info [ 1 ] . TextContent . Split ( ' ' ) ;
121121
122- string [ ] res = data [ 0 ] . Split ( 'x' ) ;
123- ir . Width = Int32 . Parse ( res [ 0 ] ) ;
124- ir . Height = Int32 . Parse ( res [ 1 ] ) ;
122+ string [ ] res = data [ 0 ] . Split ( 'x' ) ;
123+ ir . Width = Int32 . Parse ( res [ 0 ] ) ;
124+ ir . Height = Int32 . Parse ( res [ 1 ] ) ;
125125
126- string fmt = data [ 1 ] ;
126+ string fmt = data [ 1 ] ;
127127
128- string size = data [ 2 ] ;
128+ string size = data [ 2 ] ;
129129
130- if ( info . Length >= 3 ) {
131- var node2 = info [ 2 ] ;
132- var desc = info . Last ( ) . FirstChild ;
133- var ns = desc . NextSibling ;
130+ if ( info . Length >= 3 ) {
131+ var node2 = info [ 2 ] ;
132+ var desc = info . Last ( ) . FirstChild ;
133+ var ns = desc . NextSibling ;
134134
135- if ( node2 . ChildNodes . Length >= 2 && node2 . ChildNodes [ 1 ] . ChildNodes . Length >= 2 ) {
136- var node2Sub = node2 . ChildNodes [ 1 ] ;
135+ if ( node2 . ChildNodes . Length >= 2 && node2 . ChildNodes [ 1 ] . ChildNodes . Length >= 2 ) {
136+ var node2Sub = node2 . ChildNodes [ 1 ] ;
137137
138- if ( node2Sub . ChildNodes . Length >= 8 ) {
139- ir . Description = node2Sub . ChildNodes [ 3 ] . TextContent . Trim ( ) ;
140- ir . Artist = node2Sub . ChildNodes [ 5 ] . TextContent . Trim ( ) ;
141- ir . Site = node2Sub . ChildNodes [ 7 ] . TextContent . Trim ( ) ;
142- }
138+ if ( node2Sub . ChildNodes . Length >= 8 ) {
139+ ir . Description = node2Sub . ChildNodes [ 3 ] . TextContent . Trim ( ) ;
140+ ir . Artist = node2Sub . ChildNodes [ 5 ] . TextContent . Trim ( ) ;
141+ ir . Site = node2Sub . ChildNodes [ 7 ] . TextContent . Trim ( ) ;
143142 }
143+ }
144144
145- if ( ns . ChildNodes . Length >= 4 ) {
146- var childNode = ns . ChildNodes [ 3 ] ;
145+ if ( ns . ChildNodes . Length >= 4 ) {
146+ var childNode = ns . ChildNodes [ 3 ] ;
147147
148- string l1 = ( ( IHtmlElement ) childNode ) . GetAttribute ( "href" ) ;
148+ string l1 = ( ( IHtmlElement ) childNode ) . GetAttribute ( "href" ) ;
149149
150- if ( l1 is not null ) {
151- ir . Url = new Uri ( l1 ) ;
152- }
150+ if ( l1 is not null ) {
151+ ir . Url = new Uri ( l1 ) ;
153152 }
154153 }
155-
156- rg . Add ( ir ) ;
157154 }
158155
159- // Skip original image
156+ rg . Add ( ir ) ;
157+ }
160158
161- rg = rg . Skip ( 1 ) . ToList ( ) ;
159+ // Skip original image
162160
163- sr . PrimaryResult = rg . First ( ) ;
161+ rg = rg . Skip ( 1 ) . ToList ( ) ;
164162
165- // sr.PrimaryResult.UpdateFrom(rg[0] );
163+ sr . PrimaryResult = rg . First ( ) ;
166164
167- sr . OtherResults . AddRange ( rg ) ;
165+ // sr.PrimaryResult.UpdateFrom (rg[0] );
168166
169- sr . PrimaryResult . Quality = sr . PrimaryResult . MegapixelResolution switch
170- {
171- null => ResultQuality . Indeterminate ,
172- >= 1 => ResultQuality . High ,
173- _ => ResultQuality . Low ,
174- } ;
167+ sr . OtherResults . AddRange ( rg ) ;
175168
169+ sr . PrimaryResult . Quality = sr . PrimaryResult . MegapixelResolution switch
170+ {
171+ null => ResultQuality . Indeterminate ,
172+ >= 1 => ResultQuality . High ,
173+ _ => ResultQuality . Low ,
174+ } ;
176175
177- return sr ;
178- }
176+
177+ return sr ;
179178 }
180179}
0 commit comments