Skip to content

Commit 903adf8

Browse files
committed
Work on Hydrus
1 parent 9fc42e2 commit 903adf8

File tree

1 file changed

+107
-19
lines changed

1 file changed

+107
-19
lines changed

SmartImage.Lib 3/Clients/HydrusClient.cs

Lines changed: 107 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ namespace SmartImage.Lib.Clients;
2424
public class HydrusClient : INotifyPropertyChanged, IDisposable
2525
{
2626

27-
private const string HDR_HYDRUS_KEY = "Hydrus-Client-API-Access-Key";
28-
private const string GET_FILES_THUMBNAIL = "/get_files/thumbnail";
29-
private const string GET_FILES_FILE = "/get_files/file";
27+
private const string HDR_HYDRUS_KEY = "Hydrus-Client-API-Access-Key";
3028

3129
public FlurlClient Client { get; }
3230

@@ -77,22 +75,25 @@ public async Task<JsonValue> GetFileHashesAsync(string hash, string hashType = "
7775

7876
}
7977

80-
public async Task<JsonValue> GetFileMetadataAsync(string hash)
78+
public async Task<JsonValue> GetFileMetadataAsync(HydrusQuery q)
8179
{
80+
var (name, value) = q.GetValue();
8281

8382
using var res = await Client.Request("/get_files/file_metadata")
84-
.SetQueryParam("hash", hash)
83+
.SetQueryParam(name, value)
8584
.GetAsync();
8685

8786
var b = await res.GetStreamAsync();
8887
var j = JsonValue.Load(b);
8988
return j;
9089
}
9190

92-
public async Task<JsonValue> GetFileRelationshipsAsync(string hash)
91+
public async Task<JsonValue> GetFileRelationshipsAsync(HydrusQuery q)
9392
{
93+
var (name, value) = q.GetValue();
94+
9495
using var res = await Client.Request("/manage_file_relationships/get_file_relationships")
95-
.SetQueryParam("hash", hash)
96+
.SetQueryParam(name, value)
9697
.GetAsync();
9798

9899
var b = await res.GetStreamAsync();
@@ -101,37 +102,41 @@ public async Task<JsonValue> GetFileRelationshipsAsync(string hash)
101102
return j;
102103
}
103104

104-
public async Task<IFlurlResponse> GetFileAsync(string hash)
105+
public async Task<IFlurlResponse> GetFileAsync(HydrusQuery q)
105106
{
106-
var res = await Client.Request(GET_FILES_FILE)
107-
.SetQueryParam("hash", hash)
107+
var (name, value) = q.GetValue();
108+
109+
var res = await Client.Request("/get_files/file")
110+
.SetQueryParam(name, value)
108111
.GetAsync();
109112

110113
return res;
111114
}
112115

113-
public async Task<IFlurlResponse> GetFileAsync(int id)
116+
public async Task<IFlurlResponse> GetFileThumbnailAsync(HydrusQuery q)
114117
{
115-
var res = await Client.Request(GET_FILES_FILE)
116-
.SetQueryParam("file_id", id)
118+
var (name, value) = q.GetValue();
119+
120+
var res = await Client.Request("/get_files/thumbnail")
121+
.SetQueryParam(name, value)
117122
.GetAsync();
118123

119124
return res;
120125
}
121126

122-
public async Task<IFlurlResponse> GetFileThumbnailAsync(string hash)
127+
public async Task<IFlurlResponse> GetUrlInfoAsync(string url)
123128
{
124-
var res = await Client.Request(GET_FILES_THUMBNAIL)
125-
.SetQueryParam("hash", hash)
129+
var res = await Client.Request("/add_urls/get_url_info")
130+
.SetQueryParam("url", url)
126131
.GetAsync();
127132

128133
return res;
129134
}
130135

131-
public async Task<IFlurlResponse> GetFileThumbnailAsync(int id)
136+
public async Task<IFlurlResponse> GetUrlFilesAsync(string url)
132137
{
133-
var res = await Client.Request(GET_FILES_THUMBNAIL)
134-
.SetQueryParam("file_id", id)
138+
var res = await Client.Request("/add_urls/get_url_files")
139+
.SetQueryParam("url", url)
135140
.GetAsync();
136141

137142
return res;
@@ -189,7 +194,90 @@ protected bool SetField<T>(ref T field, T value, [CallerMemberName] string prope
189194
return true;
190195
}
191196

197+
public static string HyEncode(object o)
198+
{
199+
return Url.Encode(JsonSerializer.Serialize(o));
200+
}
201+
192202
}
203+
204+
public sealed class HydrusQuery
205+
{
206+
207+
/*
208+
209+
file_id: (selective, a numerical file id)
210+
file_ids: (selective, a list of numerical file ids)
211+
hash: (selective, a hexadecimal SHA256 hash)
212+
hashes: (selective, a list of hexadecimal SHA256 hashes)
213+
*
214+
*/
215+
216+
public long? FileId { get; init; }
217+
218+
public IEnumerable<long> FileIds { get; init; }
219+
220+
public string Hash { get; init; }
221+
222+
public IEnumerable<string> Hashes { get; init; }
223+
224+
public HydrusQuery(long? fileId)
225+
{
226+
FileId = fileId;
227+
}
228+
229+
public HydrusQuery(string h)
230+
{
231+
Hash = h;
232+
}
233+
234+
public HydrusQuery(IEnumerable<string> hashes)
235+
{
236+
Hashes = hashes;
237+
}
238+
239+
public HydrusQuery(IEnumerable<long> fileIds)
240+
{
241+
FileIds = fileIds;
242+
}
243+
244+
public static implicit operator HydrusQuery(long l)
245+
=> new(l);
246+
247+
public static implicit operator HydrusQuery(long[] l)
248+
=> new(l);
249+
250+
public static implicit operator HydrusQuery(string s)
251+
=> new(s);
252+
253+
public static implicit operator HydrusQuery(string[] s)
254+
=> new(s);
255+
256+
public (string Name, string Value) GetValue()
257+
{
258+
if (FileId.HasValue) {
259+
return ("file_id", FileId.Value.ToString());
260+
}
261+
262+
if (FileIds != null && FileIds.Any()) {
263+
return ("file_ids", HydrusClient.HyEncode(FileIds));
264+
}
265+
266+
if (!String.IsNullOrWhiteSpace(Hash)) {
267+
return ("hash", Hash);
268+
}
269+
270+
if (Hashes != null && Hashes.Any()) {
271+
return ("hashes", HydrusClient.HyEncode(Hashes));
272+
273+
// return ("hashes", $"[{Url.Encode(String.Join(", ", Hashes), true)}]");
274+
}
275+
276+
return (null, null);
277+
}
278+
279+
}
280+
193281
#pragma warning disable IL2026
194282

195283
public partial class HydrusFileRelationship

0 commit comments

Comments
 (0)