Skip to content

Commit 63cd5f4

Browse files
committed
Type reworking; ...
1 parent bcfae70 commit 63cd5f4

15 files changed

+114
-93
lines changed

SmartImage.Lib/Clients/FlareSolverrClient.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public bool Configure(string api)
3737
Client = new HttpClient(Clearance);
3838

3939
Trace.WriteLine($"{nameof(FlareSolverrClient)}: init {api}");
40+
4041
return HasClient;
4142
}
4243

@@ -57,12 +58,15 @@ public void Dispose()
5758

5859
#region Implementation of ISearchConfigReceiver
5960

60-
public ValueTask ApplyConfigAsync(SearchConfig cfg)
61+
public ValueTask<bool> ApplyConfigAsync(SearchConfig cfg, CancellationToken ct = default)
6162
{
63+
var ok = false;
64+
6265
if (cfg.FlareSolverr) {
63-
Configure(cfg.FlareSolverrApiUrl);
66+
ok = Configure(cfg.FlareSolverrApiUrl);
6467
}
65-
return ValueTask.CompletedTask;
68+
69+
return ValueTask.FromResult(ok);
6670
}
6771

6872
#endregion

SmartImage.Lib/Engines/BaseSearchEngine.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using SmartImage.Lib.Engines.Impl.Search;
1212
using SmartImage.Lib.Engines.Impl.Search.Other;
1313
using SmartImage.Lib.Results;
14+
using SmartImage.Lib.Results.Data;
1415
using SmartImage.Lib.Utilities.Diagnostics;
1516

1617
namespace SmartImage.Lib.Engines;
@@ -189,6 +190,8 @@ public override string ToString()
189190
return $"{Name}: {BaseUrl} {Timeout}";
190191
}
191192

193+
// public abstract ValueTask ApplyConfigAsync(SearchConfig cfg, CancellationToken ct = default);
194+
192195
public override bool Equals(object? obj)
193196
{
194197
if (obj is null) {

SmartImage.Lib/Engines/Impl/Search/Ascii2DEngine.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace SmartImage.Lib.Engines.Impl.Search;
2121

2222
// todo
2323

24-
public sealed class Ascii2DEngine : WebSearchEngine, ICookiesReceiver
24+
public sealed class Ascii2DEngine : WebSearchEngine, ICookiesEngine, ISearchConfigReceiver
2525
{
2626

2727
protected override string NodesSelector => Serialization.S_Ascii2D_Images2;
@@ -30,6 +30,8 @@ public sealed class Ascii2DEngine : WebSearchEngine, ICookiesReceiver
3030

3131
public CookieJar Jar { get; }
3232

33+
public ICookiesProvider Provider { get; set; }
34+
3335
protected override string[] ErrorBodyMessages
3436
=>
3537
[
@@ -44,13 +46,13 @@ public Ascii2DEngine() : base("https://ascii2d.net/search/url/")
4446
Jar = new CookieJar();
4547
}
4648

47-
public async ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, CancellationToken ct)
49+
public async ValueTask<bool> ApplyCookiesAsync(CancellationToken ct)
4850
{
49-
if (FlareSolverrClient.Value.IsInitialized) {
51+
if ( /*FlareSolverrClient.Value.IsInitialized*/ Provider == null) {
5052
return false;
5153
}
5254

53-
var cookies = await provider.LoadCookiesAsync(ct);
55+
var cookies = await Provider.GetOrLoadCookiesAsync(ct);
5456

5557
foreach (var bck in cookies) {
5658
var ck = bck.AsCookie();
@@ -64,6 +66,11 @@ public async ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, Cancel
6466
return true;
6567
}
6668

69+
public ValueTask<bool> ApplyConfigAsync(SearchConfig cfg, CancellationToken ct = default)
70+
{
71+
return ApplyCookiesAsync(ct);
72+
}
73+
6774
public override void Dispose() { }
6875

6976
// public const int MAX_WIDTH = 1000;
@@ -148,17 +155,17 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
148155
}
149156

150157
using var res = await Client.Request(newUrl)
151-
.WithSettings(x => { x.HttpVersion = "2.0"; })
152-
.AllowAnyHttpStatus()
153-
.WithCookies(Jar)
154-
.WithTimeout(Timeout)
155-
/*.OnError(s =>
156-
{
157-
Debug.WriteLine($"{s.Response}");
158-
s.ExceptionHandled = true;
159-
160-
})*/
161-
.GetAsync(cancellationToken: token);
158+
.WithSettings(x => { x.HttpVersion = "2.0"; })
159+
.AllowAnyHttpStatus()
160+
.WithCookies(Jar)
161+
.WithTimeout(Timeout)
162+
/*.OnError(s =>
163+
{
164+
Debug.WriteLine($"{s.Response}");
165+
s.ExceptionHandled = true;
166+
167+
})*/
168+
.GetAsync(cancellationToken: token);
162169

163170

164171
// var res1 = await FlareSolverrClient.Client.SendAsync(msg, token);

SmartImage.Lib/Engines/Impl/Search/EHentaiEngine.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace SmartImage.Lib.Engines.Impl.Search;
2222
/// <see cref="SearchEngineOptions.EHentai" />
2323
/// </summary>
2424
/// <remarks>Handles both ExHentai and E-Hentai</remarks>
25-
public sealed class EHentaiEngine : WebSearchEngine, ISearchConfigReceiver, ICookiesReceiver, INotifyPropertyChanged
25+
public sealed class EHentaiEngine : WebSearchEngine, INotifyPropertyChanged, ICookiesEngine, ISearchConfigReceiver
2626
{
2727

2828
static EHentaiEngine() { }
@@ -167,11 +167,23 @@ protected override ValueTask<INode[]> GetNodes(IDocument d)
167167
*/
168168

169169

170-
public async ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, CancellationToken ct = default)
170+
public async ValueTask<bool> ApplyCookiesAsync(CancellationToken ct = default)
171171
{
172-
Trace.WriteLine($"Applying cookies to {Name}");
172+
if (Provider == null) {
173+
return false;
174+
}
175+
176+
if (IsLoggedIn) {
177+
Trace.WriteLine($"Not applying cookies to {Name}; already logged in");
178+
return IsLoggedIn;
179+
180+
}
181+
else {
182+
Trace.WriteLine($"Applying cookies to {Name}");
183+
}
184+
173185

174-
var cookies = await provider.LoadCookiesAsync(ct);
186+
var cookies = await Provider.GetOrLoadCookiesAsync(ct);
175187

176188
foreach (var bck in cookies) {
177189
var cookie = bck.AsCookie();
@@ -193,7 +205,6 @@ public async ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, Cancel
193205
var response = await GetSessionAsync();
194206

195207
return IsLoggedIn = response.ResponseMessage.IsSuccessStatusCode;
196-
197208
}
198209

199210
/*
@@ -206,7 +217,7 @@ public async ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, Cancel
206217
* https://gitlab.com/NekoInverter/EhViewer/-/blob/master/app/src/main/java/com/hippo/ehviewer/client/EhCookieStore.java
207218
*/
208219

209-
public ValueTask ApplyConfigAsync(SearchConfig cfg)
220+
public ValueTask<bool> ApplyConfigAsync(SearchConfig cfg, CancellationToken ct = default)
210221
{
211222
/*if (this is { IsLoggedIn: true }/* && !(Username != cfg.EhUsername && Password != cfg.EhPassword)#1#) {
212223
Debug.WriteLine($"{Name} is already logged in", nameof(ApplyConfigAsync));
@@ -215,10 +226,13 @@ public ValueTask ApplyConfigAsync(SearchConfig cfg)
215226
}*/
216227
//
217228

229+
Provider = cfg.CookiesProvider;
218230

219-
return ValueTask.CompletedTask;
231+
return ValueTask.FromResult(true);
220232
}
221233

234+
public ICookiesProvider Provider { get; set; }
235+
222236
#region
223237

224238
public static readonly Url EHentaiIndex = "https://forums.e-hentai.org/index.php";

SmartImage.Lib/Engines/Impl/Search/IqdbEngine.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Kantan.Text;
1515
using Microsoft.Extensions.Logging;
1616
using SmartImage.Lib.Results;
17+
using SmartImage.Lib.Results.Data;
1718
using SmartImage.Lib.Utilities;
1819

1920
// ReSharper disable StringLiteralTypo
@@ -35,7 +36,7 @@ protected IqdbEngine(string b, string e) : base(b, e)
3536
{
3637
MaxSize = MAX_FILE_SIZE; // NOTE: assuming IQDB uses kilobytes instead of kibibytes
3738

38-
Timeout = TimeSpan.FromSeconds(30);
39+
Timeout = TimeSpan.FromSeconds(15);
3940
}
4041

4142
private const int MAX_FILE_SIZE = 8_388_608;
@@ -55,27 +56,39 @@ private async Task<IDocument> GetDocumentAsync(SearchQuery query, CancellationTo
5556
IDocument document = null;
5657

5758
try {
58-
/*var response = await Client.Request(EndpointUrl)
59+
var response = await Client.Request(EndpointUrl)
5960
.OnError(r =>
6061
{
61-
Debug.WriteLine($"{r.Exception}", Name);
62+
Logger.LogError(r.Exception, Name);
63+
64+
65+
// Debugger.Break();
6266
r.ExceptionHandled = true;
67+
68+
// var sz = await r.Response.GetStringAsync();
6369
}
6470
)
6571
.WithTimeout(Timeout)
6672
.PostMultipartAsync(m =>
6773
{
6874
m.AddString("MAX_FILE_SIZE", MAX_FILE_SIZE.ToString());
69-
m.AddString("url", query.Source.IsUri ? query.Source.ValueString : String.Empty);
7075

71-
if (query.Source.IsUri) { }
76+
/*if (query.Source.IsUri) {
77+
m.AddString("url", query.Source.ValueString);
78+
}
7279
else if (query.Source.IsFile) {
7380
m.AddFile("file", query.Source.Value.ToString(), fileName: "image.jpg");
7481
}
82+
else {
83+
84+
}*/
85+
86+
m.AddString("url", query.Upload);
7587

7688
return;
77-
}, cancellationToken: ct);*/
89+
}, cancellationToken: ct);
7890

91+
/*
7992
var response = await Client.Request(URL_QUERY)
8093
.OnError(r =>
8194
{
@@ -91,6 +104,7 @@ private async Task<IDocument> GetDocumentAsync(SearchQuery query, CancellationTo
91104
.SetQueryParam("url", query.Upload)
92105
.WithTimeout(Timeout)
93106
.GetAsync(cancellationToken: ct);
107+
*/
94108

95109
if (response != null) {
96110
var s = await response.GetStringAsync();

SmartImage.Lib/Engines/Impl/Search/SauceNaoEngine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
namespace SmartImage.Lib.Engines.Impl.Search;
3333

34-
public sealed class SauceNaoEngine : BaseSearchEngine, ISearchConfigReceiver, IDisposable
34+
public sealed class SauceNaoEngine : BaseSearchEngine, IDisposable, ISearchConfigReceiver
3535
{
3636

3737
private const string URL_BASE = "https://saucenao.com/";
@@ -286,10 +286,10 @@ private async ValueTask GetAPIResultsAsync(SearchQuery url, SearchResult sr)
286286
return;
287287
}
288288

289-
public ValueTask ApplyConfigAsync(SearchConfig cfg)
289+
public ValueTask<bool> ApplyConfigAsync(SearchConfig cfg, CancellationToken ct = default)
290290
{
291291
Authentication = cfg.SauceNaoKey;
292-
return ValueTask.CompletedTask;
292+
return ValueTask.FromResult(UsingAPI);
293293
}
294294

295295
private static void Parse(INode result, SearchResult sr)

SmartImage.Lib/Engines/WebSearchEngine.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,6 @@ protected bool Validate([CBN] IDocument doc, SearchResult sr)
127127

128128
}
129129

130+
131+
130132
}

SmartImage.Lib/Results/Data/DefaultCookiesProvider.cs renamed to SmartImage.Lib/Results/Data/BrowserCookiesProvider.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace SmartImage.Lib.Results.Data;
1414
using System.Runtime.Caching;
1515
using Results.Data;
1616

17-
public class DefaultCookiesProvider : ICookiesProvider
17+
public class BrowserCookiesProvider : ICookiesProvider
1818
{
1919

2020
private const string CH_NAME = "cookies";
@@ -23,16 +23,10 @@ public class DefaultCookiesProvider : ICookiesProvider
2323

2424
public MemoryCache Cache { get; }
2525

26-
public DefaultCookiesProvider()
27-
: this(new FirefoxCookieReader())
28-
{
29-
// todo
30-
}
31-
32-
public DefaultCookiesProvider(BaseCookieReader reader)
26+
public BrowserCookiesProvider(BaseCookieReader reader)
3327
{
3428
Reader = reader;
35-
Cache = new MemoryCache($"{nameof(DefaultCookiesProvider)}_Cache");
29+
Cache = new MemoryCache($"{nameof(BrowserCookiesProvider)}_Cache");
3630
}
3731

3832
public async ValueTask OpenAsync()
@@ -46,7 +40,7 @@ public async ValueTask CloseAsync()
4640
await Reader.Connection.CloseAsync();
4741
}
4842

49-
public async ValueTask<IList<IBrowserCookie>> LoadCookiesAsync(CancellationToken ct = default)
43+
public async ValueTask<IList<IBrowserCookie>> GetOrLoadCookiesAsync(CancellationToken ct = default)
5044
{
5145
if (!IsOpen) {
5246
await OpenAsync();
@@ -88,11 +82,9 @@ public async ValueTask<IList<IBrowserCookie>> LoadCookiesAsync(CancellationToken
8882

8983
public bool IsClosedOrBroken => Reader.Connection.State is ConnectionState.Broken or ConnectionState.Closed;
9084

91-
public static ICookiesProvider Instance {get;} = new DefaultCookiesProvider();
92-
9385
public void Dispose()
9486
{
95-
Debug.WriteLine($"Disposing {nameof(DefaultCookiesProvider)}");
87+
Debug.WriteLine($"Disposing {nameof(BrowserCookiesProvider)}");
9688
Reader.Dispose();
9789
Cache.Dispose();
9890
}

SmartImage.Lib/Results/Data/ICookiesReceiver.cs renamed to SmartImage.Lib/Results/Data/ICookiesEngine.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@
77

88
namespace SmartImage.Lib.Results.Data;
99

10-
public interface ICookiesReceiver
10+
public interface ICookiesEngine
1111
{
12-
1312
public CookieJar Jar { get; }
1413

15-
[MNNW(true, nameof(Jar))]
16-
public bool Loaded => Jar != null && Jar.Count != 0;
17-
18-
public ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, CancellationToken token = default);
19-
14+
public ICookiesProvider Provider { get; set; }
2015

16+
public ValueTask<bool> ApplyCookiesAsync(CancellationToken token = default);
2117
}

SmartImage.Lib/Results/Data/ICookiesProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace SmartImage.Lib.Results.Data;
1010
public interface ICookiesProvider : IDisposable
1111
{
1212

13-
public ValueTask<IList<IBrowserCookie>> LoadCookiesAsync(CancellationToken ct = default);
13+
public ValueTask<IList<IBrowserCookie>> GetOrLoadCookiesAsync(CancellationToken ct = default);
14+
15+
public static ICookiesProvider Default { get; set; }
1416

1517
}

0 commit comments

Comments
 (0)