static void Main(string[] args) { try { string url = args[0]; string path = args[1]; bool isPlayList = (url.ToLower().Contains("list")); string stringCount = (args.Length == 3) ? args[2] : "0"; int count = int.TryParse(stringCount, out count) ? count : 0; //in case try parse fails if (isPlayList) { YoutubePlaylist.DownloadPlayListInformationOneAtATime(url, path, count).Wait(); } else { YoutubeVideo.DownloadYoutubeInformation(args).Wait(); } Console.WriteLine("Task Completed"); } catch (Exception e) { Console.WriteLine("Error"); Console.WriteLine(e); Console.WriteLine(); Console.WriteLine("There was a problem with the inputs. \n" + "Input -> Required { url:string path:string } || NOT-Required {count:int} "); Console.WriteLine(); Console.WriteLine("The inputs taken in."); for (int i = 0; i < args.Length; i++) { Console.WriteLine($"{i} : {args[i]}"); } } }
public static async Task DownloadPlayListInformationOneAtATime(string playlistUrl, string path, int count = 0, int waitTime = 500) { var youtube = new YoutubeClient(); // Get playlist metadata var playlist = await youtube.Playlists.GetAsync(playlistUrl); var title = playlist.Title; Console.WriteLine($"Downloading Playlist: {title}"); var author = playlist.Author; var description = playlist.Description; IReadOnlyList <YoutubeExplode.Videos.Video> somePlayListVideos = default; // /* * It is possible to just call the playlist onetime and use the GetAsyncEnumerator * then using that count/length property to figure which is smaller. * The lazy option was choosen instead */ try { //if the count is equal to zero move to the catch to get all the videos if (count == 0) { throw new Exception(); } // Get videos from 0 to the count somePlayListVideos = await youtube.Playlists .GetVideosAsync(playlist.Id) .BufferAsync(count); } catch { somePlayListVideos = await youtube.Playlists.GetVideosAsync(playlist.Id); } finally { string newPath = $"{path}/{title}/"; System.IO.FileInfo file = new System.IO.FileInfo(newPath); file.Directory.Create(); // If the directory already exists, this method does nothing. _ = File.WriteAllLinesAsync($"{file.FullName}/playlist-info.txt", new string[] { title, author, description }); List <Task> tempTaskList = new List <Task>(); for (int i = 0; i < somePlayListVideos.Count; i++) { Console.WriteLine($"Starting videos {i+1} of {somePlayListVideos.Count}"); YoutubeVideo.DownloadAudioVideoMuxButNoCaption(somePlayListVideos[i], newPath).Wait(); System.Threading.Thread.Sleep(waitTime); } }; }
public static void GetVideoInfo(YoutubeVideo video) { var videoRequest = ytService.Videos.List("snippet"); videoRequest.Id = video.id; var response = videoRequest.Execute(); if (response.Items.Count > 0) { video.title = response.Items[0].Snippet.Title; video.description = response.Items[0].Snippet.Description; video.publishedDate = response.Items[0].Snippet.PublishedAt.Value; } else { video.title = "No video with this ID."; video.description = "-"; } }