/// <summary> /// Refreshes the <see cref="YoutubeVideo"/> object with the specified <see cref="VideoRefreshFlags"/> and updates it. /// </summary> /// /// <param name="flags"> /// <see cref="VideoRefreshFlags"/> with which to update the <see cref="YoutubeVideo"/> object. /// </param> public void Refresh(VideoRefreshFlags flags) { //TODO: Use get_video_info where possible. // Downloads the HTML code of the Youtube video web page. if (_url == null) { throw new InvalidOperationException("Cannot Refresh YoutubeVideo object when Url is null."); } if (flags == VideoRefreshFlags.None) { return; } var webpage = (string)null; using (var client = new WebClient() { Encoding = Encoding.UTF8 }) webpage = client.DownloadString(_url); var extractor = new YoutubeVideoExtractor(webpage); if (!extractor.ExtractAvailability()) { throw new YoutubeVideoNotAvailableException(this, "Youtube video is not available or is private."); } //if (flags.HasFlag(VideoRefreshFlags.Playlist)) // _playlist.Refresh(PlaylistRefreshFlags.None); if (flags.HasFlag(VideoRefreshFlags.Title)) { _title = extractor.ExtractTitle(); } if (flags.HasFlag(VideoRefreshFlags.Description)) { _description = extractor.ExtractDescription(); } if (flags.HasFlag(VideoRefreshFlags.ViewCount)) { _views = extractor.ExtractViewCount(); } if (flags.HasFlag(VideoRefreshFlags.Length)) { _length = extractor.ExtractLength(); } if (flags.HasFlag(VideoRefreshFlags.Formats)) { _formats = extractor.ExtractFormats(); } }
/// <summary> /// Initializes a new instance of the <see cref="YoutubeVideo"/> class with /// the specified URL pointing to the video on Youtube and the specified <see cref="VideoRefreshFlags"/> which will be passed to /// the <see cref="Refresh(VideoRefreshFlags)"/> method when the <see cref="YoutubeVideo"/> object is done initializing. /// </summary> /// /// <param name="url"> /// URL pointing to the video on Youtube. /// </param> /// <param name="flags"> /// The <see cref="VideoRefreshFlags"/> which will be passed to the <see cref="Refresh(VideoRefreshFlags)"/> method /// when the <see cref="YoutubeVideo"/> is done initializing. /// </param> public YoutubeVideo(Uri url, VideoRefreshFlags flags) { if (url == null) { throw new ArgumentNullException(nameof(url)); } Url = url; if (flags != VideoRefreshFlags.None) { Refresh(); } }
/// <summary> /// Initializes a new instance of the <see cref="YoutubePlaylist"/> class with the specified /// URL pointing to the video on Youtube and the specified <see cref="PlaylistRefreshFlags"/> and <see cref="VideoRefreshFlags"/> /// which will be passed to the <see cref="Refresh(PlaylistRefreshFlags, VideoRefreshFlags)"/> method when the <see cref="YoutubePlaylist"/> /// object is done initializing. /// </summary> /// /// <param name="url"> /// URL pointing to the video on Youtube. /// </param> /// <param name="flags"> /// The <see cref="PlaylistRefreshFlags"/> which will be passed to the <see cref="Refresh(PlaylistRefreshFlags, VideoRefreshFlags)"/> method /// when the <see cref="YoutubePlaylist"/> is done initializing. /// </param> /// <param name="vidFlags"> /// The <see cref="VideoRefreshFlags"/> which will be passed to the <see cref="Refresh(PlaylistRefreshFlags, VideoRefreshFlags)"/> method /// when the <see cref="YoutubePlaylist"/> is done initializing. /// </param> public YoutubePlaylist(Uri url, PlaylistRefreshFlags flags, VideoRefreshFlags vidFlags) { if (url == null) { throw new ArgumentNullException(nameof(url)); } Url = url; if (flags != PlaylistRefreshFlags.None) { Refresh(flags, vidFlags); } }
/// <summary> /// Initializes a new instance of the <see cref="YoutubePlaylist"/> class with the specified /// Youtube playlist ID and the specified <see cref="PlaylistRefreshFlags"/> and <see cref="VideoRefreshFlags"/> /// which will be passed to the <see cref="Refresh(PlaylistRefreshFlags, VideoRefreshFlags)"/> method when the <see cref="YoutubePlaylist"/> /// object is done initializing. /// </summary> /// /// <param name="playlistId"> /// Youtube playlist ID. /// </param> /// <param name="flags"> /// The <see cref="PlaylistRefreshFlags"/> which will be passed to the <see cref="Refresh(PlaylistRefreshFlags, VideoRefreshFlags)"/> method /// when the <see cref="YoutubePlaylist"/> is done initializing. /// </param> /// <param name="vidFlags"> /// The <see cref="VideoRefreshFlags"/> which will be passed to the <see cref="Refresh(PlaylistRefreshFlags, VideoRefreshFlags)"/> method /// when the <see cref="YoutubePlaylist"/> is done initializing. /// </param> public YoutubePlaylist(string playlistId, PlaylistRefreshFlags flags, VideoRefreshFlags vidFlags) { if (string.IsNullOrWhiteSpace(playlistId)) { throw new ArgumentNullException(nameof(playlistId)); } _playlist = new List <YoutubeVideo>(); Url = new Uri(BaseUrl.OriginalString + playlistId); if (flags != PlaylistRefreshFlags.None) { Refresh(flags, vidFlags); } }
/// <summary> /// Initializes a new instance of the <see cref="YoutubeVideo"/> class with /// the specified Youtube video ID and the specified <see cref="VideoRefreshFlags"/> which will be passed to /// the <see cref="Refresh(VideoRefreshFlags)"/> method when the <see cref="YoutubeVideo"/> object is done initializing. /// </summary> /// /// <param name="videoId"> /// Youtube video ID. /// </param> /// <param name="flags"> /// The <see cref="VideoRefreshFlags"/> which will be passed to the <see cref="Refresh(VideoRefreshFlags)"/> method /// when the <see cref="YoutubeVideo"/> is done initializing. /// </param> public YoutubeVideo(string videoId, VideoRefreshFlags flags) { if (string.IsNullOrWhiteSpace(videoId)) { throw new ArgumentNullException(nameof(videoId)); } if (videoId.Length != VideoIDLength) { throw new ArgumentException("Video ID must be 11 characters long.", nameof(videoId)); } if (!Utils.ValidYoutubeID(videoId)) { throw new ArgumentException("Video ID contains invalid Youtube ID characters.", nameof(videoId)); } Url = new Uri(BaseUrl.OriginalString + videoId); if (flags != VideoRefreshFlags.None) { Refresh(flags); } }
public List <YoutubeVideo> ExtractPlaylist(VideoRefreshFlags flags) { var playlist = new List <YoutubeVideo>(); var baseIndex = Data.IndexOf("<tr class=\"pl-video yt-uix-tile \""); while (baseIndex != -1) { // Extracts the HTML element <tr> with attribute "class" = "pl-video yt-uix-tile " // from the web page. var trElement = GetBetween("<", ">", baseIndex); // Extracts the HTML attribute "data-video-id" from the element // which contains the video ID of the playlist entry. var videoId = GetBetween(trElement, "data-video-id=\"", "\""); var video = new YoutubeVideo(videoId, flags); playlist.Add(video); baseIndex = Data.IndexOf("<tr class=\"pl-video yt-uix-tile \"", baseIndex + trElement.Length); } return(playlist); }
/// <summary> /// Refreshes the <see cref="YoutubePlaylist"/> object with the specified <see cref="PlaylistRefreshFlags"/> and /// the specified <see cref="VideoRefreshFlags"/> with which to update the <see cref="YoutubeVideo"/> objects in /// the <see cref="YoutubePlaylist"/>. /// </summary> /// /// <param name="flags"> /// <see cref="PlaylistRefreshFlags"/> with which to update the <see cref="YoutubePlaylist"/> object. /// </param> /// <param name="vidFlags"> /// <see cref="VideoRefreshFlags"/> with which to update the <see cref="YoutubeVideo"/> objects in the /// <see cref="YoutubePlaylist"/>. /// </param> public void Refresh(PlaylistRefreshFlags flags, VideoRefreshFlags vidFlags) { if (_url == null) { throw new InvalidOperationException("Cannot Refresh YoutubePlaylist object when Url is null."); } if (flags == PlaylistRefreshFlags.None) { return; } var webpage = (string)null; using (var client = new WebClient()) webpage = client.DownloadString(_url); var extractor = new YoutubePlaylistExtractor(webpage); if (!extractor.ExtractAvailability()) { throw new YoutubePlaylistNotAvailableException(this, "Youtube playlist is unavailable."); } if (flags.HasFlag(PlaylistRefreshFlags.Title)) { _title = extractor.ExtractTitle(); } if (flags.HasFlag(PlaylistRefreshFlags.ViewCount)) { _views = extractor.ExtractViews(); } if (flags.HasFlag(PlaylistRefreshFlags.Playlist)) { _playlist = extractor.ExtractPlaylist(vidFlags); } }