Skip to content

Commit 1e77908

Browse files
committed
Fix engine timeouts and deserialization errors
1 parent 9f230de commit 1e77908

File tree

13 files changed

+152
-100
lines changed

13 files changed

+152
-100
lines changed

SmartImage.Lib 3/Engines/BaseSearchEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public virtual async Task<SearchResult> GetResultAsync(SearchQuery query, Cancel
7777
ErrorMessage = null
7878
};
7979

80-
Debug.WriteLine($"{query} - {res.Status}", nameof(GetResultAsync));
80+
Debug.WriteLine($"{Name} | {query} - {res.Status}", nameof(GetResultAsync));
8181

8282
return res;
8383
}

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
using System.Diagnostics;
99
using System.Net;
10+
using AngleSharp.Css.Values;
1011
using AngleSharp.Dom;
1112
using AngleSharp.Html.Dom;
1213
using AngleSharp.Html.Parser;
@@ -130,18 +131,19 @@ private async Task<IDocument> GetDocumentAsync(SearchQuery query, CancellationTo
130131
{
131132

132133
try {
133-
var response = await EndpointUrl.ConfigureRequest(NetHelper.Configure()).PostMultipartAsync(m =>
134-
{
135-
m.AddString("MAX_FILE_SIZE", MAX_FILE_SIZE.ToString());
136-
m.AddString("url", query.Uni.IsUri ? query.Uni.Value.ToString() : String.Empty);
137-
138-
if (query.Uni.IsUri) { }
139-
else if (query.Uni.IsFile) {
140-
m.AddFile("file", query.Uni.Value.ToString(), fileName: "image.jpg");
141-
}
142-
143-
return;
144-
}, cancellationToken: ct);
134+
var response = await EndpointUrl.ConfigureRequest(NetHelper.Configure()).WithTimeout(Timeout)
135+
.PostMultipartAsync(m =>
136+
{
137+
m.AddString("MAX_FILE_SIZE", MAX_FILE_SIZE.ToString());
138+
m.AddString("url", query.Uni.IsUri ? query.Uni.Value.ToString() : String.Empty);
139+
140+
if (query.Uni.IsUri) { }
141+
else if (query.Uni.IsFile) {
142+
m.AddFile("file", query.Uni.Value.ToString(), fileName: "image.jpg");
143+
}
144+
145+
return;
146+
}, cancellationToken: ct);
145147

146148
var s = await response.GetStringAsync();
147149

SmartImage.Lib 3/Engines/Impl/Search/RepostSleuthEngine.cs

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Reflection;
2-
using System.Text.Json;
3-
using System.Text.Json.Serialization;
1+
using System.Text.Json;
42
using Flurl.Http;
53
using Jint.Native.Json;
64
using SmartImage.Lib.Results;
@@ -62,6 +60,11 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
6260
};
6361
obj = JsonSerializer.Deserialize<Root>(s, js);
6462
}
63+
catch (JsonException e) {
64+
sr.ErrorMessage = e.Message;
65+
sr.Status = SearchResultStatus.Failure;
66+
goto ret;
67+
}
6568
catch (FlurlHttpException e) {
6669
sr.ErrorMessage = e.Message;
6770
sr.Status = SearchResultStatus.Unavailable;
@@ -117,7 +120,7 @@ private class Match
117120
public int hash_size;
118121
public string searched_url;
119122
public Post post;
120-
public int title_similarity;
123+
public double title_similarity;
121124
}
122125

123126
private class Post
@@ -160,9 +163,9 @@ private class SearchSettings
160163
public bool check_title;
161164
public int max_depth;
162165
public bool meme_filter;
163-
public int target_annoy_distance;
164-
public int target_meme_match_percent;
165-
public int target_match_percent;
166+
public double target_annoy_distance;
167+
public double target_meme_match_percent;
168+
public double target_match_percent;
166169
}
167170

168171
private class SearchTimes
@@ -187,44 +190,4 @@ private class SearchTimes
187190
}
188191

189192
#endregion
190-
}
191-
192-
public class NonPublicMembersConverter<T> : JsonConverter<T> where T : class
193-
{
194-
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
195-
{
196-
T instance = (T) Activator.CreateInstance(typeToConvert, nonPublic: true);
197-
198-
while (reader.Read()) {
199-
if (reader.TokenType == JsonTokenType.EndObject) {
200-
break;
201-
}
202-
203-
if (reader.TokenType != JsonTokenType.PropertyName) {
204-
throw new JsonException();
205-
}
206-
207-
string propertyName = reader.GetString();
208-
209-
PropertyInfo propertyInfo =
210-
typeToConvert.GetProperty(propertyName,
211-
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
212-
213-
if (propertyInfo != null && propertyInfo.CanWrite) {
214-
reader.Read(); // Move to the property value
215-
object value = JsonSerializer.Deserialize(ref reader, propertyInfo.PropertyType, options);
216-
propertyInfo.SetValue(instance, value);
217-
}
218-
else {
219-
reader.Skip();
220-
}
221-
}
222-
223-
return instance;
224-
}
225-
226-
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
227-
{
228-
JsonSerializer.Serialize(writer, value, options);
229-
}
230193
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ private async Task<IEnumerable<SauceNaoDataResult>> GetWebResultsAsync(SearchQue
158158

159159
try {
160160
response = await EndpointUrl.AllowHttpStatus()
161+
.WithTimeout(Timeout)
161162
.PostMultipartAsync(m =>
162163
{
163164
m.AddString("url", query.Uni.IsUri ? query.Uni.Value.ToString() : string.Empty);
@@ -367,6 +368,7 @@ private async Task<IEnumerable<SauceNaoDataResult>> GetAPIResultsAsync(SearchQue
367368
var content = new FormUrlEncodedContent(values);
368369

369370
var res = await BASE_ENDPOINT.AllowAnyHttpStatus()
371+
.WithTimeout(Timeout)
370372
.PostAsync(content);
371373
var c = await res.GetStringAsync();
372374

SmartImage.Lib 3/Engines/Impl/Search/TraceMoeEngine.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
4444
var r = await base.GetResultAsync(query, token);
4545

4646
try {
47-
IFlurlRequest request = (EndpointUrl.AppendPathSegment("/search"))
47+
IFlurlRequest request = (EndpointUrl.AppendPathSegment("/search"))
4848
.AllowAnyHttpStatus()
49+
.WithTimeout(Timeout)
4950
.SetQueryParam("url", query.Upload, true);
5051

5152
var response = await request.GetAsync(cancellationToken: token);

SmartImage.Lib 3/Engines/WebSearchEngine.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Diagnostics;
2+
using System.Text.Json;
23
using AngleSharp.Dom;
34
using AngleSharp.Html.Parser;
45
using AngleSharp.XPath;
@@ -90,7 +91,7 @@ protected virtual async Task<IDocument> GetDocumentAsync(SearchResult sr, Search
9091
return document;
9192

9293
}
93-
catch (FlurlHttpException e) {
94+
catch (Exception e) {
9495
// return await Task.FromException<IDocument>(e);
9596
Debug.WriteLine($"{this} :: {e.Message}", nameof(GetDocumentAsync));
9697

SmartImage.Lib 3/SearchClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ public List<Task<SearchResult>> GetSearchTasks(SearchQuery query, CancellationTo
228228

229229
var tasks = Engines.Select(e =>
230230
{
231+
Debug.WriteLine($"Starting {e} for {query}");
231232
var res = e.GetResultAsync(query, token);
232233

233234
return res;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Read S SmartImage.Lib NonPublicMembersConverter.cs
2+
// 2023-09-26 @ 10:51 PM
3+
4+
using System.Reflection;
5+
using System.Text.Json;
6+
using System.Text.Json.Serialization;
7+
8+
namespace SmartImage.Lib.Utilities;
9+
10+
#pragma warning disable CS0649
11+
public class NonPublicMembersConverter<T> : JsonConverter<T> where T : class
12+
{
13+
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
14+
{
15+
T instance = (T)Activator.CreateInstance(typeToConvert, nonPublic: true);
16+
17+
while (reader.Read())
18+
{
19+
if (reader.TokenType == JsonTokenType.EndObject)
20+
{
21+
break;
22+
}
23+
24+
if (reader.TokenType != JsonTokenType.PropertyName)
25+
{
26+
throw new JsonException();
27+
}
28+
29+
string propertyName = reader.GetString();
30+
31+
PropertyInfo propertyInfo =
32+
typeToConvert.GetProperty(propertyName,
33+
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
34+
35+
if (propertyInfo != null && propertyInfo.CanWrite)
36+
{
37+
reader.Read(); // Move to the property value
38+
object value = JsonSerializer.Deserialize(ref reader, propertyInfo.PropertyType, options);
39+
propertyInfo.SetValue(instance, value);
40+
}
41+
else
42+
{
43+
reader.Skip();
44+
}
45+
}
46+
47+
return instance;
48+
}
49+
50+
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
51+
{
52+
JsonSerializer.Serialize(writer, value, options);
53+
}
54+
}

SmartImage.UI/MainWindow.Handlers.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private void Btn_Restart_Click(object sender, RoutedEventArgs e)
196196
var i = Queue.IndexOf(cpy);
197197
Queue.Remove(cpy);
198198
cpy.Dispose();
199-
ResultModel rm = new ResultModel(cpy.Value);
199+
QueryModel rm = new QueryModel(cpy.Value);
200200
Queue.Insert(i, rm);
201201
CurrentQueueItem = rm;
202202

@@ -239,13 +239,23 @@ private void Btn_Remove_Click(object sender, RoutedEventArgs e)
239239

240240
var old = CurrentQueueItem;
241241

242-
if (old == null || old.IsPrimitive) {
242+
if (old == null || string.IsNullOrWhiteSpace(old.Value)) {
243243
goto ret;
244244
}
245245

246+
if (old.IsPrimitive) {
247+
248+
}
249+
246250
var i = Queue.IndexOf(old);
247251
Queue.Remove(old);
248252
old?.Dispose();
253+
254+
// SetQueue(string.Empty);
255+
if (Queue.Count > 0) {
256+
Lb_Queue.SelectedIndex = 0;
257+
258+
}
249259
// TrySeekQueue(q);
250260
// AdvanceQueue(-1);
251261

SmartImage.UI/MainWindow.State.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public bool IsSelected
8888

8989
#region
9090

91-
public ResultModel? FindQueue(string s)
91+
public QueryModel? FindQueue(string s)
9292
{
9393
var x = Queue.FirstOrDefault(x => x.Value == s);
9494

@@ -105,7 +105,7 @@ public void ClearQueue()
105105
{
106106
Lb_Queue.SelectedIndex = -1;
107107
Queue.Clear();
108-
var rm = new ResultModel();
108+
var rm = new QueryModel();
109109
Queue.Add(rm);
110110
// Lb_Queue.SelectedIndex = 0;
111111
CurrentQueueItem = rm;
@@ -126,7 +126,7 @@ public bool SetQueue(string s)
126126
var b = x == null;
127127

128128
if (b) {
129-
x = new ResultModel(s);
129+
x = new QueryModel(s);
130130
Queue.Add(x);
131131
}
132132

0 commit comments

Comments
 (0)