@@ -43,7 +43,7 @@ public async Task<Job> GetTracks(string input, ExtractionSettings extraction, Ex
4343 if ( _yt . GetDeleted )
4444 {
4545 context . Log . Info ( "Getting deleted videos.." ) ;
46- var archive = new YouTube . YouTubeArchiveRetriever ( context . Log ) ;
46+ using var archive = new YouTube . YouTubeArchiveRetriever ( context . Log ) ;
4747 deleted = await archive . RetrieveDeleted ( input , printFailed : _yt . DeletedOnly ) ;
4848 }
4949 if ( ! _yt . DeletedOnly )
@@ -115,7 +115,7 @@ public static string ApiKey
115115
116116 var playlistRequest = service . Playlists . List ( "snippet" ) ;
117117 playlistRequest . Id = playlistId ;
118- var playlistResponse = playlistRequest . Execute ( ) ;
118+ var playlistResponse = await playlistRequest . ExecuteAsync ( ) ;
119119 if ( playlistResponse . Items . Count == 0 )
120120 throw new InvalidOperationException ( $ "Could not retrieve YouTube playlist '{ playlistId } '.") ;
121121
@@ -131,7 +131,7 @@ public static string ApiKey
131131
132132 while ( playlistItemsRequest != null && count < max + offset )
133133 {
134- var playlistItemsResponse = playlistItemsRequest . Execute ( ) ;
134+ var playlistItemsResponse = await playlistItemsRequest . ExecuteAsync ( ) ;
135135 foreach ( var playlistItem in playlistItemsResponse . Items )
136136 {
137137 if ( count >= offset )
@@ -149,7 +149,7 @@ public static string ApiKey
149149
150150 var videoRequest = service . Videos . List ( "contentDetails,snippet" ) ;
151151 videoRequest . Id = playlistItem . Snippet . ResourceId . VideoId ;
152- var videoResponse = videoRequest . Execute ( ) ;
152+ var videoResponse = await videoRequest . ExecuteAsync ( ) ;
153153
154154 title = playlistItem . Snippet . Title ;
155155 if ( videoResponse . Items . Count == 0 ) continue ;
@@ -295,7 +295,7 @@ public static async Task<SongJob> ParseTrackInfo(string title, string uploader,
295295 var service = RequireService ( ) ;
296296 var videoRequest = service . Videos . List ( "contentDetails,snippet" ) ;
297297 videoRequest . Id = id ;
298- var videoResponse = videoRequest . Execute ( ) ;
298+ var videoResponse = await videoRequest . ExecuteAsync ( ) ;
299299 o . title = videoResponse . Items [ 0 ] . Snippet . Title ;
300300 o . uploader = videoResponse . Items [ 0 ] . Snippet . ChannelTitle ;
301301 o . length = ( int ) XmlConvert . ToTimeSpan ( videoResponse . Items [ 0 ] . ContentDetails . Duration ) . TotalSeconds ;
@@ -333,7 +333,7 @@ public static void StopService()
333333 public static async Task < Dictionary < string , SongJob > > GetDictYtExplode ( string url , int max = int . MaxValue , int offset = 0 , IJobLog ? log = null )
334334 {
335335 log ??= ExtractorContext . None . Log ;
336- var youtube = new YoutubeClient ( ) ;
336+ using var youtube = new YoutubeClient ( ) ;
337337 var playlist = await youtube . Playlists . GetAsync ( url ) ;
338338 var songs = new Dictionary < string , SongJob > ( ) ;
339339 int count = 0 ;
@@ -360,15 +360,15 @@ public static async Task<Dictionary<string, SongJob>> GetDictYtExplode(string ur
360360
361361 public static async Task < string > GetPlaylistTitle ( string url )
362362 {
363- var youtube = new YoutubeClient ( ) ;
363+ using var youtube = new YoutubeClient ( ) ;
364364 var playlist = await youtube . Playlists . GetAsync ( url ) ;
365365 return playlist . Title ;
366366 }
367367
368368 public static async Task < ( string , List < SongJob > ) > GetSongsYtExplode ( string url , int max = int . MaxValue , int offset = 0 , IJobLog ? log = null )
369369 {
370370 log ??= ExtractorContext . None . Log ;
371- var youtube = new YoutubeClient ( ) ;
371+ using var youtube = new YoutubeClient ( ) ;
372372 var playlist = await youtube . Playlists . GetAsync ( url ) ;
373373 var playlistTitle = playlist . Title ;
374374 var songs = new List < SongJob > ( ) ;
@@ -410,7 +410,7 @@ public static async Task<string> UrlToId(string url)
410410 [ GeneratedRegex ( @"document\.title\s*=\s*""(.+?) - YouTube"";" ) ]
411411 private static partial Regex DocumentTitleRegex ( ) ;
412412
413- public class YouTubeArchiveRetriever
413+ public class YouTubeArchiveRetriever : IDisposable
414414 {
415415 private readonly HttpClient _client ;
416416 private readonly IJobLog _log ;
@@ -424,7 +424,7 @@ public YouTubeArchiveRetriever(IJobLog? log = null)
424424
425425 public async Task < List < SongJob > > RetrieveDeleted ( string url , bool printFailed = true )
426426 {
427- var deletedVideoUrls = new BlockingCollection < string > ( ) ;
427+ using var deletedVideoUrls = new BlockingCollection < string > ( ) ;
428428
429429 int totalCount = 0 ;
430430 int archivedCount = 0 ;
@@ -435,7 +435,7 @@ public async Task<List<SongJob>> RetrieveDeleted(string url, bool printFailed =
435435 int workerCount = 4 ;
436436 var workers = new List < Task > ( ) ;
437437
438- var process = new Process
438+ using var process = new Process
439439 {
440440 StartInfo = new ProcessStartInfo
441441 {
@@ -498,7 +498,7 @@ public async Task<List<SongJob>> RetrieveDeleted(string url, bool printFailed =
498498 }
499499
500500 await Task . WhenAll ( workers ) ;
501- process . WaitForExit ( ) ;
501+ await process . WaitForExitAsync ( ) ;
502502 deletedVideoUrls . CompleteAdding ( ) ;
503503 _log . Info ( $ "Deleted metadata total/archived/retrieved: { totalCount } /{ archivedCount } /{ songs . Count } ") ;
504504
@@ -521,6 +521,12 @@ public async Task<List<SongJob>> RetrieveDeleted(string url, bool printFailed =
521521 return songs . ToList ( ) ;
522522 }
523523
524+ public void Dispose ( )
525+ {
526+ _client . Dispose ( ) ;
527+ GC . SuppressFinalize ( this ) ;
528+ }
529+
524530 private async Task < List < string > ? > GetOldestArchiveUrls ( string url , int limit )
525531 {
526532 var url2 = $ "http://web.archive.org/cdx/search/cdx?url={ url } &fl=timestamp,original&filter=statuscode:200&sort=timestamp:asc&limit={ limit } ";
0 commit comments