Skip to content

Commit 7612db7

Browse files
committed
Work on FlareSolverr; TinEye; etc
1 parent 2125dff commit 7612db7

File tree

9 files changed

+448
-346
lines changed

9 files changed

+448
-346
lines changed

SmartImage.Lib/Clients/FlareSolverr.cs renamed to SmartImage.Lib/Clients/FlareSolverrClient.cs

Lines changed: 115 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,124 @@
11
// Root myDeserializedClass = JsonSerializer.Deserialize<Root>(myJsonResponse);
22

3+
using System.Text.Json;
34
using System.Text.Json.Serialization;
45
using Flurl.Http;
6+
using Flurl.Http.Configuration;
57

68
namespace SmartImage.Lib.Clients;
79

10+
public class FlareSolverrClient : IDisposable
11+
{
12+
13+
public const string CMD_REQUEST_GET = "request.get";
14+
public const string CMD_REQUEST_POST = "request.post";
15+
16+
public const int DEFAULT_MAX_TIMEOUT = 60000;
17+
18+
private static readonly JsonSerializerOptions s_jsonSerializerOptions = new(JsonSerializerOptions.Default)
19+
{
20+
DefaultIgnoreCondition =
21+
JsonIgnoreCondition.WhenWritingDefault
22+
};
23+
24+
private static readonly DefaultJsonSerializer s_settingsJsonSerializer = new(s_jsonSerializerOptions)
25+
{ };
26+
27+
// TODO: FlareSolverrSharp doesn't work 10/8/24
28+
29+
public FlurlClient Client { get; }
30+
31+
public string BaseUrl { get; }
32+
33+
public FlareSolverrClient(string baseUrl)
34+
{
35+
BaseUrl = baseUrl;
36+
37+
Client = (FlurlClient) FlurlHttp.Clients.GetOrAdd(nameof(FlareSolverrClient), BaseUrl, builder =>
38+
{
39+
builder.AllowAnyHttpStatus();
40+
41+
builder.Settings.JsonSerializer = s_settingsJsonSerializer;
42+
});
43+
44+
}
45+
46+
public async ValueTask<bool> IsConnectedAsync(int maxTimeout = DEFAULT_MAX_TIMEOUT)
47+
{
48+
try {
49+
using var res = await SendAsync("https://nowsecure.nl/", CMD_REQUEST_GET, maxTimeout);
50+
51+
if (res == null) {
52+
return false;
53+
}
54+
55+
var obj = await res.GetJsonAsync<FlareSolverrRoot>();
56+
return obj.Status == "ok";
57+
}
58+
catch {
59+
return false;
60+
}
61+
finally { }
62+
63+
64+
}
65+
66+
public static readonly FlareSolverrClient Instance = new("http://localhost:8191/v1");
67+
68+
public Task<IFlurlResponse> SendAsync(FlareSolverrRequest request)
69+
{
70+
var resp = Client.Request()
71+
.PostJsonAsync(request);
72+
73+
return resp;
74+
}
75+
76+
public Task<IFlurlResponse> SendAsync(string url, string cmd, int maxTimeout = DEFAULT_MAX_TIMEOUT)
77+
{
78+
79+
return SendAsync(new FlareSolverrRequest() { Url = url, Command = cmd, MaxTimeout = maxTimeout });
80+
81+
}
82+
83+
public void Dispose()
84+
{
85+
Client?.Dispose();
86+
}
87+
88+
}
89+
90+
#region API Objects
91+
92+
public record FlareSolverrRequest
93+
{
94+
95+
// todo
96+
97+
[JsonPropertyName("cmd")]
98+
public string Command { get; set; }
99+
100+
public List<FlareSolverrCookie> Cookies { get; set; }
101+
102+
public int MaxTimeout { get; set; }
103+
104+
public Dictionary<string, object> Proxy { get; set; } //todo
105+
106+
public string Session { get; set; }
107+
108+
[JsonPropertyName("session_ttl_minutes")]
109+
public int SessionTtl { get; set; }
110+
111+
public string Url { get; set; }
112+
113+
public string PostData { get; set; }
114+
115+
public bool ReturnOnlyCookies { get; set; }
116+
117+
118+
public FlareSolverrRequest() { }
119+
120+
}
121+
8122
public class FlareSolverrCookie
9123
{
10124

@@ -130,43 +244,4 @@ public class FlareSolverrSolution
130244

131245
}
132246

133-
public class FlareSolverrClient : IDisposable
134-
{
135-
136-
// TODO: FlareSolverrSharp doesn't work 10/8/24
137-
138-
public FlurlClient Client { get; }
139-
140-
public string BaseUrl { get; }
141-
142-
public FlareSolverrClient(string baseUrl)
143-
{
144-
BaseUrl = baseUrl;
145-
Client = new FlurlClient(BaseUrl);
146-
}
147-
148-
public static readonly FlareSolverrClient Instance = new("http://localhost:8191/v1");
149-
150-
public Task<IFlurlResponse> Get(string url, string cmd, int maxTimeout = 60000)
151-
{
152-
var body = new
153-
{
154-
cmd = cmd,
155-
url = url,
156-
maxTimeout = maxTimeout
157-
};
158-
159-
var resp = Client.Request()
160-
.WithHeader("Accept","text/html; charset=utf-8")
161-
.PostJsonAsync(body);
162-
163-
return resp;
164-
165-
}
166-
167-
public void Dispose()
168-
{
169-
Client?.Dispose();
170-
}
171-
172-
}
247+
#endregion

SmartImage.Lib/Clients/HydrusClient.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ public static string HyEncode(object o)
201201

202202
}
203203

204+
#region API Objects
205+
204206
public sealed class HydrusQuery
205207
{
206208

@@ -316,4 +318,6 @@ public static Dictionary<string, HydrusFileRelationship> Deserialize(JsonValue v
316318
return re;
317319
}
318320

319-
}
321+
}
322+
323+
#endregion

SmartImage.Lib/Engines/BaseSearchEngine.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using AngleSharp.Dom;
1111
using AngleSharp.Html.Parser;
1212
using Flurl.Http;
13+
using Flurl.Http.Configuration;
1314
using Kantan.Diagnostics;
1415
using Kantan.Net.Utilities;
1516
using Kantan.Net.Web;
@@ -94,6 +95,8 @@ static BaseSearchEngine()
9495
Client = (FlurlClient) FlurlHttp.Clients.GetOrAdd(nameof(BaseSearchEngine), null, builder =>
9596
{
9697
builder.Headers.AddOrReplace("User-Agent", HttpUtilities.UserAgent);
98+
99+
// builder.Settings.JsonSerializer = new DefaultJsonSerializer();
97100

98101
builder.Settings.AllowedHttpStatusRange = "*";
99102
builder.OnError(f=>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
8888

8989
var origin = sr.RawUrl;
9090

91-
var res = await FlareSolverrClient.Instance.Get(origin, "request.get", Timeout.Milliseconds);
91+
var res = await FlareSolverrClient.Instance.SendAsync(origin,
92+
FlareSolverrClient.CMD_REQUEST_GET, Timeout.Milliseconds);
9293

9394
string str;
9495

0 commit comments

Comments
 (0)