1616using SmartImage . Lib . Clients ;
1717using SmartImage . Lib . Cookies ;
1818using SmartImage . Lib . Engines . Results ;
19+ using SmartImage . Lib . Model ;
1920
2021// ReSharper disable CognitiveComplexity
2122
@@ -26,7 +27,7 @@ namespace SmartImage.Lib.Engines.Search;
2627
2728// todo
2829
29- public sealed class Ascii2DEngine : WebSearchEngine < Ascii2DItem , IList < INode > > , ICookiesReceiver
30+ public sealed class Ascii2DEngine : WebSearchEngine < Ascii2DItem , IList < INode > > , ICookiesReceiver , ISearchConfigReceiver
3031{
3132
3233 public override SearchEngineOptions EngineOption => SearchEngineOptions . Ascii2D ;
@@ -43,34 +44,15 @@ public sealed class Ascii2DEngine : WebSearchEngine<Ascii2DItem, IList<INode>>,
4344
4445 public const string MAIN_URL = "https://ascii2d.net/search/url/" ;
4546
47+ private FlareSolverrClient m_fsClient ;
48+
4649 public Ascii2DEngine ( ) : base ( MAIN_URL )
4750 {
48- Timeout = TimeSpan . FromSeconds ( 30 ) ;
51+ Timeout = TimeSpan . FromSeconds ( 30 ) ;
4952 MaxLength = 10_000_000 ;
50- Jar = new CookieJar ( ) ;
51- }
52-
53- public async ValueTask < bool > ApplyCookiesAsync ( ICookiesSource source , CancellationToken ct )
54- {
55- if ( /*FlareSolverrClient.Value.IsInitialized*/ source == null ) {
56- return false ;
57- }
58-
59- var cookies = await source . GetOrLoadCookiesAsync ( ct ) . ConfigureAwait ( false ) ;
60-
61- foreach ( var bck in cookies ) {
62- var ck = bck . AsCookie ( ) ;
63-
64- if ( ck . Domain . Contains ( "ascii2d" ) ) {
65- Jar . AddOrReplace ( new FlurlCookie ( ck . Name , ck . Value , BaseUrl ) ) ;
66- }
67- }
68-
69-
70- return true ;
53+ Jar = new CookieJar ( ) ;
7154 }
7255
73-
7456
7557 public override void Dispose ( ) { }
7658
@@ -135,18 +117,10 @@ protected override ValueTask<IList<INode>> ParseIntermediateAsync(IDocument src)
135117
136118 protected override ValueTask < IEnumerable < Ascii2DItem > > ParseItemsAsync ( IList < INode > source , SearchResult r )
137119 {
138- var buf = new List < Ascii2DItem > ( source . Count ) ;
139-
140- foreach ( var node in source ) {
141- var item = Ascii2DItem . ParseSource ( node , r ) ;
142- buf . Add ( item ) ;
143- }
144-
145- return ValueTask . FromResult < IEnumerable < Ascii2DItem > > ( buf ) ;
120+ return ValueTask . FromResult ( source . Select ( node => Ascii2DItem . ParseSource ( node , r ) ) ) ;
146121 }
147122
148- protected override async Task < IDocument > GetSourceAsync ( SearchResult sr , SearchQuery query ,
149- CancellationToken token = default )
123+ protected override async Task < IDocument > GetSourceAsync ( SearchResult sr , SearchQuery query , CancellationToken token = default )
150124 {
151125 var parser = new HtmlParser ( ) ;
152126
@@ -169,48 +143,29 @@ protected override async Task<IDocument> GetSourceAsync(SearchResult sr, SearchQ
169143
170144 }*/
171145
172- if ( FlareSolverrClient . Value . IsInitialized ) {
146+ if ( m_fsClient . IsInitialized ) {
173147
174148 var msg = new HttpRequestMessage ( HttpMethod . Get , origin ) ;
175149
176- var fsr = await FlareSolverrClient . Value . Clearance . Solverr . SolveAsync ( msg ) . ConfigureAwait ( false ) ;
150+ var fsr = await m_fsClient . Clearance . Solverr . SolveAsync ( msg ) . ConfigureAwait ( false ) ;
177151 var cookies = fsr . Solution . Cookies ;
178152 var newUrl = fsr . Solution . Url ;
179153
154+ Logger . LogTrace ( "{Name} using {Fs}: {CookieCnt} {NewUrl}" , Name , fsr , cookies . Length , newUrl ) ;
180155
181156 foreach ( FlareSolverrCookie cookie in cookies ) {
182157 Jar . AddOrReplace ( new FlurlCookie ( cookie . Name , cookie . Value , fsr . Solution . Url ) ) ;
183158 }
184-
185- using var res = await Client . Request ( newUrl )
186- . WithSettings ( static x => { x . HttpVersion = "2.0" ; } )
187- . AllowAnyHttpStatus ( )
188- . WithCookies ( Jar )
189- . WithTimeout ( Timeout )
190- /*.OnError(s =>
191- {
192- Debug.WriteLine($"{s.Response}");
193- s.ExceptionHandled = true;
194-
195- })*/
196- . GetAsync ( cancellationToken : token ) . ConfigureAwait ( false ) ;
197-
198-
199- // var res1 = await FlareSolverrClient.Client.SendAsync(msg, token);
200- // str = await res1.Content.ReadAsStringAsync(token);
201- str = await res . GetStringAsync ( ) . ConfigureAwait ( false ) ;
202-
203159 }
204- else {
205- using var res = await GetResponseByUrlAsync ( origin , token ) . ConfigureAwait ( false ) ;
206160
207- if ( res . StatusCode == ( int ) HttpStatusCode . BadGateway ) {
208- return null ;
209- }
161+ using var res = await GetResponseByUrlAsync ( origin , token ) . ConfigureAwait ( false ) ;
210162
211- str = await res . GetStringAsync ( ) . ConfigureAwait ( false ) ;
163+ if ( res . StatusCode == ( int ) HttpStatusCode . BadGateway ) {
164+ return null ;
212165 }
213166
167+ str = await res . GetStringAsync ( ) . ConfigureAwait ( false ) ;
168+
214169 var document = await parser . ParseDocumentAsync ( str , token ) . ConfigureAwait ( false ) ;
215170
216171 return document ;
@@ -234,12 +189,38 @@ private async Task<IFlurlResponse> GetResponseByUrlAsync(Url origin, Cancellatio
234189 {
235190 var res = await Client . Request ( origin )
236191 . AllowAnyHttpStatus ( )
237- . WithCookies ( out var cj )
192+ . WithCookies ( Jar )
238193 . WithTimeout ( Timeout )
239- . GetAsync ( cancellationToken : token ) . ConfigureAwait ( false ) ;
194+ . GetAsync ( cancellationToken : token )
195+ . ConfigureAwait ( false ) ;
240196 return res ;
241197 }
242198
199+ public async ValueTask < bool > ApplyCookiesAsync ( ICookiesSource source , CancellationToken ct )
200+ {
201+ var cookies = await source . GetOrLoadCookiesAsync ( ct ) . ConfigureAwait ( false ) ;
202+
203+ foreach ( var bck in cookies ) {
204+ var ck = bck . AsCookie ( ) ;
205+
206+ if ( ck . Domain . Contains ( "ascii2d" ) ) {
207+ Jar . AddOrReplace ( new FlurlCookie ( ck . Name , ck . Value , BaseUrl ) ) ;
208+ }
209+ }
210+
211+
212+ return true ;
213+ }
214+
215+ public ValueTask < bool > ApplyConfigAsync ( SearchConfig cfg , CancellationToken ct = default )
216+ {
217+ if ( cfg . FlareSolverr ) {
218+ m_fsClient = new FlareSolverrClient ( cfg . FlareSolverrApiUrl ) ;
219+ }
220+
221+ return ValueTask . FromResult ( m_fsClient is not null ) ;
222+ }
223+
243224}
244225
245226public class Ascii2DItem : SearchResultItem , IParseableSource < INode , Ascii2DItem >
@@ -279,14 +260,14 @@ public static Ascii2DItem ParseSource(INode nx, SearchResult r)
279260 sri . Format = data [ 1 ] ;
280261
281262 string size = data [ 2 ] ;
282- string title1 = ( n as IHtmlElement ) . FirstChild . TryGetAttribute ( "Title" ) ;
263+ string title1 = ( n as IHtmlElement ) ? . FirstChild . TryGetAttribute ( "Title" ) ;
283264
284265 if ( info . Length >= 3 ) {
285266 var node2 = info [ 2 ] ;
286267 var desc = info . Last ( ) . FirstChild ;
287268 var ns = desc . NextSibling ;
288269
289- if ( node2 . ChildNodes . Length >= 2 && node2 . ChildNodes [ 1 ] . ChildNodes . Length >= 2 ) {
270+ if ( node2 . ChildNodes is [ _ , { ChildNodes . Length : >= 2 } , .. ] ) {
290271 var node2Sub = node2 . ChildNodes [ 1 ] ;
291272
292273 if ( node2Sub . ChildNodes . Length >= 8 ) {
0 commit comments