Exemple #1
0
        private void aButtonGetData_Click(object sender, EventArgs e)
        {
            string       videoId = aTextId.Text;
            YouTubeVideo video   = new YouTubeVideo(videoId);

            aLabelTitle.Text         = video.title;
            aLabelPublishedDate.Text = video.publishedDate.ToShortDateString();
        }
Exemple #2
0
        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 ID not found
            }
        }
        // Returns a YouTubeVideo object array based on the provided IDs
        // Returns null if all IDs are invalid, else returns all valid ID requests
        public static YouTubeVideo[] GetVideos(string[] videoIDs)
        {
            const int maxVideosPerRequest = 50;

            YouTubeVideo[] returnedVideos;

            // Check for empty array
            if (videoIDs != null && videoIDs.Length > 0)
            {
                // Convert videoID array into requests of the maximum allowed number of IDs
                // Find the number of requests we'll be making
                int      numberOfRequests = videoIDs.Length / maxVideosPerRequest + 1;
                string[] requestIds       = new string[numberOfRequests];

                // Transform array of IDs into an array of CSV requests
                int videoIdIndex = 0;
                for (int i = 0; i < numberOfRequests; i++)
                {
                    requestIds[i] = "";

                    for (int j = 0; videoIdIndex < videoIDs.Length && j < maxVideosPerRequest; j++)
                    {
                        requestIds[i] += videoIDs[videoIdIndex];
                        if (j < maxVideosPerRequest - 1)
                        {
                            requestIds[i] += ",";
                        }
                        videoIdIndex++;
                    }
                }

                // Send and capture each request
                VideoListResponse[] responses = new VideoListResponse[numberOfRequests];
                int numberOfResponses         = 0;

                for (int i = 0; i < responses.Length; i++)
                {
                    // Request video info snippet
                    var request = ytService.Videos.List("snippet");
                    request.Id = requestIds[i];

                    // Executes the request and captures the response
                    responses[i] = request.Execute();

                    // Count how many valid responses are returned
                    numberOfResponses += responses[i].Items.Count;
                }


                // If any videos were found
                if (numberOfResponses > 0)
                {
                    // Create new YouTubeVideo object for each response item, and add it to the array
                    returnedVideos = new YouTubeVideo[numberOfResponses];
                    int returnedVideoIndex = 0;

                    foreach (var response in responses)
                    {
                        for (int i = 0; i < response.Items.Count; i++)
                        {
                            string   id            = response.Items[i].Id;
                            string   title         = response.Items[i].Snippet.Title;
                            string   description   = response.Items[i].Snippet.Description;
                            DateTime publishedDate = response.Items[i].Snippet.PublishedAt.Value;

                            returnedVideos[returnedVideoIndex] = new YouTubeVideo(id, title, description, publishedDate);
                            returnedVideoIndex++;
                        }
                    }
                    return(returnedVideos);
                }
            }
            // Empty input or no responses
            return(null);
        }