Skip to content

Commit fdea60d

Browse files
committed
Handle trace.moe deserialization edge case
1 parent f1a4d91 commit fdea60d

File tree

2 files changed

+53
-28
lines changed

2 files changed

+53
-28
lines changed

SmartImage.Lib/Engines/Search/TraceMoeEngine.cs

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Diagnostics;
45
using System.Linq;
56
using Flurl.Http;
67
using JetBrains.Annotations;
8+
using Kantan.Collections;
9+
using Kantan.Text;
710
using Newtonsoft.Json;
811
using Newtonsoft.Json.Linq;
912
using RestSharp.Serialization.Json;
1013
using SmartImage.Lib.Clients;
1114
using SmartImage.Lib.Engines.Search.Base;
1215
using SmartImage.Lib.Searching;
1316
using static Kantan.Diagnostics.LogCategories;
17+
using JsonSerializer = Newtonsoft.Json.JsonSerializer;
1418

1519
// ReSharper disable InconsistentNaming
1620
#pragma warning disable IDE1006, IDE0051
@@ -54,45 +58,54 @@ protected override SearchResult Process(object obj, SearchResult r)
5458
.SetQueryParam("url",
5559
query.UploadUri.ToString(),
5660
true);
57-
5861
var task = request.GetStringAsync();
59-
6062
task.Wait(Timeout);
61-
var result = task.Result;
62-
63-
/*JObject jo;
64-
65-
jo = JObject.Parse(result);
66-
var token = jo["result"];
67-
68-
Debug.WriteLine(result);
69-
var rg = JsonConvert.DeserializeObject<List<TraceMoeDoc>>(token.ToString());
70-
71-
tm = new();
72-
tm.result = rg.ToList();
73-
tm.error = jo["error"].ToString();
74-
tm.frameCount = long.Parse(jo["frameCount"].ToString());*/
63+
var json = task.Result;
7564

76-
77-
// tm = JsonConvert.DeserializeObject<TraceMoeRootObject>(result);
78-
7965
var settings = new JsonSerializerSettings
8066
{
8167
Error = (sender, args) =>
8268
{
83-
if (object.Equals(args.ErrorContext.Member, nameof(TraceMoeDoc.episode)) &&
84-
args.ErrorContext.OriginalObject.GetType() == typeof(TraceMoeDoc))
85-
{
69+
70+
if (object.Equals(args.ErrorContext.Member, nameof(TraceMoeDoc.episode)) /*&&
71+
args.ErrorContext.OriginalObject.GetType() == typeof(TraceMoeRootObject)*/) {
8672
args.ErrorContext.Handled = true;
73+
8774
}
75+
76+
Debug.WriteLine($"{args.ErrorContext}");
8877
}
8978
};
90-
tm = JsonConvert.DeserializeObject<TraceMoeRootObject>(result, settings);
9179

80+
/*var tm2t = request.GetJsonAsync();
81+
tm2t.Wait();
82+
var tm2 = tm2t.Result;
83+
84+
dynamic result = tm2.result;
85+
Debug.WriteLine($"{result}");
86+
tm = new() { result = new(), error = tm2.error, frameCount = tm2.frameCount };
9287
88+
for (int i = 0; i < result.Count; i++) {
89+
Debug.WriteLine($"{result[i]}");
90+
91+
var d = result[i];
92+
93+
var doc = new TraceMoeDoc()
94+
{
95+
episode = d.episode,
96+
from = d.from,
97+
to = d.to,
98+
filename = d.filename, anilist = d.anilist
99+
};
100+
101+
tm.result.Add(doc);
102+
}*/
103+
104+
105+
tm = JsonConvert.DeserializeObject<TraceMoeRootObject>(json, settings);
93106
}
94107
catch (Exception e) {
95-
Debug.WriteLine($"{e.Message}");
108+
Debug.WriteLine($">>>>{e.Message}");
96109

97110
goto ret;
98111
}
@@ -145,10 +158,22 @@ private IEnumerable<ImageResult> ConvertResults(TraceMoeRootObject obj, SearchRe
145158
var doc = docs[i];
146159
float sim = MathF.Round((float) (doc.similarity * 100.0f), 2);
147160

161+
var episode = doc.episode;
162+
163+
string epStr = episode.ToString();
164+
165+
if (episode is string) {
166+
epStr = episode as string;
167+
}
168+
else if (episode is IEnumerable enumerable) {
169+
var ls = enumerable.CopyToList().Select(x => Int64.Parse(x.ToString() ?? String.Empty));
170+
epStr = ls.QuickJoin();
171+
}
172+
148173
var result = new ImageResult(r)
149174
{
150175
Similarity = sim,
151-
Description = $"Episode #{doc.episode} @ {TimeSpan.FromSeconds(doc.from)}"
176+
Description = $"Episode #{epStr} @ {TimeSpan.FromSeconds(doc.from)}"
152177
};
153178

154179
try {
@@ -199,8 +224,8 @@ private class TraceMoeDoc
199224

200225

201226
/// <remarks>Episode field may contain multiple possible results delimited by <c>|</c></remarks>
202-
[JsonIgnore]
203-
public string episode { get; set; }
227+
// [JsonIgnore]
228+
public object episode { get; set; }
204229

205230
public double similarity { get; set; }
206231

SmartImage/SmartImage.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110

111111
<PropertyGroup>
112112
<PackageId>SmartImage</PackageId>
113-
<Version>2.2.2</Version>
113+
<Version>2.2.3</Version>
114114
<Authors>Read Stanton (Decimation)</Authors>
115115
<PackageTags>Image reverse search identification source sauce</PackageTags>
116116
<RepositoryUrl>https://github.com/Decimation/SmartImage</RepositoryUrl>

0 commit comments

Comments
 (0)