-
-
Notifications
You must be signed in to change notification settings - Fork 586
Expand file tree
/
Copy pathSearchController.cs
More file actions
53 lines (47 loc) · 1.58 KB
/
Copy pathSearchController.cs
File metadata and controls
53 lines (47 loc) · 1.58 KB
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
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using YoutubeExplode.Bridge;
using YoutubeExplode.Utils;
namespace YoutubeExplode.Search;
internal class SearchController(HttpClient http)
{
public async ValueTask<SearchResponse> GetSearchResponseAsync(
string searchQuery,
SearchFilter searchFilter,
string? continuationToken,
string hl,
string gl,
CancellationToken cancellationToken = default
)
{
using var request = new HttpRequestMessage(
HttpMethod.Post,
"https://www.youtube.com/youtubei/v1/search"
);
var filter = searchFilter.ToString();
request.Content = new StringContent(
// lang=json
$$"""
{
"query": {{Json.Encode(searchQuery)}},
"params": {{Json.Encode(!string.IsNullOrWhiteSpace(filter) ? filter : null)}},
"continuation": {{Json.Encode(continuationToken)}},
"context": {
"client": {
"clientName": "WEB",
"clientVersion": "2.20210408.08.00",
"hl": {{Json.Encode(hl)}},
"gl": {{Json.Encode(gl)}},
"persist_hl": "1",
"utcOffsetMinutes": 0
}
}
}
"""
);
using var response = await http.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();
return SearchResponse.Parse(await response.Content.ReadAsStringAsync(cancellationToken));
}
}