Skip to content

Commit 61c9e43

Browse files
committed
Hyper refactor
1 parent 72dc3f9 commit 61c9e43

34 files changed

+437
-291
lines changed

SmartImage.Lib/Cookies/BrowserCookiesProvider.cs renamed to SmartImage.Lib/Cookies/BrowserCookiesSource.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Author: Deci | Project: SmartImage.Lib | Name: CookiesManager.cs
22

3+
using System.Collections;
34
using System.Collections.Frozen;
45
using System.Data;
56
using System.Diagnostics;
@@ -11,7 +12,7 @@
1112

1213
namespace SmartImage.Lib.Cookies;
1314

14-
public class BrowserCookiesProvider : ICookiesProvider
15+
public class BrowserCookiesSource : ICookiesSource
1516
{
1617

1718
private const string CH_NAME = "cookies";
@@ -21,21 +22,21 @@ public class BrowserCookiesProvider : ICookiesProvider
2122
private IList<ICookie> m_cookies;
2223

2324
[ICBN]
24-
public static readonly Lazy<ICookiesProvider> Default = new(() =>
25+
public static readonly Lazy<ICookiesSource> Default = new(() =>
2526
{
2627
if (BaseOSIntegration.Integration.IsFirefoxInstalled) {
2728
var cookieFile = FirefoxCookiesDatabaseReader.FindCookieFile();
2829

2930
if (cookieFile != null) {
30-
return new BrowserCookiesProvider(new FirefoxCookiesDatabaseReader(cookieFile.FullName));
31+
return new BrowserCookiesSource(new FirefoxCookiesDatabaseReader(cookieFile.FullName));
3132
}
3233

3334
}
3435

3536
return null;
3637
});
3738

38-
internal BrowserCookiesProvider(BaseCookiesDatabaseReader reader)
39+
internal BrowserCookiesSource(BaseCookiesDatabaseReader reader)
3940
{
4041
m_reader = reader;
4142
m_cookies = null;
@@ -54,16 +55,17 @@ public async ValueTask CloseAsync()
5455

5556
public async ValueTask<IList<ICookie>> GetOrLoadCookiesAsync(CancellationToken ct = default)
5657
{
57-
if (!IsOpen) {
58-
await OpenAsync();
59-
}
58+
6059

6160
/*if (IsClosedOrBroken) {
6261
throw new InvalidOperationException();
6362
}*/
6463

6564

6665
if (m_cookies == null) {
66+
if (!IsOpen) {
67+
await OpenAsync();
68+
}
6769

6870
var cookies = await m_reader.ReadCookiesAsync();
6971
m_cookies = cookies.AsReadOnly();
@@ -82,7 +84,7 @@ public async ValueTask<IList<ICookie>> GetOrLoadCookiesAsync(CancellationToken c
8284

8385
public void Dispose()
8486
{
85-
Debug.WriteLine($"Disposing {nameof(BrowserCookiesProvider)}");
87+
Debug.WriteLine($"Disposing {nameof(BrowserCookiesSource)}");
8688
m_reader.Dispose();
8789
m_cookies.Clear();
8890
}

SmartImage.Lib/Cookies/ICookiesReceiver.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ namespace SmartImage.Lib.Cookies;
88

99
public interface ICookiesReceiver
1010
{
11+
1112
public CookieJar Jar { get; }
1213

13-
public ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, CancellationToken token = default);
14+
// public ICookiesSource CookiesSource { get; }
15+
16+
public ValueTask<bool> ApplyCookiesAsync(ICookiesSource source, CancellationToken token = default);
17+
1418
}

SmartImage.Lib/Cookies/ICookiesProvider.cs renamed to SmartImage.Lib/Cookies/ICookiesSource.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,9 @@
66

77
namespace SmartImage.Lib.Cookies;
88

9-
public interface ICookiesProvider : IDisposable
9+
public interface ICookiesSource : IDisposable
1010
{
1111

1212
public ValueTask<IList<ICookie>> GetOrLoadCookiesAsync(CancellationToken ct = default);
1313

14-
public static ICookiesProvider GetProvider()
15-
{
16-
ICookiesProvider cp = BrowserCookiesProvider.Default.Value
17-
?? new ListCookiesProvider();
18-
19-
return cp;
20-
}
21-
2214
}

SmartImage.Lib/Cookies/ListCookiesProvider.cs renamed to SmartImage.Lib/Cookies/ListCookiesSource.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66

77
namespace SmartImage.Lib.Cookies;
88

9-
public class ListCookiesProvider : ICookiesProvider, IEnumerable<ICookie>
9+
public class ListCookiesSource : ICookiesSource, IEnumerable<ICookie>
1010
{
1111

12-
private IList<ICookie> m_cookies;
12+
private readonly IList<ICookie> m_cookies;
1313

14-
public ListCookiesProvider()
14+
public ListCookiesSource()
1515
{
1616
m_cookies = new List<ICookie>();
1717
}
1818

19+
public static ICookiesSource Default { get; } = new ListCookiesSource();
20+
1921
public ValueTask<IList<ICookie>> GetOrLoadCookiesAsync(CancellationToken ct = default)
2022
{
2123
return ValueTask.FromResult(m_cookies);

SmartImage.Lib/Engines/BaseSearchEngine.cs

Lines changed: 63 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
global using R1 = SmartImage.Lib.Resources;
55
global using Url = Flurl.Url;
6+
using System.Collections.Frozen;
67
using System.Diagnostics;
78
using System.Net.Http.Headers;
89
using System.Runtime.CompilerServices;
@@ -12,6 +13,7 @@
1213
using Microsoft.Extensions.Logging;
1314
using Microsoft.Net.Http.Headers;
1415
using SmartImage.Lib.Clients;
16+
using SmartImage.Lib.Cookies;
1517
using SmartImage.Lib.Engines.Impl.Search;
1618
using SmartImage.Lib.Engines.Impl.Search.Other;
1719
using SmartImage.Lib.Results;
@@ -24,14 +26,7 @@ namespace SmartImage.Lib.Engines;
2426

2527
#nullable enable
2628

27-
public interface ISearchQueryVerifiable
28-
{
29-
30-
public ValueTask<bool> VerifyQueryAsync(SearchQuery query);
31-
32-
}
33-
34-
public abstract class BaseSearchEngine : IDisposable, IEquatable<BaseSearchEngine>
29+
public abstract class BaseSearchEngine : IDisposable, IEquatable<BaseSearchEngine>,ISearchConfigReceiver
3530
{
3631

3732
static BaseSearchEngine()
@@ -90,62 +85,6 @@ protected BaseSearchEngine([NN] Url baseUrl)
9085

9186
protected static FlurlClient Client { get; }
9287

93-
public static IEnumerable<BaseSearchEngine> GetSelectedEngines(SearchEngineOptions options)
94-
{
95-
/*return BaseSearchEngine.All.Where(e =>
96-
{
97-
return e.EngineOption != default && options.HasFlag(e.EngineOption);
98-
})
99-
.ToArray();*/
100-
101-
if (options.HasFlag(SearchEngineOptions.SauceNao))
102-
yield return new SauceNaoEngine();
103-
104-
if (options.HasFlag(SearchEngineOptions.ImgOps))
105-
yield return new ImgOpsEngine();
106-
107-
if (options.HasFlag(SearchEngineOptions.GoogleImages))
108-
yield return new GoogleImagesEngine();
109-
110-
if (options.HasFlag(SearchEngineOptions.TinEye))
111-
yield return new TinEyeEngine();
112-
113-
if (options.HasFlag(SearchEngineOptions.Iqdb))
114-
yield return new IqdbEngine();
115-
116-
if (options.HasFlag(SearchEngineOptions.TraceMoe))
117-
yield return new TraceMoeEngine();
118-
119-
if (options.HasFlag(SearchEngineOptions.KarmaDecay))
120-
yield return new KarmaDecayEngine();
121-
122-
if (options.HasFlag(SearchEngineOptions.Yandex))
123-
yield return new YandexEngine();
124-
125-
if (options.HasFlag(SearchEngineOptions.Bing))
126-
yield return new BingEngine();
127-
128-
if (options.HasFlag(SearchEngineOptions.Ascii2D))
129-
yield return new Ascii2DEngine();
130-
131-
if (options.HasFlag(SearchEngineOptions.RepostSleuth))
132-
yield return new RepostSleuthEngine();
133-
134-
if (options.HasFlag(SearchEngineOptions.EHentai))
135-
yield return new EHentaiEngine();
136-
137-
if (options.HasFlag(SearchEngineOptions.ArchiveMoe))
138-
yield return new ArchiveMoeEngine();
139-
140-
if (options.HasFlag(SearchEngineOptions.Iqdb3D))
141-
yield return new Iqdb3DEngine();
142-
143-
if (options.HasFlag(SearchEngineOptions.Fluffle))
144-
yield return new FluffleEngine();
145-
146-
if (options.HasFlag(SearchEngineOptions.GoogleLens))
147-
yield return new GoogleLensEngine();
148-
}
14988

15089
/*public Task<SearchResult> GetTaskAsync(SearchQuery query, CancellationToken token = default)
15190
{
@@ -209,6 +148,8 @@ public override string ToString()
209148
return $"{Name}: {BaseUrl} {Timeout}";
210149
}
211150

151+
public abstract ValueTask<bool> ApplyConfigAsync(SearchConfig cfg, CancellationToken ct = default);
152+
212153
// public abstract ValueTask ApplyConfigAsync(SearchConfig cfg, CancellationToken ct = default);
213154

214155
public override bool Equals(object? obj)
@@ -254,4 +195,62 @@ public bool Equals(BaseSearchEngine? other)
254195
public static bool operator !=(BaseSearchEngine? left, BaseSearchEngine? right)
255196
=> !Equals(left, right);
256197

198+
199+
public static IEnumerable<BaseSearchEngine> GetSelectedEngines(SearchEngineOptions options)
200+
{
201+
/*return BaseSearchEngine.All.Where(e =>
202+
{
203+
return e.EngineOption != default && options.HasFlag(e.EngineOption);
204+
})
205+
.ToArray();*/
206+
207+
if (options.HasFlag(SearchEngineOptions.SauceNao))
208+
yield return new SauceNaoEngine();
209+
210+
if (options.HasFlag(SearchEngineOptions.ImgOps))
211+
yield return new ImgOpsEngine();
212+
213+
if (options.HasFlag(SearchEngineOptions.GoogleImages))
214+
yield return new GoogleImagesEngine();
215+
216+
if (options.HasFlag(SearchEngineOptions.TinEye))
217+
yield return new TinEyeEngine();
218+
219+
if (options.HasFlag(SearchEngineOptions.Iqdb))
220+
yield return new IqdbEngine();
221+
222+
if (options.HasFlag(SearchEngineOptions.TraceMoe))
223+
yield return new TraceMoeEngine();
224+
225+
if (options.HasFlag(SearchEngineOptions.KarmaDecay))
226+
yield return new KarmaDecayEngine();
227+
228+
if (options.HasFlag(SearchEngineOptions.Yandex))
229+
yield return new YandexEngine();
230+
231+
if (options.HasFlag(SearchEngineOptions.Bing))
232+
yield return new BingEngine();
233+
234+
if (options.HasFlag(SearchEngineOptions.Ascii2D))
235+
yield return new Ascii2DEngine();
236+
237+
if (options.HasFlag(SearchEngineOptions.RepostSleuth))
238+
yield return new RepostSleuthEngine();
239+
240+
if (options.HasFlag(SearchEngineOptions.EHentai))
241+
yield return new EHentaiEngine();
242+
243+
if (options.HasFlag(SearchEngineOptions.ArchiveMoe))
244+
yield return new ArchiveMoeEngine();
245+
246+
if (options.HasFlag(SearchEngineOptions.Iqdb3D))
247+
yield return new Iqdb3DEngine();
248+
249+
if (options.HasFlag(SearchEngineOptions.Fluffle))
250+
yield return new FluffleEngine();
251+
252+
if (options.HasFlag(SearchEngineOptions.GoogleLens))
253+
yield return new GoogleLensEngine();
254+
}
255+
257256
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ protected static string GetHash(SearchQuery q)
5959
return b64;
6060
}
6161

62+
public override ValueTask<bool> ApplyConfigAsync(SearchConfig cfg, CancellationToken ct = default)
63+
{
64+
return ValueTask.FromResult(true);
65+
66+
}
67+
6268
public override void Dispose()
6369
{
6470
GC.SuppressFinalize(this);

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace SmartImage.Lib.Engines.Impl.Search;
2525

2626
// todo
2727

28-
public sealed class Ascii2DEngine : WebSearchEngine, ICookiesReceiver, ISearchConfigReceiver
28+
public sealed class Ascii2DEngine : WebSearchEngine, ICookiesReceiver
2929
{
3030

3131
protected override string NodesSelector => Serialization.S_Ascii2D_Images2;
@@ -52,13 +52,13 @@ public Ascii2DEngine() : base(MAIN_URL)
5252
Jar = new CookieJar();
5353
}
5454

55-
public async ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, CancellationToken ct)
55+
public async ValueTask<bool> ApplyCookiesAsync(ICookiesSource source, CancellationToken ct)
5656
{
57-
if ( /*FlareSolverrClient.Value.IsInitialized*/ provider == null) {
57+
if ( /*FlareSolverrClient.Value.IsInitialized*/ source == null) {
5858
return false;
5959
}
6060

61-
var cookies = await provider.GetOrLoadCookiesAsync(ct);
61+
var cookies = await source.GetOrLoadCookiesAsync(ct);
6262

6363
foreach (var bck in cookies) {
6464
var ck = bck.AsCookie();
@@ -72,7 +72,7 @@ public async ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, Cancel
7272
return true;
7373
}
7474

75-
public ValueTask<bool> ApplyConfigAsync(SearchConfig cfg, CancellationToken ct = default)
75+
public override ValueTask<bool> ApplyConfigAsync(SearchConfig cfg, CancellationToken ct = default)
7676
{
7777
return ValueTask.FromResult(true);
7878
}
@@ -308,4 +308,5 @@ protected override ValueTask<SearchResultItem> ParseResultItem(INode nx, SearchR
308308
return ValueTask.FromResult(sri);
309309
}
310310

311+
311312
}

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

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

3131
static EHentaiEngine() { }
@@ -197,9 +197,9 @@ protected override Url GetRawUrl(SearchQuery query)
197197

198198
#endregion
199199

200-
public async ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, CancellationToken ct = default)
200+
public async ValueTask<bool> ApplyCookiesAsync(ICookiesSource source, CancellationToken ct = default)
201201
{
202-
if (provider == null) {
202+
if (source == null) {
203203
return false;
204204
}
205205

@@ -212,7 +212,7 @@ public async ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, Cancel
212212
Trace.WriteLine($"Applying cookies to {Name}");
213213
}
214214

215-
var cookies = await provider.GetOrLoadCookiesAsync(ct);
215+
var cookies = await source.GetOrLoadCookiesAsync(ct);
216216

217217
foreach (var bck in cookies) {
218218

@@ -305,7 +305,7 @@ public async Task<bool> LoginAsync(string username, string password)
305305
* https://github.com/Ehviewer-Overhauled/Ehviewer/issues/873
306306
*/
307307

308-
public ValueTask<bool> ApplyConfigAsync(SearchConfig cfg, CancellationToken ct = default)
308+
public override ValueTask<bool> ApplyConfigAsync(SearchConfig cfg, CancellationToken ct = default)
309309
{
310310
/*if (this is { IsLoggedIn: true }/* && !(Username != cfg.EhUsername && Password != cfg.EhPassword)#1#) {
311311
Debug.WriteLine($"{Name} is already logged in", nameof(ApplyConfigAsync));

0 commit comments

Comments
 (0)