-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Booru.cs
460 lines (398 loc) · 12.6 KB
/
Booru.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
using System;
using System.Net;
using System.Net.Http;
using System.Security.Authentication;
using System.Threading.Tasks;
using BooruDex.Exceptions;
using BooruDex.Models;
using Litdex.Security.RNG;
using Litdex.Security.RNG.PRNG;
namespace BooruDex.Booru
{
/// <summary>
/// Base class for all booru client.
/// </summary>
public abstract class Booru
{
#region Member
/// <summary>
/// Browser
/// </summary>
private HttpClient _HttpClient;
/// <summary>
/// Base API request URL.
/// </summary>
protected Uri _BaseUrl;
/// <summary>
/// Max retrieved post for each request.
/// </summary>
protected byte _PostLimit;
/// <summary>
/// Max tags to use for search a post.
/// </summary>
protected byte _TagsLimit;
/// <summary>
/// Max page number.
/// </summary>
protected byte _PageLimit;
/// <summary>
/// Random generator.
/// </summary>
protected IRNG _RNG;
/// <summary>
/// Your username of the site (Required only for
/// functions that modify the content).
/// </summary>
protected string _Username;
/// <summary>
/// Your user password in plain text (Required only
/// for functions that modify the content).
/// </summary>
protected string _Password;
/// <summary>
/// String that is append to password (required to login).
/// (See the API documentation of the site for more information).
/// </summary>
protected string _PasswordSalt;
/// <summary>
/// Version of Booru API.
/// </summary>
protected string _ApiVersion;
/// <summary>
/// Authentication check.
/// </summary>
protected bool _Authentication;
#endregion Member
#region Constructor & Destructor
/// <summary>
/// Base object for booru client.
/// </summary>
/// <param name="domain">URL of booru based sites.</param>
public Booru(string domain) : this(domain, null, new JSF32())
{
}
/// <summary>
/// Base object for booru client.
/// </summary>
/// <param name="domain">URL of booru based sites.</param>
/// <param name="httpClient">Client for sending and receive http response.</param>
public Booru(string domain, HttpClient httpClient = null) : this(domain, httpClient, new JSF32())
{
}
/// <summary>
/// Base object for booru client.
/// </summary>
/// <param name="domain">URL of booru based sites.</param>
/// <param name="httpClient">Client for sending and receive http response.</param>
/// <param name="rng">Random generator for random post.</param>
public Booru(string domain, HttpClient httpClient, IRNG rng)
{
this._BaseUrl = new Uri(domain, UriKind.Absolute);
this.HttpClient = httpClient;
this._RNG = rng;
this._Authentication = false;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
}
/// <summary>
/// Release all resource that this object hold.
/// </summary>
~Booru()
{
}
#endregion Constructor & Destructor
#region Properties
/// <summary>
/// For sending HTTP requests and receiving HTTP responses.
/// </summary>
public HttpClient HttpClient
{
set
{
if (value == null)
{
return;
}
else
{
this._HttpClient = value;
if (!this._HttpClient.DefaultRequestHeaders.Contains("User-Agent"))
{
this._HttpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
}
}
}
get
{
if (this._HttpClient == null)
{
this._HttpClient = _LazyHttpClient.Value;
}
return this._HttpClient;
}
}
/// <summary>
/// Gets or sets Booru API version.
/// </summary>
public string ApiVersion
{
private set
{
this._ApiVersion = value;
}
get
{
return this._ApiVersion;
}
}
/// <summary>
/// Gets or sets whether this booru contains explicit content or not.
/// </summary>
public bool IsSafe { set; get; }
/// <summary>
/// Gets or sets maximum page number for booru.
/// </summary>
public byte PageLimit
{
set
{
if (value < 10)
{
this._PageLimit = 10;
}
else
{
this._PageLimit = value;
}
}
get
{
return this._PageLimit;
}
}
#endregion Properties
#region Private Method
private static readonly Lazy<HttpClient> _LazyHttpClient = new Lazy<HttpClient>(() =>
{
var http = new HttpClient();
http.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 Unknown");
return http;
});
#endregion Private Method
#region Protected Method
/// <summary>
/// Download reponse from url.
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
/// <exception cref="AuthenticationException"></exception>
/// <exception cref="HttpRequestException"></exception>
/// <exception cref="HttpResponseException"></exception>
protected async Task<string> GetJsonAsync(string url)
{
try
{
using (var response = await this.HttpClient.GetAsync(url))
{
if (response.StatusCode == HttpStatusCode.OK)
{
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
else if (response.StatusCode == HttpStatusCode.Forbidden || response.StatusCode == HttpStatusCode.Unauthorized)
{
throw new AuthenticationException("Authentication is required.");
}
else
{
throw new HttpResponseException("Unexpected error occured.");
}
}
}
catch (HttpRequestException e)
{
throw e;
}
}
/// <summary>
/// Create base API call url.
/// </summary>
/// <param name="query">Categories.</param>
/// <returns></returns>
protected abstract string CreateBaseApiCall(string query);
/// <summary>
/// Convert string rating to <see cref="Rating"/>.
/// </summary>
/// <param name="rating">String rating</param>
/// <returns></returns>
protected Rating ConvertRating(string rating)
{
switch (rating)
{
case "e":
case "E":
return Rating.Explicit;
case "q":
case "Q":
return Rating.Questionable;
case "s":
case "S":
return Rating.Safe;
default:
return Rating.None;
}
}
#endregion Protected Method
#region Public Method
/// <summary>
/// Login with booru username and password.
/// </summary>
/// <param name="username">Your username.</param>
/// <param name="password">Your password.</param>
/// <returns></returns>
public bool Authenticate(string username, string password)
{
throw new NotImplementedException($"Method { nameof(Authenticate) } is not implemented yet.");
this._Username = username;
this._Password = this._PasswordSalt.Replace("{}", password);
return false;
}
#region Artist
/// <summary>
/// Get a list of artists.
/// </summary>
/// <param name="name">The name (or a fragment of the name) of the artist.</param>
/// <param name="page">The page number.</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="AuthenticationException"></exception>
/// <exception cref="HttpRequestException"></exception>
/// <exception cref="HttpResponseException"></exception>
public virtual async Task<Artist[]> ArtistListAsync(string name, uint page = 0)
{
throw new NotImplementedException($"Method { nameof(ArtistListAsync) } is not implemented yet.");
}
#endregion Artist
#region Pool
/// <summary>
/// Search a pool.
/// </summary>
/// <param name="title">The title of pool.</param>
/// <param name="page">Tha page number.</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="AuthenticationException"></exception>
/// <exception cref="HttpRequestException"></exception>
/// <exception cref="HttpResponseException"></exception>
public virtual async Task<Pool[]> PoolList(string title, uint page = 0)
{
throw new NotImplementedException($"Method { nameof(PoolList) } is not implemented yet.");
}
/// <summary>
/// Get list of post inside the pool.
/// </summary>
/// <param name="poolId">The <see cref="Pool"/> id.</param>
/// <param name="page">The page number.</param>
/// <returns></returns>
/// <exception cref="AuthenticationException"></exception>
/// <exception cref="HttpRequestException"></exception>
/// <exception cref="HttpResponseException"></exception>
public virtual async Task<Post[]> PoolPostList(uint poolId, uint page = 0)
{
throw new NotImplementedException($"Method { nameof(PoolPostList) } is not implemented yet.");
}
#endregion Pool
#region Post
/// <summary>
/// Get a list of <see cref="Post"/>.
/// </summary>
/// <param name="limit">How many <see cref="Post"/> to retrieve.</param>
/// <param name="page">The page number.</param>
/// <param name="tags">The tags to search for.</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="AuthenticationException"></exception>
/// <exception cref="HttpRequestException"></exception>
/// <exception cref="HttpResponseException"></exception>
/// <exception cref="SearchNotFoundException"></exception>
public virtual async Task<Post[]> PostListAsync(uint limit, string[] tags, uint page = 0)
{
throw new NotImplementedException($"Method { nameof(PostListAsync) } is not implemented yet.");
}
/// <summary>
/// Search a single random post from booru with the given tags.
/// </summary>
/// <param name="tags"><see cref="Tag"/> to search.</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="AuthenticationException"></exception>
/// <exception cref="HttpRequestException"></exception>
/// <exception cref="HttpResponseException"></exception>
/// <exception cref="SearchNotFoundException"></exception>
public virtual async Task<Post> GetRandomPostAsync(string[] tags = null)
{
throw new NotImplementedException($"Method { nameof(GetRandomPostAsync) } is not implemented yet.");
}
/// <summary>
/// Search some post from booru with the given tags.
/// </summary>
/// <param name="tags"><see cref="Tag"/> to search.</param>
/// <param name="limit">How many post to retrieve.</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="AuthenticationException"></exception>
/// <exception cref="HttpRequestException"></exception>
/// <exception cref="HttpResponseException"></exception>
public virtual async Task<Post[]> GetRandomPostAsync(uint limit, string[] tags = null)
{
throw new NotImplementedException($"Method { nameof(GetRandomPostAsync) } is not implemented yet.");
}
#endregion Post
#region Tag
/// <summary>
/// Get a list of tag that contains
/// </summary>
/// <param name="name">The tag names to query.</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="AuthenticationException"></exception>
/// <exception cref="HttpRequestException"></exception>
/// <exception cref="HttpResponseException"></exception>
public virtual async Task<Tag[]> TagListAsync(string name)
{
throw new NotImplementedException($"Method { nameof(TagListAsync) } is not implemented yet.");
}
/// <summary>
/// Get a list of related tags.
/// </summary>
/// <param name="name">The tag names to query.</param>
/// <param name="type">Restrict results to tag type (can be general, artist, copyright, or character).</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="AuthenticationException"></exception>
/// <exception cref="HttpRequestException"></exception>
/// <exception cref="HttpResponseException"></exception>
public virtual async Task<TagRelated[]> TagRelatedAsync(string name, TagType type = TagType.General)
{
throw new NotImplementedException($"Method { nameof(TagRelatedAsync) } is not implemented yet.");
}
#endregion Tag
#region Wiki
/// <summary>
/// Search a wiki content.
/// </summary>
/// <param name="title">Wiki title.</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="AuthenticationException"></exception>
/// <exception cref="HttpRequestException"></exception>
/// <exception cref="HttpResponseException"></exception>
public virtual async Task<Wiki[]> WikiListAsync(string title)
{
throw new NotImplementedException($"Method { nameof(WikiListAsync) } is not implemented yet.");
}
#endregion Wiki
#endregion Public Method
}
}