-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenTriviaClient.cs
723 lines (654 loc) · 17.8 KB
/
OpenTriviaClient.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Web;
using OpenTriviaSharp.Exceptions;
using OpenTriviaSharp.Models;
namespace OpenTriviaSharp
{
/// <summary>
/// Open Trivia Database client.
/// </summary>
public class OpenTriviaClient
{
#region Member
private HttpClient _HttpClient;
private readonly bool _Supplied;
private readonly string _BaseApiUrl = "https://opentdb.com/api.php?";
private readonly string _BaseTokenApiUrl = "https://opentdb.com/api_token.php?";
private string _SessionToken;
#endregion Member
#region Constructor & Destructor
/// <summary>
/// Create <see cref="OpenTriviaClient"/> instance.
/// </summary>
/// <param name="client">
/// <see cref="HttpClient"/> object to send and receiving response.
/// </param>
/// <param name="sessionToken">
/// Session token of Open Trivia for tracking requested question.
/// </param>
public OpenTriviaClient(HttpClient client = null, string sessionToken = null)
{
if (client == null)
{
this._HttpClient = new HttpClient();
this._Supplied = false;
}
else
{
this._HttpClient = client;
this._Supplied = true;
}
this.AddUserAgent();
if (sessionToken == null || sessionToken.Trim() == "")
{
this._SessionToken = "";
}
else
{
this._SessionToken = sessionToken;
}
}
/// <summary>
/// Release all resource that this object handle.
/// </summary>
~OpenTriviaClient()
{
if (this._Supplied == false)
{
this._HttpClient.Dispose();
}
}
#endregion Constructor & Destructor
#region Properties
/// <summary>
/// Get current session token.
/// </summary>
public string Token
{
set
{
if (value == null || value.Trim() == "")
{
return;
}
this._SessionToken = value;
}
get
{
return this._SessionToken;
}
}
#endregion Properties
#region Protected Method
/// <summary>
/// Get JSON response from url.
/// </summary>
/// <typeparam name="T">
/// The type of the object to deserialize.
/// </typeparam>
/// <param name="url">
/// API URL.
/// </param>
/// <returns>
/// The instance of <typeparamref name="T"/> being deserialized.
/// </returns>
/// <exception cref="OpenTriviaException">
/// Unexpected error occured.
/// </exception>
/// <exception cref="HttpRequestException">
/// The request failed due to an underlying issue such as network connectivity, DNS
/// failure, server certificate validation or timeout.
/// </exception>
/// <exception cref="JsonException">
/// The JSON is invalid.
/// </exception>
protected async Task<T> GetJsonResponseAsync<T>(string url)
{
try
{
using (var request = new HttpRequestMessage(HttpMethod.Get, url))
using (var response = await this._HttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead))
using (var stream = await response.Content.ReadAsStreamAsync())
{
if (response.IsSuccessStatusCode)
{
try
{
return await JsonSerializer.DeserializeAsync<T>(stream);
}
catch (JsonException)
{
throw;
}
}
throw new OpenTriviaException(
$"Unexpected error occured.\n Status code = { (int)response.StatusCode }\n Reason = { response.ReasonPhrase }.");
}
}
catch (HttpRequestException)
{
throw;
}
}
/// <summary>
/// Read JSON data with encoding <see cref="ResponseEncoding.Default"/>.
/// </summary>
/// <param name="item"></param>
/// <returns>
/// A <see cref="TriviaQuestion"/>.
/// </returns>
protected TriviaQuestion ReadTriviaQuestion(JsonElement item)
{
var incorrect = new List<string>();
foreach (var ans in item.GetProperty("incorrect_answers").EnumerateArray())
{
incorrect.Add(
HttpUtility.HtmlDecode(
ans.GetString()));
}
return new TriviaQuestion(
this.DetermineCategory(item.GetProperty("category").GetString()),
this.DetermineType(item.GetProperty("type").GetString()),
this.DetermineDifficulty(item.GetProperty("difficulty").GetString()),
HttpUtility.HtmlDecode(item.GetProperty("question").GetString()),
HttpUtility.HtmlDecode(item.GetProperty("correct_answer").GetString()),
incorrect.ToArray()
);
}
/// <summary>
/// Read JSON data with encoding <see cref="ResponseEncoding.Url3986"/>.
/// </summary>
/// <param name="item"></param>
/// <returns>
/// A <see cref="TriviaQuestion"/>.
/// </returns>
protected TriviaQuestion ReadTriviaQuestionURL(JsonElement item)
{
var incorrect = new List<string>();
foreach (var ans in item.GetProperty("incorrect_answers").EnumerateArray())
{
incorrect.Add(
HttpUtility.UrlDecode(
ans.GetString()));
}
return new TriviaQuestion(
this.DetermineCategory(HttpUtility.UrlDecode(item.GetProperty("category").GetString())),
this.DetermineType(item.GetProperty("type").GetString()),
this.DetermineDifficulty(item.GetProperty("difficulty").GetString()),
HttpUtility.UrlDecode(item.GetProperty("question").GetString()),
HttpUtility.UrlDecode(item.GetProperty("correct_answer").GetString()),
incorrect.ToArray()
);
}
/// <summary>
/// Read JSON data with encoding <see cref="ResponseEncoding.Base64"/>.
/// </summary>
/// <param name="item"></param>
/// <returns>
/// A <see cref="TriviaQuestion"/>.
/// </returns>
protected TriviaQuestion ReadTriviaQuestionBase64(JsonElement item)
{
var incorrect = new List<string>();
foreach (var ans in item.GetProperty("incorrect_answers").EnumerateArray())
{
incorrect.Add(
this.FromBase64(
ans.GetString()));
}
return new TriviaQuestion(
this.DetermineCategory(this.FromBase64(item.GetProperty("category").GetString())),
this.DetermineType(this.FromBase64(item.GetProperty("type").GetString())),
this.DetermineDifficulty(this.FromBase64(item.GetProperty("difficulty").GetString())),
this.FromBase64(item.GetProperty("question").GetString()),
this.FromBase64(item.GetProperty("correct_answer").GetString()),
incorrect.ToArray()
);
}
/// <summary>
/// Add browser user agent to <see cref="HttpClient"/>.
/// </summary>
protected void AddUserAgent()
{
if (this._HttpClient == null)
{
return;
}
if (this._HttpClient.DefaultRequestHeaders.UserAgent.Count == 0)
{
this._HttpClient.DefaultRequestHeaders.Add(
"User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36");
}
}
#region Helper Method
/// <summary>
/// Convert base64 string to UTF-8 string.
/// </summary>
/// <param name="base64String">
/// Base64 string to convert.
/// </param>
/// <returns>
/// UTF-8 string.
/// </returns>
protected string FromBase64(string base64String)
{
return Encoding.UTF8.GetString(
Convert.FromBase64String(base64String));
}
/// <summary>
/// Convert JSON reponse category property to <see cref="Category"/>.
/// </summary>
/// <param name="category">
/// Category string from JSON response.
/// </param>
/// <returns>
/// <see cref="Category"/> based on paramater <paramref name="category"/>.
/// </returns>
protected Category DetermineCategory(string category)
{
if (category.Contains("General"))
{
return Category.General;
}
else if (category.Contains("Books"))
{
return Category.Books;
}
else if (category.Contains("Film"))
{
return Category.Film;
}
else if (category.Contains("Music"))
{
return Category.Music;
}
else if (category.Contains("Theatres"))
{
return Category.Theatres;
}
else if (category.Contains("Television"))
{
return Category.Television;
}
else if (category.Contains("Video Games"))
{
return Category.VideoGames;
}
else if (category.Contains("Board Games"))
{
return Category.BoardGames;
}
else if (category.Contains("Science"))
{
return Category.Science;
}
else if (category.Contains("Computers"))
{
return Category.Computers;
}
else if (category.Contains("Mathematics"))
{
return Category.Mathematics;
}
else if (category.Contains("Mythology"))
{
return Category.Mythology;
}
else if (category.Contains("Sports"))
{
return Category.Sports;
}
else if (category.Contains("Geography"))
{
return Category.Geography;
}
else if (category.Contains("History"))
{
return Category.History;
}
else if (category.Contains("Politics"))
{
return Category.Politics;
}
else if (category.Contains("Art"))
{
return Category.Art;
}
else if (category.Contains("Celebrities"))
{
return Category.Celebrities;
}
else if (category.Contains("Animals"))
{
return Category.Animals;
}
else if (category.Contains("Vehicles"))
{
return Category.Vehicles;
}
else if (category.Contains("Comics"))
{
return Category.Comics;
}
else if (category.Contains("Gadgets"))
{
return Category.Gadgets;
}
else if (category.Contains("Anime") || category.Contains("Manga"))
{
return Category.AnimeManga;
}
else if (category.Contains("Cartoon"))
{
return Category.Cartoon;
}
else
{
return Category.Any;
}
}
/// <summary>
/// Convert JSON reponse difficulty property to <see cref="Difficulty"/>.
/// </summary>
/// <param name="difficulty">
/// Difficulty string from JSON response.
/// </param>
/// <returns>
/// <see cref="Difficulty"/> based on parameter <paramref name="difficulty"/>.
/// </returns>
protected Difficulty DetermineDifficulty(string difficulty)
{
switch (difficulty)
{
case "hard":
return Difficulty.Hard;
case "medium":
return Difficulty.Medium;
case "easy":
return Difficulty.Easy;
default:
return Difficulty.Any;
}
}
/// <summary>
/// Convert JSON reponse type property to <see cref="TriviaType"/>.
/// </summary>
/// <param name="type">
/// Type string from JSON response.
/// </param>
/// <returns>
/// <see cref="TriviaType"/> based on parameter <paramref name="type"/>.
/// </returns>
protected TriviaType DetermineType(string type)
{
switch (type)
{
case "multiple":
return TriviaType.Multiple;
case "boolean":
return TriviaType.Boolean;
default:
return TriviaType.Any;
}
}
/// <summary>
/// Get response message based on response code.
/// </summary>
/// <param name="responseCode">
/// Response code in the JSON response.
/// </param>
/// <returns>
/// Message based on response code.
/// </returns>
protected string ResponseError(byte responseCode)
{
switch (responseCode)
{
case 0:
return "Returned results successfully.";
case 1:
return "The API doesn't have enough questions for your query.";
case 2:
return "Invalid parameter(s). Arguments passed aren't valid.";
case 3:
return "Invalid session token.";
case 4:
return "Session token has retrieved all possible questions for the specified query. Reset the token.";
default:
return "An error has occured in the API";
}
}
#endregion Helper Method
#endregion Protected Method
#region Public Method
/// <summary>
/// Retrieves random <see cref="TriviaQuestion"/>.
/// </summary>
/// <param name="amount">
/// Amount of questions to be retrieved.
/// </param>
/// <param name="category">
/// Category of the questions to be retrieved.
/// </param>
/// <param name="type">
/// Type of the questions to be retrieved.
/// </param>
/// <param name="difficulty">
/// Difficulty of the questions to be retrieved.
/// </param>
/// <param name="encoding">
/// Encoding of the API response to be used.
/// </param>
/// <param name="sessionToken">
/// Session token to be used.
/// </param>
/// <returns>
/// Array of <see cref="TriviaQuestion"/> based on specified parameters.
/// </returns>
/// <exception cref="OpenTriviaException">
/// Unexpected error occured.
/// </exception>
/// <exception cref="HttpRequestException">
/// The request failed due to an underlying issue such as network connectivity, DNS
/// failure, server certificate validation or timeout.
/// </exception>
/// <exception cref="JsonException">
/// The JSON is invalid.
/// </exception>
public async Task<TriviaQuestion[]> GetQuestionAsync(byte amount = 10, Category category = Category.Any, TriviaType type = TriviaType.Any, Difficulty difficulty = Difficulty.Any, ResponseEncoding encoding = ResponseEncoding.Default, string sessionToken = "")
{
if (amount <= 0)
{
amount = 1;
}
else if (amount > 50) // hard limit
{
amount = 50;
}
var url = new StringBuilder(this._BaseApiUrl + $"amount={ amount }");
if (category != Category.Any)
{
url.Append($"&category={ (byte)category }");
}
if (difficulty != Difficulty.Any)
{
url.Append($"&difficulty={ difficulty.ToString().ToLower() }");
}
if (type != TriviaType.Any)
{
url.Append($"&type={ type.ToString().ToLower() }");
}
if (encoding != ResponseEncoding.Default)
{
url.Append($"&encode={ encoding.ToString().ToLower() }");
}
if (sessionToken != "")
{
url.Append($"&token={ sessionToken }");
}
else
{
if (this._SessionToken != "")
{
url.Append($"&token={ this._SessionToken }");
}
}
using (var doc = await this.GetJsonResponseAsync<JsonDocument>(url.ToString()))
{
var responseCode = doc.RootElement.GetProperty("response_code").GetByte();
if (responseCode != 0)
{
throw new OpenTriviaException(this.ResponseError(responseCode));
}
var jsonArray = doc.RootElement.GetProperty("results");
var questions = new List<TriviaQuestion>();
if (encoding == ResponseEncoding.Default)
{
foreach (var item in jsonArray.EnumerateArray())
{
questions.Add(this.ReadTriviaQuestion(item));
}
}
else if (encoding == ResponseEncoding.Url3986)
{
foreach (var item in jsonArray.EnumerateArray())
{
questions.Add(this.ReadTriviaQuestionURL(item));
}
}
else
{
foreach (var item in jsonArray.EnumerateArray())
{
questions.Add(this.ReadTriviaQuestionBase64(item));
}
}
return questions.ToArray();
}
}
/// <summary>
/// Retrieves a new session token.
/// </summary>
/// <returns>
/// New session token.
/// </returns>
public async Task<string> RequestTokenAsync()
{
var url = $"{ this._BaseTokenApiUrl }command=request";
using (var doc = await this.GetJsonResponseAsync<JsonDocument>(url))
{
var responseCode = doc.RootElement.GetProperty("response_code").GetByte();
if (responseCode != 0)
{
throw new OpenTriviaException(this.ResponseError(responseCode));
}
return doc.RootElement.GetProperty("token").GetString();
}
}
/// <summary>
/// Resets a session token.
/// </summary>
/// <param name="sessionToken">
/// Session token to be reset.
/// </param>
/// <returns>
/// <see langword="true"/> if session token reseted; <see langword="false"/> otherwise.
/// </returns>
/// <exception cref="JsonException">
/// The JSON is invalid.
/// </exception>
public async Task<bool> ResetTokenAsync(string sessionToken)
{
var url = $"{ this._BaseTokenApiUrl }command=reset&token={ sessionToken }";
try
{
using (var doc = await this.GetJsonResponseAsync<JsonDocument>(url))
{
var responseCode = doc.RootElement.GetProperty("response_code").GetByte();
if (responseCode == 0)
{
return true;
}
return false;
}
}
catch (JsonException)
{
throw new OpenTriviaException(this.ResponseError(3));
}
}
/// <summary>
/// Retrieves number of question based on <see cref="Category"/>.
/// </summary>
/// <param name="category">
/// Category of question.
/// </param>
/// <param name="difficulty">
/// Difficulty of question.
/// </param>
/// <returns>
/// Number of question.
/// </returns>
public async Task<uint> CountQuestionAsync(Category category = Category.General, Difficulty difficulty = Difficulty.Any)
{
var url = $"https://opentdb.com/api_count.php?category={ (byte)category }";
try
{
using (var doc = await this.GetJsonResponseAsync<JsonDocument>(url))
{
var categoryId = doc.RootElement.GetProperty("category_id").GetByte();
if ((byte)category != categoryId)
{
throw new OpenTriviaException(this.ResponseError(2));
}
var result = doc.RootElement.GetProperty("category_question_count");
switch (difficulty)
{
case Difficulty.Easy:
return result.GetProperty("total_easy_question_count").GetUInt32();
case Difficulty.Medium:
return result.GetProperty("total_medium_question_count").GetUInt32();
case Difficulty.Hard:
return result.GetProperty("total_hard_question_count").GetUInt32();
case Difficulty.Any:
default:
return result.GetProperty("total_question_count").GetUInt32();
}
}
}
catch (JsonException)
{
throw new OpenTriviaException(this.ResponseError(2));
}
}
/// <summary>
/// Get category list.
/// </summary>
/// <returns>
/// Array of tuple (id, name) of category.
/// </returns>
public async Task<(byte, string)[]> CategoryListAsync()
{
var url = "https://opentdb.com/api_category.php";
using (var doc = await this.GetJsonResponseAsync<JsonDocument>(url))
{
var category = doc.RootElement.GetProperty("trivia_categories");
var tuple = new List<(byte, string)>();
foreach (var item in category.EnumerateArray())
{
tuple.Add(
(item.GetProperty("id").GetByte(),
item.GetProperty("name").GetString()));
}
return tuple.ToArray();
}
}
#endregion Public Method
}
}