Skip to content

Commit 42ff19e

Browse files
committed
Updates
1 parent da32d04 commit 42ff19e

File tree

19 files changed

+257
-344
lines changed

19 files changed

+257
-344
lines changed

SmartImage.Lib/Engines/Impl/AnilistClient.cs renamed to SmartImage.Lib/Clients/AnilistClient.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
using Newtonsoft.Json.Linq;
22
using SimpleCore.Net;
3+
// ReSharper disable PossibleNullReferenceException
34

45
// ReSharper disable UnusedMember.Global
56

6-
namespace SmartImage.Lib.Engines.Impl
7+
namespace SmartImage.Lib.Clients
78
{
89
public sealed class AnilistClient
910
{
10-
private readonly SimpleGraphQLClient m_client;
11+
private readonly GraphQLClient m_client;
1112

1213
public AnilistClient()
1314
{
14-
m_client = new SimpleGraphQLClient("https://graphql.anilist.co");
15+
m_client = new GraphQLClient("https://graphql.anilist.co");
1516
}
1617

1718
public string GetTitle(int anilistId)

SmartImage.Lib/Engines/Impl/IqdbEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private static ImageResult ParseResult(IHtmlCollection<IElement> tr)
5454
if (tr.Length >= 4) {
5555
var res = tr[3];
5656

57-
var wh = res.TextContent.Split(Formatting.MUL_SIGN);
57+
var wh = res.TextContent.Split(StringConstants.MUL_SIGN);
5858

5959
var wStr = wh[0].SelectOnlyDigits();
6060
w = Int32.Parse(wStr);

SmartImage.Lib/Engines/Impl/SauceNaoEngine.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
using AngleSharp.Html.Parser;
1111
using AngleSharp.XPath;
1212
using RestSharp;
13+
using SimpleCore.Diagnostics;
1314
using SimpleCore.Net;
1415
using SimpleCore.Utilities;
1516
using SmartImage.Lib.Searching;
1617
using static SimpleCore.Diagnostics.LogCategories;
1718
using JsonArray = System.Json.JsonArray;
1819
using JsonObject = System.Json.JsonObject;
20+
// ReSharper disable PropertyCanBeMadeInitOnly.Local
1921

2022
// ReSharper disable PossibleMultipleEnumeration
2123

@@ -50,7 +52,7 @@ public SauceNaoEngine() : this(String.Empty) { } //todo
5052
public string Authentication { get; init; }
5153

5254
public override SearchEngineOptions EngineOption => SearchEngineOptions.SauceNao;
53-
55+
5456

5557
private static IEnumerable<SauceNaoDataResult> ParseResults(string url)
5658
{
@@ -69,17 +71,19 @@ private static IEnumerable<SauceNaoDataResult> ParseResults(string url)
6971
* 208.110.232.218, your IP has exceeded the unregistered user's daily limit of 100 searches.
7072
*/
7173

72-
if (html.Contains("Search Limit Exceeded")) {
74+
const string COOLDOWN = "Search Limit Exceeded";
75+
76+
if (html.Contains(COOLDOWN)) {
7377
Trace.WriteLine("SauceNao on cooldown!", C_WARN);
74-
78+
7579
return null;
7680
}
7781

7882
var doc = docp.ParseDocument(html);
7983

80-
// todo: improve
84+
const string RESULT_NODE = "//div[@class='result']";
8185

82-
var results = doc.Body.SelectNodes("//div[@class='result']");
86+
var results = doc.Body.SelectNodes(RESULT_NODE);
8387

8488
return results.Select(Parse).ToList();
8589
}
@@ -90,20 +94,22 @@ private static SauceNaoDataResult Parse(INode result)
9094
return null;
9195
}
9296

93-
if (result.TryGetAttribute("id") == "result-hidden-notification") {
97+
const string HIDDEN_ID_VAL = "result-hidden-notification";
98+
99+
if (result.TryGetAttribute("id") == HIDDEN_ID_VAL) {
94100
return null;
95101
}
96-
102+
97103
var resulttablecontent = result.FirstChild.FirstChild.FirstChild.ChildNodes[1];
98-
104+
99105
var resultmatchinfo = resulttablecontent.FirstChild;
100106
var resultsimilarityinfo = resultmatchinfo.FirstChild;
101107

102108
// Contains links
103109
var resultmiscinfo = resultmatchinfo.ChildNodes[1];
104-
110+
105111
var resultcontent = resulttablecontent.ChildNodes[1];
106-
112+
107113
var resultcontentcolumn = resultcontent.ChildNodes[1];
108114

109115
string link = null;
@@ -125,22 +131,21 @@ private static SauceNaoDataResult Parse(INode result)
125131
string rcci = resultcontentcolumn1?.TextContent;
126132

127133
string material1 = rcci?.SubstringAfter("Material: ");
128-
134+
129135
string creator1 = rti ?? rcci;
130136
creator1 = creator1?.SubstringAfter("Creator: ");
131-
132137

133138

134139
float similarity = Single.Parse(resultsimilarityinfo.TextContent.Replace("%", String.Empty));
135140

136-
var i = new SauceNaoDataResult
141+
var dataResult = new SauceNaoDataResult
137142
{
138143
Urls = new[] {link}!,
139144
Similarity = similarity,
140145
Creator = creator1
141146
};
142147

143-
return i;
148+
return dataResult;
144149
}
145150

146151
public override SearchResult GetResult(ImageQuery url)
@@ -154,7 +159,7 @@ public override SearchResult GetResult(ImageQuery url)
154159

155160
if (orig == null) {
156161
//return result;
157-
Debug.WriteLine("[info] Parsing HTML from SN!");
162+
Debug.WriteLine($"[{Name}] Parsing HTML", LogCategories.C_INFO);
158163

159164
string urlStr = url.Uri.ToString();
160165

@@ -200,12 +205,11 @@ public override SearchResult GetResult(ImageQuery url)
200205
sresult.OtherResults.AddRange(extended);
201206

202207
if (!String.IsNullOrWhiteSpace(Authentication)) {
203-
Debug.WriteLine($"SN API key: {Authentication}");
208+
Debug.WriteLine($"{Name} API key: {Authentication}");
204209
}
205210
}
206211
catch (Exception e) {
207-
Debug.WriteLine($"SauceNao error: {e.StackTrace}");
208-
//todo
212+
Debug.WriteLine($"{Name} error: {e.StackTrace}", C_ERROR);
209213
sresult.Status = ResultStatus.Failure;
210214
}
211215

@@ -258,7 +262,7 @@ private static ImageResult ConvertToImageResult(SauceNaoDataResult sn)
258262

259263
// var x = new BasicSearchResult(url, sn.Similarity,
260264
// sn.WebsiteTitle, sn.Creator, sn.Material, sn.Character, siteName);
261-
var x = new ImageResult
265+
var imageResult = new ImageResult
262266
{
263267
Url = String.IsNullOrWhiteSpace(url) ? default : new Uri(url),
264268
Similarity = MathF.Round(sn.Similarity, 2),
@@ -269,7 +273,7 @@ private static ImageResult ConvertToImageResult(SauceNaoDataResult sn)
269273
Site = siteName
270274
};
271275

272-
return x;
276+
return imageResult;
273277
}
274278

275279
return null;

SmartImage.Lib/Engines/Impl/TidderEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected override SearchResult Process(IDocument doc, SearchResult sr)
5555
string? author = authorNode.TextContent;
5656
string? subreddit = subredditNode.TextContent;
5757

58-
//deentize!
58+
//DeEntize!
5959
string link = titleNode.FirstChild.TryGetAttribute("href");
6060

6161
var bsr = new ImageResult()

SmartImage.Lib/Engines/Impl/TraceMoeEngine.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
using System.Net;
66
using JetBrains.Annotations;
77
using RestSharp;
8+
using SmartImage.Lib.Clients;
89
using SmartImage.Lib.Searching;
10+
using static SimpleCore.Diagnostics.LogCategories;
911

1012
// ReSharper disable InconsistentNaming
1113
#pragma warning disable IDE1006, IDE0051
@@ -125,7 +127,7 @@ public override SearchResult GetResult(ImageQuery url)
125127
}
126128
catch (Exception e) {
127129
r = base.GetResult(url);
128-
Debug.WriteLine($"tracemoe: {e.Message}");
130+
Debug.WriteLine($"[{Name}] Error: {e.Message}");
129131
//r.AddErrorMessage(e.Message);
130132
r.Status = ResultStatus.Failure;
131133
return r;
@@ -135,7 +137,7 @@ public override SearchResult GetResult(ImageQuery url)
135137
}
136138
else {
137139
r = base.GetResult(url);
138-
Debug.WriteLine($"[error] tracemoe: api error");
140+
Debug.WriteLine($"[{Name}] API error", C_ERROR);
139141
//r.AddErrorMessage(msg);
140142
}
141143

SmartImage.Lib/Engines/Impl/YandexEngine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public YandexEngine() : base("https://yandex.com/images/search?rpt=imageview&url
5454
const string item = "//li[@class='other-sites__item']";
5555

5656
var tagsItem = doc.Body.SelectNodes(item);
57-
57+
5858
if (tagsItem == null) {
5959
return null;
6060
}
@@ -93,7 +93,7 @@ static ImageResult Parse(INode siz)
9393

9494
private static (int? w, int? h) ParseResolution(string resText)
9595
{
96-
string[] resFull = resText.Split(Formatting.MUL_SIGN);
96+
string[] resFull = resText.Split(StringConstants.MUL_SIGN);
9797

9898
int? w = null, h = null;
9999

@@ -163,7 +163,7 @@ static ImageResult Parse(INode siz)
163163
return yi;
164164
}
165165

166-
166+
167167
images.AddRange(sizeTags.AsParallel().Select(Parse));
168168

169169
return images;

SmartImage.Lib/Engines/InterpretedSearchEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public override SearchResult GetResult(ImageQuery query)
4343
}
4444
catch (Exception e) {
4545
sr.Status = ResultStatus.Failure;
46-
Trace.WriteLine($"{Name}: {e.Message} {e.Source} {e.StackTrace}", C_ERROR);
46+
Trace.WriteLine($"{Name}: {e.Message}", C_ERROR);
4747
}
4848

4949
return sr;

SmartImage.Lib/SearchClient.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public List<SearchResult> MaximizeResults<T>(Func<SearchResult, T> property)
9494
//await t;
9595

9696
if (!IsComplete) {
97-
throw new SmartImageException();
97+
throw new SmartImageException($"Search must be completed");
9898
}
9999

100100
var res = Results.OrderByDescending(property).ToList();
@@ -107,10 +107,10 @@ public List<SearchResult> MaximizeResults<T>(Func<SearchResult, T> property)
107107
public async Task RefineSearchAsync()
108108
{
109109
if (!IsComplete) {
110-
throw new SmartImageException();
110+
throw new SmartImageException($"Search must be completed");
111111
}
112112

113-
Trace.WriteLine($"Finding best result");
113+
Trace.WriteLine("Finding best result");
114114

115115
var best = FindBestResult();
116116

@@ -156,9 +156,9 @@ public async Task RunSearchAsync()
156156
}
157157

158158
Trace.WriteLine($"{nameof(SearchClient)}: Search complete", C_SUCCESS);
159+
159160
SearchCompleted?.Invoke(null, EventArgs.Empty);
160161

161-
return;
162162
}
163163

164164
public static BaseUploadEngine[] GetAllUploadEngines()
@@ -169,6 +169,7 @@ public static BaseUploadEngine[] GetAllUploadEngines()
169169
.ToArray();
170170
}
171171

172+
172173
public static BaseSearchEngine[] GetAllSearchEngines()
173174
{
174175
return typeof(BaseSearchEngine).GetAllSubclasses()
@@ -179,8 +180,7 @@ public static BaseSearchEngine[] GetAllSearchEngines()
179180

180181
public event EventHandler<SearchResultEventArgs> ResultCompleted;
181182

182-
public event EventHandler SearchCompleted;
183-
183+
public event EventHandler SearchCompleted;
184184
}
185185

186186
public sealed class SearchResultEventArgs : EventArgs

SmartImage.Lib/SearchConfig.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@ public sealed class SearchConfig
3838
public bool Filter { get; set; } = true;
3939

4040

41-
4241
public override string ToString()
4342
{
4443
var sb = new ExtendedStringBuilder();
45-
sb.AppendLine("Config");
4644
sb.Append(nameof(SearchEngines), SearchEngines);
4745
sb.Append(nameof(PriorityEngines), PriorityEngines);
4846
sb.Append(nameof(Filter), Filter);

0 commit comments

Comments
 (0)