@@ -30,27 +30,14 @@ namespace SmartImage.Lib.Engines.Impl.Search;
3030public sealed class EHentaiEngine : WebSearchEngine , ISearchConfigReceiver , ICookiesReceiver , INotifyPropertyChanged
3131{
3232
33- private const string HOST_EH = ".e-hentai.org" ;
34- private const string HOST_EX = ".exhentai.org" ;
35-
36- private readonly HttpClient m_client ;
37-
3833 // NOTE: a separate HttpClient is used for EHentai because of special network requests and other unique requirements...
3934
40- private readonly HttpClientHandler m_clientHandler = new ( )
41- {
42- AllowAutoRedirect = true ,
43- MaxAutomaticRedirections = 15 ,
44- CheckCertificateRevocationList = false ,
45- UseCookies = true ,
46- CookieContainer = new ( ) { } ,
47-
48- } ;
49-
5035 public override Url BaseUrl => IsLoggedIn ? ExHentaiBase : EHentaiBase ;
5136
5237 private Url LookupUrl => IsLoggedIn ? ExHentaiLookup : EHentaiLookup ;
5338
39+ private Url OriginUrl => UseExHentai ? ExHentaiBase : EHentaiBase ;
40+
5441 public override SearchEngineOptions EngineOption => SearchEngineOptions . EHentai ;
5542
5643 protected override string NodesSelector => Serialization . S_EHentai ;
@@ -59,7 +46,8 @@ public sealed class EHentaiEngine : WebSearchEngine, ISearchConfigReceiver, ICoo
5946
6047 public bool UseExHentai { get ; set ; }
6148
62- private readonly CookieCollection m_cookies ;
49+
50+ public CookieJar Jar { get ; }
6351
6452 #region
6553
@@ -70,16 +58,21 @@ public sealed class EHentaiEngine : WebSearchEngine, ISearchConfigReceiver, ICoo
7058 public static readonly Url ExHentaiBase = "https://exhentai.org/" ;
7159 public static readonly Url ExHentaiLookup = "https://upld.exhentai.org/upld/image_lookup.php" ;
7260
61+ #region
62+
63+ private const string HOST_EH = ".e-hentai.org" ;
64+ private const string HOST_EX = ".exhentai.org" ;
65+
66+ #endregion
67+
7368 #endregion
7469
7570 static EHentaiEngine ( ) { }
7671
7772 public EHentaiEngine ( bool useExHentai = true ) : base ( EHentaiBase )
7873 {
79- m_client = new HttpClient ( m_clientHandler ) ;
8074 IsLoggedIn = false ;
8175
82- m_cookies = new ( ) ;
8376 UseExHentai = useExHentai ;
8477 Jar = new CookieJar ( ) { } ;
8578 }
@@ -111,67 +104,38 @@ public async ValueTask ApplyConfigAsync(SearchConfig cfg)
111104 */
112105
113106
114- public CookieJar Jar { get ; }
115-
116-
117107 public async ValueTask < bool > ApplyCookiesAsync ( ICookiesProvider provider , CancellationToken ct = default )
118108 {
119109 Trace . WriteLine ( $ "Applying cookies to { Name } ") ;
120110
121- /*if (await CookiesManager.Instance.LoadCookiesAsync()) {
122- cookies ??= CookiesManager.Instance.Cookies;
123- }
124- else {
125- return false;
126- }*/
127-
128111 var cookies = await provider . LoadCookiesAsync ( ct ) ;
129112
130- foreach ( var cookie in cookies ) {
131- var x = cookie . AsCookie ( ) ;
132- bool a = false ;
113+ foreach ( var bck in cookies ) {
114+ var cookie = bck . AsCookie ( ) ;
115+ bool c = false ;
133116
134117 if ( UseExHentai ) {
135- bool c = x . Domain . Contains ( HOST_EX ) ;
136- a |= c ;
118+ c |= cookie . Domain . Contains ( HOST_EX ) ;
137119 }
138120
139- var b = x . Domain . Contains ( HOST_EH ) ;
121+ var dmnEh = cookie . Domain . Contains ( HOST_EH ) ;
140122
141- // return b;
142- a |= b ;
123+ c |= dmnEh ;
143124
144- if ( a ) {
145- // m_cookies.Add(new Cookie(x.Name, x.Value, x.Path, x.Domain) { });
146- Jar . AddOrReplace ( new FlurlCookie ( x . Name , x . Value , UseExHentai ? ExHentaiBase : EHentaiBase ) ) ;
125+ if ( c ) {
126+ Jar . AddOrReplace ( new FlurlCookie ( cookie . Name , cookie . Value , OriginUrl ) ) ;
147127 }
148128 }
149129
130+ var response = await GetSessionAsync ( ) ;
150131
151- var res2 = await GetSessionAsync ( ) ;
152-
153- /*var res2 = await EHentaiBase.WithCookies(m_clientHandler.CookieContainer)
154- .WithHeaders(new
155- {
156- User_Agent = HttpUtilities.UserAgent
157- })
158- .WithAutoRedirect(true)
159- .GetAsync();*/
160-
161- /*
162- m_clientHandler.CookieContainer.Add(m_cookies);
163- m_client.Timeout = Timeout;
164- */
165-
166-
167- return IsLoggedIn = res2 . ResponseMessage . IsSuccessStatusCode ;
132+ return IsLoggedIn = response . ResponseMessage . IsSuccessStatusCode ;
168133
169134 }
170135
171136 private Task < IFlurlResponse > GetSessionAsync ( )
172137 {
173138 return ( UseExHentai ? ExHentaiBase : EHentaiBase )
174- // .WithCookies(m_cookies)
175139 . WithCookies ( Jar )
176140 . WithTimeout ( Timeout )
177141 . WithHeaders ( new
@@ -226,37 +190,31 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
226190 { new StringContent ( "dm_l" ) , "inline_set" }
227191 } ;
228192
229- // data.Add(new FileContent(f.FullName), "sfile", "a.jpg");
230-
231- //todo
232- /*m_clientHandler.CookieContainer.Add(m_cookies);
233-
234- m_client.Timeout = Timeout;*/
235193
236194 Debug . WriteLine ( $ "{ LookupUrl } ", nameof ( GetDocumentAsync ) ) ;
237195
238- var req2 = new FlurlRequest ( LookupUrl )
196+ var req = new FlurlRequest ( LookupUrl )
239197 {
240198 CookieJar = Jar ,
241- Verb = HttpMethod . Post ,
242- Content = data ,
199+ Verb = HttpMethod . Post ,
200+ Content = data ,
243201 Headers =
244202 {
245203 { "User-Agent" , HttpUtilities . UserAgent }
246204 }
247205 } ;
248206
249- var res1 = await Client . SendAsync ( req2 , cancellationToken : token ) ;
250- var res = res1 . ResponseMessage ;
207+ var flurlRes = await Client . SendAsync ( req , cancellationToken : token ) ;
208+ var httpRes = flurlRes . ResponseMessage ;
251209
252210 // Debug.WriteLine($"{res.StatusCode}");
253211
254- sr . RawUrl = res . RequestMessage . RequestUri ;
212+ sr . RawUrl = httpRes . RequestMessage . RequestUri ;
255213 var old = sr . Results . Find ( r => r . IsRaw ) ;
256214 old . Url = sr . RawUrl ;
257215
258216 // Debug.WriteLine($"{sr.RawUrl}");
259- var content = await res . Content . ReadAsStringAsync ( token ) ;
217+ var content = await httpRes . Content . ReadAsStringAsync ( token ) ;
260218
261219 // var content2 = await sr.RawUrl.GetStringAsync(cancellationToken: token);
262220
0 commit comments