Beispiel #1
0
        /// <summary>
        /// Search video in youtube
        /// </summary>
        /// <param name="searchRequest"></param>
        /// <returns></returns>
        private async Task <List <ResultVideo> > SearchInYoutubeAsync(SearchRequest searchRequest)
        {
            var channelService = _serviceFactory.CreateChannelService();
            var videoService   = _serviceFactory.CreateVideoService();

            var resultVideos = new List <ResultVideo>();

            // Create the service.
            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey          = "AIzaSyAZQwG0x87WljOsVjJGfL1fIC30EWf42pg",
                ApplicationName = "BulbaCourses"
            });

            // Run the request.
            var searchListRequest = youtubeService.Search.List("snippet");

            searchListRequest.Type            = "video";
            searchListRequest.PublishedAfter  = searchRequest.PublishedAfter;
            searchListRequest.PublishedBefore = searchRequest.PublishedBefore;
            searchListRequest.VideoDefinition = (VideoDefinitionEnum)Enum.Parse(typeof(VideoDefinitionEnum), searchRequest.Definition);
            searchListRequest.VideoDimension  = (VideoDimensionEnum)Enum.Parse(typeof(VideoDimensionEnum), searchRequest.Dimension);
            searchListRequest.VideoDuration   = (VideoDurationEnum)Enum.Parse(typeof(VideoDurationEnum), searchRequest.Duration);
            searchListRequest.VideoCaption    = (VideoCaptionEnum)Enum.Parse(typeof(VideoCaptionEnum), searchRequest.VideoCaption);
            searchListRequest.Q          = searchRequest.Title;
            searchListRequest.MaxResults = 10;

            // Call the search.list method to retrieve results matching the specified searchRequest
            var searchListResponse = await searchListRequest.ExecuteAsync();

            foreach (var searchResult in searchListResponse.Items)
            {
                var searchListVideo = youtubeService.Videos.List("contentDetails");
                searchListVideo.Id = searchResult.Id.VideoId;
                var responceVideo = await searchListVideo.ExecuteAsync();

                var videoContentDetails = responceVideo.Items[0].ContentDetails;

                var resultVideo = new ResultVideo();
                resultVideo.Id           = searchResult.Id.VideoId;
                resultVideo.Title        = searchResult.Snippet.Title;
                resultVideo.PublishedAt  = searchResult.Snippet.PublishedAt;
                resultVideo.Description  = searchResult.Snippet.Description;
                resultVideo.Thumbnail    = searchResult.Snippet.Thumbnails.High.Url;
                resultVideo.Definition   = videoContentDetails.Definition;
                resultVideo.Dimension    = videoContentDetails.Dimension;
                resultVideo.Duration     = videoContentDetails.Duration;
                resultVideo.VideoCaption = videoContentDetails.Caption;

                var channel = new Channel()
                {
                    Id   = searchResult.Snippet.ChannelId,
                    Name = searchResult.Snippet.ChannelTitle,
                };
                if (!channelService.Exists(channel))
                {
                    channelService.Save(channel);
                }

                resultVideo.Channel_Id = channel.Id;

                var videoFromDb = videoService.GetById(resultVideo.Id);
                if (videoFromDb == null)
                {
                    resultVideo = videoService.Save(resultVideo);
                }
                else
                {
                    resultVideo = videoFromDb;
                }

                resultVideos.Add(resultVideo);
            }
            return(resultVideos);
        }