Skip to content

Commit b0f37bc

Browse files
authored
Make YoutubeClient implement IDisposable to release inner client (#913)
1 parent 552c4db commit b0f37bc

File tree

13 files changed

+90
-84
lines changed

13 files changed

+90
-84
lines changed

Readme.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ To retrieve the metadata associated with a YouTube video, call `Videos.GetAsync(
6262
```csharp
6363
using YoutubeExplode;
6464

65-
var youtube = new YoutubeClient();
65+
using var youtube = new YoutubeClient();
6666

6767
// You can specify either the video URL or its ID
6868
var videoUrl = "https://youtube.com/watch?v=u_yIGGhubZs";
@@ -96,7 +96,7 @@ You can request the manifest that lists all available streams for a particular v
9696
```csharp
9797
using YoutubeExplode;
9898

99-
var youtube = new YoutubeClient();
99+
using var youtube = new YoutubeClient();
100100

101101
var videoUrl = "https://youtube.com/watch?v=u_yIGGhubZs";
102102
var streamManifest = await youtube.Videos.Streams.GetManifestAsync(videoUrl);
@@ -144,7 +144,7 @@ To get the list of available closed caption tracks, call `Videos.ClosedCaptions.
144144
```csharp
145145
using YoutubeExplode;
146146

147-
var youtube = new YoutubeClient();
147+
using var youtube = new YoutubeClient();
148148

149149
var videoUrl = "https://youtube.com/watch?v=u_yIGGhubZs";
150150
var trackManifest = await youtube.Videos.ClosedCaptions.GetManifestAsync(videoUrl);
@@ -188,7 +188,7 @@ You can get the metadata associated with a YouTube playlist by calling the `Play
188188
```csharp
189189
using YoutubeExplode;
190190

191-
var youtube = new YoutubeClient();
191+
using var youtube = new YoutubeClient();
192192

193193
var playlistUrl = "https://youtube.com/playlist?list=PLa1F2ddGya_-UvuAqHAksYnB0qL9yWDO6";
194194
var playlist = await youtube.Playlists.GetAsync(playlistUrl);
@@ -205,7 +205,7 @@ To get the videos included in a playlist, call `Playlists.GetVideosAsync(...)`:
205205
using YoutubeExplode;
206206
using YoutubeExplode.Common;
207207

208-
var youtube = new YoutubeClient();
208+
using var youtube = new YoutubeClient();
209209
var playlistUrl = "https://youtube.com/playlist?list=PLa1F2ddGya_-UvuAqHAksYnB0qL9yWDO6";
210210

211211
// Get all playlist videos
@@ -220,7 +220,7 @@ You can also enumerate the videos iteratively without waiting for the whole list
220220
```csharp
221221
using YoutubeExplode;
222222

223-
var youtube = new YoutubeClient();
223+
using var youtube = new YoutubeClient();
224224
var playlistUrl = "https://youtube.com/playlist?list=PLa1F2ddGya_-UvuAqHAksYnB0qL9yWDO6";
225225

226226
await foreach (var video in youtube.Playlists.GetVideosAsync(playlistUrl))
@@ -235,7 +235,7 @@ If you need precise control over how many requests you send to YouTube, use `Pla
235235
```csharp
236236
using YoutubeExplode;
237237

238-
var youtube = new YoutubeClient();
238+
using var youtube = new YoutubeClient();
239239
var playlistUrl = "https://youtube.com/playlist?list=PLa1F2ddGya_-UvuAqHAksYnB0qL9yWDO6";
240240

241241
// Each batch corresponds to one request
@@ -262,7 +262,7 @@ You can get the metadata associated with a YouTube channel by calling the `Chann
262262
```csharp
263263
using YoutubeExplode;
264264

265-
var youtube = new YoutubeClient();
265+
using var youtube = new YoutubeClient();
266266

267267
var channelUrl = "https://youtube.com/channel/UCSMOQeBJ2RAnuFungnQOxLg";
268268
var channel = await youtube.Channels.GetAsync(channelUrl);
@@ -275,7 +275,7 @@ You can also get the channel metadata by username or profile URL with `Channels.
275275
```csharp
276276
using YoutubeExplode;
277277

278-
var youtube = new YoutubeClient();
278+
using var youtube = new YoutubeClient();
279279

280280
var channelUrl = "https://youtube.com/user/BlenderFoundation";
281281
var channel = await youtube.Channels.GetByUserAsync(channelUrl);
@@ -288,7 +288,7 @@ To get the channel metadata by slug or legacy custom URL, use `Channels.GetBySlu
288288
```csharp
289289
using YoutubeExplode;
290290

291-
var youtube = new YoutubeClient();
291+
using var youtube = new YoutubeClient();
292292

293293
var channelUrl = "https://youtube.com/c/BlenderFoundation";
294294
var channel = await youtube.Channels.GetBySlugAsync(channelUrl);
@@ -301,7 +301,7 @@ To get the channel metadata by handle or custom URL, use `Channels.GetByHandleAs
301301
```csharp
302302
using YoutubeExplode;
303303

304-
var youtube = new YoutubeClient();
304+
using var youtube = new YoutubeClient();
305305

306306
var channelUrl = "https://youtube.com/@BlenderOfficial";
307307
var channel = await youtube.Channels.GetByHandleAsync(channelUrl);
@@ -317,7 +317,7 @@ To get the list of videos uploaded by a channel, call `Channels.GetUploadsAsync(
317317
using YoutubeExplode;
318318
using YoutubeExplode.Common;
319319

320-
var youtube = new YoutubeClient();
320+
using var youtube = new YoutubeClient();
321321
var channelUrl = "https://youtube.com/channel/UCSMOQeBJ2RAnuFungnQOxLg";
322322

323323
var videos = await youtube.Channels.GetUploadsAsync(channelUrl);
@@ -331,7 +331,7 @@ Each search result may represent either a video, a playlist, or a channel, so yo
331331
```csharp
332332
using YoutubeExplode;
333333

334-
var youtube = new YoutubeClient();
334+
using var youtube = new YoutubeClient();
335335

336336
await foreach (var result in youtube.Search.GetResultsAsync("blender tutorials"))
337337
{
@@ -367,7 +367,7 @@ To limit the results to a specific type, use `Search.GetVideosAsync(...)`, `Sear
367367
using YoutubeExplode;
368368
using YoutubeExplode.Common;
369369

370-
var youtube = new YoutubeClient();
370+
using var youtube = new YoutubeClient();
371371

372372
var videos = await youtube.Search.GetVideosAsync("blender tutorials");
373373
var playlists = await youtube.Search.GetPlaylistsAsync("blender tutorials");
@@ -379,7 +379,7 @@ Similarly to playlists, you can also enumerate results in batches by calling `Se
379379
```csharp
380380
using YoutubeExplode;
381381

382-
var youtube = new YoutubeClient();
382+
using var youtube = new YoutubeClient();
383383

384384
// Each batch corresponds to one request
385385
await foreach (var batch in youtube.Search.GetResultBatchesAsync("blender tutorials"))
@@ -417,7 +417,7 @@ using YoutubeExplode;
417417
var cookies = ...;
418418

419419
// Cookie collection must be of type IReadOnlyList<System.Net.Cookie>
420-
var youtube = new YoutubeClient(cookies);
420+
using var youtube = new YoutubeClient(cookies);
421421
```
422422

423423
In order to actually perform the authentication, you can use an embedded browser such as [WebView](https://nuget.org/packages/Microsoft.Web.WebView2) to navigate the user to the [YouTube login page](https://accounts.google.com/ServiceLogin?continue=https%3A%2F%2Fwww.youtube.com), let them log in, and then extract the cookies from the browser.

YoutubeExplode.Converter.Tests/EnvironmentSpecs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class EnvironmentSpecs(ITestOutputHelper testOutput) : IAsyncLifetime
1717
public async Task I_can_download_a_video_with_custom_environment_variables_passed_to_FFmpeg()
1818
{
1919
// Arrange
20-
var youtube = new YoutubeClient();
20+
using var youtube = new YoutubeClient();
2121

2222
using var dir = TempDir.Create();
2323
var filePath = Path.Combine(dir.Path, "video.mp4");

YoutubeExplode.Converter.Tests/GeneralSpecs.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class GeneralSpecs(ITestOutputHelper testOutput) : IAsyncLifetime
2222
public async Task I_can_download_a_video_as_a_single_mp4_file()
2323
{
2424
// Arrange
25-
var youtube = new YoutubeClient();
25+
using var youtube = new YoutubeClient();
2626

2727
using var dir = TempDir.Create();
2828
var filePath = Path.Combine(dir.Path, "video.mp4");
@@ -38,7 +38,7 @@ public async Task I_can_download_a_video_as_a_single_mp4_file()
3838
public async Task I_can_download_a_video_as_a_single_webm_file()
3939
{
4040
// Arrange
41-
var youtube = new YoutubeClient();
41+
using var youtube = new YoutubeClient();
4242

4343
using var dir = TempDir.Create();
4444
var filePath = Path.Combine(dir.Path, "video.webm");
@@ -54,7 +54,7 @@ public async Task I_can_download_a_video_as_a_single_webm_file()
5454
public async Task I_can_download_a_video_as_a_single_mp3_file()
5555
{
5656
// Arrange
57-
var youtube = new YoutubeClient();
57+
using var youtube = new YoutubeClient();
5858

5959
using var dir = TempDir.Create();
6060
var filePath = Path.Combine(dir.Path, "video.mp3");
@@ -70,7 +70,7 @@ public async Task I_can_download_a_video_as_a_single_mp3_file()
7070
public async Task I_can_download_a_video_as_a_single_ogg_file()
7171
{
7272
// Arrange
73-
var youtube = new YoutubeClient();
73+
using var youtube = new YoutubeClient();
7474

7575
using var dir = TempDir.Create();
7676
var filePath = Path.Combine(dir.Path, "video.ogg");
@@ -86,7 +86,7 @@ public async Task I_can_download_a_video_as_a_single_ogg_file()
8686
public async Task I_can_download_a_video_as_a_single_mp4_file_with_multiple_streams()
8787
{
8888
// Arrange
89-
var youtube = new YoutubeClient();
89+
using var youtube = new YoutubeClient();
9090

9191
using var dir = TempDir.Create();
9292
var filePath = Path.Combine(dir.Path, "video.mp4");
@@ -144,7 +144,7 @@ await youtube.Videos.DownloadAsync(
144144
public async Task I_can_download_a_video_as_a_single_webm_file_with_multiple_streams()
145145
{
146146
// Arrange
147-
var youtube = new YoutubeClient();
147+
using var youtube = new YoutubeClient();
148148

149149
using var dir = TempDir.Create();
150150
var filePath = Path.Combine(dir.Path, "video.webm");
@@ -202,7 +202,7 @@ await youtube.Videos.DownloadAsync(
202202
public async Task I_can_download_a_video_with_custom_conversion_settings()
203203
{
204204
// Arrange
205-
var youtube = new YoutubeClient();
205+
using var youtube = new YoutubeClient();
206206

207207
using var dir = TempDir.Create();
208208
var filePath = Path.Combine(dir.Path, "video.mp3");
@@ -225,7 +225,7 @@ await youtube.Videos.DownloadAsync(
225225
public async Task I_can_try_to_download_a_video_and_get_an_error_if_the_conversion_settings_are_invalid()
226226
{
227227
// Arrange
228-
var youtube = new YoutubeClient();
228+
using var youtube = new YoutubeClient();
229229

230230
using var dir = TempDir.Create();
231231
var filePath = Path.Combine(dir.Path, "video.mp4");
@@ -251,7 +251,7 @@ await youtube.Videos.DownloadAsync(
251251
public async Task I_can_download_a_video_while_tracking_progress()
252252
{
253253
// Arrange
254-
var youtube = new YoutubeClient();
254+
using var youtube = new YoutubeClient();
255255

256256
using var dir = TempDir.Create();
257257
var filePath = Path.Combine(dir.Path, "video.mp3");

YoutubeExplode.Converter.Tests/SubtitleSpecs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class SubtitleSpecs : IAsyncLifetime
1919
public async Task I_can_download_a_video_as_a_single_mp4_file_with_subtitles()
2020
{
2121
// Arrange
22-
var youtube = new YoutubeClient();
22+
using var youtube = new YoutubeClient();
2323

2424
using var dir = TempDir.Create();
2525
var filePath = Path.Combine(dir.Path, "video.mp4");
@@ -58,7 +58,7 @@ await youtube.Videos.DownloadAsync(
5858
public async Task I_can_download_a_video_as_a_single_webm_file_with_subtitles()
5959
{
6060
// Arrange
61-
var youtube = new YoutubeClient();
61+
using var youtube = new YoutubeClient();
6262

6363
using var dir = TempDir.Create();
6464
var filePath = Path.Combine(dir.Path, "video.webm");

YoutubeExplode.Converter/Readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ For example, to download a video in the specified format using the highest quali
2828
using YoutubeExplode;
2929
using YoutubeExplode.Converter;
3030

31-
var youtube = new YoutubeClient();
31+
using var youtube = new YoutubeClient();
3232

3333
var videoUrl = "https://youtube.com/watch?v=u_yIGGhubZs";
3434
await youtube.Videos.DownloadAsync(videoUrl, "video.mp4");
@@ -51,7 +51,7 @@ To configure various aspects of the conversion process, use the following overlo
5151
using YoutubeExplode;
5252
using YoutubeExplode.Converter;
5353

54-
var youtube = new YoutubeClient();
54+
using var youtube = new YoutubeClient();
5555
var videoUrl = "https://youtube.com/watch?v=u_yIGGhubZs";
5656

5757
await youtube.Videos.DownloadAsync(videoUrl, "video.mp4", o => o
@@ -71,7 +71,7 @@ using YoutubeExplode;
7171
using YoutubeExplode.Videos.Streams;
7272
using YoutubeExplode.Converter;
7373

74-
var youtube = new YoutubeClient();
74+
using var youtube = new YoutubeClient();
7575

7676
// Get stream manifest
7777
var videoUrl = "https://youtube.com/watch?v=u_yIGGhubZs";

YoutubeExplode.Demo.Cli/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static async Task Main()
1515
{
1616
Console.Title = "YoutubeExplode Demo";
1717

18-
var youtube = new YoutubeClient();
18+
using var youtube = new YoutubeClient();
1919

2020
// Get the video ID
2121
Console.Write("Enter YouTube video ID or URL: ");

YoutubeExplode.Tests/ChannelSpecs.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ChannelSpecs
1313
public async Task I_can_get_the_metadata_of_a_channel()
1414
{
1515
// Arrange
16-
var youtube = new YoutubeClient();
16+
using var youtube = new YoutubeClient();
1717

1818
// Act
1919
var channel = await youtube.Channels.GetAsync(ChannelIds.Normal);
@@ -29,7 +29,7 @@ public async Task I_can_get_the_metadata_of_a_channel()
2929
public async Task I_can_get_the_metadata_of_a_channel_by_user_name()
3030
{
3131
// Arrange
32-
var youtube = new YoutubeClient();
32+
using var youtube = new YoutubeClient();
3333

3434
// Act
3535
var channel = await youtube.Channels.GetByUserAsync(UserNames.Normal);
@@ -45,7 +45,7 @@ public async Task I_can_get_the_metadata_of_a_channel_by_user_name()
4545
public async Task I_can_get_the_metadata_of_a_channel_by_slug()
4646
{
4747
// Arrange
48-
var youtube = new YoutubeClient();
48+
using var youtube = new YoutubeClient();
4949

5050
// Act
5151
var channel = await youtube.Channels.GetBySlugAsync(ChannelSlugs.Normal);
@@ -61,7 +61,7 @@ public async Task I_can_get_the_metadata_of_a_channel_by_slug()
6161
public async Task I_can_get_the_metadata_of_a_channel_by_handle()
6262
{
6363
// Arrange
64-
var youtube = new YoutubeClient();
64+
using var youtube = new YoutubeClient();
6565

6666
// Act
6767
var channel = await youtube.Channels.GetByHandleAsync(ChannelHandles.Normal);
@@ -79,7 +79,7 @@ public async Task I_can_get_the_metadata_of_a_channel_by_handle()
7979
public async Task I_can_get_the_metadata_of_any_available_channel(string channelId)
8080
{
8181
// Arrange
82-
var youtube = new YoutubeClient();
82+
using var youtube = new YoutubeClient();
8383

8484
// Act
8585
var channel = await youtube.Channels.GetAsync(channelId);
@@ -95,7 +95,7 @@ public async Task I_can_get_the_metadata_of_any_available_channel(string channel
9595
public async Task I_can_get_videos_uploaded_by_a_channel()
9696
{
9797
// Arrange
98-
var youtube = new YoutubeClient();
98+
using var youtube = new YoutubeClient();
9999

100100
// Act
101101
var videos = await youtube.Channels.GetUploadsAsync(ChannelIds.Normal);

YoutubeExplode.Tests/ClosedCaptionSpecs.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ClosedCaptionSpecs
1414
public async Task I_can_get_the_list_of_available_closed_caption_tracks_on_a_video()
1515
{
1616
// Arrange
17-
var youtube = new YoutubeClient();
17+
using var youtube = new YoutubeClient();
1818

1919
// Act
2020
var manifest = await youtube.Videos.ClosedCaptions.GetManifestAsync(
@@ -53,7 +53,7 @@ public async Task I_can_get_the_list_of_available_closed_caption_tracks_on_a_vid
5353
public async Task I_can_get_a_specific_closed_caption_track_from_a_video()
5454
{
5555
// Arrange
56-
var youtube = new YoutubeClient();
56+
using var youtube = new YoutubeClient();
5757

5858
// Act
5959
var manifest = await youtube.Videos.ClosedCaptions.GetManifestAsync(
@@ -71,7 +71,7 @@ public async Task I_can_get_a_specific_closed_caption_track_from_a_video()
7171
public async Task I_can_get_a_specific_closed_caption_track_from_a_video_that_has_broken_autogenerated_captions()
7272
{
7373
// Arrange
74-
var youtube = new YoutubeClient();
74+
using var youtube = new YoutubeClient();
7575

7676
// Act
7777
var manifest = await youtube.Videos.ClosedCaptions.GetManifestAsync(
@@ -89,7 +89,7 @@ public async Task I_can_get_a_specific_closed_caption_track_from_a_video_that_ha
8989
public async Task I_can_get_an_individual_closed_caption_from_a_video()
9090
{
9191
// Arrange
92-
var youtube = new YoutubeClient();
92+
using var youtube = new YoutubeClient();
9393

9494
// Act
9595
var manifest = await youtube.Videos.ClosedCaptions.GetManifestAsync(
@@ -109,7 +109,7 @@ public async Task I_can_get_an_individual_closed_caption_from_a_video()
109109
public async Task I_can_get_an_individual_closed_caption_part_from_a_video()
110110
{
111111
// Arrange
112-
var youtube = new YoutubeClient();
112+
using var youtube = new YoutubeClient();
113113

114114
// Act
115115
var manifest = await youtube.Videos.ClosedCaptions.GetManifestAsync(
@@ -132,7 +132,7 @@ public async Task I_can_download_a_specific_closed_caption_track_from_a_video()
132132
{
133133
// Arrange
134134
using var file = TempFile.Create();
135-
var youtube = new YoutubeClient();
135+
using var youtube = new YoutubeClient();
136136

137137
// Act
138138
var manifest = await youtube.Videos.ClosedCaptions.GetManifestAsync(

0 commit comments

Comments
 (0)