/// <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 }); }
public static Uri BuildUrl(string entity, RedditEntity entityType, string cursor = null) { if (entity == null) { return(null); } string url = "https://www.reddit.com"; if (RedditEntity.Subreddit.Equals(entityType)) { url += "/r/" + entity + ".json"; } else { url += "/user/" + entity + "/submitted.json"; } if (cursor != null) { url += "?after=" + cursor; } return(new Uri(url)); }