コード例 #1
0
        private static async void AddDownloadItems(string url, Panel pnl)
        {
            try
            {
                YoutubeClient client = new YoutubeClient();

                //Get the playlist object from the id
                YoutubeExplode.Models.Playlist playList = await client.GetPlaylistAsync(url);

                //We now have a playlist with every video from that playlist. (.Videos)
                foreach (YoutubeExplode.Models.Video video in playList.Videos)
                {
                    DownloadItem item = new DownloadItem(video);
                    //Determines where to place the downloadItem
                    int y = 0;
                    if (downloadItems.Count > 0)
                    {
                        y = downloadItems[downloadItems.Count - 1].Location.Y + item.Height;
                    }


                    pnl.Controls.Add(item);
                    item.Location = new Point(item.Location.X, y);
                    downloadItems.Add(item);
                }
            }
            catch (Exception)
            {
                MessageFormManager.MakeMessagePopup("Something went wrong!", "Could not find a video with that youtube url!\r\nPlease try again.", 5);
            }
        }
コード例 #2
0
        static async Task getplaylist(string channelid, string path)
        {
            var client = new YoutubeClient();

            YoutubeExplode.Models.Playlist video = null;
            int  retry2 = 10;
            bool Bretry = false;

            Console.WriteLine("Downloading playlist started " + channelid);
            while (video == null)
            {
                if (retry2 == 0)
                {
                    Bretry = true;
                    break;
                }
                try
                {
                    video = await client.GetPlaylistAsync(channelid);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Retrying playlist download " + (11 - retry2) + " attempt");
                }
                retry2--;
                if (video == null)
                {
                    Thread.Sleep(1000);
                }
                else
                {
                    break;
                }
            }

            //  var video = await client.GetChannelUploadsAsync(channelid);
            if (video != null)
            {
                try
                {
                    foreach (var x in video.Videos)
                    {
                        await getshit(x.Id, path, false, null);
                    }
                }
                catch (Exception x)
                {
                    Console.WriteLine(x);
                }
                // Console.WriteLine("Downloading channel finished " + channelid);
            }
        }
コード例 #3
0
        private async Task DownloadPlaylistAsync(string id, YoutubeExplode.Models.Playlist playlist)
        {
            var dTasks = new Task[playlist.Videos.Count];
            var i      = 0;

            foreach (var video in playlist.Videos)
            {
                dTasks[i] = DownloadAndConvertVideoAsync(video.Id, video);
                Console.WriteLine($"Downloading: {video.Title}");
                i++;
            }
            Task.WaitAll(dTasks);
            Console.WriteLine("Everything downloaded");
        }
コード例 #4
0
        private void DownloadItem_Load(object sender, EventArgs e)
        {
            try
            {
                downloadTask = new Task(Download); //just so it isn't null and we can check on IsCanceled
                convertTask  = new Task(Download); //just so it isn't null and we can check on IsCanceled

                YoutubeClient client = new YoutubeClient();
                pbLoad.Image = Properties.Resources.load25x25;

                //Attempt to get the single video id
                string singleVideoId = "";
                if (theVideo != null)
                {
                    singleVideoId = theVideo.Id;
                }
                else
                {
                    YoutubeClient.TryParseVideoId(youtubeUrl, out singleVideoId);
                }


                //Attempt to get the playlist id
                string playlistId = "";
                if (thePlaylist != null)
                {
                    playlistId = thePlaylist.Id;
                }
                else
                {
                    YoutubeClient.TryParsePlaylistId(youtubeUrl, out playlistId);
                }



                //Thread that handles playlists loading data
                Thread playlistThread = null;
                playlistThread = new Thread(async() =>
                {
                    if (!string.IsNullOrEmpty(playlistId))
                    {
                        if (thePlaylist == null)
                        {
                            thePlaylist = await client.GetPlaylistAsync(playlistId);
                        }
                    }
                });
                playlistThread.IsBackground = true;
                playlistThread.Start();



                //Thread that handles loading single video data
                Thread singleVideoThread = new Thread(async() =>
                {
                    if (!string.IsNullOrEmpty(singleVideoId))
                    {
                        try
                        {
                            //Put the thumbnail into the picturebox
                            pbYoutubeThumbnail.Load("http://img.youtube.com/vi/" + singleVideoId + "/0.jpg");
                            //Get the video
                            try
                            {
                                if (theVideo == null)
                                {
                                    theVideo = await client.GetVideoAsync(singleVideoId);
                                }
                            }
                            catch (Exception ex)
                            {
                                SetVideoUnavailableErrorText(ex);
                                return;
                            }



                            string title      = theVideo.Title;
                            string author     = theVideo.Author;
                            TimeSpan duration = theVideo.Duration;

                            string subTitle  = "";
                            string subAuthor = "";

                            string completeString = ""; //The complete string containing title + author + duration


                            if (title.Length > 33)
                            {
                                subTitle        = title.Substring(0, 33) + "...";
                                completeString += subTitle + "   ";
                            }
                            else
                            {
                                completeString += title + "   ";
                            }


                            if (author.Length > 23)
                            {
                                subAuthor       = author.Substring(0, 23) + "...";
                                completeString += subAuthor + "   ";
                            }
                            else
                            {
                                completeString += author + "   ";
                            }

                            completeString += duration;

                            lblTitle.Invoke((MethodInvoker)(() =>
                            {
                                lblTitle.Text = completeString;
                                lblExit.Invoke((MethodInvoker)(() =>
                                {
                                    lblExit.Enabled = true;
                                }));
                                btnDownload.Invoke((MethodInvoker)(() =>
                                {
                                    btnDownload.Enabled = true;
                                }));
                                pbLoad.Invoke((MethodInvoker)(() =>
                                {
                                    pbLoad.Image = null;
                                    pbLoad.BackgroundImage = Properties.Resources.Check;
                                }));
                                lblStatus.Invoke((MethodInvoker)(() =>
                                {
                                    if (!isHistory)
                                    {
                                        lblStatus.Text = "Ready.";
                                    }
                                    isReady = true;
                                }));
                            }));
                        }
                        catch (System.Net.WebException ex)
                        {
                            SetVideoUnavailableErrorText(ex);
                        }
                    }
                });
                singleVideoThread.IsBackground = true;
                singleVideoThread.Start();
            }
            catch (Exception)
            {
                lblTitle.Text  = "An error occured - Could not load this entry";
                lblStatus.Text = "Error.";
                canDownload    = false;
            }
        }