示例#1
0
        /// <summary>
        /// Gets the metadata associated with the specified video.
        /// </summary>
        public async Task <Video> GetAsync(VideoId id)
        {
            // We can try to extract video metadata from two sources: mix playlist and the video watch page.
            // First is significantly faster but doesn't always work.

            try
            {
                return(await GetVideoFromMixPlaylistAsync(id));
            }
            catch (Exception ex) when(ex is YoutubeExplodeException || ex is InvalidOperationException)
            {
                return(await GetVideoFromWatchPageAsync(id));
            }
        }
示例#2
0
 /// <summary>
 /// Initializes an instance of <see cref="Video"/>.
 /// </summary>
 public Video(
     VideoId id,
     string title,
     string author,
     DateTimeOffset uploadDate,
     string description,
     TimeSpan duration,
     ThumbnailSet thumbnails,
     IReadOnlyList <string> keywords,
     Engagement engagement)
 {
     Id          = id;
     Title       = title;
     Author      = author;
     UploadDate  = uploadDate;
     Description = description;
     Duration    = duration;
     Thumbnails  = thumbnails;
     Keywords    = keywords;
     Engagement  = engagement;
 }
示例#3
0
        private async Task <Video> GetVideoFromMixPlaylistAsync(VideoId id)
        {
            var playlistInfo = await PlaylistResponse.GetAsync(_httpClient, "RD" + id.Value);

            var video = playlistInfo.GetVideos().First(x => x.GetId() == id.Value);

            return(new Video(
                       id,
                       video.GetTitle(),
                       video.GetAuthor(),
                       video.GetChannelId(),
                       video.GetUploadDate(),
                       video.GetDescription(),
                       video.GetDuration(),
                       new ThumbnailSet(id),
                       video.GetKeywords(),
                       new Engagement(
                           video.GetViewCount(),
                           video.GetLikeCount(),
                           video.GetDislikeCount()
                           )
                       ));
        }
示例#4
0
        /// <summary>
        /// Gets the metadata associated with the specified video.
        /// </summary>
        public async Task <Video> GetAsync(VideoId id)
        {
            var videoInfoResponse = await VideoInfoResponse.GetAsync(_httpClient, id);

            var playerResponse = videoInfoResponse.GetPlayerResponse();

            var watchPage = await WatchPage.GetAsync(_httpClient, id);

            return(new Video(
                       id,
                       playerResponse.GetVideoTitle(),
                       playerResponse.GetVideoAuthor(),
                       playerResponse.GetVideoUploadDate(),
                       playerResponse.GetVideoDescription(),
                       playerResponse.GetVideoDuration(),
                       new ThumbnailSet(id),
                       playerResponse.GetVideoKeywords(),
                       new Engagement(
                           playerResponse.TryGetVideoViewCount() ?? 0,
                           watchPage.TryGetVideoLikeCount() ?? 0,
                           watchPage.TryGetVideoDislikeCount() ?? 0
                           )
                       ));
        }
示例#5
0
        /// <summary>
        /// Gets the metadata associated with the specified video.
        /// </summary>
        public async ValueTask <Video> GetAsync(
            VideoId videoId,
            CancellationToken cancellationToken = default)
        {
            var watchPage = await _controller.GetVideoWatchPageAsync(videoId, cancellationToken);

            var playerResponse =
                watchPage.TryGetPlayerResponse() ??
                throw new YoutubeExplodeException("Could not extract player response.");

            var title =
                playerResponse.TryGetVideoTitle() ??
                throw new YoutubeExplodeException("Could not extract video title.");

            var channelTitle =
                playerResponse.TryGetVideoAuthor() ??
                throw new YoutubeExplodeException("Could not extract video author.");

            var channelId =
                playerResponse.TryGetVideoChannelId() ??
                throw new YoutubeExplodeException("Could not extract video channel ID.");

            var uploadDate =
                playerResponse.TryGetVideoUploadDate() ??
                throw new YoutubeExplodeException("Could not extract video upload date.");

            var description = playerResponse.TryGetVideoDescription() ?? "";
            var duration    = playerResponse.TryGetVideoDuration();

            var thumbnails = playerResponse
                             .GetVideoThumbnails()
                             .Select(t =>
            {
                var thumbnailUrl =
                    t.TryGetUrl() ??
                    throw new YoutubeExplodeException("Could not extract thumbnail URL.");

                var thumbnailWidth =
                    t.TryGetWidth() ??
                    throw new YoutubeExplodeException("Could not extract thumbnail width.");

                var thumbnailHeight =
                    t.TryGetHeight() ??
                    throw new YoutubeExplodeException("Could not extract thumbnail height.");

                var thumbnailResolution = new Resolution(thumbnailWidth, thumbnailHeight);

                return(new Thumbnail(thumbnailUrl, thumbnailResolution));
            })
                             .Concat(Thumbnail.GetDefaultSet(videoId))
                             .ToArray();

            var keywords = playerResponse.GetVideoKeywords();

            // Engagement statistics may be hidden
            var viewCount    = playerResponse.TryGetVideoViewCount() ?? 0;
            var likeCount    = watchPage.TryGetVideoLikeCount() ?? 0;
            var dislikeCount = watchPage.TryGetVideoDislikeCount() ?? 0;

            return(new Video(
                       videoId,
                       title,
                       new Author(channelId, channelTitle),
                       uploadDate,
                       description,
                       duration,
                       thumbnails,
                       keywords,
                       new Engagement(viewCount, likeCount, dislikeCount)
                       ));
        }
 /// <summary>
 /// Gets the metadata associated with the specified video.
 /// </summary>
 public async Task <Video> GetAsync(VideoId id)
 {
     return(await GetVideoFromWatchPageAsync(id));
 }