Skip to content

Commit 0d9fccf

Browse files
committed
Work on E-Hentai
1 parent 9f08e08 commit 0d9fccf

File tree

4 files changed

+264
-215
lines changed

4 files changed

+264
-215
lines changed

SmartImage.Lib 3/Clients/HydrusClient.cs

Lines changed: 191 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -23,200 +23,209 @@ namespace SmartImage.Lib.Clients;
2323

2424
public class HydrusClient : INotifyPropertyChanged, IDisposable
2525
{
26-
private const string HDR_HYDRUS_KEY = "Hydrus-Client-API-Access-Key";
27-
private const string GET_FILES_THUMBNAIL = "/get_files/thumbnail";
28-
private const string GET_FILES_FILE = "/get_files/file";
29-
30-
public FlurlClient Client { get; }
31-
32-
public HydrusClient(string endpoint, string key)
33-
{
34-
EndpointUrl = endpoint;
35-
Key = key;
36-
37-
Client = new FlurlClient(EndpointUrl)
38-
{
39-
Headers =
40-
{
41-
{ HDR_HYDRUS_KEY, key }
42-
}
43-
};
44-
45-
PropertyChanged += (sender, args) =>
46-
{
47-
switch (args.PropertyName)
48-
{
49-
case nameof(Key):
50-
Client.Headers.AddOrReplace(HDR_HYDRUS_KEY, Key);
51-
break;
52-
case nameof(EndpointUrl):
53-
Client.BaseUrl = EndpointUrl;
54-
break;
55-
}
56-
57-
};
58-
}
59-
60-
public HydrusClient() : this(null, null) { }
61-
62-
public bool IsValid => EndpointUrl != null && Key != null;
63-
64-
public async Task<JsonValue> GetFileHashesAsync(string hash, string hashType = "sha256")
65-
{
66-
67-
using var res = await Client.Request("/get_files/file_hashes")
68-
.SetQueryParam("hash", hash)
69-
.SetQueryParam("source_hash_type", hashType)
70-
.SetQueryParam("desired_hash_type", hashType)
71-
.GetAsync();
72-
73-
var b = await res.GetStreamAsync();
74-
var j = JsonValue.Load(b);
75-
return j;
76-
77-
}
78-
79-
public async Task<JsonValue> GetFileMetadataAsync(string hash)
80-
{
81-
82-
using var res = await Client.Request("/get_files/file_metadata")
83-
.SetQueryParam("hash", hash)
84-
.GetAsync();
85-
86-
var b = await res.GetStreamAsync();
87-
var j = JsonValue.Load(b);
88-
return j;
89-
}
90-
91-
public async Task<JsonValue> GetFileRelationshipsAsync(string hash)
92-
{
93-
using var res = await Client.Request("/manage_file_relationships/get_file_relationships")
94-
.SetQueryParam("hash", hash)
95-
.GetAsync();
96-
97-
var b = await res.GetStreamAsync();
98-
var j = JsonValue.Load(b);
99-
100-
return j;
101-
}
102-
103-
public async Task<IFlurlResponse> GetFileAsync(string hash)
104-
{
105-
var res = await Client.Request(GET_FILES_FILE)
106-
.SetQueryParam("hash", hash)
107-
.GetAsync();
108-
109-
return res;
110-
}
111-
public async Task<IFlurlResponse> GetFileAsync(int id)
112-
{
113-
var res = await Client.Request(GET_FILES_FILE)
114-
.SetQueryParam("file_id", id)
115-
.GetAsync();
116-
117-
return res;
118-
}
119-
public async Task<IFlurlResponse> GetFileThumbnailAsync(string hash)
120-
{
121-
var res = await Client.Request(GET_FILES_THUMBNAIL)
122-
.SetQueryParam("hash", hash)
123-
.GetAsync();
124-
125-
return res;
126-
}
127-
128-
public async Task<IFlurlResponse> GetFileThumbnailAsync(int id)
129-
{
130-
var res = await Client.Request(GET_FILES_THUMBNAIL)
131-
.SetQueryParam("file_id", id)
132-
.GetAsync();
133-
134-
return res;
135-
}
136-
137-
private string m_key;
138-
139-
public string Key
140-
{
141-
get => m_key;
142-
set
143-
{
144-
if (value == m_key) return;
145-
m_key = value;
146-
OnPropertyChanged();
147-
OnPropertyChanged(nameof(IsValid));
148-
}
149-
}
150-
151-
private string m_endpointUrl;
152-
153-
public string EndpointUrl
154-
{
155-
get => m_endpointUrl;
156-
set
157-
{
158-
if (value == m_endpointUrl) return;
159-
m_endpointUrl = value;
160-
OnPropertyChanged();
161-
OnPropertyChanged(nameof(IsValid));
162-
}
163-
}
164-
165-
public void Dispose()
166-
{
167-
GC.SuppressFinalize(this);
168-
Client.Dispose();
169-
}
170-
171-
public event PropertyChangedEventHandler PropertyChanged;
172-
173-
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
174-
{
175-
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
176-
}
177-
178-
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
179-
{
180-
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
181-
field = value;
182-
OnPropertyChanged(propertyName);
183-
return true;
184-
}
26+
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";
30+
31+
public FlurlClient Client { get; }
32+
33+
public HydrusClient(string endpoint, string key)
34+
{
35+
EndpointUrl = endpoint;
36+
Key = key;
37+
38+
Client = new FlurlClient(EndpointUrl)
39+
{
40+
Headers =
41+
{
42+
{ HDR_HYDRUS_KEY, key }
43+
}
44+
};
45+
46+
PropertyChanged += (sender, args) =>
47+
{
48+
switch (args.PropertyName) {
49+
case nameof(Key):
50+
Client.Headers.AddOrReplace(HDR_HYDRUS_KEY, Key);
51+
break;
52+
53+
case nameof(EndpointUrl):
54+
Client.BaseUrl = EndpointUrl;
55+
break;
56+
}
57+
58+
};
59+
}
60+
61+
public HydrusClient() : this(null, null) { }
62+
63+
public bool IsValid => EndpointUrl != null && Key != null;
64+
65+
public async Task<JsonValue> GetFileHashesAsync(string hash, string hashType = "sha256")
66+
{
67+
68+
using var res = await Client.Request("/get_files/file_hashes")
69+
.SetQueryParam("hash", hash)
70+
.SetQueryParam("source_hash_type", hashType)
71+
.SetQueryParam("desired_hash_type", hashType)
72+
.GetAsync();
73+
74+
var b = await res.GetStreamAsync();
75+
var j = JsonValue.Load(b);
76+
return j;
77+
78+
}
79+
80+
public async Task<JsonValue> GetFileMetadataAsync(string hash)
81+
{
82+
83+
using var res = await Client.Request("/get_files/file_metadata")
84+
.SetQueryParam("hash", hash)
85+
.GetAsync();
86+
87+
var b = await res.GetStreamAsync();
88+
var j = JsonValue.Load(b);
89+
return j;
90+
}
91+
92+
public async Task<JsonValue> GetFileRelationshipsAsync(string hash)
93+
{
94+
using var res = await Client.Request("/manage_file_relationships/get_file_relationships")
95+
.SetQueryParam("hash", hash)
96+
.GetAsync();
97+
98+
var b = await res.GetStreamAsync();
99+
var j = JsonValue.Load(b);
100+
101+
return j;
102+
}
103+
104+
public async Task<IFlurlResponse> GetFileAsync(string hash)
105+
{
106+
var res = await Client.Request(GET_FILES_FILE)
107+
.SetQueryParam("hash", hash)
108+
.GetAsync();
109+
110+
return res;
111+
}
112+
113+
public async Task<IFlurlResponse> GetFileAsync(int id)
114+
{
115+
var res = await Client.Request(GET_FILES_FILE)
116+
.SetQueryParam("file_id", id)
117+
.GetAsync();
118+
119+
return res;
120+
}
121+
122+
public async Task<IFlurlResponse> GetFileThumbnailAsync(string hash)
123+
{
124+
var res = await Client.Request(GET_FILES_THUMBNAIL)
125+
.SetQueryParam("hash", hash)
126+
.GetAsync();
127+
128+
return res;
129+
}
130+
131+
public async Task<IFlurlResponse> GetFileThumbnailAsync(int id)
132+
{
133+
var res = await Client.Request(GET_FILES_THUMBNAIL)
134+
.SetQueryParam("file_id", id)
135+
.GetAsync();
136+
137+
return res;
138+
}
139+
140+
private string m_key;
141+
142+
public string Key
143+
{
144+
get => m_key;
145+
set
146+
{
147+
if (value == m_key) return;
148+
149+
m_key = value;
150+
OnPropertyChanged();
151+
OnPropertyChanged(nameof(IsValid));
152+
}
153+
}
154+
155+
private string m_endpointUrl;
156+
157+
public string EndpointUrl
158+
{
159+
get => m_endpointUrl;
160+
set
161+
{
162+
if (value == m_endpointUrl) return;
163+
164+
m_endpointUrl = value;
165+
OnPropertyChanged();
166+
OnPropertyChanged(nameof(IsValid));
167+
}
168+
}
169+
170+
public void Dispose()
171+
{
172+
GC.SuppressFinalize(this);
173+
Client.Dispose();
174+
}
175+
176+
public event PropertyChangedEventHandler PropertyChanged;
177+
178+
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
179+
{
180+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
181+
}
182+
183+
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
184+
{
185+
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
186+
187+
field = value;
188+
OnPropertyChanged(propertyName);
189+
return true;
190+
}
191+
185192
}
186193
#pragma warning disable IL2026
187194

188195
public partial class FileRelationship
189196
{
190-
[JsonPropertyName("0")]
191-
public string[] PotentialDuplicates { get; set; }
192197

193-
[JsonPropertyName("1")]
194-
public string[] FalsePositives { get; set; }
198+
[JsonPropertyName("0")]
199+
public string[] PotentialDuplicates { get; set; }
200+
201+
[JsonPropertyName("1")]
202+
public string[] FalsePositives { get; set; }
203+
204+
[JsonPropertyName("3")]
205+
public string[] Alternates { get; set; }
206+
207+
[JsonPropertyName("8")]
208+
public string[] Duplicates { get; set; }
195209

196-
[JsonPropertyName("3")]
197-
public string[] Alternates { get; set; }
210+
[JsonPropertyName("is_king")]
211+
public bool IsKing { get; set; }
198212

199-
[JsonPropertyName("8")]
200-
public string[] Duplicates { get; set; }
213+
[JsonPropertyName("king")]
214+
public string King { get; set; }
201215

202-
[JsonPropertyName("is_king")]
203-
public bool IsKing { get; set; }
216+
[JsonPropertyName("king_is_local")]
217+
public bool KingIsLocal { get; set; }
204218

205-
[JsonPropertyName("king")]
206-
public string King { get; set; }
219+
[JsonPropertyName("king_is_on_file_domain")]
220+
public bool KingIsOnFileDomain { get; set; }
207221

208-
[JsonPropertyName("king_is_local")]
209-
public bool KingIsLocal { get; set; }
222+
public static Dictionary<string, FileRelationship> Deserialize(JsonValue v)
223+
{
224+
var vs = ((JsonObject) v)["file_relationships"];
210225

211-
[JsonPropertyName("king_is_on_file_domain")]
212-
public bool KingIsOnFileDomain { get; set; }
226+
var re = JsonSerializer.Deserialize<Dictionary<string, FileRelationship>>(vs.ToString());
213227

214-
public static Dictionary<string, FileRelationship> Deserialize(JsonValue v)
215-
{
216-
var vs = ((JsonObject)v)["file_relationships"];
217-
218-
var re = JsonSerializer.Deserialize<Dictionary<string, FileRelationship>>(vs.ToString());
228+
return re;
229+
}
219230

220-
return re;
221-
}
222231
}

0 commit comments

Comments
 (0)