Skip to content

Commit 4a1aadc

Browse files
committed
*
1 parent 3b0ae02 commit 4a1aadc

File tree

4 files changed

+52
-91
lines changed

4 files changed

+52
-91
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public async ValueTask ApplyConfigAsync(SearchConfig cfg)
104104
*/
105105

106106

107-
public async ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, CancellationToken ct)
107+
public async ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, CancellationToken ct = default)
108108
{
109109
Trace.WriteLine($"Applying cookies to {Name}");
110110

SmartImage.Lib/Images/ImageScanner.cs

Lines changed: 37 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ static ImageScanner()
6161
});
6262

6363
Cookies = new CookieJar();
64-
65-
6664
}
6765

6866

@@ -72,62 +70,9 @@ static ImageScanner()
7270

7371

7472
/*
75-
* TODO: ImageScanner, AutoCookiesProvider, and FlareSolverrr
73+
* TODO: AutoCookiesProvider, and FlareSolverr
7674
*/
7775

78-
/*public static IFlurlRequest BuildRequest(params object[] urlSeg)
79-
{
80-
var request = Client.Request(urlSeg);
81-
82-
if (r_donmai.IsMatch(request.Url.Host)) {
83-
request.Headers.AddOrReplace("User-Agent", R1.Name);
84-
}
85-
86-
return request
87-
.WithCookies(Cookies);
88-
}*/
89-
90-
private static bool r_donmaiInit;
91-
92-
public static async ValueTask<IFlurlRequest> BuildRequest(Url u, CancellationToken ct = default)
93-
{
94-
var req = Client.Request(u);
95-
96-
if (r_donmai.IsMatch(req.Url.Host)) {
97-
if (!r_donmaiInit) {
98-
var req2 = Client.Request(req.Url);
99-
100-
req2.Headers.AddOrReplace("User-Agent", R1.Name);
101-
102-
using (var res2 = await req2.WithCookies(Cookies).GetAsync(cancellationToken: ct)) {
103-
Debugger.Break();
104-
}
105-
106-
r_donmaiInit = true;
107-
108-
}
109-
else { }
110-
}
111-
112-
return req
113-
.WithCookies(Cookies);
114-
}
115-
116-
public static async Task<IReadOnlyList<FlurlCookie>> GetCookies(IFlurlRequest req, CancellationToken ct = default)
117-
{
118-
IReadOnlyList<FlurlCookie> ret = [];
119-
120-
if (r_donmai.IsMatch(req.Url.Host)) {
121-
122-
req.Headers.AddOrReplace("User-Agent", R1.Name);
123-
124-
using (var res2 = await req.GetAsync(cancellationToken: ct)) {
125-
ret = res2.Cookies;
126-
}
127-
}
128-
129-
return ret;
130-
}
13176

13277
/*public static readonly BaseImageHost[] All =
13378
ReflectionHelper.CreateAllInAssembly<BaseImageHost>(InheritanceProperties.Subclass).ToArray();*/
@@ -184,14 +129,8 @@ public static async Task<IEnumerable<string>> GetImageUrlsAsync(Url u, IImageFil
184129
}
185130
*/
186131

187-
188132
private const char URL_DELIM = '/';
189133

190-
internal static readonly Regex r_donmai = new(
191-
"""\.donmai\.us""",
192-
RegexOptions.Compiled
193-
);
194-
195134
/*
196135
* TODO:
197136
*
@@ -216,6 +155,13 @@ public static async Task<IEnumerable<string>> GetImageUrlsAsync(Url u, IImageFil
216155
RegexOptions.Compiled
217156
);
218157

158+
public static async ValueTask<IFlurlRequest> BuildRequest(Url u, CancellationToken ct = default)
159+
{
160+
var req = Client.Request(u);
161+
162+
return req.WithCookies(Cookies);
163+
}
164+
219165
public static async Task<List<UniSimilarity>> Analyze(List<Task<UniImage>> tasks, SearchQuery query,
220166
CancellationToken ct = default)
221167
{
@@ -224,18 +170,21 @@ public static async Task<List<UniSimilarity>> Analyze(List<Task<UniImage>> tasks
224170
query.Uni.Stream.TrySeek();
225171
var rg = new List<UniSimilarity>();
226172

227-
while (tasks.Count != 0) {
173+
while (tasks.Count != 0)
174+
{
228175
var task = await Task.WhenAny(tasks);
229176
tasks.Remove(task);
230177
var ux = await task;
231178

232-
if (ux != UniImage.Null && ux.HasImageFormat) {
179+
if (ux != UniImage.Null && ux.HasImageFormat)
180+
{
233181
var cmp = ph.Hash(ux.Stream);
234182
var sim = CompareHash.Similarity(orig, cmp);
235183
rg.Add(new UniSimilarity(ux, sim));
236184
ux.Stream.TrySeek();
237185
}
238-
else {
186+
else
187+
{
239188
ux.Dispose();
240189
ux = null;
241190
}
@@ -250,7 +199,6 @@ public static async Task<List<UniSimilarity>> Analyze(List<Task<UniImage>> tasks
250199
/// </summary>
251200
public static async Task<List<Task<UniImage>>> ScanImagesAsync(Url u, CancellationToken ct = default)
252201
{
253-
254202
List<Task<UniImage>> tasks = null;
255203
IFlurlRequest req;
256204
IFlurlResponse res;
@@ -332,20 +280,23 @@ public static async Task<List<Task<UniImage>>> ScanImagesAsync(Url u, Cancellati
332280
autoDisposeOnError: false, ct: ct);
333281

334282

335-
if (uf != UniImage.Null) {
336-
if (uf.HasImageFormat) {
283+
if (uf != UniImage.Null)
284+
{
285+
if (uf.HasImageFormat)
286+
{
337287
tasks = [Task.FromResult(uf)];
338288

339289
goto ret;
340290
}
341291
}
342-
else {
292+
else
293+
{
343294
stream = uf.Stream;
344295
stream.Position = 0;
345-
346296
}
347297

348-
if (!stream.CanRead) {
298+
if (!stream.CanRead)
299+
{
349300
stream.Dispose();
350301
goto ret;
351302
}
@@ -364,8 +315,6 @@ public static async Task<List<Task<UniImage>>> ScanImagesAsync(Url u, Cancellati
364315
}).ToList();
365316

366317
// var rr = await u.WithHeader("User-Agent", "SI").GetStreamAsync(HttpCompletionOption.ResponseContentRead);
367-
368-
369318
// var parser = new HtmlParser();
370319

371320
// var doc = await parser.ParseDocumentAsync(stream);
@@ -420,26 +369,28 @@ await Parallel.ForEachAsync(urls, po, async (s, token) =>
420369

421370
public static IEnumerable<string> GetImageUrls(string html, Url url)
422371
{
423-
var imgUrlsSrc = r_imgSource.Matches(html).Select(m => m.Groups["URL"].Value);
424-
var imgUrlsExt = r_imgExt.Matches(html).Select(m => m.Value);
372+
var imgUrlsSrc = r_imgSource.Matches(html).Select(static m => m.Groups["URL"].Value);
373+
var imgUrlsExt = r_imgExt.Matches(html).Select(static m => m.Value);
425374
var imgUrls = imgUrlsSrc.Concat(imgUrlsExt);
426375

427376
Match baseMatch = r_imgHtml.Match(html);
428377
string baseUrl;
429378

430-
if (baseMatch.Success) {
379+
if (baseMatch.Success)
380+
{
431381
baseUrl = baseMatch.Groups["url"].Value.TrimEnd(URL_DELIM);
432-
433382
}
434-
else {
435-
if (url.ToString().EndsWith(URL_DELIM)) {
383+
else
384+
{
385+
if (url.ToString().EndsWith(URL_DELIM))
386+
{
436387
baseUrl = url.ToString().TrimEnd(URL_DELIM);
437388
}
438-
else {
389+
else
390+
{
439391
baseUrl = Url.Parse(url); //todo
440392

441393
// or Path.GetDirectoryName?
442-
443394
}
444395
}
445396

@@ -465,12 +416,10 @@ public static IEnumerable<string> GetImageUrls(IHtmlDocument doc)
465416
// var a = doc.QueryAllAttribute("a", "href");
466417
// var b = doc.QueryAllAttribute("img", "src");
467418

468-
var a = doc.Links.Select(x => x.GetAttribute("href"));
469-
var b = doc.Images.Select(x => x.Source);
470-
419+
var a = doc.Links.Select(static x => x.GetAttribute("href"));
420+
var b = doc.Images.Select(static x => x.Source);
471421
var c = a.Union(b);
472422

473-
474423
c = c.Distinct();
475424

476425
return c;
@@ -487,6 +436,7 @@ public static async Task<UniImage[]> RunGalleryDLAsync(Url cri, CancellationToke
487436
RedirectStandardError = true,
488437
});
489438
await p.WaitForExitAsync(ct);
439+
490440
var s = await p.StandardOutput.ReadToEndAsync(ct);
491441
var s2 = s.Split(Environment.NewLine);
492442
var rg = new ConcurrentBag<UniImage>();
@@ -495,7 +445,8 @@ await Parallel.ForEachAsync(s2, ct, async (s1, token) =>
495445
{
496446
var uni = await UniImage.TryCreateAsync(s1, ct: token);
497447

498-
if (uni != null) {
448+
if (uni != null)
449+
{
499450
rg.Add(uni);
500451
}
501452

SmartImage.Lib/Results/Data/AutoCookiesProvider.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ public AutoCookiesProvider(BaseCookieReader reader)
3636

3737
public async ValueTask OpenAsync()
3838
{
39-
await Reader.OpenAsync();
39+
// Opening is idempotent
40+
await Reader.Connection.OpenAsync();
41+
}
42+
43+
public async ValueTask CloseAsync()
44+
{
45+
await Reader.Connection.CloseAsync();
4046
}
4147

4248
public async ValueTask<IList<IBrowserCookie>> LoadCookiesAsync(CancellationToken ct = default)
@@ -45,9 +51,11 @@ public async ValueTask<IList<IBrowserCookie>> LoadCookiesAsync(CancellationToken
4551
await OpenAsync();
4652
}
4753

48-
if (IsClosedOrBroken) {
54+
/*if (IsClosedOrBroken) {
4955
throw new InvalidOperationException();
50-
}
56+
}*/
57+
58+
5159

5260
var itemPolicy = new CacheItemPolicy()
5361
{
@@ -58,6 +66,7 @@ public async ValueTask<IList<IBrowserCookie>> LoadCookiesAsync(CancellationToken
5866
var chi = (IList<IBrowserCookie>) Cache.Get(CH_NAME);
5967

6068
if (chi == null) {
69+
6170
var cookies = await Reader.ReadCookiesAsync();
6271
var addOk = Cache.Add(CH_NAME, cookies, itemPolicy);
6372

@@ -74,6 +83,7 @@ public async ValueTask<IList<IBrowserCookie>> LoadCookiesAsync(CancellationToken
7483
}
7584

7685
public bool IsOpen => Reader.Connection.State is ConnectionState.Open;
86+
7787
public bool IsOpenOrInUse => Reader.Connection.State is < ConnectionState.Broken and >= ConnectionState.Open;
7888

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

SmartImage.Lib/Results/Data/ICookiesReceiver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public interface ICookiesReceiver
1515
[MNNW(true, nameof(Jar))]
1616
public bool Loaded => Jar != null && Jar.Count != 0;
1717

18-
public ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, CancellationToken token);
18+
public ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, CancellationToken token = default);
1919

2020

2121
}

0 commit comments

Comments
 (0)