Skip to content

Commit

Permalink
Search server; other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Decimation committed Dec 12, 2024
1 parent c726734 commit 9074746
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 38 deletions.
4 changes: 2 additions & 2 deletions SmartImage.Lib/Clients/HydrusClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ public void Dispose()

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
protected virtual void OnPropertyChanged([CMN] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
protected bool SetField<T>(ref T field, T value, [CMN] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;

Expand Down
5 changes: 4 additions & 1 deletion SmartImage.Lib/Engines/BaseSearchEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
namespace SmartImage.Lib.Engines;
#nullable enable


public abstract class BaseSearchEngine : IDisposable, IEquatable<BaseSearchEngine>
{

Expand Down Expand Up @@ -53,6 +52,7 @@ protected BaseSearchEngine(Url baseUrl, Url? endpoint = null)
/// <summary>
/// The corresponding <see cref="SearchEngineOptions" /> of this engine
/// </summary>
[JI]
public abstract SearchEngineOptions EngineOption { get; }

/// <summary>
Expand All @@ -62,12 +62,15 @@ protected BaseSearchEngine(Url baseUrl, Url? endpoint = null)

public virtual Url BaseUrl { get; }

[JI]
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(15);

public Url? EndpointUrl { get; }

[JI]
protected long? MaxSize { get; set; }

[JI]
protected virtual string[] ErrorBodyMessages { get; } = [];

protected static FlurlClient Client { get; }
Expand Down
4 changes: 2 additions & 2 deletions SmartImage.Lib/Images/Uni/UniImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ protected string WriteStreamToFile(string fn = null)

public virtual void Dispose()
{
Debug.WriteLine($"Disposing {ValueString} w/ {Size}");
Trace.WriteLine($"Disposing {ValueString} w/ {Size}");
Stream?.Dispose();
Image?.Dispose();

Expand All @@ -357,7 +357,7 @@ public virtual void Dispose()

public virtual async ValueTask DisposeAsync()
{
Debug.WriteLine($"Disposing {ValueString} w/ {Size}");
Trace.WriteLine($"Disposing {ValueString} w/ {Size}");

if (Stream != null)
await Stream.DisposeAsync();
Expand Down
10 changes: 6 additions & 4 deletions SmartImage.Lib/Results/SearchResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class SearchResult : IDisposable, INotifyPropertyChanged
/// <summary>
/// Engine which returned this result
/// </summary>
[JI]
public BaseSearchEngine Engine { get; }

// todo: make the engine reference weak
Expand All @@ -84,6 +85,7 @@ public class SearchResult : IDisposable, INotifyPropertyChanged
/// </summary>
public Url RawUrl { get; internal set; }

[JI]
public bool HasResults
{
get
Expand All @@ -96,7 +98,7 @@ public bool HasResults
public bool IsSuccessful => Status.IsSuccessful();

/// <summary>
/// Results; first element should be <see cref="GetRawResultItem"/>
/// Results; first element should be <see cref="RawResultItem"/>
/// </summary>
[NN]
public List<SearchResultItem> Results { get; }
Expand Down Expand Up @@ -143,12 +145,12 @@ public void Update()

public event PropertyChangedEventHandler PropertyChanged;

private void OnPropertyChanged([CallerMemberName] string propertyName = null)
private void OnPropertyChanged([CMN] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

private bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
private bool SetField<T>(ref T field, T value, [CMN] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value))
return false;
Expand All @@ -171,7 +173,7 @@ public SearchResultItem GetBestResult()
.FirstOrDefault(static r => Url.IsValid(r.Url));
}


[JI]
public SearchResultItem RawResultItem
{
get
Expand Down
21 changes: 14 additions & 7 deletions SmartImage.Lib/SearchClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
global using JI = System.Text.Json.Serialization.JsonIgnoreAttribute;
global using CMN = System.Runtime.CompilerServices.CallerMemberNameAttribute;
global using JI = System.Text.Json.Serialization.JsonIgnoreAttribute;
global using ICBN = JetBrains.Annotations.ItemCanBeNullAttribute;
global using INN = JetBrains.Annotations.ItemNotNullAttribute;
using System.Collections;
Expand Down Expand Up @@ -111,7 +112,7 @@ public void OpenChannel()
/// </summary>
/// <param name="query">Search query</param>
/// <param name="scheduler"></param>
/// <param name="token">Cancellation token passed to <see cref="BaseSearchEngine.GetResultAsync"/></param>
/// <param name="token">Cancellation token passed to <see cref="WebSearchEngine{T}.GetResultAsync(SmartImage.Lib.SearchQuery,System.Threading.CancellationToken)"/></param>
public async Task<SearchResult[]> RunSearchAsync(SearchQuery query,
TaskScheduler scheduler = default,
CancellationToken token = default)
Expand Down Expand Up @@ -172,11 +173,7 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query,

try {

var ordered = results.Select(x => x.GetBestResult())
.Where(x => x != null)
.OrderByDescending(x => x.Similarity);

var item = ordered.FirstOrDefault();
SearchResultItem item = GetBest(results);

if (item != null) {
OpenResult(item.Url);
Expand All @@ -195,6 +192,16 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query,
return results;
}

public static SearchResultItem GetBest(SearchResult[] results)
{
var ordered = results.Select(x => x.GetBestResult())
.Where(x => x != null)
.OrderByDescending(x => x.Similarity);

var item = ordered.FirstOrDefault();
return item;
}

private void CompleteSearchAsync()
{
ResultChannel?.Writer.Complete();
Expand Down
26 changes: 13 additions & 13 deletions SmartImage.Lib/SearchConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public SearchEngineOptions SearchEngines
public SearchEngineOptions PriorityEngines
{
get => Get(PE_DEFAULT);
set { Set(value); }
set => Set(value);
}

/// <summary>
Expand All @@ -82,7 +82,7 @@ public SearchEngineOptions PriorityEngines
public bool OnTop
{
get => Get(ON_TOP_DEFAULT);
set { Set(value); }
set => Set(value);
}

/*
Expand Down Expand Up @@ -116,25 +116,25 @@ public string HydrusKey
public bool OpenRaw
{
get => Get(false);
set { Set(value); }
set => Set(value);
}

public bool Silent
{
get => Get(false);
set { Set(value); }
set => Set(value);
}

public bool Clipboard
{
get => Get(true);
set { Set(value); }
set => Set(value);
}

public bool AutoSearch
{
get => Get(false);
set { Set(value); }
set => Set(value);
}

/// <summary>
Expand All @@ -143,7 +143,7 @@ public bool AutoSearch
public string SauceNaoKey
{
get => Get(String.Empty);
set { Set(value); }
set => Set(value);
}

/// <summary>
Expand All @@ -156,7 +156,7 @@ public string SauceNaoKey
public bool ReadCookies
{
get => Get(READCOOKIES_DEFAULT);
set { Set(value); }
set => Set(value);
}


Expand All @@ -168,7 +168,7 @@ public bool ReadCookies
public bool FlareSolverr
{
get => Get(FLARESOLVERR_DEFAULT);
set { Set(value); }
set => Set(value);
}


Expand All @@ -178,7 +178,7 @@ public bool FlareSolverr
public string FlareSolverrApiUrl
{
get => Get(FLARE_SOLVERR_API_URL_DEFAULT);
set { Set(value); }
set => Set(value);
}

/// <summary>
Expand All @@ -203,14 +203,14 @@ public SearchConfig()
public static readonly Configuration Configuration =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

private bool Set<T>(T s = default, [CallerMemberName] string name = default)
private bool Set<T>(T s = default, [CMN] string name = default)
{
bool b = Configuration.AddUpdateSetting(name, s.ToString());
OnPropertyChanged(name);
return b;
}

private T Get<T>(T t = default, [CallerMemberName] string name = default)
private T Get<T>(T t = default, [CMN] string name = default)
{
T v = Configuration.ReadSetting(name, t);
return v;
Expand Down Expand Up @@ -252,7 +252,7 @@ public void Save()

public event PropertyChangedEventHandler PropertyChanged;

private void OnPropertyChanged([CallerMemberName] string propertyName = null)
private void OnPropertyChanged([CMN] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
Expand Down
1 change: 1 addition & 0 deletions SmartImage.Lib/SearchQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public async Task<Url> UploadAsync(BaseUploadEngine engine = null, CancellationT

public void Dispose()
{
Trace.WriteLine($"Disposing {Source}");
Source?.Dispose();
}

Expand Down
55 changes: 51 additions & 4 deletions SmartImage.Lib/SearchServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Kantan.Net;
using Kantan.Net.Utilities;
using SmartImage.Lib.Images;
using SmartImage.Lib.Results;
using SmartImage.Lib.Utilities;

namespace SmartImage.Lib;
Expand All @@ -21,7 +22,14 @@ public class SearchServer : IDisposable

public static readonly JsonSerializerOptions Options2 = new(HttpUtilities.Options)
{
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters =
{
new UrlTypeConverter(),
new BaseSearchEngineTypeConverter(),
// new SearchResultTypeConverter(),
}

};

Expand All @@ -38,7 +46,8 @@ public SearchServer(SearchClient client, int port)

Handlers = new RouteCallbackMap()
{
["search"] = HandleRequestAsync
["search"] = HandleRequestAsync,

};

var uriPrefix = $"http://*:{port}/";
Expand All @@ -51,7 +60,11 @@ private async Task<object> HandleRequestAsync(HttpListenerRequest request, HttpL
{
object ok;

var redirHdr = request.Headers["redirect"];


try {

var sz = await request.ReadRequestStringAsync();

if (String.IsNullOrWhiteSpace(sz)) {
Expand All @@ -70,14 +83,30 @@ private async Task<object> HandleRequestAsync(HttpListenerRequest request, HttpL

var results = await Client.RunSearchAsync(sq);

var allResults = results.SelectMany(x => x.Results).ToArray();
var ok1 = await response.WriteResponseJsonAsync(allResults);
var best = SearchClient.GetBest(results);

var allResults = new SearchResults(results, best)
{ };

var bytes = JsonSerializer.Serialize(allResults, Options2);
var rg = Listener.Encoding.GetBytes(bytes);

var ok1 = await response.WriteResponseDataAsync(rg);

ok = ok1;

/*
var json = JsonSerializer.Serialize(allResults, Options2);
ok = await response.WriteResponseStringAsync(json);
*/
*/

if (!String.IsNullOrWhiteSpace(redirHdr)) {
response.Redirect(allResults.Best.Url);
}

response.OutputStream.Close();
response.Close();

}
catch (IOException io) {
Trace.WriteLine($"{io}");
Expand All @@ -88,6 +117,24 @@ private async Task<object> HandleRequestAsync(HttpListenerRequest request, HttpL
return ok;
}

public class SearchResults
{

[JsonPropertyOrder(0)]
[MN]
public SearchResultItem Best { get; internal set; }

[JsonPropertyOrder(1)]
public SearchResult[] Results { get; }

public SearchResults(SearchResult[] results, SearchResultItem best)
{
Best = best;
Results = results;
}

}

public Task StartAsync(CancellationToken ct = default)
{
return Listener.StartAsync(ct);
Expand Down
Loading

0 comments on commit 9074746

Please sign in to comment.