Skip to content

Commit 1b76d09

Browse files
committed
work on Google Lens
1 parent 60e1a0b commit 1b76d09

File tree

8 files changed

+112
-124
lines changed

8 files changed

+112
-124
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ protected override Url GetRawUrl(SearchQuery query)
2828
{
2929
Base64Hash = GetHash(query);
3030

31-
var r=Url.Combine(BaseUrl, "image", Base64Hash);
31+
var r = Url.Combine(BaseUrl, "image", Base64Hash);
3232
return r;
33+
3334
// return (BaseUrl.AppendPathSegments("image").AppendPathSegment(Base64Hash));
3435
}
3536

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using FlareSolverrSharp.Types;
1111
using Flurl.Http;
1212
using Kantan.Net.Utilities;
13+
using Microsoft.Extensions.Logging;
1314
using SmartImage.Lib.Clients;
1415
using SmartImage.Lib.Cookies;
1516
using SmartImage.Lib.Results;
@@ -203,8 +204,8 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
203204
}
204205
catch (FlurlHttpException e) {
205206
// return await Task.FromException<IDocument>(e);
206-
Debug.WriteLine($"{this} :: {e.Message}", nameof(GetDocumentAsync));
207-
207+
// Debug.WriteLine($"{this} :: {e.Message}", nameof(GetDocumentAsync));
208+
Logger.LogError(e, "{Name} error in {Fn}",Name, nameof(GetDocumentAsync));
208209
return null;
209210
}
210211
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Flurl.Http.Content;
1313
using Kantan.Net.Utilities;
1414
using Kantan.Text;
15+
using Microsoft.Extensions.Logging;
1516
using SmartImage.Lib.Cookies;
1617
using SmartImage.Lib.Results;
1718
using SmartImage.Lib.Results.Data;
@@ -96,7 +97,8 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
9697
}
9798

9899
if (filePath != null) {
99-
Trace.WriteLine($"allocated {filePath}", nameof(GetDocumentAsync));
100+
// Trace.WriteLine($"allocated {filePath}", nameof(GetDocumentAsync));
101+
Logger.LogTrace("Allocated {Path}", filePath);
100102
}
101103

102104
var data = new MultipartFormDataContent
@@ -112,7 +114,7 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
112114
};
113115

114116

115-
Debug.WriteLine($"{LookupUrl}", nameof(GetDocumentAsync));
117+
// Debug.WriteLine($"{LookupUrl}", nameof(GetDocumentAsync));
116118

117119
var req = new FlurlRequest(LookupUrl)
118120
{
@@ -140,7 +142,7 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
140142
// var content2 = await sr.RawUrl.GetStringAsync(cancellationToken: token);
141143

142144
if (content.Contains("Please wait a bit longer between each file search.")) {
143-
Debug.WriteLine("cooldown", Name);
145+
// Debug.WriteLine("cooldown", Name);
144146
sr.Status = SearchResultStatus.Cooldown;
145147

146148
return null;
@@ -338,7 +340,7 @@ private bool SetField<T>(ref T field, T value, [CallerMemberName] string propert
338340

339341
}
340342

341-
public sealed record EhResult : ISearchResultItemConverter<EhResult>
343+
public sealed record EhResult : ISearchResultItemConvertable
342344
{
343345

344346
public string Type { get; internal set; }

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

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
using AngleSharp.Html.Parser;
99
using AngleSharp.XPath;
1010
using Flurl.Http;
11+
using Flurl.Http.Content;
1112
using Kantan.Net.Utilities;
1213
using Kantan.Net.Web;
14+
using Microsoft.Extensions.Logging;
1315
using SmartImage.Lib.Cookies;
1416
using SmartImage.Lib.Images.Uni;
1517
using SmartImage.Lib.Results;
@@ -72,16 +74,17 @@ public class GoogleLensEngine : WebSearchEngine, IEndpointEngine, ICookiesEngine
7274

7375
// TODO: WIP
7476

75-
public const string URL_BASE = "https://lens.google.com";
77+
public const string URL_BASE = "https://lens.google.com/";
78+
public const string URL_BASE2 = "https://www.google.com/";
7679

7780
public override string Name => "Google Lens";
7881

79-
public override Url BaseUrl => URL_BASE;
80-
8182
public override SearchEngineOptions EngineOption => SearchEngineOptions.GoogleLens;
8283

8384
protected override string NodesSelector => null;
8485

86+
public override Url BaseUrl => URL_BASE;
87+
8588
public Url EndpointUrl => URL_BASE;
8689

8790
public GoogleLensEngine() : base(URL_BASE) { }
@@ -112,28 +115,63 @@ protected override ValueTask<SearchResultItem> ParseResultItem(INode n, SearchRe
112115
public override async Task<SearchResult> GetResultAsync(SearchQuery query, CancellationToken token = default)
113116
{
114117
var br = await base.GetResultAsync(query, token);
118+
115119
return br;
116120
}
117121

122+
private Task<IFlurlResponse> SearchFileAsync(SearchQuery query, CancellationToken token)
123+
{
124+
string endpoint;
125+
UniImageFile uif = query.Source as UniImageFile;
126+
string filename = uif.FileInfo.Name;
127+
Task<IFlurlResponse> req;
128+
endpoint = "v3/upload";
129+
130+
// filename = "image.jpg";
131+
// filename = (query.Source is UniImageFile uif) ? uif.FileInfo.Name : "image.jpg";
132+
filename = uif.FileInfo.Name;
133+
134+
req = Client.Request(EndpointUrl, endpoint)
135+
.SetQueryParam("hl", HlParam)
136+
.WithTimeout(Timeout)
137+
.WithCookie(Nid.Name, Nid.Value)
138+
.WithHeaders(Headers)
139+
.PostMultipartAsync(bc =>
140+
{
141+
//
142+
bc.AddFile(filename, uif.FilePath, contentType:"image/jpeg");
143+
}, cancellationToken: token);
144+
145+
return req;
146+
}
147+
118148
protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, SearchQuery query, CancellationToken token = default)
119149
{
120150
string endpoint, filename;
121-
Task<IFlurlResponse> req;
122-
IFlurlResponse res;
151+
Task<IFlurlResponse> req = null;
152+
153+
IFlurlResponse res = null;
154+
155+
//todo
156+
123157

124-
if (query.Source.IsFile) { }
125-
else if (query.Source.IsUri) { }
158+
if (query.Source.IsUri) {
159+
160+
req = SearchUrlAsync(query, token);
161+
res = await req;
162+
}
163+
else if (query.Source.IsFile) {
164+
req = SearchFileAsync(query, token);
165+
res = await req;
166+
}
126167
else {
127168
return null;
128169
}
129170

130-
//todo
131-
req = SearchUrlAsync(query, token);
132-
133-
res = await req;
171+
Logger.LogTrace("{Uri} {Code}", res.ResponseMessage.RequestMessage.RequestUri, res.StatusCode);
134172

135173
// var stream = await res.GetStringAsync();
136-
var url = res.ResponseMessage.RequestMessage.RequestUri;
174+
/*var url = res.ResponseMessage.RequestMessage.RequestUri;
137175
138176
using var res2 = await Client.Request(url)
139177
.WithTimeout(Timeout)
@@ -142,7 +180,10 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
142180
// .WithHeaders(Headers)
143181
.GetAsync(cancellationToken: token);
144182
145-
var resData = await res2.GetStreamAsync();
183+
var resData = await res2.GetStreamAsync();*/
184+
var str = await res.GetStringAsync();
185+
186+
var resData = await res.GetStreamAsync();
146187

147188
var parser = new HtmlParser(new HtmlParserOptions()
148189
{
@@ -159,32 +200,6 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
159200
return doc;
160201
}
161202

162-
private Task<IFlurlResponse> SearchFileAsync(SearchQuery query, CancellationToken token)
163-
{
164-
string endpoint;
165-
string filename;
166-
UniImageFile uif = query.Source as UniImageFile;
167-
Task<IFlurlResponse> req;
168-
endpoint = "v3/upload";
169-
170-
// filename = "image.jpg";
171-
// filename = (query.Source is UniImageFile uif) ? uif.FileInfo.Name : "image.jpg";
172-
filename = uif.FileInfo.Name;
173-
174-
req = Client.Request(EndpointUrl, endpoint)
175-
.SetQueryParam("hl", HlParam)
176-
.WithTimeout(Timeout)
177-
.WithCookie(Nid.Name, Nid.Value)
178-
.WithHeaders(Headers)
179-
.PostMultipartAsync(bc =>
180-
{
181-
//
182-
bc.AddFile(filename, uif.FilePath);
183-
}, cancellationToken: token);
184-
185-
return req;
186-
}
187-
188203
private Task<IFlurlResponse> SearchUrlAsync(SearchQuery query, CancellationToken token)
189204
{
190205
return SearchUrlAsync(query.Upload, token);
@@ -196,13 +211,14 @@ private Task<IFlurlResponse> SearchUrlAsync(Url url, CancellationToken token)
196211
Task<IFlurlResponse> req;
197212
endpoint = "uploadbyurl";
198213

199-
req = Client.Request(EndpointUrl, endpoint)
214+
var req1 = Client.Request(EndpointUrl, endpoint)
200215
.SetQueryParam("hl", HlParam)
201216
.SetQueryParam("url", url)
202217
.WithCookie(Nid.Name, Nid.Value)
203218
.WithHeaders(Headers)
204-
.WithTimeout(Timeout)
205-
.GetAsync(cancellationToken: token);
219+
.WithTimeout(Timeout);
220+
221+
req = req1.GetAsync(cancellationToken: token);
206222

207223
// var sz = await (await req).GetStringAsync();
208224

@@ -245,7 +261,7 @@ public async ValueTask<bool> ApplyCookiesAsync(CancellationToken token = default
245261
public ValueTask<bool> ApplyConfigAsync(SearchConfig cfg, CancellationToken ct = default)
246262
{
247263
Provider = cfg.CookiesProvider;
248-
264+
249265
return ApplyCookiesAsync(ct);
250266
}
251267

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Text.Json.Serialization;
44
using Flurl.Http;
55
using Kantan.Monad;
6+
using Microsoft.Extensions.Logging;
67
using SmartImage.Lib.Results;
78

89
namespace SmartImage.Lib.Engines.Impl.Search;
@@ -21,7 +22,8 @@ public TinEyeEngine() : base("https://www.tineye.com/search?url=")
2122

2223
public override void Dispose()
2324
{
24-
Debug.WriteLine($"Disposing {Name}");
25+
// Debug.WriteLine($"Disposing {Name}");
26+
Logger.LogTrace("Disposing {Name}", Name);
2527
}
2628

2729
public override async ValueTask<bool> TryVerifyQueryAsync(SearchQuery q)
@@ -66,7 +68,7 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
6668
}
6769
catch (Exception e) {
6870
// Debugger.Break();
69-
Trace.WriteLine(e.Message);
71+
Logger.LogError(e, "{Name}", Name);
7072
sr.Status = SearchResultStatus.UnknownError;
7173
goto ret;
7274
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using JetBrains.Annotations;
77
using Kantan.Collections;
88
using Kantan.Text;
9+
using Microsoft.Extensions.Logging;
910
using SmartImage.Lib.Clients;
1011
using SmartImage.Lib.Results;
1112
using SmartImage.Lib.Results.Data;
@@ -86,7 +87,8 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
8687
// tm = JsonSerializer.Deserialize<TraceMoeRootObject>(json);
8788
}
8889
catch (Exception e) {
89-
Debug.WriteLine($"{Name} :: {nameof(Process)}: {e.Message}", nameof(GetResultAsync));
90+
// Debug.WriteLine($"{Name} :: {nameof(Process)}: {e.Message}", nameof(GetResultAsync));
91+
Logger.LogError(e, "{Name} in {Fn}", Name, nameof(GetResultAsync));
9092
r.ErrorMessage = e.Message;
9193
r.Status = SearchResultStatus.UnknownError;
9294
goto ret;
@@ -109,7 +111,8 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
109111

110112
}
111113
else if (tm.Error != null) {
112-
Debug.WriteLine($"{Name} :: API error: {tm.Error}", nameof(GetResultAsync));
114+
// Debug.WriteLine($"{Name} :: API error: {tm.Error}", nameof(GetResultAsync));
115+
Logger.LogDebug("{Name} :: API error {Err} in {Fn}", Name, tm.Error, nameof(GetResultAsync));
113116
r.ErrorMessage = tm.Error;
114117
r.Status = SearchResultStatus.IllegalInput;
115118

@@ -142,7 +145,8 @@ private async Task<IEnumerable<SearchResultItem>> ConvertResultsAsync(TraceMoeRo
142145
result.Metadata = doc;
143146
}
144147
catch (Exception e) {
145-
Debug.WriteLine($"{this} :: {e.Message}", nameof(ConvertResultsAsync));
148+
// Debug.WriteLine($"{this} :: {e.Message}", nameof(ConvertResultsAsync));
149+
Logger.LogError(e, "{Name} error in {Fn}", Name, nameof(ConvertResultsAsync));
146150
}
147151

148152
items[i] = result;

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Extensions.Logging;
1717
using SmartImage.Lib.Images.Uni;
1818
using SmartImage.Lib.Results;
19+
using SmartImage.Lib.Results.Data;
1920

2021
// ReSharper disable SuggestVarOrType_SimpleTypes
2122

@@ -78,17 +79,6 @@ protected override Url GetRawUrl(SearchQuery query)
7879

7980
#endregion
8081

81-
private static SearchResultItem Convert(SearchResult sr, YandexSite obj)
82-
{
83-
return new SearchResultItem(sr)
84-
{
85-
Title = obj.Title,
86-
Description = obj.Description,
87-
Url = obj.OriginalImage.Url,
88-
Site = obj.Domain,
89-
Thumbnail = obj.Thumb.Url.StartsWith("//") ? "https:" + obj.Thumb.Url : obj.Thumb.Url
90-
};
91-
}
9282

9383
public override async Task<SearchResult> GetResultAsync(SearchQuery query, CancellationToken token = default)
9484
{
@@ -113,6 +103,7 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
113103

114104
try {
115105
res = await Client.Request(sr.RawUrl)
106+
.WithTimeout(Timeout)
116107
.GetAsync(cancellationToken: token);
117108

118109
str = await res.GetStreamAsync();
@@ -126,10 +117,9 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
126117
var jsonNode = JsonNode.Parse(json);
127118
var sites = jsonNode["initialState"]["cbirSites"]["sites"];
128119
var sitesObj = sites.Deserialize<YandexSite[]>();
120+
var sri = sitesObj.AsParallel().Select(e => e.ToItem(sr));
121+
sr.Results.AddRange(sri);
129122

130-
foreach (var ys in sitesObj) {
131-
sr.Results.Add(Convert(sr, ys));
132-
}
133123

134124
}
135125
catch (Exception e) {
@@ -157,7 +147,7 @@ public override void Dispose() { }
157147

158148
}
159149

160-
public class YandexImage
150+
public record YandexImage
161151
{
162152

163153
[JsonPropertyName("url")]
@@ -171,7 +161,7 @@ public class YandexImage
171161

172162
}
173163

174-
public class YandexSite
164+
public record YandexSite : ISearchResultItemConvertable
175165
{
176166

177167
[JsonPropertyName("title")]
@@ -192,4 +182,16 @@ public class YandexSite
192182
[JsonPropertyName("originalImage")]
193183
public YandexImage OriginalImage { get; set; }
194184

185+
public SearchResultItem ToItem(SearchResult sr)
186+
{
187+
return new SearchResultItem(sr)
188+
{
189+
Title = Title,
190+
Description = Description,
191+
Url = OriginalImage.Url,
192+
Site = Domain,
193+
Thumbnail = Thumb.Url.StartsWith("//") ? "https:" + Thumb.Url : Thumb.Url
194+
};
195+
}
196+
195197
}

0 commit comments

Comments
 (0)