1010using AngleSharp . Dom ;
1111using AngleSharp . Html . Parser ;
1212using AngleSharp . XPath ;
13+ using CliWrap ;
1314using Flurl . Http ;
1415using Flurl . Http . Content ;
1516using Kantan . Net ;
@@ -26,7 +27,7 @@ namespace SmartImage.Lib.Engines.Impl.Search;
2627/// <see cref="SearchEngineOptions.EHentai" />
2728/// </summary>
2829/// <remarks>Handles both ExHentai and E-Hentai</remarks>
29- public sealed class EHentaiEngine : WebSearchEngine , IConfigurable , ICookieReceiver , INotifyPropertyChanged
30+ public sealed class EHentaiEngine : WebSearchEngine , ISearchConfigReceiver , ICookiesReceiver , INotifyPropertyChanged
3031{
3132
3233 private const string HOST_EH = ".e-hentai.org" ;
@@ -75,10 +76,12 @@ static EHentaiEngine() { }
7576
7677 public EHentaiEngine ( bool useExHentai = true ) : base ( EHentaiBase )
7778 {
78- m_client = new HttpClient ( m_clientHandler ) ;
79- IsLoggedIn = false ;
79+ m_client = new HttpClient ( m_clientHandler ) ;
80+ IsLoggedIn = false ;
81+
8082 m_cookies = new ( ) ;
8183 UseExHentai = useExHentai ;
84+ Jar = new CookieJar ( ) { } ;
8285 }
8386
8487 /*
@@ -91,10 +94,10 @@ public EHentaiEngine(bool useExHentai = true) : base(EHentaiBase)
9194 * https://gitlab.com/NekoInverter/EhViewer/-/blob/master/app/src/main/java/com/hippo/ehviewer/client/EhCookieStore.java
9295 */
9396
94- public async ValueTask ApplyAsync ( SearchConfig cfg )
97+ public async ValueTask ApplyConfigAsync ( SearchConfig cfg )
9598 {
9699 /*if (this is { IsLoggedIn: true }/* && !(Username != cfg.EhUsername && Password != cfg.EhPassword)#1#) {
97- Debug.WriteLine($"{Name} is already logged in", nameof(ApplyAsync ));
100+ Debug.WriteLine($"{Name} is already logged in", nameof(ApplyConfigAsync ));
98101
99102 return;
100103 }*/
@@ -108,7 +111,10 @@ public async ValueTask ApplyAsync(SearchConfig cfg)
108111 */
109112
110113
111- public async ValueTask < bool > ApplyCookiesAsync ( ICookieProvider provider , CancellationToken ct = default )
114+ public CookieJar Jar { get ; }
115+
116+
117+ public async ValueTask < bool > ApplyCookiesAsync ( ICookiesProvider provider , CancellationToken ct = default )
112118 {
113119 Trace . WriteLine ( $ "Applying cookies to { Name } ") ;
114120
@@ -119,11 +125,10 @@ public async ValueTask<bool> ApplyCookiesAsync(ICookieProvider provider, Cancell
119125 return false;
120126 }*/
121127
122- var ok = await provider . LoadCookiesAsync ( Browser . Firefox , ct ) ;
123- var cookies = provider . Jar ;
128+ var cookies = await provider . LoadCookiesAsync ( ct ) ;
124129
125- var fcc = cookies . Where ( x =>
126- {
130+ foreach ( var cookie in cookies ) {
131+ var x = cookie . AsCookie ( ) ;
127132 bool a = false ;
128133
129134 if ( UseExHentai ) {
@@ -136,14 +141,13 @@ public async ValueTask<bool> ApplyCookiesAsync(ICookieProvider provider, Cancell
136141 // return b;
137142 a |= b ;
138143
139- return a ;
140- } ) ;
141-
142- foreach ( var cookie in fcc ) {
143- m_cookies . Add (
144- new Cookie ( cookie . Name , cookie . Value , cookie . Path , cookie . Domain ) { } ) ;
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 ) ) ;
147+ }
145148 }
146149
150+
147151 var res2 = await GetSessionAsync ( ) ;
148152
149153 /*var res2 = await EHentaiBase.WithCookies(m_clientHandler.CookieContainer)
@@ -154,8 +158,11 @@ public async ValueTask<bool> ApplyCookiesAsync(ICookieProvider provider, Cancell
154158 .WithAutoRedirect(true)
155159 .GetAsync();*/
156160
161+ /*
157162 m_clientHandler.CookieContainer.Add(m_cookies);
158163 m_client.Timeout = Timeout;
164+ */
165+
159166
160167 return IsLoggedIn = res2 . ResponseMessage . IsSuccessStatusCode ;
161168
@@ -164,7 +171,8 @@ public async ValueTask<bool> ApplyCookiesAsync(ICookieProvider provider, Cancell
164171 private Task < IFlurlResponse > GetSessionAsync ( )
165172 {
166173 return ( UseExHentai ? ExHentaiBase : EHentaiBase )
167- . WithCookies ( m_cookies )
174+ // .WithCookies(m_cookies)
175+ . WithCookies ( Jar )
168176 . WithTimeout ( Timeout )
169177 . WithHeaders ( new
170178 {
@@ -227,17 +235,19 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
227235
228236 Debug . WriteLine ( $ "{ LookupUrl } ", nameof ( GetDocumentAsync ) ) ;
229237
230- var req = new HttpRequestMessage ( HttpMethod . Post , LookupUrl )
238+ var req2 = new FlurlRequest ( LookupUrl )
231239 {
240+ CookieJar = Jar ,
241+ Verb = HttpMethod . Post ,
232242 Content = data ,
233243 Headers =
234244 {
235245 { "User-Agent" , HttpUtilities . UserAgent }
236- } ,
237-
246+ }
238247 } ;
239248
240- var res = await m_client . SendAsync ( req , token ) ;
249+ var res1 = await Client . SendAsync ( req2 , cancellationToken : token ) ;
250+ var res = res1 . ResponseMessage ;
241251
242252 // Debug.WriteLine($"{res.StatusCode}");
243253
@@ -300,9 +310,9 @@ protected override ValueTask<SearchResultItem> ParseResultItem(INode n, SearchRe
300310
301311 public override void Dispose ( )
302312 {
303- m_client . Dispose ( ) ;
304- m_clientHandler . Dispose ( ) ;
305- m_cookies . Clear ( ) ;
313+ // m_client.Dispose();
314+ // m_clientHandler.Dispose();
315+ Jar . Clear ( ) ;
306316
307317 IsLoggedIn = false ;
308318 }
0 commit comments