Skip to content

Commit 01d4b3c

Browse files
committed
Migrate to AngleSharp
1 parent a4c1ea1 commit 01d4b3c

File tree

9 files changed

+57
-46
lines changed

9 files changed

+57
-46
lines changed

SmartImage.Lib/Engines/Impl/Ascii2DEngine.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using AngleSharp.Html.Dom;
77
using AngleSharp.Html.Parser;
88
using AngleSharp.XPath;
9-
using HtmlAgilityPack;
109
using SimpleCore.Net;
1110
using SmartImage.Lib.Searching;
1211

SmartImage.Lib/Engines/Impl/IqdbEngine.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using AngleSharp.Dom;
99
using AngleSharp.Html.Dom;
1010
using AngleSharp.XPath;
11-
using HtmlAgilityPack;
1211
using SimpleCore.Net;
1312
using SimpleCore.Utilities;
1413
using SmartImage.Lib.Searching;

SmartImage.Lib/Engines/Impl/SauceNaoEngine.cs

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// ReSharper disable UnusedMember.Global
22

3-
using HtmlAgilityPack;
43
using RestSharp;
54
using SimpleCore.Net;
65
using SimpleCore.Utilities;
@@ -11,6 +10,10 @@
1110
using System.Json;
1211
using System.Linq;
1312
using System.Net;
13+
using AngleSharp.Dom;
14+
using AngleSharp.Html.Dom;
15+
using AngleSharp.Html.Parser;
16+
using AngleSharp.XPath;
1417
using static SimpleCore.Diagnostics.LogCategories;
1518
using JsonArray = System.Json.JsonArray;
1619
using JsonObject = System.Json.JsonObject;
@@ -89,7 +92,7 @@ public SauceNaoEngine() : this(String.Empty) { } //todo
8992

9093
private static IEnumerable<SauceNaoDataResult> ParseResults(string url)
9194
{
92-
var doc = new HtmlDocument();
95+
var docp = new HtmlParser();
9396

9497
var rc = new RestClient(BASE_URL);
9598
var req = new RestRequest("search.php");
@@ -105,77 +108,92 @@ private static IEnumerable<SauceNaoDataResult> ParseResults(string url)
105108
*/
106109

107110
if (html.Contains("Search Limit Exceeded")) {
108-
Trace.WriteLine("SauceNao on cooldown!",C_WARN);
111+
Trace.WriteLine("SauceNao on cooldown!", C_WARN);
109112
return null;
110113
}
111114

112-
doc.LoadHtml(html);
115+
var doc = docp.ParseDocument(html);
113116

114117
// todo: improve
115118

116-
var results = doc.DocumentNode.SelectNodes("//div[@class='result']");
119+
var results = doc.Body.SelectNodes("//div[@class='result']");
117120

118-
var images = results.AsParallel().Select(Parse);
119121

120-
return images;
122+
return results.Select(node => Parse(node)).ToList();
121123
}
122124

123-
private static SauceNaoDataResult Parse(HtmlNode result)
125+
private static SauceNaoDataResult Parse(INode result)
124126
{
125127
if (result == null) {
126128
return null;
127129
}
128130

129-
if (result.GetAttributeValue("id", String.Empty) == "result-hidden-notification") {
131+
if (result.GetAttr("id") == "result-hidden-notification") {
130132
return null;
131133
}
132134

133-
var n = result.FirstChild.FirstChild;
135+
//var n = result.FirstChild.FirstChild;
134136

135137
//var resulttableimage = n.ChildNodes[0];
136-
var resulttablecontent = n.ChildNodes[1];
138+
var resulttablecontent = result.FirstChild.FirstChild.FirstChild.ChildNodes[1];
139+
//var resulttablecontent = n.ChildNodes[1];
137140

138141
var resultmatchinfo = resulttablecontent.FirstChild;
139142
var resultsimilarityinfo = resultmatchinfo.FirstChild;
140143

141144
// Contains links
142145
var resultmiscinfo = resultmatchinfo.ChildNodes[1];
143146

144-
var links1 = resultmiscinfo.SelectNodes("a/@href");
145-
string link1 = links1?[0].GetAttributeValue("href", null);
147+
// var links1 = ((IHtmlElement)resultmiscinfo).SelectNodes("a/@href");
148+
// string link1 = links1?[0].GetAttr("href");
146149

147150
var resultcontent = resulttablecontent.ChildNodes[1];
148151

149152
//var resulttitle = resultcontent.ChildNodes[0];
150153

151154
var resultcontentcolumn = resultcontent.ChildNodes[1];
152155

153-
// Other way of getting links
154-
var links2 = resultcontentcolumn.SelectNodes("a/@href");
155-
string link2 = links2?[0].GetAttributeValue("href", null);
156+
string link = null;
157+
158+
var g = resultcontentcolumn.ChildNodes.GetElementsByTagName("a")
159+
.FirstOrDefault(x => x.GetAttribute("href") != null);
160+
161+
if (g != null) {
162+
link = g.GetAttribute("href");
163+
}
164+
156165

157-
string link = link1 ?? link2;
166+
// Other way of getting links
167+
// var links2 = ((IHtmlElement)resultcontentcolumn).SelectNodes("a/@href");
168+
// string link2 = links2?[0].GetAttr("href");
169+
//
170+
// string link = link1 ?? link2;
158171

159172
(string creator, string material) = FindInfo(resultcontent);
160-
float similarity = Single.Parse(resultsimilarityinfo.InnerText.Replace("%", String.Empty));
173+
float similarity = Single.Parse(resultsimilarityinfo.TextContent.Replace("%", String.Empty));
174+
175+
var i = new SauceNaoDataResult
176+
{
177+
Urls = new[] {link}!,
178+
Similarity = similarity,
179+
Creator = creator
180+
};
161181

162-
var i = new SauceNaoDataResult {Urls = new[] {link}!, Similarity = similarity, Creator = creator};
163-
164182
return i;
165183
}
166184

167-
private static (string Creator, string Material) FindInfo(HtmlNode resultcontent)
185+
private static (string Creator, string Material) FindInfo(INode resultcontent)
168186
{
169-
187+
170188

171189
// //div[contains(@class, 'resulttitle')]
172190
// //div/node()[self::strong]
173191

174-
var resulttitle = resultcontent.ChildNodes[0];
175-
string rti = resulttitle?.InnerText;
192+
var resulttitle = resultcontent.ChildNodes[0];
193+
string rti = resulttitle?.TextContent;
176194

177195
var resultcontentcolumn = resultcontent.ChildNodes[1];
178-
string rcci = resultcontentcolumn?.InnerText;
196+
string rcci = resultcontentcolumn?.TextContent;
179197

180198
string material = rcci?.SubstringAfter("Material: ");
181199

@@ -212,23 +230,23 @@ public override SearchResult GetResult(ImageQuery url)
212230
sresult.Status = ResultStatus.Unavailable;
213231
return sresult;
214232
}
215-
216-
orig = sauceNaoDataResults.Where(o=>o!=null).ToArray();
233+
234+
orig = sauceNaoDataResults.Where(o => o != null).ToArray();
217235
}
218236

219237
// aggregate all info for primary result
220238

221239
string character = orig.FirstOrDefault(o => !String.IsNullOrWhiteSpace(o.Character))?.Character;
222-
string creator = orig.FirstOrDefault(o => !String.IsNullOrWhiteSpace(o.Creator))?.Creator;
223-
string material = orig.FirstOrDefault(o => !String.IsNullOrWhiteSpace(o.Material))?.Material;
240+
string creator = orig.FirstOrDefault(o => !String.IsNullOrWhiteSpace(o.Creator))?.Creator;
241+
string material = orig.FirstOrDefault(o => !String.IsNullOrWhiteSpace(o.Material))?.Material;
224242

225243
var extended = orig.AsParallel().Select(ConvertToImageResult);
226244

227-
245+
228246
var ordered = extended
229-
.Where(e => e.Url != null)
230-
.OrderByDescending(e => e.Similarity)
231-
.ToList();
247+
.Where(e => e.Url != null)
248+
.OrderByDescending(e => e.Similarity)
249+
.ToList();
232250

233251
if (!ordered.Any()) {
234252
// No good results
@@ -242,8 +260,8 @@ public override SearchResult GetResult(ImageQuery url)
242260
result.UpdateFrom(best);
243261

244262
result.Characters = character;
245-
result.Artist = creator;
246-
result.Source = material;
263+
result.Artist = creator;
264+
result.Source = material;
247265

248266
sresult.OtherResults.AddRange(extended);
249267

SmartImage.Lib/Engines/Impl/TidderEngine.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using AngleSharp.Dom;
66
using AngleSharp.Html.Dom;
77
using AngleSharp.XPath;
8-
using HtmlAgilityPack;
98
using SimpleCore.Net;
109
using SmartImage.Lib.Searching;
1110
using SmartImage.Lib.Utilities;

SmartImage.Lib/Engines/Impl/YandexEngine.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using AngleSharp.Dom;
77
using AngleSharp.Html.Dom;
88
using AngleSharp.XPath;
9-
using HtmlAgilityPack;
109
using SimpleCore.Net;
1110
using SimpleCore.Utilities;
1211
using SmartImage.Lib.Searching;

SmartImage.Lib/Engines/InterpretedSearchEngine.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Threading.Tasks;
77
using AngleSharp.Dom;
88
using AngleSharp.Html.Parser;
9-
using HtmlAgilityPack;
109
using RestSharp;
1110
using SimpleCore.Net;
1211
using SmartImage.Lib.Searching;

SmartImage.Lib/SmartImage.Lib.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
<PackageReference Include="AngleSharp.Io" Version="0.15.0" />
1313
<PackageReference Include="AngleSharp.Js" Version="0.14.0" />
1414
<PackageReference Include="AngleSharp.XPath" Version="1.1.7" />
15-
<PackageReference Include="HtmlAgilityPack" Version="1.11.33" />
1615
<PackageReference Include="JetBrains.Annotations" Version="2021.1.0" />
1716
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
1817
<PackageReference Include="RestSharp" Version="106.11.7" />

SmartImage.Lib/Utilities/ImageHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using AngleSharp.Html.Parser;
1212
using AngleSharp.Io;
1313
using AngleSharp.XPath;
14-
using HtmlAgilityPack;
1514
using Newtonsoft.Json.Linq;
1615
using SimpleCore.Net;
1716
using MimeType = SimpleCore.Net.MimeType;
@@ -151,7 +150,8 @@ public static string ResolveDirectLink(string s)
151150
string host = uri.Host;
152151

153152

154-
var doc = new HtmlDocument();
153+
var docp = new HtmlParser();
154+
155155
var html = Network.GetSimpleResponse(s);
156156

157157
if (host.Contains("danbooru")) {
@@ -166,11 +166,11 @@ public static string ResolveDirectLink(string s)
166166
return d;
167167
}
168168

169-
doc.LoadHtml(html.Content);
169+
var doc=docp.ParseDocument(html.Content);
170170

171171
string sel = "//img";
172172

173-
var nodes = doc.DocumentNode.SelectNodes(sel);
173+
var nodes = doc.Body.SelectNodes(sel);
174174

175175
if (nodes == null) {
176176
return null;

SmartImage/SmartImage.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
</PropertyGroup>
3030

3131
<ItemGroup>
32-
<PackageReference Include="HtmlAgilityPack" Version="1.11.33" />
3332
<PackageReference Include="JetBrains.Annotations" Version="2021.1.0" />
3433
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
3534
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />

0 commit comments

Comments
 (0)