Example #1
0
        private void ParseYouTubeSearchResults(YouTubeSearchRequest searchRequest, SearchListResponse searchResults)
        //================================================================================================================
        // Parse the given YouTube search results into a search results list
        //
        // Parameters
        //      searchRequest: YouTube search request data
        //      searchResults: YouTube search results data
        //
        // Outputs
        //      The SearchResults list in the searchRequest object is populated
        //================================================================================================================
        {
            int resultCount = 0;

            foreach (SearchResult searchResult in searchResults.Items)
            {
                // Bump the result count
                resultCount++;

                // Create and initialize a new YouTube search result
                YouTubeSearchResult item = new YouTubeSearchResult
                {
                    SessionKey     = searchRequest.SessionKey,
                    ResultNo       = resultCount,
                    YouTubeVideoId = searchResult.Id.VideoId,
                    Title          = searchResult.Snippet.Title,
                    Description    = searchResult.Snippet.Description,
                    ThumbnailURL   = searchResult.Snippet.Thumbnails.Default__.Url,
                    YouTubeURL     = string.Format(YOU_TUBE_LINK, searchResult.Id.VideoId)
                };

                // Add this result to the collection
                searchRequest.SearchResults.Add(item);
            }
        }
        /// <summary>
        /// Retrieves the information about the YouTube video.
        /// </summary>
        /// <param name="videoId"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        private async Task <bool> GetVideoAsync(string videoId, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(false);
            }

            YouTubeService youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey          = Settings.Default.YouTubeApiKey,  // "AIzaSyBvZfGSa9NUidAMyvT73Kja3ShotaI9VO0",
                ApplicationName = Settings.Default.YouTubeClientId // "YouTubeClient"
            });

            _log.Info("successfully init youtubeService in GetVideoAsync");
            try
            {
                string part   = "snippet";
                string fields = "items(id, snippet(title, description))";
                if (_extendedProps)
                {
                    part   = "snippet, statistics, contentDetails";
                    fields = "items(id, snippet(title, channelTitle, channelId, publishedAt, description, thumbnails), statistics(viewCount, likeCount, dislikeCount), contentDetails(duration))";
                }

                var videosListRequest = youtubeService.Videos.List(part);
                _log.Info("Got video list in GetVideoAsync()");
                // Set the request filters
                videosListRequest.Id     = videoId;
                videosListRequest.Fields = fields;

                // Call the videos.list method to retrieve results matching the specified query term.
                var videosListResponse = await videosListRequest.ExecuteAsync(cancellationToken);

                _log.Info("Called the videos.list method to retrieve results matching the specified query term. ");
                // Create the YouTubeSearchResult object from the YouTube Video.
                YouTubeSearchResult result = YouTubeParser.ParseResult(videosListResponse.Items[0], _parser, _query);

                await System.Windows.Application.Current.Dispatcher.BeginInvoke(
                    System.Windows.Threading.DispatcherPriority.Normal, new Action(() =>
                {
                    if (!cancellationToken.IsCancellationRequested)
                    {
                        _store.Add(result);
                    }
                }));

                return(true);
            }
            catch (Exception ex)
            {
                _log.Error("Exception in GetVideoAsync", ex);

                return(false);
            }
            finally
            {
                youtubeService.Dispose();
            }
        }
Example #3
0
 public static SearchResult From(YouTubeSearchResult result)
 {
     return(new SearchResult
     {
         Type = result.Type,
         Author = result.Author,
         Description = result.Description,
         Duration = result.Duration,
         Thumbnail = result.Thumbnail,
         Title = result.Title,
         Url = result.Url,
         ViewCount = result.ViewCount,
         VideoCount = result.VideoCount
     });
 }
        public static YouTubeSearchResult ParseResult(Video video, ParserFunction parser, string keyword)
        {
            YouTubeSearchResult result = new YouTubeSearchResult();

            result.Id            = video.Id;
            result.Title         = video.Snippet.Title;
            result.ChannelTitle  = video.Snippet.ChannelTitle;
            result.ViewCount     = video.Statistics?.ViewCount;
            result.PublishedDate = video.Snippet.PublishedAt;
            result.LikeCount     = video.Statistics?.LikeCount;
            result.DislikeCount  = video.Statistics?.DislikeCount;
            result.Description   = video.Snippet.Description;
            result.Source        = "YouTube";
            result.SourceAddress = string.Format("https://www.youtube.com/watch?v={0}", video.Id);
            result.Domains       = parser.Invoke(video.Snippet.Description, "YouTube", keyword);
            result.ThumbnailUrl  = video.Snippet.Thumbnails?.Medium.Url;
            result.Duration      = Assistant.ToTimeSpan(video.ContentDetails?.Duration).ToString();

            return(result);
        }