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 void GetVideoId_WithSingleVideoUri()
    {
      var uriConverter = new UriConverter(_webClient.Object);

      var youtubeVideoUri = "http://www.youtube.com/watch?v=test&feature=PlayList&p=575EC920C5A0FA78&index=28";
      var videoId = uriConverter.GetVideoId(youtubeVideoUri);

      Assert.AreEqual("test", videoId);
    }
    public void FromYoutubePlaylistToGDataPlaylist_WithPlaylistUri()
    {
      var uriConverter = new UriConverter(_webClient.Object);

      var playlistId = "foobar";
      var expectedUri = "http://gdata.youtube.com/feeds/api/playlists/foobar";

      Assert.AreEqual(expectedUri, uriConverter.FromYoutubePlaylistToGDataPlaylist(playlistId));
    }
    public void GetPlaylistId_WithPlaylistUri()
    {
      var uriConverter = new UriConverter(_webClient.Object);

      var youtubePlaylistUri = "http://www.youtube.com/view_play_list?p=test&playnext=1";
      var playlistId = uriConverter.GetPlaylistId(youtubePlaylistUri);

      Assert.AreEqual("test", playlistId);
    }
    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 FromYoutubeVideoToGDataVideo_WithSingleVideoUri()
    {
      var uriConverter = new UriConverter(_webClient.Object);

      var videoId = "test";
      var expectedUri = "http://gdata.youtube.com/feeds/api/videos/test";

      Assert.AreEqual(expectedUri, uriConverter.FromYoutubeVideoToGDataVideo(videoId));
    }
    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 IsYoutubeUri_WithSingleVideoUri()
    {
      var uriConverter = new UriConverter(_webClient.Object);

      const string videoUri = "http://www.youtube.com/watch?v=foo&feature=popular";

      Assert.IsTrue(uriConverter.IsYoutubeUri(videoUri));
    }
    public void IsYoutubeUri_WithPlaylistUri()
    {
      var uriConverter = new UriConverter(_webClient.Object);

      const string playlistUri = "http://www.youtube.com/view_play_list?p=B30D2B52964020D6&playnext=1";

      Assert.IsTrue(uriConverter.IsYoutubeUri(playlistUri));
    }
 private void ShowActionsGridIfNecessary()
 {
   var uriConverter = new UriConverter(new WebClientImpl());
   if (ActionsVisible == Visibility.Hidden && uriConverter.IsYoutubeUri(TextBoxText))
   {
     showActionGrid();
   }
   else if (ActionsVisible == Visibility.Visible && !uriConverter.IsYoutubeUri(TextBoxText))
   {
     hideActionGrid();
   }
 }
    public void IsYoutubeUri_WithNonYoutubeUri()
    {
      var uriConverter = new UriConverter(_webClient.Object);

      var nonYoutubeUri = "http://www.google.com";

      Assert.IsFalse(uriConverter.IsYoutubeUri(nonYoutubeUri));
    }
    public void IsPlaylistUri_WithSingleVideoUri()
    {
      var uriConverter = new UriConverter(_webClient.Object);

      string videoUri = "http://www.youtube.com/watch?v=nXBOfXj6cvE";

      Assert.IsFalse(uriConverter.IsPlaylistUri(videoUri));
    }
    private void downloadAsVideo(object args)
    {
      var uri = ((object[])args)[0] as string;
      var outputDirectory = ((object[])args)[1] as string;

      var uriConverter = new UriConverter(new WebClientImpl());
      if (uriConverter.IsPlaylistUri(uri))
      {
        downloadPlaylistAsVideo(uri, outputDirectory);
      }
      else
      {
        downloadSingleVideoAsVideo(uri, outputDirectory);
      }
    }
    public void IsYoutubeTheHostname_WithInvalidUri()
    {
        var uriConverter = new UriConverter(_webClient.Object);

        string uri = "foobar";

        Assert.IsFalse(uriConverter.IsYoutubeTheHostname(uri));
    }
    public void IsValidYoutubeUri_WithNonYoutubeWebsite()
    {
      var uriConverter = new UriConverter(_webClient.Object);

      string uri = "http://www.google.com";

      Assert.IsFalse(uriConverter.IsYoutubeUri(uri));
    }
    public void IsYoutubeTheHostname_WithYoutubeHostname()
    {
      var uriConverter = new UriConverter(_webClient.Object);

      string uri = "http://www.youtube.com";

      Assert.IsTrue(uriConverter.IsYoutubeTheHostname(uri));
    }
    public void IsPlaylistUri_WithValidPlaylistUri()
    {
      var uriConverter = new UriConverter(_webClient.Object);

      var playlistUri = "http://www.youtube.com/view_play_list?p=B30D2B52964020D6";

      Assert.IsTrue(uriConverter.IsPlaylistUri(playlistUri));
    }
    public void IsSingleVideoUri_WithValidVideoUri()
    {
      var uriConverter = new UriConverter(_webClient.Object);

      var videoUri = "http://www.youtube.com/watch?v=6mgZZ3og1g0";

      Assert.IsTrue(uriConverter.IsSingleVideoUri(videoUri));
    }
 private bool isClipboardTextOkToPopulate(string clipboardText)
 {
   var uriConverter = new UriConverter(new WebClientImpl());
   return uriConverter.IsYoutubeUri(clipboardText) &&
          _clipboardText != clipboardText;
 }
    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));
    }
    private IList<YoutubeVideo> GetListOfVideosFromPlaylist(string uri)
    {
      SetState("Getting list of videos");

      var uriConverter = new UriConverter(new WebClientImpl());
      var playlistId = uriConverter.GetPlaylistId(uri);
      var gdataUri = uriConverter.FromYoutubePlaylistToGDataPlaylist(playlistId);
      var resultAggregator = new ResultAggregator(gdataUri);
      var videos = resultAggregator.GetVideos();

      SetState("Videos found: " + videos.Count);
      return videos;
    }
    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);
    }
    private void videoClicked(object sender, RoutedEventArgs e)
    {
      var uriConverter = new UriConverter(new WebClientImpl());
      if (!uriConverter.IsYoutubeUri(TextBoxText)) return;

      var outputDirectory = GetDirectoryPath();
      if (string.IsNullOrEmpty(outputDirectory)) return;

      string playlistUri = TextBoxText;
      textBox.Text = string.Empty;
      textBox.Visibility = pasteLabel.Visibility = Visibility.Hidden;
      currentState.Visibility = Visibility.Visible;

      SetProgress(0, 100); //default progress to nothing
      slideInProgressBar();

      _worker = new BackgroundWorker
      {
        WorkerSupportsCancellation = true
      };
      _worker.DoWork += delegate { this.downloadAsVideo(new object[] { playlistUri, outputDirectory }); };
      _worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(downloadCompleted);
      _worker.RunWorkerAsync();
    }
    public void IsYoutubeUri_WithEmptyString()
    {
      var uriConverter = new UriConverter(_webClient.Object);

      Assert.IsFalse(uriConverter.IsYoutubeUri(string.Empty));
    }
    public void IsSingleVideoUri_WithPlaylistUri()
    {
      var uriConverter = new UriConverter(_webClient.Object);

      string playlistUri = "http://www.youtube.com/view_play_list?p=7BD938126500CF21";

      Assert.IsFalse(uriConverter.IsSingleVideoUri(playlistUri));
    }
    public void IsYoutubeUri_WithNullString()
    {
      var uriConverter = new UriConverter(_webClient.Object);

      Assert.IsFalse(uriConverter.IsYoutubeUri(null));
    }