Skip to content

Commit

Permalink
Fixes; updates; refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Decimation committed Jun 8, 2021
1 parent 92cbb05 commit 13f2f3a
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 116 deletions.
13 changes: 2 additions & 11 deletions SmartImage.Lib/Engines/BaseSearchEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,15 @@ public async Task<SearchResult> GetResultAsync(ImageQuery query)
var task = Task.Run(delegate
{
Debug.WriteLine($"{Name}: getting result async",C_INFO);
var sw = Stopwatch.StartNew();

var res = GetResult(query);

sw.Stop();
var res = GetResult(query);

Debug.WriteLine($"{Name}: result done {sw.Elapsed.TotalSeconds}",C_SUCCESS);
Debug.WriteLine($"{Name}: result done",C_SUCCESS);

return res;
});

return await task;

//if (!task.Wait(span)) TokenSource.Cancel();

//await task.AwaitWithTimeout(10, () => { }, () => { Debug.WriteLine($"cancel {Name}"); });

//return task.Result;
}

public Uri GetRawResultUrl(ImageQuery query)
Expand Down
15 changes: 10 additions & 5 deletions SmartImage.Lib/Engines/Impl/IqdbEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@ private static ImageResult ParseResult(IHtmlCollection<IElement> tr)

string url = null!;

//img.ChildNodes[0].ChildNodes[0].TryGetAttribute("href")

try {
url = src.FirstChild.ChildNodes[2].ChildNodes[0].TryGetAttribute("href");

try
{
//url = src.FirstChild.ChildNodes[2].ChildNodes[0].TryGetAttribute("href");

url = img.ChildNodes[0].ChildNodes[0].TryGetAttribute("href");

// Links must begin with http:// in order to work with "start"
if (url.StartsWith("//")) {
url = "http:" + url;
}
//if (url.StartsWith("//")) {
// url = "http:" + url;
//}
}
catch {
// ignored
Expand Down
65 changes: 26 additions & 39 deletions SmartImage.Lib/Engines/InterpretedSearchEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
using SimpleCore.Net;
using SmartImage.Lib.Searching;
using SmartImage.Lib.Utilities;
using static SimpleCore.Diagnostics.LogCategories;

namespace SmartImage.Lib.Engines
{
/// <summary>
/// Represents a search engine whose results are parsed.
/// </summary>
public abstract class InterpretedSearchEngine : BaseSearchEngine
{
public abstract override SearchEngineOptions Engine { get; }
Expand All @@ -22,63 +26,46 @@ public abstract class InterpretedSearchEngine : BaseSearchEngine
protected InterpretedSearchEngine(string baseUrl) : base(baseUrl) { }


protected virtual IDocument GetDocument(SearchResult sr)
{
/*if (!Network.TryGetString(sr.RawUri.ToString()!, out var html))
{
sr.RawUri = null;
sr.PrimaryResult.Url = null;
//sr.AddErrorMessage("Unavailable");
throw new SmartImageException();
}*/


string response = Network.GetString(sr.RawUri.ToString()!);

//var doc = new HtmlWeb().Load(sr.RawUri);
var p = new HtmlParser();
return p.ParseDocument(response);

//var response = Network.GetSimpleResponse(sr.RawUri.ToString()!);

//var req = new RestRequest(BaseUrl);
//req.AddQueryParameter("url", sr.RawUri.ToString());
//var rc = new RestClient();
//var res =rc.Execute(req);
//var response = res.Content;

//var doc = new HtmlDocument();
//doc.LoadHtml(response.Content);
//doc.LoadHtml(response);


}

protected abstract SearchResult Process(IDocument doc, SearchResult sr);

[DebuggerHidden]
public override SearchResult GetResult(ImageQuery query)
{

var sr = base.GetResult(query);

if (!sr.IsSuccessful) {
return sr;
}

try {

var doc = GetDocument(sr);

sr = Process(doc, sr);
}
catch (Exception e) {
sr.Status = ResultStatus.Failure;
Trace.WriteLine($"{Name}: {e.Message} {e.Source} {e.StackTrace}");

Trace.WriteLine($"{Name}: {e.Message} {e.Source} {e.StackTrace}", C_ERROR);
}

return sr;
}

protected virtual IDocument GetDocument(SearchResult sr)
{
/*if (!Network.TryGetString(sr.RawUri.ToString()!, out var html))
{
sr.RawUri = null;
sr.PrimaryResult.Url = null;
//sr.AddErrorMessage("Unavailable");
throw new SmartImageException();
}*/


string response = Network.GetString(sr.RawUri.ToString()!);

var parser = new HtmlParser();
return parser.ParseDocument(response);
}

protected abstract SearchResult Process(IDocument doc, SearchResult sr);
}
}
19 changes: 11 additions & 8 deletions SmartImage.Lib/SearchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ImageResult[] FindBestResults(int n)
//todo: WIP
Debug.WriteLine($"Finding best results");

var best = Results.Where(r => r.Status != ResultStatus.Extraneous && !r.IsPrimitive)
var best = Results.Where(r => r.IsNonPrimitive)
.SelectMany(r =>
{
var x = r.OtherResults;
Expand Down Expand Up @@ -87,7 +87,7 @@ public List<SearchResult> MaximizeResults<T>(Func<SearchResult, T> property)

var res = Results.OrderByDescending(property).ToList();

res.RemoveAll(r => r.IsPrimitive);
res.RemoveAll(r => !r.IsNonPrimitive);

return res;
}
Expand Down Expand Up @@ -130,12 +130,15 @@ public async Task RunSearchAsync()
var value = await finished;

tasks.Remove(finished);

Results.Add(value);

// Call event
ResultCompleted?.Invoke(null, new SearchResultEventArgs(value));


if (!(Config.Filter && !value.IsNonPrimitive)) {
Results.Add(value);

// Call event
ResultCompleted?.Invoke(null, new SearchResultEventArgs(value));
}


IsComplete = !tasks.Any();
}

Expand Down
8 changes: 7 additions & 1 deletion SmartImage.Lib/SearchConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@

namespace SmartImage.Lib
{
/// <summary>
/// Contains configuration for <see cref="SearchClient"/>
/// </summary>
public sealed class SearchConfig
{
public ImageQuery Query { get; set; }

public SearchEngineOptions SearchEngines { get; set; } = SearchEngineOptions.All;

public SearchEngineOptions PriorityEngines { get; set; } //todo
public SearchEngineOptions PriorityEngines { get; set; }

public bool Filter { get; set; } = true;

}
}
15 changes: 10 additions & 5 deletions SmartImage.Lib/Searching/ImageResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,11 @@ public void UpdateFrom(ImageResult result)
Description = result.Description;
Date = result.Date;
}
public override string ToString()

public string ToString(bool indent)
{
var sb = new ExtendedStringBuilder() {};

var sb = new ExtendedStringBuilder() { };

sb.Append(nameof(Url), Url);

Expand All @@ -189,7 +190,7 @@ public override string ToString()
}

if (HasImageDimensions) {
string? val = $"{Width}x{Height} ({MegapixelResolution:F} MP)";
string val = $"{Width}x{Height} ({MegapixelResolution:F} MP)";


var resType = DisplayResolution;
Expand All @@ -215,7 +216,11 @@ public override string ToString()

sb.Append($"Detail score", $"{DetailScore}/{DetailFields.Count} ({(IsDetailed ? "Y" : "N")})");

return sb.ToString().RemoveLastOccurrence("\n");
var xs = sb.ToString().RemoveLastOccurrence("\n");

return indent ? Strings.IndentFields(xs) : xs;
}

public override string ToString() => ToString(false);
}
}
18 changes: 7 additions & 11 deletions SmartImage.Lib/Searching/SearchResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,8 @@ public class SearchResult
[CanBeNull]
public string ErrorMessage { get; set; }

public bool IsPrimitive
{
get
{
//todo: WIP
return PrimaryResult.Url == null;
}
}
public bool IsNonPrimitive => Status != ResultStatus.Extraneous && (PrimaryResult.Url != null);


public bool IsSuccessful
{
Expand Down Expand Up @@ -85,11 +79,13 @@ public override string ToString()

var name = $"[{Engine.Name}]";

sb.AppendLine($"{name} :: ({Status}; {(IsPrimitive ? RANK_P : RANK_S)})");
sb.AppendLine($"{name} :: ({Status}; {(!IsNonPrimitive ? RANK_P : RANK_S)})");

if (PrimaryResult.Url != null) {
var resStr = sb.IndentFields(PrimaryResult.ToString());
string separator = sb.Indent + new string('-', 20);
//var resStr = sb.IndentFields(PrimaryResult.ToString());

var resStr = PrimaryResult.ToString(true);
string separator = Strings.Indent + new string('-', 20);

sb.Append($"{resStr}\n{separator}\n");
}
Expand Down
7 changes: 5 additions & 2 deletions SmartImage.Lib/Utilities/ImageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public static DisplayResolutionType GetDisplayResolution(int w, int h)

public static string[] Scan(string s)
{

// TODO: WIP

//<img.*?src="(.*?)"
//href\s*=\s*"(.+?)"

Expand Down Expand Up @@ -130,7 +133,7 @@ public static string[] Scan(string s)
}


public static string ResolveDirectLink(string s)
/*public static string ResolveDirectLink(string s)
{
//todo: WIP
string d = "";
Expand Down Expand Up @@ -178,7 +181,7 @@ public static string ResolveDirectLink(string s)
return d;
}
}*/
}

public enum DisplayResolutionType
Expand Down
4 changes: 2 additions & 2 deletions SmartImage.UI/Form.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void HandleResult(object o, SearchResultEventArgs args)
{
var searchResult = args.Result;

if (searchResult.IsPrimitive && filterCheckBox.Checked) {
if (!searchResult.IsNonPrimitive && filterCheckBox.Checked) {
searchProgressBar.PerformStep();
return;
}
Expand Down Expand Up @@ -146,7 +146,7 @@ private void AddSearchResult(SearchResult searchResult, ListViewGroup listViewGr
var imageResult = searchResult.PrimaryResult;

if (searchResult.IsSuccessful) {
listViewItem.SubItems.Add(searchResult.IsPrimitive
listViewItem.SubItems.Add(!searchResult.IsNonPrimitive
? searchResult.RawUri.ToString()
: imageResult.Url?.ToString());
}
Expand Down
Loading

0 comments on commit 13f2f3a

Please sign in to comment.