private static YouTubeVideo ParseEntry(XElement xEntry) { // Get namespaces XNamespace atom = xEntry.GetDefaultNamespace(); XNamespace media = xEntry.GetNamespaceOfPrefix("media"); XNamespace yt = xEntry.GetNamespaceOfPrefix("yt"); XNamespace gd = xEntry.GetNamespaceOfPrefix("gd"); // Some pre-parsing XElement xAuthor = xEntry.Element(atom + "author"); XElement xMedia = xEntry.Element(media + "group"); XElement xDuration = xMedia.Element(yt + "duration"); XElement xLink = xEntry.Elements(atom + "link").FirstOrDefault(x => x.GetAttributeValue("rel") == "alternate"); // Get the <yt:statistics> element describing the number of favorites and // number of views XElement xStatistics = xEntry.Element(yt + "statistics"); // Get the <yt:rating> element describing the number of lines and // number of dislikes (may not always be present) XElement xYouTubeRating = xEntry.Element(yt + "rating"); // Get the <gd:rating> element describing the video rating and // number of raters XElement xGoogleRating = xEntry.Element(gd + "rating"); // Initialize and return the object YouTubeVideo video = new YouTubeVideo(); video.Id = xLink == null ? null : Regex.Match(xLink.GetAttributeValue("href"), "v=([\\w-]{11})").Groups[1].Value; video.Published = xEntry.GetElementValue <DateTime>(atom + "published"); video.LastUpdated = xEntry.GetElementValue <DateTime>(atom + "updated"); video.Title = xEntry.GetElementValue(atom + "title"); video.Description = xMedia.GetElementValue(media + "description"); video.Link = xLink == null ? null : xLink.GetAttributeValue("href"); video.Author = xAuthor.GetElementValue(atom + "name"); video.Duration = TimeSpan.FromSeconds(xDuration.GetAttributeValue <int>("seconds")); video.FavoriteCount = xStatistics == null ? 0 : xStatistics.GetAttributeValue <int>("favoriteCount"); video.ViewCount = xStatistics == null ? 0 : xStatistics.GetAttributeValue <int>("viewCount"); video.Likes = xYouTubeRating == null ? 0 : xYouTubeRating.GetAttributeValue <int>("numLikes"); video.Dislikes = xYouTubeRating == null ? 0 : xYouTubeRating.GetAttributeValue <int>("numDislikes"); video.Rating = xGoogleRating == null ? 0 : xGoogleRating.GetAttributeValue <double>("average", CultureInfo.InvariantCulture); video.NumberOfRaters = xGoogleRating == null ? 0 : xGoogleRating.GetAttributeValue <int>("numRaters"); return(video); }
private static YouTubeVideo ParseItem(XElement xItem) { // Get namespaces XNamespace atom = xItem.GetNamespaceOfPrefix("atom"); XNamespace media = xItem.GetNamespaceOfPrefix("media"); XNamespace yt = xItem.GetNamespaceOfPrefix("yt"); XNamespace gd = xItem.GetNamespaceOfPrefix("gd"); // Some pre-parsing XElement xMedia = xItem.Element(media + "group"); XElement xDuration = xMedia.Element(yt + "duration"); // Get the <yt:statistics> element describing the number of favorites and // number of views XElement xStatistics = xItem.Element(yt + "statistics"); // Get the <yt:rating> element describing the number of lines and // number of dislikes (may not always be present) XElement xYouTubeRating = xItem.Element(yt + "rating"); // Get the <gd:rating> element describing the video rating and // number of raters XElement xGoogleRating = xItem.Element(gd + "rating"); // Initialize and return the object YouTubeVideo video = new YouTubeVideo(); video.Id = GetVideoId(xItem); video.Published = xItem.GetElementValue <DateTime>("pubDate"); video.LastUpdated = xItem.GetElementValue <DateTime>(atom + "updated"); video.Title = xItem.GetElementValue("title"); video.Description = xItem.GetElementValue("description"); video.Link = xItem.GetElementValue("link"); video.Author = xItem.GetElementValue("author"); video.Duration = TimeSpan.FromSeconds(xDuration.GetAttributeValue <int>("seconds")); video.FavoriteCount = xStatistics == null ? 0 : xStatistics.GetAttributeValue <int>("favoriteCount"); video.ViewCount = xStatistics == null ? 0 : xStatistics.GetAttributeValue <int>("viewCount"); video.Likes = xYouTubeRating == null ? 0 : xYouTubeRating.GetAttributeValue <int>("numLikes"); video.Dislikes = xYouTubeRating == null ? 0 : xYouTubeRating.GetAttributeValue <int>("numDislikes"); video.Rating = xGoogleRating == null ? 0 : xGoogleRating.GetAttributeValue <int>("average"); video.NumberOfRaters = xGoogleRating == null ? 0 : xGoogleRating.GetAttributeValue <int>("numRaters"); return(video); }
private static YouTubeVideo ParseItem(XElement xItem) { // Get namespaces XNamespace atom = xItem.GetNamespaceOfPrefix("atom"); XNamespace media = xItem.GetNamespaceOfPrefix("media"); XNamespace yt = xItem.GetNamespaceOfPrefix("yt"); XNamespace gd = xItem.GetNamespaceOfPrefix("gd"); // Some pre-parsing XElement xMedia = xItem.Element(media + "group"); XElement xDuration = xMedia.Element(yt + "duration"); // Get the <yt:statistics> element describing the number of favorites and // number of views XElement xStatistics = xItem.Element(yt + "statistics"); // Get the <yt:rating> element describing the number of lines and // number of dislikes (may not always be present) XElement xYouTubeRating = xItem.Element(yt + "rating"); // Get the <gd:rating> element describing the video rating and // number of raters XElement xGoogleRating = xItem.Element(gd + "rating"); // Initialize and return the object YouTubeVideo video = new YouTubeVideo(); video.Id = GetVideoId(xItem); video.Published = xItem.GetElementValue<DateTime>("pubDate"); video.LastUpdated = xItem.GetElementValue<DateTime>(atom + "updated"); video.Title = xItem.GetElementValue("title"); video.Description = xItem.GetElementValue("description"); video.Link = xItem.GetElementValue("link"); video.Author = xItem.GetElementValue("author"); video.Duration = TimeSpan.FromSeconds(xDuration.GetAttributeValue<int>("seconds")); video.FavoriteCount = xStatistics == null ? 0 : xStatistics.GetAttributeValue<int>("favoriteCount"); video.ViewCount = xStatistics == null ? 0 : xStatistics.GetAttributeValue<int>("viewCount"); video.Likes = xYouTubeRating == null ? 0 : xYouTubeRating.GetAttributeValue<int>("numLikes"); video.Dislikes = xYouTubeRating == null ? 0 : xYouTubeRating.GetAttributeValue<int>("numDislikes"); video.Rating = xGoogleRating == null ? 0 : xGoogleRating.GetAttributeValue<int>("average"); video.NumberOfRaters = xGoogleRating == null ? 0 : xGoogleRating.GetAttributeValue<int>("numRaters"); return video; }
private static YouTubeVideo ParseEntry(XElement xEntry) { // Get namespaces XNamespace atom = xEntry.GetDefaultNamespace(); XNamespace media = xEntry.GetNamespaceOfPrefix("media"); XNamespace yt = xEntry.GetNamespaceOfPrefix("yt"); XNamespace gd = xEntry.GetNamespaceOfPrefix("gd"); // Some pre-parsing XElement xAuthor = xEntry.Element(atom + "author"); XElement xMedia = xEntry.Element(media + "group"); XElement xDuration = xMedia.Element(yt + "duration"); XElement xLink = xEntry.Elements(atom + "link").FirstOrDefault(x => x.GetAttributeValue("rel") == "alternate"); // Get the <yt:statistics> element describing the number of favorites and // number of views XElement xStatistics = xEntry.Element(yt + "statistics"); // Get the <yt:rating> element describing the number of lines and // number of dislikes (may not always be present) XElement xYouTubeRating = xEntry.Element(yt + "rating"); // Get the <gd:rating> element describing the video rating and // number of raters XElement xGoogleRating = xEntry.Element(gd + "rating"); // Initialize and return the object YouTubeVideo video = new YouTubeVideo(); video.Id = xLink == null ? null : Regex.Match(xLink.GetAttributeValue("href"), "v=([\\w-]{11})").Groups[1].Value; video.Published = xEntry.GetElementValue<DateTime>(atom + "published"); video.LastUpdated = xEntry.GetElementValue<DateTime>(atom + "updated"); video.Title = xEntry.GetElementValue(atom + "title"); video.Description = xMedia.GetElementValue(media + "description"); video.Link = xLink == null ? null : xLink.GetAttributeValue("href"); video.Author = xAuthor.GetElementValue(atom + "name"); video.Duration = TimeSpan.FromSeconds(xDuration.GetAttributeValue<int>("seconds")); video.FavoriteCount = xStatistics == null ? 0 : xStatistics.GetAttributeValue<int>("favoriteCount"); video.ViewCount = xStatistics == null ? 0 : xStatistics.GetAttributeValue<int>("viewCount"); video.Likes = xYouTubeRating == null ? 0 : xYouTubeRating.GetAttributeValue<int>("numLikes"); video.Dislikes = xYouTubeRating == null ? 0 : xYouTubeRating.GetAttributeValue<int>("numDislikes"); video.Rating = xGoogleRating == null ? 0 : xGoogleRating.GetAttributeValue<double>("average", CultureInfo.InvariantCulture); video.NumberOfRaters = xGoogleRating == null ? 0 : xGoogleRating.GetAttributeValue<int>("numRaters"); return video; }
/// <summary> /// Attempts to find a YouTube video ID the specified string and get /// information about that video. /// </summary> /// <param name="subject">The string to search.</param> public static YouTubeVideo GetVideoFromString(string subject) { return(YouTubeVideo.GetVideoFromId(GetIdFromString(subject))); }
/// <summary> /// Gets information about a video with the specified. /// </summary> /// <param name="videoId">The ID of the video.</param> public static YouTubeVideo GetVideoFromId(string videoId) { return(YouTubeVideo.GetVideoFromId(videoId)); }