Skip to content

Commit f7f2b32

Browse files
committed
Improve IQDB engine
1 parent a7ae70a commit f7f2b32

File tree

5 files changed

+24
-64
lines changed

5 files changed

+24
-64
lines changed

SmartImage.Lib/Engines/BaseSearchEngine.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ public async Task<SearchResult> GetResultAsync(ImageQuery query)
6868
public Uri GetRawResultUrl(ImageQuery query)
6969
{
7070
var uri = new Uri(BaseUrl + query.Image);
71-
72-
//var hostUri = Network.GetHostUri(new Uri(BaseUrl));
73-
71+
7472
bool ok = Network.IsUriAlive(uri, Timeout);
7573

7674
if (!ok) {

SmartImage.Lib/Engines/Impl/IqdbEngine.cs

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
using SmartImage.Lib.Searching;
2020
using SmartImage.Lib.Utilities;
2121

22+
// ReSharper disable StringLiteralTypo
23+
2224
namespace SmartImage.Lib.Engines.Impl
2325
{
2426
public sealed class IqdbEngine : ClientSearchEngine
@@ -113,60 +115,32 @@ private static ImageResult ParseResult(IHtmlCollection<IElement> tr)
113115

114116
private IDocument GetDocument(ImageQuery query)
115117
{
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-
118+
var rq = new RestRequest(Method.POST);
135119

136-
var h = new HttpClient();
137-
h.BaseAddress = new Uri(EndpointUrl);
138-
var r=h.PostAsync("/", form);
139-
r.Wait();*/
120+
const int MAX_FILE_SIZE = 8388608;
140121

141-
var rq = new RestRequest(Method.POST);
142-
rq.AddParameter("MAX_FILE_SIZE", 8388608, ParameterType.GetOrPost);
122+
rq.AddParameter("MAX_FILE_SIZE", MAX_FILE_SIZE, ParameterType.GetOrPost);
143123
rq.AddHeader("Content-Type", "multipart/form-data");
144124

145-
byte[] rg = Array.Empty<byte>();
146-
object u = query.Value;
125+
byte[] fileBytes = Array.Empty<byte>();
126+
object uri = string.Empty;
147127

148128
if (query.IsFile) {
149-
rg = File.ReadAllBytes(query.Value);
150-
151-
129+
fileBytes = File.ReadAllBytes(query.Value);
130+
}
131+
else if (query.IsUri) {
132+
uri = query.Value;
152133
}
153-
else if (query.IsUri) { }
154134
else {
155135
throw new SmartImageException();
156136
}
157137

158-
rq.AddFile("file", rg, "image.jpg");
159-
rq.AddParameter("url", u, ParameterType.GetOrPost);
138+
rq.AddFile("file", fileBytes, "image.jpg");
139+
rq.AddParameter("url", uri, ParameterType.GetOrPost);
160140

161141
//rq.AddParameter("service[]", new[] {1, 2, 3, 4, 5, 6, 11, 13}, ParameterType.GetOrPost);
162142

163143
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;
170144

171145
var parser = new HtmlParser();
172146
return parser.ParseDocument(response.Content);
@@ -179,7 +153,6 @@ public override SearchResult GetResult(ImageQuery query)
179153
//var sr = base.GetResult(query);
180154
var sr = new SearchResult(this);
181155

182-
183156
try {
184157

185158
sr = Process(query, sr);
@@ -194,16 +167,10 @@ public override SearchResult GetResult(ImageQuery query)
194167

195168
protected override SearchResult Process(ImageQuery query, SearchResult sr)
196169
{
197-
198-
// var re = new RestRequest(Method.POST);
199-
// re.AddFile("file", File.ReadAllBytes(q.Value), "image.jpg");
200-
201170
// Don't select other results
202171

203-
204172
var doc = GetDocument(query);
205173

206-
207174
var pages = doc.Body.SelectSingleNode("//div[@id='pages']");
208175
var tables = ((IHtmlElement) pages).SelectNodes("div/table");
209176

SmartImage.Lib/Engines/InterpretedSearchEngine.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public abstract class InterpretedSearchEngine : BaseSearchEngine
2525

2626
protected InterpretedSearchEngine(string baseUrl) : base(baseUrl) { }
2727

28-
2928

3029
[DebuggerHidden]
3130
public override SearchResult GetResult(ImageQuery query)
@@ -51,16 +50,6 @@ public override SearchResult GetResult(ImageQuery query)
5150

5251
protected virtual IDocument GetDocument(SearchResult sr)
5352
{
54-
/*if (!Network.TryGetString(sr.RawUri.ToString()!, out var html))
55-
{
56-
sr.RawUri = null;
57-
sr.PrimaryResult.Url = null;
58-
59-
//sr.AddErrorMessage("Unavailable");
60-
throw new SmartImageException();
61-
}*/
62-
63-
6453
string response = WebUtilities.GetString(sr.RawUri.ToString()!);
6554

6655
var parser = new HtmlParser();

SmartImage.Lib/Utilities/ImageHelper.cs

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

104103
/// <summary>
105104
/// Scans for direct image links in <paramref name="url"/>
@@ -135,7 +134,6 @@ public static async Task<string[]> FindDirectImagesAsync(string url)
135134
rg.AddRange(a.Select(s => s.GetAttribute("href")));
136135

137136

138-
139137
/*var matches = Regex.Matches(html, "<a\\s+(?:[^>]*?\\s+)?href=\"([^\"]*)\"");
140138
141139
@@ -157,7 +155,8 @@ public static async Task<string[]> FindDirectImagesAsync(string url)
157155
{
158156

159157
string[] results = rg.AsParallel()
160-
.Where(e => Network.IsUri(e, out var u) && IsDirect2(u == null ? e : u.ToString()))
158+
.Where(e => Network.IsUri(e, out var u)
159+
&& IsDirect2(u == null ? e : u.ToString()))
161160
.ToArray();
162161

163162

Test/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,18 @@ public static async Task Main(string[] args)
140140
Console.WriteLine(result);
141141
}*/
142142

143+
143144
var i = new IqdbEngine();
144145
var i2 = i.GetResultAsync(q);
145146
var r2 = await i2;
146147

147148
Console.WriteLine(">> {0}", r2);
149+
150+
var ix = new SauceNaoEngine() { };
151+
var i2x = ix.GetResultAsync(q);
152+
var r2x = await i2x;
153+
154+
Console.WriteLine(">> {0}", r2x);
148155
}
149156
}
150157
}

0 commit comments

Comments
 (0)