Skip to content

Commit a7ae70a

Browse files
committed
IQDB
1 parent 3a704c9 commit a7ae70a

File tree

9 files changed

+144
-89
lines changed

9 files changed

+144
-89
lines changed

SmartImage.Lib/Engines/BaseSearchEngine.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,22 @@ namespace SmartImage.Lib.Engines
1414
/// </summary>
1515
public abstract class BaseSearchEngine
1616
{
17-
public string BaseUrl { get; }
18-
1917
protected BaseSearchEngine(string baseUrl)
2018
{
2119
BaseUrl = baseUrl;
2220
Timeout = TimeSpan.FromSeconds(5);
2321
}
2422

23+
public string BaseUrl { get; }
24+
2525
public abstract SearchEngineOptions EngineOption { get; }
2626

2727

2828
public virtual string Name => EngineOption.ToString();
2929

3030

31+
public TimeSpan Timeout { get; set; }
32+
3133
public virtual SearchResult GetResult(ImageQuery query)
3234
{
3335
var rawUrl = GetRawResultUrl(query);
@@ -63,8 +65,6 @@ public async Task<SearchResult> GetResultAsync(ImageQuery query)
6365
return await task;
6466
}
6567

66-
public TimeSpan Timeout { get; set; }
67-
6868
public Uri GetRawResultUrl(ImageQuery query)
6969
{
7070
var uri = new Uri(BaseUrl + query.Image);

SmartImage.Lib/Engines/Impl/IqdbEngine.cs

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,27 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Diagnostics;
6+
using System.IO;
67
using System.Linq;
8+
using System.Net.Http;
79
using System.Threading;
810
using System.Threading.Tasks;
911
using AngleSharp.Dom;
1012
using AngleSharp.Html.Dom;
13+
using AngleSharp.Html.Parser;
1114
using AngleSharp.XPath;
15+
using RestSharp;
16+
using SimpleCore.Diagnostics;
1217
using SimpleCore.Net;
1318
using SimpleCore.Utilities;
1419
using SmartImage.Lib.Searching;
1520
using SmartImage.Lib.Utilities;
1621

1722
namespace SmartImage.Lib.Engines.Impl
1823
{
19-
public sealed class IqdbEngine : InterpretedSearchEngine
24+
public sealed class IqdbEngine : ClientSearchEngine
2025
{
21-
public IqdbEngine() : base("https://iqdb.org/?url=") { }
26+
public IqdbEngine() : base("https://iqdb.org/?url=", "https://iqdb.org/") { }
2227

2328
public override SearchEngineOptions EngineOption => SearchEngineOptions.Iqdb;
2429

@@ -105,10 +110,100 @@ private static ImageResult ParseResult(IHtmlCollection<IElement> tr)
105110
return result;
106111
}
107112

108-
protected override SearchResult Process(IDocument doc, SearchResult sr)
113+
114+
private IDocument GetDocument(ImageQuery query)
115+
{
116+
//return base.GetDocument(sr);
117+
118+
/*MultipartFormDataContent form = new MultipartFormDataContent();
119+
120+
form.Add(new StringContent("8388608"), "MAX_FILE_SIZE");
121+
122+
for (int i = 1; i <= 13; i++)
123+
{
124+
if (new[] { 7, 8, 9, 12 }.Contains(i))
125+
{
126+
continue;
127+
}
128+
129+
form.Add(new StringContent(i.ToString()), "service[]");
130+
}
131+
132+
form.Add(new StreamContent(query.Stream), "file", "image.jpg");
133+
form.Add(new StringContent(string.Empty), "url");
134+
135+
136+
var h = new HttpClient();
137+
h.BaseAddress = new Uri(EndpointUrl);
138+
var r=h.PostAsync("/", form);
139+
r.Wait();*/
140+
141+
var rq = new RestRequest(Method.POST);
142+
rq.AddParameter("MAX_FILE_SIZE", 8388608, ParameterType.GetOrPost);
143+
rq.AddHeader("Content-Type", "multipart/form-data");
144+
145+
byte[] rg = Array.Empty<byte>();
146+
object u = query.Value;
147+
148+
if (query.IsFile) {
149+
rg = File.ReadAllBytes(query.Value);
150+
151+
152+
}
153+
else if (query.IsUri) { }
154+
else {
155+
throw new SmartImageException();
156+
}
157+
158+
rq.AddFile("file", rg, "image.jpg");
159+
rq.AddParameter("url", u, ParameterType.GetOrPost);
160+
161+
//rq.AddParameter("service[]", new[] {1, 2, 3, 4, 5, 6, 11, 13}, ParameterType.GetOrPost);
162+
163+
var response = Client.Execute(rq);
164+
Network.DumpResponse(response);
165+
Debug.Assert(response.IsSuccessful);
166+
167+
// var html2 = r.Result.Content.ReadAsStringAsync();
168+
// html2.Wait();
169+
// var html = html2.Result;
170+
171+
var parser = new HtmlParser();
172+
return parser.ParseDocument(response.Content);
173+
}
174+
175+
176+
[DebuggerHidden]
177+
public override SearchResult GetResult(ImageQuery query)
178+
{
179+
//var sr = base.GetResult(query);
180+
var sr = new SearchResult(this);
181+
182+
183+
try {
184+
185+
sr = Process(query, sr);
186+
}
187+
catch (Exception e) {
188+
sr.Status = ResultStatus.Failure;
189+
Trace.WriteLine($"{Name}: {e.Message}", LogCategories.C_ERROR);
190+
}
191+
192+
return sr;
193+
}
194+
195+
protected override SearchResult Process(ImageQuery query, SearchResult sr)
109196
{
197+
198+
// var re = new RestRequest(Method.POST);
199+
// re.AddFile("file", File.ReadAllBytes(q.Value), "image.jpg");
200+
110201
// Don't select other results
111202

203+
204+
var doc = GetDocument(query);
205+
206+
112207
var pages = doc.Body.SelectSingleNode("//div[@id='pages']");
113208
var tables = ((IHtmlElement) pages).SelectNodes("div/table");
114209

SmartImage.Lib/Engines/Impl/SauceNaoEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private IEnumerable<SauceNaoDataResult> GetHTMLResults(ImageQuery query)
126126
req.AddFile("file", File.ReadAllBytes(query.Value), "image.png");
127127
}
128128
else if (query.IsUri) {
129-
req.AddParameter("url", query, ParameterType.GetOrPost);
129+
req.AddParameter("url", query.Value, ParameterType.GetOrPost);
130130
}
131131
else {
132132
throw new SmartImageException();

SmartImage.Lib/SearchClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ public async Task RunSearchAsync()
112112
//
113113
// Filtering
114114
// / \
115-
// true false
116-
// / \
117-
// IsNonPrimitive [FilteredResults]
115+
// true false
116+
// / \
117+
// IsNonPrimitive [FilteredResults]
118118
// / \
119119
// true false
120120
// / \

SmartImage.Lib/Utilities/ImageHelper.cs

Lines changed: 18 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,13 @@ public static bool IsDirect2(string url)
9898
@"(?:([^:\/?#]+):)?(?:\/\/([^\/?#]*))?([^?#]*\.(?:bmp|gif|ico|jfif|jpe?g|png|svg|tiff?|webp))(?:\?([^#]*))?(?:#(.*))?",
9999
RegexOptions.IgnoreCase);
100100
}
101+
102+
101103

102104
/// <summary>
103105
/// Scans for direct image links in <paramref name="url"/>
104106
/// </summary>
105-
public static async Task<string[]> FindDirectImagesAsync2(string url)
107+
public static async Task<string[]> FindDirectImagesAsync(string url)
106108
{
107109
var rg = new List<string>();
108110

@@ -121,7 +123,20 @@ public static async Task<string[]> FindDirectImagesAsync2(string url)
121123
return null;
122124
}
123125

124-
var matches = Regex.Matches(html, "<a\\s+(?:[^>]*?\\s+)?href=\"([^\"]*)\"");
126+
var p = new HtmlParser();
127+
var d = p.ParseDocument(html);
128+
129+
var img = d.QuerySelectorAll("img");
130+
var a = d.QuerySelectorAll("a");
131+
132+
//Debug.WriteLine($"{img.Length} | {a.Length}");
133+
134+
//rg.AddRange(img.Select(s=>s.GetAttribute("src")));
135+
rg.AddRange(a.Select(s => s.GetAttribute("href")));
136+
137+
138+
139+
/*var matches = Regex.Matches(html, "<a\\s+(?:[^>]*?\\s+)?href=\"([^\"]*)\"");
125140
126141
127142
for (int i = 0; i < matches.Count; i++) {
@@ -136,57 +151,7 @@ public static async Task<string[]> FindDirectImagesAsync2(string url)
136151
rg.Add(capture.Value);
137152
}
138153
}
139-
}
140-
141-
142-
var task = Task.Run(() =>
143-
{
144-
145-
string[] results = rg.AsParallel()
146-
.Where(e => Network.IsUri(e, out var u) && IsDirect2(u == null ? e : u.ToString()))
147-
.ToArray();
148-
149-
150-
return results;
151-
});
152-
153-
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-
154+
}*/
190155

191156
var task = Task.Run(() =>
192157
{

SmartImage/Core/MainDialog.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Drawing;
88
using System.IO;
99
using System.Linq;
10+
using System.Linq.Expressions;
1011
using System.Reflection;
1112
using Microsoft.Toolkit.Uwp.Notifications;
1213
using Novus.Utilities;
@@ -121,6 +122,7 @@ internal static class MainDialog
121122
Function = () =>
122123
{
123124
Program.Config.Notification = !Program.Config.Notification;
125+
124126

125127
MainMenuOptions[4].Name = GetNotificationName(Program.Config.Notification);
126128
SaveAndUpdateConfig();

SmartImage/Core/NConsoleFactory.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,8 @@ public static NConsoleOption Create(SearchResult result)
6969

7070
result.OtherResults.AsParallel().ForAll(f => f.FindDirectImagesAsync());
7171

72-
73-
//foreach (var otherResult in result.OtherResults) {
74-
// otherResult.FindDirectImagesAsync();
75-
//}
76-
7772
result.PrimaryResult = result.OtherResults.First();
78-
79-
//result.PrimaryResult.UpdateFrom(result.OtherResults.First());
73+
8074

8175
cts.Cancel();
8276
cts.Dispose();

SmartImage/Core/OSIntegration.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,12 @@ internal static void HandlePath(IntegrationOption option)
140140

141141
internal static void ResetIntegrations()
142142
{
143-
144-
145143
// Computer\HKEY_CLASSES_ROOT\*\shell\SmartImage
146144

147145
if (IsContextMenuAdded) {
148146
HandleContextMenu(IntegrationOption.Remove);
149147
}
150148

151-
152-
// will be added automatically if run again
153-
//Path.Remove();
154-
155149
Trace.WriteLine("Reset config");
156150
}
157151

@@ -200,7 +194,5 @@ internal static bool IsContextMenuAdded
200194
return reg != null;
201195
}
202196
}
203-
204-
205197
}
206198
}

0 commit comments

Comments
 (0)