Esempio n. 1
0
        public void TestImgurParseAlbum()
        {
            List <string> imageUrls = ImgurUtils.ParseAlbum(new Uri("http://imgur.com/gallery/qLp4E"));

            Assert.AreEqual(10, imageUrls.Count);
            Assert.IsTrue(imageUrls.Contains("http://i.imgur.com/zUDXxa9.jpg"));
            Assert.IsTrue(imageUrls.Contains("http://i.imgur.com/S4em4Pr.jpg"));
            Assert.IsTrue(imageUrls.Contains("http://i.imgur.com/3YxxHZG.jpg"));
            Assert.IsTrue(imageUrls.Contains("http://i.imgur.com/tAw2DK1.jpg"));
            Assert.IsTrue(imageUrls.Contains("http://i.imgur.com/CiBlYyN.jpg"));
            Assert.IsTrue(imageUrls.Contains("http://i.imgur.com/i5FWfzA.jpg"));
            Assert.IsTrue(imageUrls.Contains("http://i.imgur.com/zP7P6pg.jpg"));
            Assert.IsTrue(imageUrls.Contains("http://i.imgur.com/TrYBtzx.jpg"));
            Assert.IsTrue(imageUrls.Contains("http://i.imgur.com/hCjiuQd.jpg"));
            Assert.IsTrue(imageUrls.Contains("http://i.imgur.com/rvw7Esb.jpg"));
        }
        /// <summary>
        /// Event handler for the "Find Images" button.  Fetches image URL's based on the field values in
        /// the first tab, and uses that information to set field values in the second tab.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FindImagesButton_Click(object sender, RoutedEventArgs e)
        {
            MainTabControl.SelectedItem = DownloadImagesTabItem;

            // TODO:  Refactor, and make this asynchronous so it doesn't block the main UI thread

            // Get user input from XAML fields
            string       entity = EntityTextBox.Text;
            RedditEntity type   = (UserEntityType.IsChecked == true) ? RedditEntity.User : RedditEntity.Subreddit;
            int          maxPages;

            if (!int.TryParse(MaxPagesTextBox.Text, out maxPages))
            {
                maxPages = 1;
            }

            // Parse Imgur image and album URL's for the given Reddit user or subuser, stopping when we
            // either run out of pages or else hit the maximum page count
            HashSet <string> imageUrls      = new HashSet <string>();
            HashSet <string> albumUrls      = new HashSet <string>();
            string           nextPageCursor = "";
            int pageCount = 1;

            for ( ; pageCount <= maxPages && nextPageCursor != null; pageCount++)
            {
                Console.WriteLine("Processing page {0} for {1} {2}", pageCount, type.ToString().ToLower(), entity);
                Uri redditUrl = RedditUtils.BuildUrl(entity, type, "".Equals(nextPageCursor) ? null : nextPageCursor);
                RedditUtils.ParsePage(redditUrl, ref imageUrls, ref albumUrls, out nextPageCursor);
                if (nextPageCursor != null)
                {
                    Console.WriteLine("nextPageCursor == {0}", nextPageCursor);
                }
            }
            Console.WriteLine("Parsed {0} image URL's and {1} album URL's from {2} pages", imageUrls.Count, albumUrls.Count, pageCount);

            // Parse out image URL's from the albums
            var imagesFromAlbums = albumUrls.SelectMany(albumUrl => ImgurUtils.ParseAlbum(new Uri(albumUrl)));

            imageUrls.UnionWith(imagesFromAlbums);
            Console.WriteLine("There are {0} total images after extracting {1} from albums", imageUrls.Count, imagesFromAlbums.ToList().Count);

            PageCountLabel.Content     = pageCount;
            ImagesCountLabel.Content   = imageUrls.Count;
            ImagesDataGrid.ItemsSource = imageUrls.Select(imageUrl => new ImgurUrl {
                URL = imageUrl
            });
        }