Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download all albums, add album name to file name #130

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ def get_photos_sync_interval(config):
return sync_interval


def get_photos_all_albums(config):
"""Return flag to download all albums from config."""
download_all = False
config_path = ["photos", "all_albums"]
if traverse_config_path(config=config, config_path=config_path):
download_all = get_config_value(config=config, config_path=config_path)
LOGGER.info("Syncing all albums.")
return download_all


def prepare_root_destination(config):
"""Prepare root destination."""
LOGGER.debug("Checking root destination ...")
Expand Down
32 changes: 28 additions & 4 deletions src/sync_photos.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,25 @@ def generate_file_name(photo, file_size, destination_path):
destination_path,
f'{"__".join([name, file_size, base64.urlsafe_b64encode(photo.id.encode()).decode()])}.{extension}',
)
album_name = destination_path.split("/")[-1]
file_size_id_album_name_path = os.path.join(
destination_path,
f'{"__".join([album_name, name, file_size, base64.urlsafe_b64encode(photo.id.encode()).decode()])}.{extension}',
)
file_size_id_album_name_short_path = os.path.join(
destination_path,
f'{"__".join([album_name, name, file_size, base64.urlsafe_b64encode(photo.id.encode()).decode()[2:10]])}.{extension}',
)

if os.path.isfile(file_path):
os.rename(file_path, file_size_id_path)
os.rename(file_path, file_size_id_album_name_short_path)
if os.path.isfile(file_size_path):
os.rename(file_size_path, file_size_id_path)
return file_size_id_path
os.rename(file_size_path, file_size_id_album_name_short_path)
if os.path.isfile(file_size_id_path):
os.rename(file_size_id_path, file_size_id_album_name_short_path)
if os.path.isfile(file_size_id_album_name_path):
os.rename(file_size_id_album_name_path, file_size_id_album_name_short_path)
return file_size_id_album_name_short_path


def photo_exists(photo, file_size, local_path):
Expand Down Expand Up @@ -133,7 +147,17 @@ def sync_photos(config, photos):
destination_path = config_parser.prepare_photos_destination(config=config)
filters = config_parser.get_photos_filters(config=config)
files = set()
if filters["albums"]:
download_all = config_parser.get_photos_all_albums(config=config)
if download_all:
for album in photos.albums.keys():
sync_album(
album=photos.albums[album],
destination_path=os.path.join(destination_path, album),
file_sizes=filters["file_sizes"],
extensions=filters["extensions"],
files=files,
)
elif filters["albums"]:
for album in iter(filters["albums"]):
sync_album(
album=photos.albums[album],
Expand Down
18 changes: 18 additions & 0 deletions tests/test_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,21 @@ def test_get_region_valid(self):
config["app"]["region"] = "china"
actual = config_parser.get_region(config=config)
self.assertEqual(actual, "china")

def test_get_all_albums_empty(self):
"""Empty all_albums."""
config = read_config(config_path=tests.CONFIG_PATH)
self.assertFalse(config_parser.get_photos_all_albums(config=config))

def test_get_all_albums_true(self):
"""True all_albums."""
config = read_config(config_path=tests.CONFIG_PATH)
config["photos"]["all_albums"] = True
self.assertTrue(config_parser.get_photos_all_albums(config=config))

def test_get_all_albums_false(self):
"""False all_albums."""
config = read_config(config_path=tests.CONFIG_PATH)
config["photos"]["all_albums"] = False
self.assertFalse(config_parser.get_photos_all_albums(config=config))

44 changes: 35 additions & 9 deletions tests/test_sync_photos.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_sync_photos_missing_photo_download(
os.remove(
os.path.join(
album_1_path,
"IMG_3148__medium__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG",
"album-1__IMG_3148__medium__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG",
)
)
# Download missing file
Expand All @@ -104,7 +104,7 @@ def test_sync_photos_missing_photo_download(
(
s
for s in captured[1]
if "album-1/IMG_3148__medium__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG ..."
if "album-1/album-1__IMG_3148__medium__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG ..."
in s
),
None,
Expand Down Expand Up @@ -144,7 +144,7 @@ def test_sync_photos_download_changed_photo(
os.remove(
os.path.join(
album_1_path,
"IMG_3148__medium__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG",
"album-1__IMG_3148__medium__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG",
)
)
shutil.copyfile(
Expand All @@ -161,7 +161,7 @@ def test_sync_photos_download_changed_photo(
(
s
for s in captured[1]
if "album-1/IMG_3148__medium__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG ..."
if "album-1/album-1__IMG_3148__medium__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG ..."
in s
),
None,
Expand Down Expand Up @@ -230,7 +230,7 @@ def test_sync_photos_rename_previous_original_photos(
os.rename(
os.path.join(
album_1_path,
"IMG_3148__original__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG",
"album-1__IMG_3148__original__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG",
),
os.path.join(album_1_path, "IMG_3148.JPG"),
)
Expand All @@ -246,11 +246,37 @@ def test_sync_photos_rename_previous_original_photos(

self.assertFalse(os.path.exists(os.path.join(album_1_path, "IMG_3148.JPG")))

# Rename previous original files - upgrade to newer version
os.rename(
os.path.join(
album_1_path,
"album-1__IMG_3148__original__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG",
),
os.path.join(album_1_path, "IMG_3148__original__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG"),
)

with self.assertLogs(logger=LOGGER, level="DEBUG") as captured:
self.assertIsNone(
sync_photos.sync_photos(config=config, photos=mock_service.photos)
)
self.assertTrue(len(captured.records) > 0)
self.assertIsNone(
next((s for s in captured[1] if "Downloading /" in s), None)
)

self.assertFalse(os.path.exists(
os.path.join(
album_1_path,
"IMG_3148__original__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG"
)
)
)

# Rename previous __original files - upgrade to newer version
os.rename(
os.path.join(
album_1_path,
"IMG_3148__original__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG",
"album-1__IMG_3148__original__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG",
),
os.path.join(album_1_path, "IMG_3148__original.JPG"),
)
Expand Down Expand Up @@ -292,7 +318,7 @@ def test_sync_photos_rename_original_photos_obsolete_false(
os.rename(
os.path.join(
album_1_path,
"IMG_3148__original__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG",
"album-1__IMG_3148__original__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG",
),
os.path.join(album_1_path, "delete_me.JPG"),
)
Expand Down Expand Up @@ -329,7 +355,7 @@ def test_sync_photos_rename_original_photos_obsolete_true(
os.rename(
os.path.join(
album_1_path,
"IMG_3148__original__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG",
"album-1__IMG_3148__original__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG",
),
os.path.join(album_1_path, "delete_me.JPG"),
)
Expand Down Expand Up @@ -381,7 +407,7 @@ def test_sync_photos_empty_albums_list(
(
s
for s in captured[1]
if "all/IMG_3148__original__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG ..."
if "all/all__IMG_3148__original__QVZ4My9WS2tiV1BkTmJXdzY4bXJXelN1ZW1YZw==.JPG ..."
in s
),
None,
Expand Down
Loading