private void btnGetAlbums_Click(object sender, EventArgs e) { BGErrorMessage = ""; dgvPhotosets.Rows.Clear(); SearchAccountUser = (User)cbSearchAccount.SelectedItem; if (SearchAccountUser == null) { MessageBox.Show("No search account selected"); return; } PhotosetList = new SortableBindingList <Photoset>(); FormProgress dlg = new FormProgress("Find albums", BGFindPhotosets); // Show dialog with Synchronous/blocking call. // BGFindPhotosets() is called by dialog. DialogResult result = dlg.ShowDialog(); if (result == DialogResult.OK) { if (!String.IsNullOrWhiteSpace(BGErrorMessage)) { MessageBox.Show(BGErrorMessage); } else { bindingSourcePhotosets.DataSource = PhotosetList; if (dgvPhotosets.Rows.Count > 0 && dgvPhotosets.Rows[0].Cells.Count > 0) { dgvPhotosets.CurrentCell = dgvPhotosets.Rows[0].Cells[0]; dgvPhotosets.Rows[0].Selected = true; } } } else { // Search was canceled. Do nothing. } }
// Background worker task to search selected photosets. private void BGDownloadPhotosets(object sender, DoWorkEventArgs e) { BackgroundWorker worker = (BackgroundWorker)sender; worker.ReportProgress(0, "Connecting"); PhotoList = new SortableBindingList <Photo>(); // Count the number of enabled photosets, so we can do an estimate of percent complete; int enabledPhotosets = 0; foreach (Photoset photoset in PhotosetList) { if (photoset.EnableSearch) { enabledPhotosets++; } } if (enabledPhotosets == 0) { // No photosets are enabled. We are done. return; } try { int indexPhotoset = 0; FlickrNet.Flickr f = FlickrManager.GetFlickrAuthInstance(); if (f == null) { BGErrorMessage = "You must authenticate before you can download data from Flickr."; return; } // Iterate over the photosets and get the photos from each set. FlickrNet.PhotosetPhotoCollection photoCollection = new FlickrNet.PhotosetPhotoCollection(); foreach (Photoset photoset in PhotosetList) { if (worker.CancellationPending) { e.Cancel = true; return; } if (photoset.EnableSearch) { int percent = indexPhotoset * 100 / enabledPhotosets; string description = "Searching Album " + photoset.Title; worker.ReportProgress(percent, description); int page = 1; int perpage = 500; do { if (worker.CancellationPending) // See if cancel button was pressed. { e.Cancel = true; return; } photoCollection = f.PhotosetsGetPhotos(photoset.PhotosetId, SearchExtras, page, perpage); #if false // It is not clear from the documentation whether the limit of 4000 photos per search applies // to album searches. If an album has more than 4000 photos, is the result of GetPhotos // accurate? I'm going to assume for now that it is. If not, you can enable this code. if (photoCollection.Total > 3999) { BGErrorMessage = $"Too many photos: {photoCollection.Total}"; return; } #endif foreach (FlickrNet.Photo flickrPhoto in photoCollection) { AddPhotoToList(f, flickrPhoto, photoset); } // Calculate percent complete based on both how many photo sets we have completed, // plus how many pages we have read percent = (indexPhotoset * 100 + page * 100 / photoCollection.Pages) / enabledPhotosets; worker.ReportProgress(percent, description); page = photoCollection.Page + 1; }while (page <= photoCollection.Pages); indexPhotoset++; } } } catch (FlickrNet.FlickrException ex) { BGErrorMessage = "Search failed. Error: " + ex.Message; return; } DownloadFiles(sender, e); }
private void BGDownloadAllPhotos(object sender, DoWorkEventArgs e) { BackgroundWorker worker = (BackgroundWorker)sender; PhotoList = new SortableBindingList <Photo>(); worker.ReportProgress(0, "Searching all photos"); FlickrNet.Flickr f = FlickrManager.GetFlickrAuthInstance(); if (f == null) { BGErrorMessage = "You must authenticate before you can download data from Flickr."; return; } try { FlickrNet.PhotoSearchOptions options = new FlickrNet.PhotoSearchOptions(); options.Extras = SearchExtras; options.SortOrder = FlickrNet.PhotoSearchSortOrder.DateTakenAscending; if (Settings.FilterByDate) { options.MinTakenDate = Settings.StartDate.Date; options.MaxTakenDate = Settings.StopDate.Date + new TimeSpan(23, 59, 59); } options.UserId = SearchAccountUser.UserId; options.Page = 1; options.PerPage = 500; FlickrNet.PhotoCollection photoCollection; do { if (worker.CancellationPending) // See if cancel button was pressed. { return; } photoCollection = f.PhotosSearch(options); if (photoCollection.Total > 3999) { BGErrorMessage = $"Too many photos: {photoCollection.Total}"; return; } foreach (FlickrNet.Photo flickrPhoto in photoCollection) { AddPhotoToList(f, flickrPhoto, null); } // Calculate percent complete based on how many pages we have completed. int percent = (options.Page * 100 / photoCollection.Pages); worker.ReportProgress(percent, "Searching all photos"); options.Page = photoCollection.Page + 1; }while (options.Page <= photoCollection.Pages); } catch (FlickrNet.FlickrException ex) { BGErrorMessage = "Download failed. Error: " + ex.Message; return; } DownloadFiles(worker, e); }