Skip to content

Commit 39de727

Browse files
committed
Fixes
1 parent dc8c3ce commit 39de727

File tree

4 files changed

+99
-89
lines changed

4 files changed

+99
-89
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,23 @@ protected override ValueTask<SearchResultItem> ParseResultItem(INode nx, SearchR
262262
return ValueTask.FromResult(sri);
263263
}
264264

265-
public async ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, CancellationToken ct)
265+
public async ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, CancellationToken ct = default)
266266
{
267-
var ck = Jar.Where(static x => x.Domain.Contains("ascii2d"));
268-
269-
foreach (var c in ck) {
270-
Jar.AddOrReplace(c);
267+
if (FlareSolverrClient.Value.IsInitialized) {
268+
return false;
271269
}
272270

271+
var cookies = await provider.LoadCookiesAsync(ct);
272+
273+
foreach (var bck in cookies) {
274+
var ck = bck.AsCookie();
275+
276+
if (ck.Domain.Contains("ascii2d")) {
277+
Jar.AddOrReplace(new FlurlCookie(ck.Name, ck.Value, BaseUrl));
278+
}
279+
}
280+
281+
273282
return true;
274283
}
275284

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

Lines changed: 28 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,14 @@ namespace SmartImage.Lib.Engines.Impl.Search;
3030
public 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

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

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,99 @@
1-
using System.Text.Json;
1+
using System.Diagnostics;
2+
using System.Text.Json;
23
using System.Text.Json.Serialization;
34
using Flurl.Http;
5+
using Kantan.Monad;
46
using SmartImage.Lib.Results;
57

68
namespace SmartImage.Lib.Engines.Impl.Search;
79

810
public sealed class TinEyeEngine : BaseSearchEngine
911
{
1012

11-
public TinEyeEngine() : base("https://www.tineye.com/search?url=") { }
13+
public TinEyeEngine() : base("https://www.tineye.com/search?url=")
14+
{
15+
MaxSize = 10_000_000;
16+
}
1217

1318
private const string API_URL = "https://tineye.com/api/v1/result_json/?sort=score&order=desc";
1419

1520
public override SearchEngineOptions EngineOption => SearchEngineOptions.TinEye;
1621

1722
public override void Dispose() { }
1823

24+
public override bool VerifyQuery(SearchQuery q)
25+
{
26+
q.Uni.AllocImage().Wait();
27+
28+
if (q.Uni.Image.Width >= 10000) {
29+
return false;
30+
}
31+
32+
return base.VerifyQuery(q);
33+
}
34+
1935
public override async Task<SearchResult> GetResultAsync(SearchQuery query, CancellationToken token = default)
2036
{
2137
var sr = await base.GetResultAsync(query, token);
2238

39+
if (sr.Status == SearchResultStatus.IllegalInput) {
40+
goto ret;
41+
}
42+
2343
var req = await Client.Request(API_URL).PostMultipartAsync(b =>
2444
{
2545
b.AddString("url", query.Upload);
2646
}, cancellationToken: token);
2747

28-
var tinEyeRoot = await req.GetJsonAsync<TinEyeRoot>();
48+
TinEyeRoot tinEyeRoot = null;
49+
50+
try {
51+
var str = await req.GetStringAsync();
52+
53+
tinEyeRoot = JsonSerializer.Deserialize<TinEyeRoot>(str);
54+
55+
// tinEyeRoot = await req.GetJsonAsync<TinEyeRoot>();
56+
}
57+
catch (Exception e) {
58+
// Debugger.Break();
59+
Trace.WriteLine(e.Message);
60+
goto ret;
61+
}
2962

3063
foreach (TinEyeMatch match in tinEyeRoot.Matches) {
64+
var backlinks = match.Backlinks;
65+
var backlink = backlinks?[0];
66+
67+
3168
var resultItem = new SearchResultItem(sr)
3269
{
33-
Metadata = match,
34-
Site = match.Domain,
35-
Url = match.Backlinks[0].Backlink,
36-
Description = match.Backlinks[0].ImageName,
70+
Metadata = match,
71+
Site = match.Domain,
3772

3873
// Thumbnail = match.Backlinks[0].Url,
3974
Thumbnail = match.ImageUrl,
4075
Width = match.Width,
4176
Height = match.Height,
42-
Time = DateTime.Parse(match.Backlinks[0].CrawlDate)
4377
};
4478

45-
if (match.Backlinks.Count > 1) {
46-
for (int m = 1; m < match.Backlinks.Count; m++) {
47-
var bl = match.Backlinks[m];
79+
if (backlink != null) {
80+
resultItem.Url = backlink.Backlink;
81+
resultItem.Description = backlink.ImageName;
82+
resultItem.Time = DateTime.Parse(backlink?.CrawlDate);
83+
84+
}
85+
86+
if (backlinks is { Count: > 1 }) {
87+
for (int m = 1; m < backlinks.Count; m++) {
88+
var bl = backlinks[m];
4889

4990
var resultItemSister = resultItem with
5091
{
5192
Url = bl.Backlink,
5293

5394
// Thumbnail = bl.Url,
5495
Description = bl.ImageName,
55-
Time = DateTime.Parse(match.Backlinks[0].CrawlDate),
96+
Time = DateTime.Parse(bl.CrawlDate),
5697
Parent = resultItem,
5798
};
5899

@@ -65,6 +106,7 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
65106
sr.Results.Add(resultItem);
66107
}
67108

109+
ret:
68110
return sr;
69111
}
70112

@@ -291,7 +333,7 @@ public class TinEyeBacklink
291333
public string CrawlDate { get; set; }
292334

293335
[JsonPropertyName("source_id")]
294-
public int SourceId { get; set; }
336+
public long SourceId { get; set; }
295337

296338
[JsonPropertyName("image_name")]
297339
public string ImageName { get; set; }

SmartImage.Lib/Results/Data/AutoCookiesProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public async ValueTask<IList<IBrowserCookie>> LoadCookiesAsync(CancellationToken
7373
return chi;
7474
}
7575

76-
public bool IsOpen => Reader.Connection.State is < ConnectionState.Broken and >= ConnectionState.Open;
76+
public bool IsOpen => Reader.Connection.State is ConnectionState.Open;
77+
public bool IsOpenOrInUse => Reader.Connection.State is < ConnectionState.Broken and >= ConnectionState.Open;
7778

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

0 commit comments

Comments
 (0)