Skip to content

Commit 08f1dbf

Browse files
committed
Refactor
1 parent 607bc5f commit 08f1dbf

File tree

6 files changed

+161
-169
lines changed

6 files changed

+161
-169
lines changed

YoutubeExplode.Tests/SearchSpecs.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Linq;
1+
using System;
22
using System.Threading.Tasks;
33
using FluentAssertions;
44
using Xunit;
@@ -19,6 +19,11 @@ public async Task I_can_get_results_from_a_search_query()
1919

2020
// Assert
2121
results.Should().HaveCountGreaterOrEqualTo(50);
22+
results
23+
.Should()
24+
.Contain(r =>
25+
r.Title.Contains("undead corporation", StringComparison.OrdinalIgnoreCase)
26+
);
2227
}
2328

2429
[Fact]
@@ -32,11 +37,14 @@ public async Task I_can_get_results_from_a_search_query_that_contains_special_ch
3237

3338
// Assert
3439
results.Should().HaveCountGreaterOrEqualTo(50);
40+
results.Should().Contain(r => r.Title.Contains("dune", StringComparison.OrdinalIgnoreCase));
3541
}
3642

3743
[Fact]
3844
public async Task I_can_get_results_from_a_search_query_that_contains_non_ascii_characters()
3945
{
46+
// https://github.com/Tyrrrz/YoutubeExplode/issues/787
47+
4048
// Arrange
4149
var youtube = new YoutubeClient();
4250

@@ -45,12 +53,16 @@ public async Task I_can_get_results_from_a_search_query_that_contains_non_ascii_
4553

4654
// Assert
4755
results.Should().HaveCountGreaterOrEqualTo(50);
48-
results.First().Title.Should().Contain("נועה קירל");
56+
results
57+
.Should()
58+
.Contain(r => r.Title.Contains("נועה קירל", StringComparison.OrdinalIgnoreCase));
4959
}
5060

5161
[Fact]
5262
public async Task I_can_get_results_from_a_search_query_that_contains_non_ascii_characters_and_special_characters()
5363
{
64+
// https://github.com/Tyrrrz/YoutubeExplode/issues/787
65+
5466
// Arrange
5567
var youtube = new YoutubeClient();
5668

@@ -59,7 +71,9 @@ public async Task I_can_get_results_from_a_search_query_that_contains_non_ascii_
5971

6072
// Assert
6173
results.Should().HaveCountGreaterOrEqualTo(50);
62-
results.First().Title.Should().Contain("נועה קירל");
74+
results
75+
.Should()
76+
.Contain(r => r.Title.Contains("נועה קירל", StringComparison.OrdinalIgnoreCase));
6377
}
6478

6579
[Fact]

YoutubeExplode/Playlists/PlaylistController.cs

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Threading.Tasks;
44
using YoutubeExplode.Bridge;
55
using YoutubeExplode.Exceptions;
6+
using YoutubeExplode.Utils;
67
using YoutubeExplode.Videos;
78

89
namespace YoutubeExplode.Playlists;
@@ -18,26 +19,25 @@ public async ValueTask<PlaylistBrowseResponse> GetPlaylistBrowseResponseAsync(
1819
using var request = new HttpRequestMessage(
1920
HttpMethod.Post,
2021
"https://www.youtube.com/youtubei/v1/browse"
21-
)
22-
{
23-
Content = new StringContent(
24-
// lang=json
25-
$$"""
26-
{
27-
"browseId": "VL{{playlistId}}",
28-
"context": {
29-
"client": {
30-
"clientName": "WEB",
31-
"clientVersion": "2.20210408.08.00",
32-
"hl": "en",
33-
"gl": "US",
34-
"utcOffsetMinutes": 0
35-
}
36-
}
22+
);
23+
24+
request.Content = new StringContent(
25+
// lang=json
26+
$$"""
27+
{
28+
"browseId": {{Json.Serialize("VL" + playlistId)}}},
29+
"context": {
30+
"client": {
31+
"clientName": "WEB",
32+
"clientVersion": "2.20210408.08.00",
33+
"hl": "en",
34+
"gl": "US",
35+
"utcOffsetMinutes": 0
3736
}
38-
"""
39-
)
40-
};
37+
}
38+
}
39+
"""
40+
);
4141

4242
using var response = await http.SendAsync(request, cancellationToken);
4343
response.EnsureSuccessStatusCode();
@@ -66,29 +66,28 @@ public async ValueTask<PlaylistNextResponse> GetPlaylistNextResponseAsync(
6666
using var request = new HttpRequestMessage(
6767
HttpMethod.Post,
6868
"https://www.youtube.com/youtubei/v1/next"
69-
)
70-
{
71-
Content = new StringContent(
72-
// lang=json
73-
$$"""
74-
{
75-
"playlistId": "{{playlistId}}",
76-
"videoId": "{{videoId}}",
77-
"playlistIndex": {{index}},
78-
"context": {
79-
"client": {
80-
"clientName": "WEB",
81-
"clientVersion": "2.20210408.08.00",
82-
"hl": "en",
83-
"gl": "US",
84-
"utcOffsetMinutes": 0,
85-
"visitorData": "{{visitorData}}"
86-
}
87-
}
69+
);
70+
71+
request.Content = new StringContent(
72+
// lang=json
73+
$$"""
74+
{
75+
"playlistId": {{Json.Serialize(playlistId)}},
76+
"videoId": {{Json.Serialize(videoId)}},
77+
"playlistIndex": {{Json.Serialize(index)}},
78+
"context": {
79+
"client": {
80+
"clientName": "WEB",
81+
"clientVersion": "2.20210408.08.00",
82+
"hl": "en",
83+
"gl": "US",
84+
"utcOffsetMinutes": 0,
85+
"visitorData": {{Json.Serialize(visitorData)}}
8886
}
89-
"""
90-
)
91-
};
87+
}
88+
}
89+
"""
90+
);
9291

9392
using var response = await http.SendAsync(request, cancellationToken);
9493
response.EnsureSuccessStatusCode();

YoutubeExplode/Search/SearchController.cs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,33 @@ public async ValueTask<SearchResponse> GetSearchResponseAsync(
1818
using var request = new HttpRequestMessage(
1919
HttpMethod.Post,
2020
"https://www.youtube.com/youtubei/v1/search"
21-
)
22-
{
23-
Content = new StringContent(
24-
// lang=json
25-
$$"""
26-
{
27-
"query": "{{UrlEncoder.EncodeIfNeeded(searchQuery)}}",
28-
"params": "{{searchFilter switch
29-
{
30-
SearchFilter.Video => "EgIQAQ%3D%3D",
31-
SearchFilter.Playlist => "EgIQAw%3D%3D",
32-
SearchFilter.Channel => "EgIQAg%3D%3D",
33-
_ => null
34-
}}}",
35-
"continuation": "{{continuationToken}}",
36-
"context": {
37-
"client": {
38-
"clientName": "WEB",
39-
"clientVersion": "2.20210408.08.00",
40-
"hl": "en",
41-
"gl": "US",
42-
"utcOffsetMinutes": 0
43-
}
44-
}
21+
);
22+
23+
request.Content = new StringContent(
24+
// lang=json
25+
$$"""
26+
{
27+
"query": {{Json.Serialize(searchQuery)}},
28+
"params": {{Json.Serialize(searchFilter switch
29+
{
30+
SearchFilter.Video => "EgIQAQ%3D%3D",
31+
SearchFilter.Playlist => "EgIQAw%3D%3D",
32+
SearchFilter.Channel => "EgIQAg%3D%3D",
33+
_ => null
34+
})}},
35+
"continuation": {{Json.Serialize(continuationToken)}},
36+
"context": {
37+
"client": {
38+
"clientName": "WEB",
39+
"clientVersion": "2.20210408.08.00",
40+
"hl": "en",
41+
"gl": "US",
42+
"utcOffsetMinutes": 0
4543
}
46-
"""
47-
)
48-
};
44+
}
45+
}
46+
"""
47+
);
4948

5049
using var response = await http.SendAsync(request, cancellationToken);
5150
response.EnsureSuccessStatusCode();

YoutubeExplode/Utils/Json.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Globalization;
12
using System.Text;
23
using System.Text.Json;
34
using YoutubeExplode.Utils.Extensions;
@@ -56,4 +57,35 @@ public static JsonElement Parse(string source)
5657
return null;
5758
}
5859
}
60+
61+
public static string Encode(string value)
62+
{
63+
var buffer = new StringBuilder(value.Length);
64+
65+
foreach (var c in value)
66+
{
67+
if (c == '\n')
68+
buffer.Append("\\n");
69+
else if (c == '\r')
70+
buffer.Append("\\r");
71+
else if (c == '\t')
72+
buffer.Append("\\t");
73+
else if (c == '\\')
74+
buffer.Append("\\\\");
75+
else if (c == '"')
76+
buffer.Append("\\\"");
77+
else
78+
buffer.Append(c);
79+
}
80+
81+
return buffer.ToString();
82+
}
83+
84+
// AOT-compatible serialization
85+
public static string Serialize(string? value) =>
86+
value is not null ? '"' + Encode(value) + '"' : "null";
87+
88+
// AOT-compatible serialization
89+
public static string Serialize(int value) =>
90+
Serialize(value.ToString(CultureInfo.InvariantCulture));
5991
}

YoutubeExplode/Utils/UrlEncoder.cs

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)