Skip to content

Commit

Permalink
Fix: Download filtered album, if present, from all libraries when no …
Browse files Browse the repository at this point in the history
…library is specified (#203)

* Download filtered album, if present, from all libraries
  • Loading branch information
mandarons authored Mar 26, 2024
1 parent 7423802 commit 9788c03
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/sync_photos.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,21 @@ def sync_photos(config, photos):
files=files,
folder_format=folder_format,
)
elif filters["albums"]:
for album in iter(filters["albums"]):
if album in photos.libraries[library].albums:
sync_album(
album=photos.libraries[library].albums[album],
destination_path=os.path.join(destination_path, album),
file_sizes=filters["file_sizes"],
extensions=filters["extensions"],
files=files,
folder_format=folder_format,
)
else:
LOGGER.warning(
f"Album {album} not found in {library}. Skipping the album {album} ..."
)
else:
sync_album(
album=photos.libraries[library].all,
Expand Down
4 changes: 2 additions & 2 deletions tests/data/test_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ photos:
all_albums: false # Optional, default false. If true preserve album structure. If same photo is in multpile albums creates duplicates on filesystem
# folder_format: "%Y/%m" # optional, if set put photos in subfolders according to format. Format cheatsheet - https://strftime.org
filters:
# libraries: # Optional, specify list of libraries to download photos from
# - PrimarySync # Library of the user
libraries: # Optional, specify list of libraries to download photos from
- PrimarySync # Library of the user
# - SharedSync-abcd # Library of another user
# if all_albums is false - albums list is used as filter-in, if all_albums is true - albums list is used as filter-out
# if albums list is empty and all_albums is false download all photos to "all" folder. if empty and all_albums is true download all folders
Expand Down
1 change: 1 addition & 0 deletions tests/test_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ def test_get_photos_folder_format_valid(self):
def test_get_photos_filters_libraries_empty(self):
"""Photos > library is missing in config."""
config = read_config(config_path=tests.CONFIG_PATH)
del config["photos"]["filters"]["libraries"]
self.assertEqual(
config_parser.get_photos_filters(config=config)["libraries"], None
)
Expand Down
35 changes: 35 additions & 0 deletions tests/test_sync_photos.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,8 @@ def test_photo_download_with_shared_libraries(
config = self.config.copy()
config["photos"]["destination"] = self.destination_path
del config["photos"]["filters"]["albums"]
# delete libraries from config
del config["photos"]["filters"]["libraries"]
mock_read_config.return_value = config
# Sync original photos
self.assertIsNone(
Expand All @@ -667,3 +669,36 @@ def test_photo_download_with_shared_libraries(
self.assertTrue(len(glob.glob(os.path.join(all_path, "IMG_3148*.JPG"))) > 0)
# Check for shared photo
self.assertTrue(len(glob.glob(os.path.join(all_path, "IMG_5513*.HEIC"))) > 0)

@patch(target="keyring.get_password", return_value=data.VALID_PASSWORD)
@patch(
target="src.config_parser.get_username", return_value=data.AUTHENTICATED_USER
)
@patch("icloudpy.ICloudPyService")
@patch("src.read_config")
def test_sync_photos_all_albums_filtered_missing_primary_sync(
self, mock_read_config, mock_service, mock_get_username, mock_get_password
):
"""Test for successful original photo size download."""
mock_service = self.service
config = self.config.copy()
config["photos"]["destination"] = self.destination_path
del config["photos"]["filters"]["libraries"]
config["photos"]["filters"]["albums"] += ["Favorites"]
mock_read_config.return_value = config
# Sync original photos
self.assertIsNone(
sync_photos.sync_photos(config=config, photos=mock_service.photos)
)
album_0_path = os.path.join(
self.destination_path, config["photos"]["filters"]["albums"][0]
)
album_1_path = os.path.join(
self.destination_path, config["photos"]["filters"]["albums"][1]
)
album_2_path = os.path.join(
self.destination_path, config["photos"]["filters"]["albums"][2]
)
self.assertTrue(os.path.isdir(album_0_path))
self.assertTrue(os.path.isdir(album_1_path))
self.assertTrue(os.path.isdir(album_2_path))

0 comments on commit 9788c03

Please sign in to comment.