public void Upload(Func <bool> cancellationPending, Flickr flickrInstance, IEnumerable <CollectionFlickr> toUpload, IEnumerable <CollectionFlickr> flickrSchema) { Dictionary <string, CollectionFlickr> dictionaryFlickrSchema = flickrSchema.ToDictionary(k => k.Title, v => v); foreach (CollectionFlickr collectionToUpload in toUpload) { if (string.IsNullOrEmpty(collectionToUpload.Id)) { if (collectionToUpload.AlbumsFlickr.Count > 0) { _log.DebugFormat("Creating new collection: [{0}]", collectionToUpload.Title); Collection createdCollection = flickrInstance.CollectionsCreate(collectionToUpload.Title, string.Empty); collectionToUpload.Id = createdCollection.CollectionId; dictionaryFlickrSchema.Add( collectionToUpload.Title, new CollectionFlickr { Id = createdCollection.CollectionId, Title = collectionToUpload.Title, AlbumsFlickr = new BindingList <AlbumFlickr>() }); } else { _log.WarnFormat("Collection [{0}] is empty. Please verify what's going on...", collectionToUpload.Title); continue; } } CollectionFlickr collectionOnFlickr = dictionaryFlickrSchema[collectionToUpload.Title]; foreach (AlbumFlickr notSyncedAlbum in collectionToUpload.AlbumsFlickr) { if (string.IsNullOrEmpty(notSyncedAlbum.Id)) { if (notSyncedAlbum.PhotoList.Count > 0) { _log.DebugFormat("Creating new album: [{0}]", notSyncedAlbum.Title); // first we send a picture because without that is it not possible to create an album PhotoFlickr photoFlickr = notSyncedAlbum.PhotoList.First(); photoFlickr.Id = flickrInstance.UploadPicture(photoFlickr.FilePath, photoFlickr.Title, string.Empty, string.Format("\"#Collection={0}\" \"#Album={1}\"", collectionToUpload.Title, notSyncedAlbum.Title), false, false, false); string albumId = flickrInstance.PhotosetsCreate(notSyncedAlbum.Title, photoFlickr.Id).PhotosetId; notSyncedAlbum.Id = albumId; collectionOnFlickr.AlbumsFlickr.Add(new AlbumFlickr { Id = albumId, Title = notSyncedAlbum.Title, PhotoList = new BindingList <PhotoFlickr> { photoFlickr } }); flickrInstance.CollectionsEditSets(collectionOnFlickr.Id, collectionOnFlickr.AlbumsFlickr.Select(s => s.Id).ToList()); // after add we can say that is syncronized so we delete it from synchronization list notSyncedAlbum.PhotoList.Remove(photoFlickr); } else { _log.WarnFormat("Album [{0}] is empty. Please verify what's going on...", notSyncedAlbum.Title); continue; } } AlbumFlickr albumFlickr = collectionOnFlickr.AlbumsFlickr.Single(s => s.Id == notSyncedAlbum.Id); Parallel.ForEach(notSyncedAlbum.PhotoList, new ParallelOptions { MaxDegreeOfParallelism = _uploadThreadsCount }, () => new List <PhotoFlickr>(), (photoFlickr, state, arg3) => { if (state.IsStopped) { return(arg3); } int retryCount = 0; while (retryCount < 3) { try { if (cancellationPending()) { state.Stop(); return(arg3); } photoFlickr.Id = flickrInstance.UploadPicture(photoFlickr.FilePath, photoFlickr.Title, string.Empty, string.Format("\"#Collection={0}\" \"#Album={1}\"", collectionToUpload.Title, notSyncedAlbum.Title), false, false, false); flickrInstance.PhotosetsAddPhoto(notSyncedAlbum.Id, photoFlickr.Id); arg3.Add(photoFlickr); _log.DebugFormat("Uploaded photo [{0}] to album [{1}].", photoFlickr.Title, albumFlickr.Title); break; } catch (Exception ex) { retryCount++; _log.Warn("Uploading failed. Retrying...", ex); } } return(arg3); }, set => { if (set != null) { set.ForEach(albumFlickr.PhotoList.Add); } } ); if (cancellationPending()) { return; } } } }
public List <CollectionFlickr> CompareSchemas(IEnumerable <CollectionFlickr> collectionsFromFlickr, IEnumerable <CollectionFlickr> fromDisk) { Dictionary <string, CollectionFlickr> dictionaryCollectionsFromFlickr = collectionsFromFlickr.ToDictionary(k => k.Title, v => v); Dictionary <string, CollectionFlickr> dictionaryCollectionsFromDisk = fromDisk.ToDictionary(k => k.Title, v => v); List <CollectionFlickr> notSynchronizedList = new List <CollectionFlickr>(); foreach (CollectionFlickr collectionFromDisk in dictionaryCollectionsFromDisk.Values) { if (dictionaryCollectionsFromFlickr.ContainsKey(collectionFromDisk.Title) == false) { notSynchronizedList.Add(collectionFromDisk); continue; } Dictionary <string, AlbumFlickr> flickrAlbums = dictionaryCollectionsFromFlickr[collectionFromDisk.Title].AlbumsFlickr.ToDictionary(k => k.Title, v => v); foreach (AlbumFlickr localAlbum in collectionFromDisk.AlbumsFlickr) { // we check if album is already on server if (flickrAlbums.ContainsKey(localAlbum.Title) == false) { // if collection of not synchronized has no then add it if (notSynchronizedList.All(a => a.Title != collectionFromDisk.Title)) { notSynchronizedList.Add(new CollectionFlickr() { Id = dictionaryCollectionsFromFlickr[collectionFromDisk.Title].Id, Title = collectionFromDisk.Title, AlbumsFlickr = new BindingList <AlbumFlickr> { localAlbum } }); } else // otherwise update whole list { notSynchronizedList.Single(s => s.Title == collectionFromDisk.Title).AlbumsFlickr.Add(localAlbum); } } else // album exists on server so we are checking if all file are already there { Dictionary <string, bool> flickrAlbumPhotos = flickrAlbums[localAlbum.Title].PhotoList.DistinctBy(d => d.Title).ToDictionary(k => k.Title, v => true); foreach (PhotoFlickr localAlbumPhoto in localAlbum.PhotoList) { if (flickrAlbumPhotos.ContainsKey(localAlbumPhoto.Title) == false) { // if collection of not synchronized hasn't it then add new one if (notSynchronizedList.All(a => a.Title != collectionFromDisk.Title)) { notSynchronizedList.Add(new CollectionFlickr() { Id = dictionaryCollectionsFromFlickr[collectionFromDisk.Title].Id, Title = collectionFromDisk.Title, AlbumsFlickr = new BindingList <AlbumFlickr> { new AlbumFlickr() { Id = flickrAlbums[localAlbum.Title].Id, PhotoList = new BindingList <PhotoFlickr> { localAlbumPhoto }, Title = localAlbum.Title } }, }); } else // otherwise update list with new photo { CollectionFlickr coll = notSynchronizedList.Single(s => s.Title == collectionFromDisk.Title); AlbumFlickr album = coll.AlbumsFlickr.SingleOrDefault(a => a.Title == localAlbum.Title); if (album != null) { album.PhotoList.Add(localAlbumPhoto); } else { coll.AlbumsFlickr.Add(new AlbumFlickr { Id = flickrAlbums[localAlbum.Title].Id, Title = localAlbum.Title, PhotoList = new BindingList <PhotoFlickr> { localAlbumPhoto } }); } } } } } } } return(notSynchronizedList); }
public IEnumerable <CollectionFlickr> GetSchema() { string cFlickrschemaTxt = FlickrSchemaCacheFilePath(); if (File.Exists(cFlickrschemaTxt) && _reloadFromFlickr == false) { string readAllText = File.ReadAllText(cFlickrschemaTxt); List <CollectionFlickr> deserializeObject = JsonConvert.DeserializeObject <List <CollectionFlickr> >(readAllText); return(deserializeObject); } CollectionCollection collectionsGetTree = _flickr.CollectionsGetTree(); List <CollectionFlickr> collections = new List <CollectionFlickr>(collectionsGetTree.Count); foreach (Collection collectionFlickr in collectionsGetTree) { CollectionFlickr flickr = new CollectionFlickr { Id = collectionFlickr.CollectionId, Title = collectionFlickr.Title, AlbumsFlickr = new BindingList <AlbumFlickr>() }; foreach (CollectionSet collectionSet in collectionFlickr.Sets) { AlbumFlickr albumFlickr = new AlbumFlickr { Id = collectionSet.SetId, Title = collectionSet.Title }; flickr.AlbumsFlickr.Add(albumFlickr); _log.DebugFormat("Getting photos from album: [{0}]", albumFlickr.Title); int page = 1; PhotosetPhotoCollection photosetPhotoCollection = new PhotosetPhotoCollection(); int numberOfPhotos = _flickr.PhotosetsGetInfo(albumFlickr.Id).NumberOfPhotos; while (true) { int count = 0; if (numberOfPhotos >= 100) { count = 100; numberOfPhotos -= 100; } else { count = numberOfPhotos; numberOfPhotos = 0; } PhotosetPhotoCollection getPhotosetPhotoCollection = _flickr.PhotosetsGetPhotos(albumFlickr.Id, page, count); foreach (Photo getPhoto in getPhotosetPhotoCollection) { photosetPhotoCollection.Add(getPhoto); } if (numberOfPhotos == 0) { break; } page++; } albumFlickr.PhotoList = new BindingList <PhotoFlickr>(); foreach (Photo photoFromSet in photosetPhotoCollection) { albumFlickr.PhotoList.Add(new PhotoFlickr { Id = photoFromSet.PhotoId, Title = photoFromSet.Title, Tags = photoFromSet.Tags.ToList() }); } } collections.Add(flickr); } string serializeObject = JsonConvert.SerializeObject(collections); File.WriteAllText(cFlickrschemaTxt, serializeObject); return(collections); }