Skip to content

Commit bd76034

Browse files
committed
Fixes & improvements
1 parent eae484a commit bd76034

File tree

10 files changed

+83
-53
lines changed

10 files changed

+83
-53
lines changed

SmartImage.Lib/Engines/BaseSearchEngine.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
using System;
44
using System.Diagnostics;
55
using System.Linq;
6+
using System.Net;
67
using System.Net.NetworkInformation;
78
using System.Threading.Tasks;
89
using Kantan.Diagnostics;
10+
using RestSharp;
911
using SmartImage.Lib.Utilities;
1012
using static Kantan.Diagnostics.LogCategories;
1113

@@ -31,14 +33,18 @@ protected BaseSearchEngine(string baseUrl)
3133

3234
public virtual TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(3);
3335

36+
protected virtual bool Redirect { get; set; } = true;
37+
3438
public virtual SearchResult GetResult(ImageQuery query)
3539
{
36-
var rawUrl = GetRawResultUri(query);
40+
var rawUrl = GetRawResultUri(query, out var r);
3741

3842
var sr = new SearchResult(this);
3943

4044
if (rawUrl == null) {
41-
sr.Status = ResultStatus.Unavailable;
45+
sr.Status = ResultStatus.Unavailable;
46+
sr.ErrorMessage = $"{r.ErrorMessage} | {r.StatusCode}";
47+
4248
}
4349
else {
4450
sr.RawUri = rawUrl;
@@ -67,29 +73,26 @@ public async Task<SearchResult> GetResultAsync(ImageQuery query)
6773
return await task;
6874
}
6975

70-
public Uri GetRawResultUri(ImageQuery query)
76+
public Uri GetRawResultUri(ImageQuery query, out IRestResponse res)
7177
{
7278
var uri = new Uri(BaseUrl + query.UploadUri);
7379

74-
//var reply = Network.Ping(uri, (long) Timeout.TotalMilliseconds);
75-
76-
////var b = Network.IsAlive(uri, (long) Timeout.TotalMilliseconds);
77-
////var b1 = ok.Status != IPStatus.Success || ok.Status == IPStatus.TimedOut;
80+
/*if (!Network.IsAlive(uri, (int) Timeout.TotalMilliseconds)) {
81+
Debug.WriteLine($"{Name} is unavailable or timed out after {Timeout:g} | {uri}", C_WARN);
82+
return null;
83+
}*/
7884

79-
//if (reply.Status != IPStatus.Success) {
80-
// Debug.WriteLine($"{Name} is unavailable or timed out after {Timeout:g} ({reply.Status})", C_WARN);
81-
// return null;
82-
//}
85+
res = Network.GetResponse(uri.ToString(), (int) Timeout.TotalMilliseconds, Method.GET, Redirect);
8386

84-
if (!Network.IsAlive(uri, (int) Timeout.TotalMilliseconds)) {
85-
Debug.WriteLine($"{Name} is unavailable or timed out after {Timeout:g} | {uri}", C_WARN);
87+
if (!res.IsSuccessful && res.StatusCode!= HttpStatusCode.Redirect) {
88+
Debug.WriteLine($"{Name} is unavailable or timed out after {Timeout:g} | {uri} {res.StatusCode}", C_WARN);
8689
return null;
8790
}
8891

8992
return uri;
9093
}
9194

92-
protected static SearchResult TryRun(SearchResult sr, Func<SearchResult, SearchResult> process)
95+
protected static SearchResult TryProcess(SearchResult sr, Func<SearchResult, SearchResult> process)
9396
{
9497
if (!sr.IsSuccessful) {
9598
return sr;
@@ -100,7 +103,8 @@ protected static SearchResult TryRun(SearchResult sr, Func<SearchResult, SearchR
100103
sr = process(sr);
101104
}
102105
catch (Exception e) {
103-
sr.Status = ResultStatus.Failure;
106+
sr.Status = ResultStatus.Failure;
107+
sr.ErrorMessage = e.Message;
104108
Trace.WriteLine($"{sr.Engine.Name}: {e.Message}", LogCategories.C_ERROR);
105109
}
106110

SmartImage.Lib/Engines/ClientSearchEngine.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ protected ClientSearchEngine(string baseUrl, string endpointUrl) : base(baseUrl)
3030

3131
protected RestClient Client { get; }
3232

33+
protected override bool Redirect { get; set; }
34+
3335
[DebuggerHidden]
3436
public override SearchResult GetResult(ImageQuery query)
3537
{
36-
return TryRun(base.GetResult(query), sr => Process(query, sr));
38+
return TryProcess(base.GetResult(query), sr => Process(query, sr));
3739
}
3840

3941
protected abstract SearchResult Process(ImageQuery query, SearchResult r);

SmartImage.Lib/Engines/Impl/Ascii2DEngine.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
using System.Collections.Generic;
88
using System.Diagnostics;
99
using System.Linq;
10+
using System.Net;
11+
using Kantan.Diagnostics;
12+
using RestSharp;
13+
1014
// ReSharper disable CognitiveComplexity
1115

1216
// ReSharper disable IdentifierTypo
@@ -18,16 +22,18 @@ public sealed class Ascii2DEngine : InterpretedSearchEngine
1822
{
1923
public Ascii2DEngine() : base("https://ascii2d.net/search/url/")
2024
{
21-
Timeout = TimeSpan.FromSeconds(5);
25+
2226
}
2327

28+
public override TimeSpan Timeout => TimeSpan.FromSeconds(5);
29+
2430
public override SearchEngineOptions EngineOption => SearchEngineOptions.Ascii2D;
2531

2632
public override string Name => EngineOption.ToString();
2733

28-
34+
protected override bool Redirect => false;
2935

30-
private static Uri ConvertToDetailUri(Uri url)
36+
private Uri ConvertToDetailUri(Uri url)
3137
{
3238
/*
3339
* URL parameters
@@ -38,7 +44,7 @@ private static Uri ConvertToDetailUri(Uri url)
3844
*/
3945

4046

41-
var res = Network.GetResponse(url.ToString());
47+
var res = Network.GetResponse(url.ToString(), (int) Timeout.TotalMilliseconds, Method.GET, false);
4248

4349
// Get redirect url (color url)
4450

SmartImage.Lib/Engines/Impl/TraceMoeEngine.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Net;
66
using JetBrains.Annotations;
7+
using Newtonsoft.Json;
78
using RestSharp;
89
using SmartImage.Lib.Clients;
910
using SmartImage.Lib.Searching;
@@ -42,14 +43,15 @@ protected override SearchResult Process(ImageQuery query, SearchResult r)
4243

4344

4445
var rq = new RestRequest("search");
45-
rq.AddQueryParameter("url", query.UploadUri.ToString());
46+
rq.AddQueryParameter("url", query.UploadUri.ToString(), true);
4647
//rq.AddQueryParameter("anilistInfo", "");
4748
rq.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
4849
rq.Timeout = Timeout.Milliseconds;
4950
rq.RequestFormat = DataFormat.Json;
5051

5152
var re = Client.Execute<TraceMoeRootObject>(rq, Method.GET);
5253
var tm = re.Data;
54+
//var tm=JsonConvert.DeserializeObject<TraceMoeRootObject>(re.Content);
5355

5456
if (tm?.result != null) {
5557
// Most similar to least similar
@@ -69,17 +71,16 @@ protected override SearchResult Process(ImageQuery query, SearchResult r)
6971
}
7072
catch (Exception e) {
7173
r = GetResult(query);
72-
Debug.WriteLine($"{Name}: Error: {e.Message}");
73-
74-
r.Status = ResultStatus.Failure;
74+
r.ErrorMessage = e.Message;
75+
r.Status = ResultStatus.Failure;
7576
return r;
7677
}
7778

7879

7980
}
8081
else {
8182
Debug.WriteLine($"{Name}: API error", C_ERROR);
82-
83+
r.ErrorMessage = $"{re.ErrorMessage} {re.StatusCode}";
8384
}
8485

8586
return r;
@@ -105,7 +106,7 @@ private IEnumerable<ImageResult> ConvertResults(TraceMoeRootObject obj)
105106
Url = new Uri(anilistUrl),
106107
Similarity = sim,
107108
Source = name,
108-
Description = $"Episode #{doc.episode} @ {TimeSpan.FromSeconds(doc.@from)}"
109+
Description = $"Episode #{doc.episode} @ {TimeSpan.FromSeconds(doc.from)}"
109110
};
110111

111112
if (result.Similarity < FILTER_THRESHOLD) {
@@ -135,16 +136,23 @@ private IEnumerable<ImageResult> ConvertResults(TraceMoeRootObject obj)
135136
[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)]
136137
private class TraceMoeDoc
137138
{
138-
public double from { get; set; }
139-
public double to { get; set; }
140-
public long anilist { get; set; }
139+
public double from { get; set; }
140+
141+
public double to { get; set; }
142+
143+
public long anilist { get; set; }
144+
141145
public string filename { get; set; }
142146

143-
public long episode { get; set; }
147+
148+
/// <remarks>Episode field may contain multiple possible results delimited by <c>|</c></remarks>
149+
public string episode { get; set; }
144150

145151
public double similarity { get; set; }
146-
public string video { get; set; }
147-
public string image { get; set; }
152+
153+
public string video { get; set; }
154+
155+
public string image { get; set; }
148156
}
149157

150158
[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)]

SmartImage.Lib/Engines/Impl/YandexEngine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public YandexEngine() : base("https://yandex.com/images/search?rpt=imageview&url
2626

2727
public override string Name => EngineOption.ToString();
2828

29+
public override TimeSpan Timeout => TimeSpan.FromSeconds(6.5);
2930

3031
private static string? GetAnalysis(IDocument doc)
3132
{

SmartImage.Lib/Engines/InterpretedSearchEngine.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,30 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Diagnostics;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
1+
using System.Diagnostics;
72
using AngleSharp.Dom;
83
using AngleSharp.Html.Parser;
9-
using RestSharp;
104
using Kantan.Net;
115
using SmartImage.Lib.Searching;
12-
using SmartImage.Lib.Utilities;
13-
using static Kantan.Diagnostics.LogCategories;
146

157
namespace SmartImage.Lib.Engines
168
{
179
/// <summary>
18-
/// Represents a search engine whose results are parsed.
10+
/// Represents a search engine whose results are parsed.
1911
/// </summary>
2012
public abstract class InterpretedSearchEngine : BaseSearchEngine
2113
{
14+
protected InterpretedSearchEngine(string baseUrl) : base(baseUrl) { }
15+
2216
public abstract override SearchEngineOptions EngineOption { get; }
2317

2418
public abstract override string Name { get; }
2519

26-
protected InterpretedSearchEngine(string baseUrl) : base(baseUrl) { }
27-
2820

21+
protected override bool Redirect { get; set; }
2922
[DebuggerHidden]
3023
public override SearchResult GetResult(ImageQuery query)
3124
{
32-
return TryRun(base.GetResult(query), sr =>
25+
return TryProcess(base.GetResult(query), sr =>
3326
{
34-
var doc = GetDocument(sr);
27+
IDocument doc = GetDocument(sr);
3528
sr = Process(doc, sr);
3629
return sr;
3730
});

SmartImage.Lib/SearchClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Runtime.CompilerServices;
1313
using System.Threading;
1414
using System.Threading.Tasks;
15+
using Kantan.Diagnostics;
1516
using Kantan.Net;
1617
using Kantan.Utilities;
1718
using SmartImage.Lib.Upload;
@@ -81,7 +82,7 @@ public void Reload()
8182
.ToArray();
8283

8384

84-
Trace.WriteLine($"Engines: {Config.SearchEngines}");
85+
Trace.WriteLine($"Engines: {Config.SearchEngines}", C_INFO);
8586
}
8687

8788
/// <summary>
@@ -236,8 +237,7 @@ public List<SearchResult> MaximizeResults<T>(Func<SearchResult, T> property)
236237
return res;
237238
}
238239

239-
240-
[CanBeNull]
240+
241241
public ImageResult FindDirectResult() => FindDirectResults().FirstOrDefault();
242242

243243
public ImageResult[] FindDirectResults(int count = 5)

SmartImage.Lib/Searching/SearchResult.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public enum ResultStatus
4343
Extraneous
4444
}
4545

46-
4746

4847
/// <summary>
4948
/// Describes a search result
@@ -88,7 +87,15 @@ public class SearchResult : IViewable
8887
/// If filtering is enabled (i.e., <see cref="SearchConfig.Filtering"/> is <c>true</c>), this determines whether the
8988
/// result is filtered.
9089
/// </summary>
91-
public bool IsNonPrimitive => Status != ResultStatus.Extraneous && PrimaryResult.Url != null;
90+
public bool IsNonPrimitive
91+
{
92+
get
93+
{
94+
//return (Status != ResultStatus.Extraneous && PrimaryResult.Url != null) || ErrorMessage!=null;
95+
return (Status != ResultStatus.Extraneous && PrimaryResult.Url != null);
96+
97+
}
98+
}
9299

93100

94101
public bool IsSuccessful
@@ -128,7 +135,6 @@ public void Consolidate()
128135

129136
public override string ToString() => Strings.ViewString(this);
130137

131-
132138

133139
public Dictionary<string, object> View
134140
{

SmartImage.Lib/Utilities/ImageHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ public static bool IsImage(string s, int d = TimeoutMS)
195195
}
196196

197197
var a = m.ContentType.StartsWith("image") && m.ContentType != "image/svg+xml";
198-
var b = m.ContentLength >= 2500;
198+
//var b = m.ContentLength >= 2500;
199+
var b = m.ContentLength >= 50_000;
199200

200201
return a && b;
201202
}

UnitTest/UnitTest1.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ public void TestImageQuery()
4141
Assert.DoesNotThrow(() => new ImageQuery("https://i.imgur.com/QtCausw.png"));
4242
}
4343

44+
[Test]
45+
[TestCase("https://i.imgur.com/QtCausw.png", true)]
46+
[TestCase("https://twitter.com/sciamano240/status/1186775807655587841", false)]
47+
public void TestImageHelper(string s, bool b)
48+
{
49+
Assert.AreEqual(ImageHelper.IsImage(s),b);
50+
51+
}
52+
4453
[Test]
4554
public void TestResolutionType()
4655
{

0 commit comments

Comments
 (0)