Skip to content

Commit

Permalink
Fixes & improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Decimation committed Jul 20, 2021
1 parent eae484a commit bd76034
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 53 deletions.
34 changes: 19 additions & 15 deletions SmartImage.Lib/Engines/BaseSearchEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using Kantan.Diagnostics;
using RestSharp;
using SmartImage.Lib.Utilities;
using static Kantan.Diagnostics.LogCategories;

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

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

protected virtual bool Redirect { get; set; } = true;

public virtual SearchResult GetResult(ImageQuery query)
{
var rawUrl = GetRawResultUri(query);
var rawUrl = GetRawResultUri(query, out var r);

var sr = new SearchResult(this);

if (rawUrl == null) {
sr.Status = ResultStatus.Unavailable;
sr.Status = ResultStatus.Unavailable;
sr.ErrorMessage = $"{r.ErrorMessage} | {r.StatusCode}";

}
else {
sr.RawUri = rawUrl;
Expand Down Expand Up @@ -67,29 +73,26 @@ public async Task<SearchResult> GetResultAsync(ImageQuery query)
return await task;
}

public Uri GetRawResultUri(ImageQuery query)
public Uri GetRawResultUri(ImageQuery query, out IRestResponse res)
{
var uri = new Uri(BaseUrl + query.UploadUri);

//var reply = Network.Ping(uri, (long) Timeout.TotalMilliseconds);

////var b = Network.IsAlive(uri, (long) Timeout.TotalMilliseconds);
////var b1 = ok.Status != IPStatus.Success || ok.Status == IPStatus.TimedOut;
/*if (!Network.IsAlive(uri, (int) Timeout.TotalMilliseconds)) {
Debug.WriteLine($"{Name} is unavailable or timed out after {Timeout:g} | {uri}", C_WARN);
return null;
}*/

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

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

return uri;
}

protected static SearchResult TryRun(SearchResult sr, Func<SearchResult, SearchResult> process)
protected static SearchResult TryProcess(SearchResult sr, Func<SearchResult, SearchResult> process)
{
if (!sr.IsSuccessful) {
return sr;
Expand All @@ -100,7 +103,8 @@ protected static SearchResult TryRun(SearchResult sr, Func<SearchResult, SearchR
sr = process(sr);
}
catch (Exception e) {
sr.Status = ResultStatus.Failure;
sr.Status = ResultStatus.Failure;
sr.ErrorMessage = e.Message;
Trace.WriteLine($"{sr.Engine.Name}: {e.Message}", LogCategories.C_ERROR);
}

Expand Down
4 changes: 3 additions & 1 deletion SmartImage.Lib/Engines/ClientSearchEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ protected ClientSearchEngine(string baseUrl, string endpointUrl) : base(baseUrl)

protected RestClient Client { get; }

protected override bool Redirect { get; set; }

[DebuggerHidden]
public override SearchResult GetResult(ImageQuery query)
{
return TryRun(base.GetResult(query), sr => Process(query, sr));
return TryProcess(base.GetResult(query), sr => Process(query, sr));
}

protected abstract SearchResult Process(ImageQuery query, SearchResult r);
Expand Down
14 changes: 10 additions & 4 deletions SmartImage.Lib/Engines/Impl/Ascii2DEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using Kantan.Diagnostics;
using RestSharp;

// ReSharper disable CognitiveComplexity

// ReSharper disable IdentifierTypo
Expand All @@ -18,16 +22,18 @@ public sealed class Ascii2DEngine : InterpretedSearchEngine
{
public Ascii2DEngine() : base("https://ascii2d.net/search/url/")
{
Timeout = TimeSpan.FromSeconds(5);

}

public override TimeSpan Timeout => TimeSpan.FromSeconds(5);

public override SearchEngineOptions EngineOption => SearchEngineOptions.Ascii2D;

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


protected override bool Redirect => false;

private static Uri ConvertToDetailUri(Uri url)
private Uri ConvertToDetailUri(Uri url)
{
/*
* URL parameters
Expand All @@ -38,7 +44,7 @@ private static Uri ConvertToDetailUri(Uri url)
*/


var res = Network.GetResponse(url.ToString());
var res = Network.GetResponse(url.ToString(), (int) Timeout.TotalMilliseconds, Method.GET, false);

// Get redirect url (color url)

Expand Down
32 changes: 20 additions & 12 deletions SmartImage.Lib/Engines/Impl/TraceMoeEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Net;
using JetBrains.Annotations;
using Newtonsoft.Json;
using RestSharp;
using SmartImage.Lib.Clients;
using SmartImage.Lib.Searching;
Expand Down Expand Up @@ -42,14 +43,15 @@ protected override SearchResult Process(ImageQuery query, SearchResult r)


var rq = new RestRequest("search");
rq.AddQueryParameter("url", query.UploadUri.ToString());
rq.AddQueryParameter("url", query.UploadUri.ToString(), true);
//rq.AddQueryParameter("anilistInfo", "");
rq.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
rq.Timeout = Timeout.Milliseconds;
rq.RequestFormat = DataFormat.Json;

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

if (tm?.result != null) {
// Most similar to least similar
Expand All @@ -69,17 +71,16 @@ protected override SearchResult Process(ImageQuery query, SearchResult r)
}
catch (Exception e) {
r = GetResult(query);
Debug.WriteLine($"{Name}: Error: {e.Message}");

r.Status = ResultStatus.Failure;
r.ErrorMessage = e.Message;
r.Status = ResultStatus.Failure;
return r;
}


}
else {
Debug.WriteLine($"{Name}: API error", C_ERROR);

r.ErrorMessage = $"{re.ErrorMessage} {re.StatusCode}";
}

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

if (result.Similarity < FILTER_THRESHOLD) {
Expand Down Expand Up @@ -135,16 +136,23 @@ private IEnumerable<ImageResult> ConvertResults(TraceMoeRootObject obj)
[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)]
private class TraceMoeDoc
{
public double from { get; set; }
public double to { get; set; }
public long anilist { get; set; }
public double from { get; set; }

public double to { get; set; }

public long anilist { get; set; }

public string filename { get; set; }

public long episode { get; set; }

/// <remarks>Episode field may contain multiple possible results delimited by <c>|</c></remarks>
public string episode { get; set; }

public double similarity { get; set; }
public string video { get; set; }
public string image { get; set; }

public string video { get; set; }

public string image { get; set; }
}

[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)]
Expand Down
1 change: 1 addition & 0 deletions SmartImage.Lib/Engines/Impl/YandexEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public YandexEngine() : base("https://yandex.com/images/search?rpt=imageview&url

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

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

private static string? GetAnalysis(IDocument doc)
{
Expand Down
21 changes: 7 additions & 14 deletions SmartImage.Lib/Engines/InterpretedSearchEngine.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using AngleSharp.Dom;
using AngleSharp.Html.Parser;
using RestSharp;
using Kantan.Net;
using SmartImage.Lib.Searching;
using SmartImage.Lib.Utilities;
using static Kantan.Diagnostics.LogCategories;

namespace SmartImage.Lib.Engines
{
/// <summary>
/// Represents a search engine whose results are parsed.
/// Represents a search engine whose results are parsed.
/// </summary>
public abstract class InterpretedSearchEngine : BaseSearchEngine
{
protected InterpretedSearchEngine(string baseUrl) : base(baseUrl) { }

public abstract override SearchEngineOptions EngineOption { get; }

public abstract override string Name { get; }

protected InterpretedSearchEngine(string baseUrl) : base(baseUrl) { }


protected override bool Redirect { get; set; }
[DebuggerHidden]
public override SearchResult GetResult(ImageQuery query)
{
return TryRun(base.GetResult(query), sr =>
return TryProcess(base.GetResult(query), sr =>
{
var doc = GetDocument(sr);
IDocument doc = GetDocument(sr);
sr = Process(doc, sr);
return sr;
});
Expand Down
6 changes: 3 additions & 3 deletions SmartImage.Lib/SearchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Kantan.Diagnostics;
using Kantan.Net;
using Kantan.Utilities;
using SmartImage.Lib.Upload;
Expand Down Expand Up @@ -81,7 +82,7 @@ public void Reload()
.ToArray();


Trace.WriteLine($"Engines: {Config.SearchEngines}");
Trace.WriteLine($"Engines: {Config.SearchEngines}", C_INFO);
}

/// <summary>
Expand Down Expand Up @@ -236,8 +237,7 @@ public List<SearchResult> MaximizeResults<T>(Func<SearchResult, T> property)
return res;
}


[CanBeNull]

public ImageResult FindDirectResult() => FindDirectResults().FirstOrDefault();

public ImageResult[] FindDirectResults(int count = 5)
Expand Down
12 changes: 9 additions & 3 deletions SmartImage.Lib/Searching/SearchResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public enum ResultStatus
Extraneous
}



/// <summary>
/// Describes a search result
Expand Down Expand Up @@ -88,7 +87,15 @@ public class SearchResult : IViewable
/// If filtering is enabled (i.e., <see cref="SearchConfig.Filtering"/> is <c>true</c>), this determines whether the
/// result is filtered.
/// </summary>
public bool IsNonPrimitive => Status != ResultStatus.Extraneous && PrimaryResult.Url != null;
public bool IsNonPrimitive
{
get
{
//return (Status != ResultStatus.Extraneous && PrimaryResult.Url != null) || ErrorMessage!=null;
return (Status != ResultStatus.Extraneous && PrimaryResult.Url != null);

}
}


public bool IsSuccessful
Expand Down Expand Up @@ -128,7 +135,6 @@ public void Consolidate()

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



public Dictionary<string, object> View
{
Expand Down
3 changes: 2 additions & 1 deletion SmartImage.Lib/Utilities/ImageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ public static bool IsImage(string s, int d = TimeoutMS)
}

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

return a && b;
}
Expand Down
9 changes: 9 additions & 0 deletions UnitTest/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public void TestImageQuery()
Assert.DoesNotThrow(() => new ImageQuery("https://i.imgur.com/QtCausw.png"));
}

[Test]
[TestCase("https://i.imgur.com/QtCausw.png", true)]
[TestCase("https://twitter.com/sciamano240/status/1186775807655587841", false)]
public void TestImageHelper(string s, bool b)
{
Assert.AreEqual(ImageHelper.IsImage(s),b);

}

[Test]
public void TestResolutionType()
{
Expand Down

0 comments on commit bd76034

Please sign in to comment.