Skip to content

Commit 8ff5f92

Browse files
committed
Refactoring & misc fixes
1 parent c9981f9 commit 8ff5f92

File tree

10 files changed

+194
-151
lines changed

10 files changed

+194
-151
lines changed

SmartImage.Lib/Engines/Impl/Search/ArchiveMoeEngine.cs

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,39 +37,8 @@ protected override ValueTask<SearchResultItem> ParseResultItem(INode n, SearchRe
3737
{
3838
// ReSharper disable PossibleNullReferenceException
3939

40-
var e = n as HtmlElement;
41-
42-
var pff = e.QuerySelector(".post_file_filename");
43-
var pfm = e.QuerySelector(".post_file_metadata").TextContent.Split(", ");
44-
var pd = e.QuerySelector(".post_data");
45-
var pt = pd.QuerySelector(".post_title").TextContent;
46-
var pa = pd.QuerySelector(".post_author").TextContent;
47-
var ptc = pd.QuerySelector(".post_tripcode").TextContent;
48-
var tw = pd.QuerySelector(".time_wrap").Children[0];
49-
var time2Ok = DateTime.TryParse(tw.GetAttribute("datetime"), out var time2);
50-
var time = tw.TextContent;
51-
var text = e.QuerySelector(".text").TextContent;
52-
53-
var wh = pfm[1].Split('x');
54-
55-
var p = new ChanPost
56-
{
57-
Id = Int64.Parse(e.GetAttribute("id")),
58-
Board = e.GetAttribute("data-board"),
59-
Filename = pff.TextContent,
60-
File = pff.GetAttribute("href"),
61-
Width = Int32.Parse(wh[0]),
62-
Height = Int32.Parse(wh[1]),
63-
Size = pfm[0],
64-
Title = pt,
65-
Author = pa,
66-
Tripcode = ptc,
67-
Time1 = time,
68-
Time2 = time2,
69-
Text = text
70-
};
71-
72-
return ValueTask.FromResult(p.Convert(r));
40+
var p = ChanPost.Parse(n);
41+
return ValueTask.FromResult(p.ToItem(r));
7342

7443
// ReSharper restore PossibleNullReferenceException
7544
}
@@ -94,7 +63,7 @@ public override void Dispose()
9463

9564
}
9665

97-
public record ChanPost : IResultConvertable
66+
public record ChanPost : ISearchResultItemConverter<ChanPost>
9867
{
9968

10069
public string Author;
@@ -112,7 +81,44 @@ public record ChanPost : IResultConvertable
11281
public string Tripcode;
11382
public int Width;
11483

115-
public SearchResultItem Convert(SearchResult sr)
84+
public static ChanPost Parse(INode n)
85+
{
86+
var e = n as HtmlElement;
87+
88+
var pff = e.QuerySelector(".post_file_filename");
89+
var pfm = e.QuerySelector(".post_file_metadata").TextContent.Split(", ");
90+
var pd = e.QuerySelector(".post_data");
91+
var pt = pd.QuerySelector(".post_title").TextContent;
92+
var pa = pd.QuerySelector(".post_author").TextContent;
93+
var ptc = pd.QuerySelector(".post_tripcode").TextContent;
94+
var tw = pd.QuerySelector(".time_wrap").Children[0];
95+
var time2Ok = DateTime.TryParse(tw.GetAttribute("datetime"), out var time2);
96+
var time = tw.TextContent;
97+
var text = e.QuerySelector(".text").TextContent;
98+
99+
var wh = pfm[1].Split('x');
100+
101+
var p = new ChanPost
102+
{
103+
Id = Int64.Parse(e.GetAttribute("id")),
104+
Board = e.GetAttribute("data-board"),
105+
Filename = pff.TextContent,
106+
File = pff.GetAttribute("href"),
107+
Width = Int32.Parse(wh[0]),
108+
Height = Int32.Parse(wh[1]),
109+
Size = pfm[0],
110+
Title = pt,
111+
Author = pa,
112+
Tripcode = ptc,
113+
Time1 = time,
114+
Time2 = time2,
115+
Text = text
116+
};
117+
118+
return p;
119+
}
120+
121+
public SearchResultItem ToItem(SearchResult sr)
116122
{
117123

118124
var sri = new SearchResultItem(sr)

SmartImage.Lib/Engines/Impl/Search/EHentaiEngine.cs

Lines changed: 76 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,78 @@ public ValueTask<bool> ApplyConfigAsync(SearchConfig cfg, CancellationToken ct =
305305

306306
protected override ValueTask<SearchResultItem> ParseResultItem(INode n, SearchResult r)
307307
{
308-
var item = new SearchResultItem(r);
308+
var eh = EhResult.Parse(n);
309+
var sri = eh.ToItem(r);
310+
return ValueTask.FromResult(sri);
311+
}
312+
313+
public override void Dispose()
314+
{
315+
// m_client.Dispose();
316+
// m_clientHandler.Dispose();
317+
Jar.Clear();
318+
319+
IsLoggedIn = false;
320+
}
321+
322+
public event PropertyChangedEventHandler PropertyChanged;
323+
324+
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
325+
{
326+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
327+
}
328+
329+
private bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
330+
{
331+
if (EqualityComparer<T>.Default.Equals(field, value))
332+
return false;
333+
334+
field = value;
335+
OnPropertyChanged(propertyName);
336+
return true;
337+
}
338+
339+
}
340+
341+
public sealed record EhResult : ISearchResultItemConverter<EhResult>
342+
{
343+
344+
public string Type { get; internal set; }
345+
346+
public string Pages { get; internal set; }
347+
348+
public string Title { get; internal set; }
349+
350+
public string Author { get; internal set; }
351+
352+
public string AuthorUrl { get; internal set; }
353+
354+
public Url Url { get; internal set; }
355+
356+
public ConcurrentDictionary<string, ConcurrentBag<string>> Tags { get; } = new();
357+
358+
359+
#region Implementation of IResultConverter2<out EhResult,out SearchResultItem>
360+
361+
public SearchResultItem ToItem(SearchResult sr)
362+
{
363+
var sb = Tags.Select(t => $"{t.Key}: {t.Value.QuickJoin()}").QuickJoin(" | ");
364+
365+
366+
var sri = new SearchResultItem(sr)
367+
{
368+
Title = Title,
369+
Url = Url,
370+
Artist = Author,
371+
Description = $"{Pages} ({sb})"
372+
};
373+
374+
return sri;
375+
}
376+
377+
public static EhResult Parse(INode n)
378+
{
379+
var item = new EhResult();
309380

310381
// ReSharper disable InconsistentNaming
311382
var eh = new EhResult();
@@ -378,11 +449,10 @@ protected override ValueTask<SearchResultItem> ParseResultItem(INode n, SearchRe
378449

379450

380451
if (eh.Tags.TryGetValue("artist", out var v)) {
381-
item.Artist = v.FirstOrDefault();
452+
item.Author = v.FirstOrDefault();
382453
}
383454

384-
var sb = eh.Tags.Select(t => $"{t.Key}: {t.Value.QuickJoin()}").QuickJoin(" | ");
385-
item.Description = $"{eh.Pages} ({sb})";
455+
386456
item.Title = eh.Title;
387457
item.Url = eh.Url;
388458

@@ -392,53 +462,10 @@ protected override ValueTask<SearchResultItem> ParseResultItem(INode n, SearchRe
392462
var gl3c = n.ChildNodes[3];
393463
var gl4c = n.ChildNodes[4];*/
394464

395-
return ValueTask.FromResult(item);
396-
397-
}
398-
399-
public override void Dispose()
400-
{
401-
// m_client.Dispose();
402-
// m_clientHandler.Dispose();
403-
Jar.Clear();
404-
405-
IsLoggedIn = false;
406-
}
407-
408-
public event PropertyChangedEventHandler PropertyChanged;
409-
410-
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
411-
{
412-
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
413-
}
414-
415-
private bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
416-
{
417-
if (EqualityComparer<T>.Default.Equals(field, value))
418-
return false;
465+
return item;
419466

420-
field = value;
421-
OnPropertyChanged(propertyName);
422-
return true;
423467
}
424468

425-
}
426-
427-
public sealed record EhResult
428-
{
429-
430-
public string Type { get; internal set; }
431-
432-
public string Pages { get; internal set; }
433-
434-
public string Title { get; internal set; }
435-
436-
public string Author { get; internal set; }
437-
438-
public string AuthorUrl { get; internal set; }
439-
440-
public Url Url { get; internal set; }
441-
442-
public ConcurrentDictionary<string, ConcurrentBag<string>> Tags { get; } = new();
469+
#endregion
443470

444471
}

SmartImage.Lib/Engines/Impl/Search/FluffleEngine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
9191
var fr = await response.GetJsonAsync<FluffleResponse>();
9292

9393
foreach (FluffleResult result in fr.Results) {
94-
var item = result.Convert(sr);
94+
var item = result.ToItem(sr);
9595
sr.Results.Add(item);
9696
}
9797

@@ -140,7 +140,7 @@ public class FluffleResultCredit
140140

141141
}
142142

143-
public class FluffleResult : IResultConvertable
143+
public class FluffleResult : ISearchResultItemConvertable
144144
{
145145

146146
[JsonPropertyName("id")]
@@ -167,7 +167,7 @@ public class FluffleResult : IResultConvertable
167167
[JsonPropertyName("credits")]
168168
public List<FluffleResultCredit> Credits { get; set; }
169169

170-
public SearchResultItem Convert(SearchResult sr)
170+
public SearchResultItem ToItem(SearchResult sr)
171171
{
172172

173173
var sri = new SearchResultItem(sr)

SmartImage.Lib/Engines/Impl/Search/GoogleLensEngine.cs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,56 @@
1414
using SmartImage.Lib.Images.Uni;
1515
using SmartImage.Lib.Results;
1616
using SmartImage.Lib.Results.Data;
17-
17+
// ReSharper disable UnusedMember.Local
18+
#pragma warning disable IDE0051
1819
namespace SmartImage.Lib.Engines.Impl.Search;
1920

20-
public class GoogleLensItem : IResultConverter2<GoogleLensItem>
21+
public class GoogleLensItem : ISearchResultItemConverter<GoogleLensItem>
2122
{
2223

2324
public string Title { get; private set; }
2425

2526
public string SiteName { get; private set; }
2627

27-
#region Implementation of IResultConverter2<out GoogleLensItem>
28+
public Url Link { get; private set; }
29+
30+
public string Ping { get; private set; }
2831

29-
public IEnumerable<SearchResultItem> ToResultItem(SearchResult sr)
32+
public SearchResultItem ToItem(SearchResult sr)
3033
{
3134
var sri = new SearchResultItem(sr)
3235
{
33-
36+
Title = Title,
37+
Site = SiteName,
38+
Url = Link
3439
};
3540

36-
return [sri];
41+
42+
return sri;
3743
}
3844

3945
public static GoogleLensItem Parse(INode n)
4046
{
4147
var gli = new GoogleLensItem();
4248

4349
if (n is IHtmlElement e) {
44-
var title = e.QuerySelector(".Yt787")?.TextContent;
50+
var attrHref = e.Attributes["href"];
51+
var attrPing = e.Attributes["ping"];
52+
var title = e.QuerySelector(".Yt787")?.TextContent;
4553

4654
//e.QuerySelector("//*[class*='gdOPf q07dbf uhHOwf ez24Df']");
47-
var siteName = e.SelectNodes("//*[contains(@class,'gdOPf')]");
55+
// var siteName = e.SelectNodes("//*[contains(@class,'gdOPf')]");
56+
//R8BTeb q8U8x LJEGod du278d i0Rdmd
57+
var siteName = e.QuerySelector(".R8BTeb");
58+
gli.Link = attrHref?.Value;
4859
gli.Title = title;
49-
gli.SiteName = siteName[0].TextContent;
60+
gli.Ping = attrPing?.Value;
61+
gli.SiteName = siteName.TextContent;
5062
}
5163

5264
return gli;
5365
}
5466

55-
#endregion
56-
5767
}
5868

5969
public class GoogleLensEngine : WebSearchEngine, IEndpointEngine /*, ICookiesEngine*/
@@ -69,7 +79,7 @@ public class GoogleLensEngine : WebSearchEngine, IEndpointEngine /*, ICookiesEng
6979

7080
public override SearchEngineOptions EngineOption => SearchEngineOptions.GoogleLens;
7181

72-
protected override string NodesSelector => throw new NotImplementedException();
82+
protected override string NodesSelector => null;
7383

7484
public Url EndpointUrl => URL_BASE;
7585

@@ -91,14 +101,11 @@ public GoogleLensEngine() : base(URL_BASE) { }
91101
Accept = "*/*"
92102
};
93103

94-
protected override async ValueTask<SearchResultItem> ParseResultItem(INode n, SearchResult r)
104+
protected override ValueTask<SearchResultItem> ParseResultItem(INode n, SearchResult r)
95105
{
96-
97-
98-
var sri = new SearchResultItem(r)
99-
{ };
100-
101-
return sri;
106+
var gli = GoogleLensItem.Parse(n);
107+
var sri = gli.ToItem(r);
108+
return ValueTask.FromResult(sri);
102109
}
103110

104111
public override async Task<SearchResult> GetResultAsync(SearchQuery query, CancellationToken token = default)
@@ -119,21 +126,22 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
119126
return null;
120127
}
121128

129+
//todo
122130
req = SearchUrlAsync(query, token);
123131

124132
res = await req;
125133

126134
// var stream = await res.GetStringAsync();
127135
var url = res.ResponseMessage.RequestMessage.RequestUri;
128136

129-
var res2 = await Client.Request(url)
130-
.WithTimeout(Timeout)
131-
.WithCookie(Nid.Name, Nid.Value)
137+
using var res2 = await Client.Request(url)
138+
.WithTimeout(Timeout)
139+
.WithCookie(Nid.Name, Nid.Value)
132140

133-
// .WithHeaders(Headers)
134-
.GetAsync(cancellationToken: token);
141+
// .WithHeaders(Headers)
142+
.GetAsync(cancellationToken: token);
135143

136-
var resData = await res2.GetStringAsync();
144+
var resData = await res2.GetStreamAsync();
137145

138146
var parser = new HtmlParser(new HtmlParserOptions()
139147
{

0 commit comments

Comments
 (0)