Skip to content

Commit

Permalink
Additional result info, SauceNao rework
Browse files Browse the repository at this point in the history
  • Loading branch information
Decimation committed Jan 16, 2021
1 parent e93810d commit 318a676
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 109 deletions.
19 changes: 13 additions & 6 deletions SmartImage/Engines/Other/IqdbEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,31 @@ private struct IqdbResult : ISearchResult

public string? Caption { get; set; }

public string Source { get; }
public string? Source { get; set; }

public int? Width { get; set; }

public int? Height { get; set; }

public string Url { get; set; }

public float? Similarity { get; set; }
public float? Similarity { get; set; }
public string? Artist { get; set; }
public string? Characters { get; set; }
public string? SiteName { get; set; }

public IqdbResult(string caption, string source, string url, int width, int height, float? similarity)
public IqdbResult(string siteName, string source, string url, int width, int height, float? similarity)
{
Caption = caption;
SiteName = siteName;
Url = url;
Source = source;
Width = width;
Height = height;
Similarity = similarity;
Filter = false; // set later
Artist = null;
Characters = null;
Caption = null;
}
}

Expand Down Expand Up @@ -105,7 +111,7 @@ private IqdbResult ParseResult(HtmlNodeCollection tr)
}


var i = new IqdbResult(caption.InnerText, src.InnerText, url, w, h, sim);
var i = new IqdbResult(src.InnerText, null, url, w, h, sim);
i.Filter = i.Similarity < FilterThreshold;
return i;
}
Expand Down Expand Up @@ -153,7 +159,8 @@ public override FullSearchResult GetResult(string url)
sr.Url = best.Url;
sr.Similarity = best.Similarity;
sr.Filter = best.Filter;

sr.Source = best.Source;
sr.SiteName = best.SiteName;
sr.AddExtendedResults(images.ToArray());


Expand Down
8 changes: 8 additions & 0 deletions SmartImage/Engines/Other/YandexEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ private struct YandexResult : ISearchResult

public string Url { get; set; }

public string? Artist { get; set; }
public string? Source { get; set; }
public string? Characters { get; set; }
public string? SiteName { get; set; }

internal YandexResult(int width, int height, string url)
{
Expand All @@ -43,6 +47,10 @@ internal YandexResult(int width, int height, string url)
Caption = null;
Similarity = null;
Filter = false;
Artist = null;
Source = null;
Characters = null;
SiteName=null;
}

public override string ToString()
Expand Down
35 changes: 3 additions & 32 deletions SmartImage/Engines/SauceNao/SauceNaoDataResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,36 @@

namespace SmartImage.Engines.SauceNao
{
[DataContract]
public class SauceNaoDataResult
{
/// <summary>
/// The url(s) where the source is from. Multiple will be returned if the exact same image is found in multiple places
/// </summary>
[DataMember(Name = "ext_urls")]
public string[] Url { get; internal set; }
public string[] Urls { get; internal set; }

/// <summary>
/// The search index of the image
/// </summary>
[DataMember(Name = "index_id")]
public SauceNaoSiteIndex Index { get; internal set; }

/// <summary>
/// How similar is the image to the one provided (Percentage)?
/// </summary>
[DataMember(Name = "similarity")]
public float Similarity { get; internal set; }

/// <summary>
/// A link to the thumbnail of the image
/// </summary>
[DataMember(Name = "thumbnail")]
public string Thumbnail { get; internal set; }

/// <summary>
/// The name of the website it came from
/// </summary>
[IgnoreDataMember]
public string WebsiteName { get; internal set; }

/// <summary>
/// How explicit is the image?
/// </summary>
[IgnoreDataMember]
public float Rating { get; internal set; }

[IgnoreDataMember]
public string WebsiteTitle { get; set; }

[IgnoreDataMember]
public string Character { get; internal set; }

[IgnoreDataMember]

public string Material { get; internal set; }

[IgnoreDataMember]

public string Creator { get; internal set; }


public override string ToString()
{
string firstUrl = Url != null ? Url[0] : "-";
string firstUrl = Urls != null ? Urls[0] : "-";

return String.Format("{0} ({1}, {2})", firstUrl, Similarity, Index);
return $"{firstUrl} ({Similarity}, {Index})";
}
}
}
141 changes: 81 additions & 60 deletions SmartImage/Engines/SauceNao/SauceNaoEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,29 @@ private struct SauceNaoSimpleResult : ISearchResult
public float? Similarity { get; set; }
public int? Width { get; set; }
public int? Height { get; set; }
public string? Artist { get; set; }
public string? Source { get; set; }
public string? Characters { get; set; }
public string? SiteName { get; set; }


public SauceNaoSimpleResult(string? title, string url, float? similarity)
public SauceNaoSimpleResult(string? title, string url, float? similarity, string? artist, string? source,
string? characters, string? siteName)
{
Caption = title;
Url = url;
Similarity = similarity;
Width = null;
Height = null;
Filter = false; //set later

Artist = artist;
Source = source;
Characters = characters;
SiteName = siteName;
}

public override string ToString()
{
return $"{Caption} {Url} {Similarity}";
return $"{Url} {Similarity}";
}
}

Expand All @@ -91,45 +98,23 @@ private ISearchResult[] ConvertResults(SauceNaoDataResult[] results)
var rg = new List<ISearchResult>();

foreach (var sn in results) {
if (sn.Url != null) {
var url = sn.Url.FirstOrDefault(u => u != null);
var x = new SauceNaoSimpleResult(sn.WebsiteTitle, url, sn.Similarity);
if (sn.Urls != null) {
var url = sn.Urls.FirstOrDefault(u => u != null);
var name = sn.Index.ToString();

var x = new SauceNaoSimpleResult(sn.WebsiteTitle, url,
sn.Similarity, sn.Creator, sn.Material, sn.Character,name);

x.Filter = x.Similarity < FilterThreshold;



x.Caption = AddCaption(sn);
rg.Add(x);
}
}

return rg.ToArray();
}

private static string? AddCaption(SauceNaoDataResult x)
{
var sb = new StringBuilder();

if (x.Character != null)
{
sb.Append($"Characters: {x.Character}");
}
if (x.Creator != null)
{
sb.Append($" Creator: {x.Creator}");
}
if (x.Material != null)
{
sb.Append($" Material: {x.Material}");
}

if (sb.Length == 0) {
return null;
}

return sb.ToString();
}


public override FullSearchResult GetResult(string url)
{
Expand All @@ -138,17 +123,19 @@ public override FullSearchResult GetResult(string url)
try {
var orig = GetResults(url);

// todo - aggregate all info for primary result

var character = orig.FirstOrDefault(o => o.Character != null)?.Character;
var creator = orig.FirstOrDefault(o => o.Creator != null)?.Creator;
var material = orig.FirstOrDefault(o => o.Material != null)?.Material;
if (orig == null) {
return result;
}

// todo - bad practice
var dummy = new SauceNaoDataResult() {Character = character, Creator = creator, Material = material};
// todo - aggregate all info for primary result

result.Caption = AddCaption(dummy);
var character = orig.FirstOrDefault(o => o.Character != null)?.Character;
var creator = orig.FirstOrDefault(o => o.Creator != null)?.Creator;
var material = orig.FirstOrDefault(o => o.Material != null)?.Material;

result.Characters = character;
result.Artist = creator;
result.Source = material;

var extended = ConvertResults(orig);

Expand All @@ -162,7 +149,6 @@ public override FullSearchResult GetResult(string url)
// Copy
result.Url = best.Url;
result.Similarity = best.Similarity;

result.Filter = best.Filter;


Expand All @@ -185,7 +171,7 @@ public override FullSearchResult GetResult(string url)
}


private SauceNaoDataResult[] GetResults(string url)
private SauceNaoDataResult[]? GetResults(string url)
{

var req = new RestRequest();
Expand All @@ -203,7 +189,7 @@ private SauceNaoDataResult[] GetResults(string url)
}


private static SauceNaoDataResult[] ReadResults(string js)
private static SauceNaoDataResult[]? ReadResults(string js)
{
// todo: rewrite this using Newtonsoft

Expand All @@ -224,30 +210,32 @@ private static SauceNaoDataResult[] ReadResults(string js)
}

string json = jsonArray.ToString();
var jsoncpy = json;

json = json.Insert(json.Length - 1, "}").Insert(0, "{\"results\":");

using var stream =
JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(json),
XmlDictionaryReaderQuotas.Max);
var jsonCpy = json;

// json = json.Insert(json.Length - 1, "}").Insert(0, "{\"results\":");
//
// using var stream =
// JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(json),
// XmlDictionaryReaderQuotas.Max);
//
//
// var serializer = new DataContractJsonSerializer(typeof(SauceNaoDataResponse));
// var result = serializer.ReadObject(stream) as SauceNaoDataResponse;

var serializer = new DataContractJsonSerializer(typeof(SauceNaoDataResponse));
var result = serializer.ReadObject(stream) as SauceNaoDataResponse;
var buffer = new List<SauceNaoDataResult>();

// todo - crappy solution
var res2 = JsonArray.Parse(jsoncpy);

var res2 = JsonArray.Parse(jsonCpy);
Debug.WriteLine(res2.Count);


stream.Dispose();
//stream.Dispose();

if (result is null)
return null;
//if (result is null)
// return null;

for (int index = 0; index < result.Results.Length; index++) {
/*for (int index = 0; index < result.Results.Length; index++) {
var t = result.Results[index];
var t2 = res2[index];
t.WebsiteTitle = Strings.SplitPascalCase(t.Index.ToString());
Expand All @@ -258,7 +246,40 @@ private static SauceNaoDataResult[] ReadResults(string js)
t.Material = t2.ContainsKey("material") ? t2["material"].ToString() : null;
}
return result.Results;
return result.Results;*/

for (int i = 0; i < res2.Count; i++) {
var t2 = res2[i];

var s = float.Parse(t2["similarity"]);
string[] strings;

if (t2.ContainsKey("ext_urls")) {
strings = (t2["ext_urls"] as JsonArray).Select(j => j.ToString()).ToArray();
}
else {
strings = null;
}


var sauceNaoSiteIndex = (SauceNaoSiteIndex) int.Parse(t2["index_id"].ToString());

var t = new SauceNaoDataResult
{
Urls = strings,
Similarity = s,
Index = sauceNaoSiteIndex,
Creator = t2.ContainsKey("creator") ? t2["creator"].ToString().CleanString() : null,
Character = t2.ContainsKey("characters") ? t2["characters"].ToString().CleanString() : null,
Material = t2.ContainsKey("material") ? t2["material"].ToString().CleanString() : null,
};


buffer.Add(t);
}

return buffer.ToArray();

}

return null;
Expand Down
4 changes: 2 additions & 2 deletions SmartImage/Engines/TraceMoe/TraceMoeEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private ISearchResult[] ConvertResults(TraceMoeRootObject obj)

results[i] = new FullSearchResult(this, malUrl, sim)
{
Caption = doc.title_english
Source = doc.title_english
};
}

Expand Down Expand Up @@ -80,7 +80,7 @@ public override FullSearchResult GetResult(string url)
var best = results[0];

r = new FullSearchResult(this, best.Url, best.Similarity);
r.Caption = best.Caption;
r.Source = best.Source;
r.Filter = r.Similarity < FilterThreshold;


Expand Down
Loading

0 comments on commit 318a676

Please sign in to comment.