Skip to content

Commit 60dbea3

Browse files
committed
SauceNao API refactor
1 parent 159754a commit 60dbea3

File tree

9 files changed

+144
-65
lines changed

9 files changed

+144
-65
lines changed

SmartImage.Lib/Engines/Impl/SauceNaoEngine.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Diagnostics;
6+
using System.IO;
67
using System.Json;
78
using System.Linq;
89
using System.Net;
@@ -14,6 +15,7 @@
1415
using SimpleCore.Net;
1516
using SimpleCore.Utilities;
1617
using SmartImage.Lib.Searching;
18+
using SmartImage.Lib.Utilities;
1719
using static SimpleCore.Diagnostics.LogCategories;
1820
using JsonArray = System.Json.JsonArray;
1921
using JsonObject = System.Json.JsonObject;
@@ -55,14 +57,23 @@ public SauceNaoEngine() : this(String.Empty) { }
5557
public override SearchEngineOptions EngineOption => SearchEngineOptions.SauceNao;
5658

5759

58-
private static IEnumerable<SauceNaoDataResult> ParseResults(string url)
60+
private static IEnumerable<SauceNaoDataResult> ParseResults(ImageQuery query)
5961
{
6062
var docp = new HtmlParser();
6163

6264
var rc = new RestClient(BASE_URL);
63-
var req = new RestRequest("search.php");
64-
req.AddQueryParameter("url", url);
65+
var req = new RestRequest("search.php", Method.POST);
6566

67+
if (query.IsFile) {
68+
req.AddFile("file", File.ReadAllBytes(query.Value), "image.png");
69+
}
70+
else if (query.IsUri) {
71+
req.AddParameter("url", query, ParameterType.GetOrPost);
72+
}
73+
else {
74+
throw new SmartImageException();
75+
}
76+
6677
var execute = rc.Execute(req);
6778

6879
string html = execute.Content;
@@ -118,11 +129,11 @@ private static SauceNaoDataResult Parse(INode result)
118129

119130
string link = null;
120131

121-
var g = resultcontentcolumn.ChildNodes.GetElementsByTagName("a")
132+
var element = resultcontentcolumn.ChildNodes.GetElementsByTagName("a")
122133
.FirstOrDefault(x => x.GetAttribute("href") != null);
123134

124-
if (g != null) {
125-
link = g.GetAttribute("href");
135+
if (element != null) {
136+
link = element.GetAttribute("href");
126137
}
127138

128139
// //div[contains(@class, 'resulttitle')]
@@ -152,22 +163,20 @@ private static SauceNaoDataResult Parse(INode result)
152163
return dataResult;
153164
}
154165

155-
public override SearchResult GetResult(ImageQuery url)
166+
public override SearchResult GetResult(ImageQuery query)
156167
{
157-
var sresult = base.GetResult(url);
168+
var sresult = base.GetResult(query);
158169

159170
var result = new ImageResult();
160171

161172
try {
162-
var orig = GetDataResults(url.Image.ToString());
173+
var orig = GetDataResults(query.Image.ToString());
163174

164175
if (orig == null) {
165-
//return result;
166-
Debug.WriteLine($"[{Name}] Parsing HTML", LogCategories.C_INFO);
167-
168-
string urlStr = url.Image.ToString();
169176

170-
var sauceNaoDataResults = ParseResults(urlStr);
177+
Debug.WriteLine($"[{Name}] Parsing HTML", LogCategories.C_INFO);
178+
179+
var sauceNaoDataResults = ParseResults(query);
171180

172181
if (sauceNaoDataResults == null) {
173182
sresult.ErrorMessage = "Daily search limit (100) exceeded";

SmartImage.Lib/SearchClient.cs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ public async Task RunSearchAsync()
110110
bool isPriority = Config.PriorityEngines.HasFlag(value.Engine.EngineOption);
111111

112112
//
113-
// Filtering
113+
// Filtering
114114
// / \
115-
// true false
115+
// true false
116116
// / \
117-
// IsNonPrimitive [FilteredResults]
118-
// / \
119-
// true false
120-
// / \
121-
// [Results] [FilteredResults]
117+
// IsNonPrimitive [FilteredResults]
118+
// / \
119+
// true false
120+
// / \
121+
// [Results] [FilteredResults]
122122
//
123123

124124
if (Config.Filtering) {
@@ -206,21 +206,25 @@ public List<SearchResult> MaximizeResults<T>(Func<SearchResult, T> property)
206206

207207

208208
[CanBeNull]
209-
public ImageResult FindDirectResult() => FindDirectResults(1).FirstOrDefault();
209+
public ImageResult FindDirectResult() => FindDirectResults().FirstOrDefault();
210210

211211

212-
public ImageResult[] FindDirectResults(int n)
212+
public ImageResult[] FindDirectResults()
213213
{
214-
var best = FindBestResults(n);
214+
var best = FindBestResults();
215215

216-
217-
Task.WaitAll(best.Select(async delegate(ImageResult f)
216+
217+
best.AsParallel().ForAll(delegate(ImageResult f)
218218
{
219-
await f.FindDirectImagesAsync();
219+
f.FindDirectImagesAsync();
220+
});
221+
222+
//foreach (var result in best) {
223+
// result.FindDirectImagesAsync();
224+
//}
220225

221-
}).ToArray());
222226

223-
best = best.Take(n).ToArray();
227+
best = best.Where(f=>f.Direct!=null).ToArray();
224228

225229
//best = best
226230
// .AsParallel()
@@ -230,19 +234,15 @@ public ImageResult[] FindDirectResults(int n)
230234

231235
return best;
232236
}
233-
234-
/// <summary>
235-
/// Selects the <paramref name="n"/> most detailed results.
236-
/// </summary>
237-
/// <returns>The <see cref="ImageResult"/>s of the <paramref name="n"/> best <see cref="Results"/></returns>
237+
238238
[CanBeNull]
239-
public ImageResult FindBestResult() => FindBestResults(1).FirstOrDefault();
239+
public ImageResult FindBestResult() => FindBestResults().FirstOrDefault();
240240

241241
/// <summary>
242242
/// Selects the <paramref name="n"/> most detailed results.
243243
/// </summary>
244244
/// <returns>The <see cref="ImageResult"/>s of the <paramref name="n"/> best <see cref="Results"/></returns>
245-
public ImageResult[] FindBestResults(int n)
245+
public ImageResult[] FindBestResults()
246246
{
247247
var best = Results.Where(r => r.IsNonPrimitive)
248248
.SelectMany(r =>
@@ -257,11 +257,9 @@ public ImageResult[] FindBestResults(int n)
257257

258258
best = best
259259
.AsParallel()
260-
.Where(r => r.Url != null)
261260
.OrderByDescending(r => r.Similarity)
262261
.ThenByDescending(r => r.PixelResolution)
263-
.ThenByDescending(r => r.DetailScore)
264-
.Take(n);
262+
.ThenByDescending(r => r.DetailScore);
265263

266264

267265
return best.ToArray();

SmartImage.Lib/Searching/ImageResult.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public void UpdateFrom(ImageResult result)
190190

191191
}
192192

193-
public async Task FindDirectImagesAsync()
193+
public async void FindDirectImagesAsync()
194194
{
195195
if (Url is not null && Direct == null) {
196196

@@ -255,6 +255,7 @@ public Dictionary<string, object> View
255255
get
256256
{
257257
#pragma warning disable CS8604
258+
258259
var map = new Dictionary<string, object>
259260
{
260261
{nameof(Url), Url},

SmartImage.Lib/Utilities/ImageHelper.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static bool IsDirect2(string url)
102102
/// <summary>
103103
/// Scans for direct image links in <paramref name="url"/>
104104
/// </summary>
105-
public static async Task<string[]> FindDirectImagesAsync(string url)
105+
public static async Task<string[]> FindDirectImagesAsync2(string url)
106106
{
107107
var rg = new List<string>();
108108

@@ -151,6 +151,55 @@ public static async Task<string[]> FindDirectImagesAsync(string url)
151151
});
152152

153153

154+
return await task;
155+
}
156+
157+
/// <summary>
158+
/// Scans for direct image links in <paramref name="url"/>
159+
/// </summary>
160+
public static async Task<string[]> FindDirectImagesAsync(string url)
161+
{
162+
var rg = new List<string>();
163+
164+
//<img.*?src="(.*?)"
165+
//href\s*=\s*"(.+?)"
166+
//var src = "<img.*?src=\"(.*?)\"";
167+
//var href = "href\\s*=\\s*\"(.+?)\"";
168+
169+
string html;
170+
171+
try {
172+
html = WebUtilities.GetString(url);
173+
}
174+
catch (Exception e) {
175+
Debug.WriteLine($"{e.Message}", C_ERROR);
176+
return null;
177+
}
178+
179+
var p = new HtmlParser();
180+
var d = p.ParseDocument(html);
181+
182+
var img = d.QuerySelectorAll("img");
183+
var a = d.QuerySelectorAll("a");
184+
185+
Debug.WriteLine($"{img.Length} | {a.Length}");
186+
187+
//rg.AddRange(img.Select(s=>s.GetAttribute("src")));
188+
rg.AddRange(a.Select(s => s.GetAttribute("href")));
189+
190+
191+
var task = Task.Run(() =>
192+
{
193+
194+
string[] results = rg.AsParallel()
195+
.Where(e => Network.IsUri(e, out var u) && IsDirect2(u == null ? e : u.ToString()))
196+
.ToArray();
197+
198+
199+
return results;
200+
});
201+
202+
154203
return await task;
155204
}
156205
}

SmartImage/Core/MainDialog.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ public static class MainDialog
4141

4242
internal static string ToToggleString(this bool b) => b ? Enabled : Disabled;
4343

44-
private static string GetFilterName(bool added) => GetName("Filter", added);
44+
private static string GetFilterName(bool added) => GetName("Filter", added);
4545
private static string GetNotificationName(bool added) => GetName("Notification", added);
46-
private static string GetNotificationImageName(bool added) => GetName("Notification image", added);
47-
private static string GetName(string s, bool added) => $"{s} ({(added.ToToggleString())})";
48-
46+
private static string GetNotificationImageName(bool added) => GetName("Notification image", added);
47+
private static string GetName(string s, bool added) => $"{s} ({(added.ToToggleString())})";
48+
4949

5050
private static string GetContextMenuName(bool added) => GetName("Context menu", added);
5151

@@ -208,9 +208,8 @@ public static class MainDialog
208208
Function = () =>
209209
{
210210
UpdateInfo.AutoUpdate();
211+
211212

212-
Environment.Exit(0);
213-
//NConsole.WaitForInput();
214213
return null;
215214
}
216215
},

SmartImage/Core/NConsoleFactory.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ public static NConsoleOption Create(SearchResult result)
6767

6868
NConsoleProgress.Queue(cts);
6969

70-
Task.WaitAll(result.OtherResults.Select(async delegate (ImageResult f)
71-
{
72-
await f.FindDirectImagesAsync();
70+
result.OtherResults.AsParallel().ForAll(f => f.FindDirectImagesAsync());
7371

74-
}).ToArray());
7572

73+
//foreach (var otherResult in result.OtherResults) {
74+
// otherResult.FindDirectImagesAsync();
75+
//}
7676

7777
result.PrimaryResult = result.OtherResults.First();
7878

SmartImage/Core/OSIntegration.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
using System;
55
using System.Diagnostics;
66
using System.Diagnostics.CodeAnalysis;
7+
using System.Globalization;
78
using System.IO;
89
using System.Linq;
10+
using System.Threading;
911
using Novus.Utilities;
12+
using SimpleCore.Utilities;
1013
using static SimpleCore.Diagnostics.LogCategories;
1114

1215
// ReSharper disable InconsistentNaming
@@ -15,9 +18,6 @@
1518

1619
namespace SmartImage.Core
1720
{
18-
19-
20-
2121
internal enum IntegrationOption
2222
{
2323
Add,
@@ -181,7 +181,7 @@ internal static void Uninstall()
181181
};
182182

183183
// Runs in background
184-
var proc=Command.Batch(commands, DEL_BAT_NAME);
184+
var proc = Command.Batch(commands, DEL_BAT_NAME);
185185
proc.Start();
186186

187187
}
@@ -206,6 +206,12 @@ internal static void Setup()
206206
if (!Info.IsAppFolderInPath) {
207207
HandlePath(IntegrationOption.Add);
208208
}
209+
210+
//Windows.System.UserProfile.GlobalizationPreferences.Languages
211+
//Thread.CurrentThread.CurrentUICulture
212+
//CultureInfo.CurrentCulture
213+
214+
209215
}
210216
}
211217
}

0 commit comments

Comments
 (0)