public static YoutubeVideo FromSingleVideo(string uri) { var uriConverter = new UriConverter(new WebClientImpl()); var videoId = uriConverter.GetVideoId(uri); var gdataUri = uriConverter.FromYoutubeVideoToGDataVideo(videoId); var webClient = new WebClientImpl(); var responseXml = webClient.DownloadString(gdataUri); var responseDocument = XDocument.Parse(responseXml, LoadOptions.SetBaseUri); XNamespace atom = "http://www.w3.org/2005/Atom"; var titleElement = responseDocument.Root.Element(atom + "title"); var videoTitle = titleElement != null ? titleElement.Value : string.Empty; var flvUri = uriConverter.GetFlvUriFromYoutubeVideoUri(videoId); return string.IsNullOrEmpty(flvUri) ? null : new YoutubeVideo { FlvUri = flvUri, Link = uri, Title = videoTitle }; }
public IList<YoutubeVideo> GetVideos() { if (_videoFeed == null) return new List<YoutubeVideo>(); var videos = new List<YoutubeVideo>(); IList<String> szErrorList = new List<String>(); foreach (Video videoFeedEntry in _videoFeed.Entries) { try { var video = new YoutubeVideo { Title = (videoFeedEntry.Title != null ? videoFeedEntry.Title : "[no title]"), Description = (videoFeedEntry.Description != null ? videoFeedEntry.Description : "[no description]"), Link = (videoFeedEntry.WatchPage != null ? videoFeedEntry.WatchPage.AbsoluteUri : "http://www.youtube.com/watch?v=" + videoFeedEntry.VideoId) }; var uriConverter = new UriConverter(new WebClientImpl()); var videoId = uriConverter.GetVideoId(video.Link); video.FlvUri = uriConverter.GetFlvUriFromYoutubeVideoUri(videoId); if (!string.IsNullOrEmpty(video.FlvUri)) { videos.Add(video); } } catch (NotSignedInException) { szErrorList.Add(videoFeedEntry.Title); } } if (szErrorList.Count > 0) ShowErrors(szErrorList); return videos; }
public void GetFlvUriFromYoutubeVideoUri_WithSingleVideoUri() { Mock<WebClient> webClient = new Mock<WebClient>(); webClient.Setup(w => w.DownloadString(It.IsAny<string>())) .Returns("\"t\": \"bar\""); var uriConverter = new UriConverter(webClient.Object); const string videoId = "foo"; string flvUri = uriConverter.GetFlvUriFromYoutubeVideoUri(videoId); string expectedUri = "http://youtube.com/get_video?video_id=foo&t=bar"; Assert.AreEqual(expectedUri, flvUri); }
public void GetFlvUriFromYoutubeVideoUri_WithAuthenticationRequiredVideo() { Mock<WebClient> webClient = new Mock<WebClient>(); webClient.Setup(w => w.DownloadString(It.IsAny<string>())) .Returns("Sign in to YouTube"); var uriConverter = new UriConverter(webClient.Object); string videoId = "nXBOfXj6cvE"; uriConverter.GetFlvUriFromYoutubeVideoUri(videoId); }
public void GetFlvUriFromYoutubeVideoUri_WithPlaylistUrl() { Mock<WebClient> webClient = new Mock<WebClient>(); webClient.Setup(w => w.DownloadString(It.IsAny<string>())) .Returns(string.Empty); var uriConverter = new UriConverter(webClient.Object); const string playlistUri = "http://www.youtube.com/view_play_list?p=B30D2B52964020D6&playnext=1"; Assert.AreEqual(string.Empty, uriConverter.GetFlvUriFromYoutubeVideoUri(playlistUri)); }