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

Added option to download X oldest items, not only X recent ones (building upon Santa77's #620) #1022

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
17 changes: 17 additions & 0 deletions src/icloudpd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ def report_version(ctx: click.Context, _param: click.Parameter, value: bool) ->
help="Number of recent photos to download (default: download all photos)",
type=click.IntRange(0),
)
@click.option(
"--oldest",
help="Number of oldest photos to download (default: download all photos)",
type=click.IntRange(0),
)
@click.option(
"--until-found",
help="Download most recently added photos until we find x number of "
Expand Down Expand Up @@ -582,6 +587,7 @@ def main(
size: Sequence[AssetVersionSize],
live_photo_size: LivePhotoVersionSize,
recent: Optional[int],
oldest: Optional[int],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What params cannot be used with --oldest? I think at least --recent. Need to check for invalid combination of params.

until_found: Optional[int],
album: str,
list_albums: bool,
Expand Down Expand Up @@ -694,6 +700,7 @@ def main(
primary_sizes=size,
live_photo_size=live_photo_size,
recent=recent,
oldest=oldest,
until_found=until_found,
album=album,
list_albums=list_albums,
Expand Down Expand Up @@ -774,6 +781,7 @@ def main(
cookie_directory,
size,
recent,
oldest,
until_found,
album,
list_albums,
Expand Down Expand Up @@ -935,6 +943,7 @@ def download_photo_(counter: Counter, photo: PhotoAsset) -> bool:
if file_exists:
counter.increment()
logger.debug("%s already exists", truncate_middle(download_path, 96))
success = True
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That logic change seems to be related to cases outside of oldest as well. Will is break anything?


if not file_exists:
counter.reset()
Expand Down Expand Up @@ -1152,6 +1161,7 @@ def core(
cookie_directory: str,
primary_sizes: Sequence[AssetVersionSize],
recent: Optional[int],
oldest: Optional[int],
until_found: Optional[int],
album: str,
list_albums: bool,
Expand Down Expand Up @@ -1289,6 +1299,13 @@ def core(
photos_count = recent
photos_enumerator = itertools.islice(photos_enumerator, recent)

# Optional: Only download the x oldest photos.
if oldest is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests for new functionality. That will ensure this feature is not broken with other code changes.

photos_count = oldest
total_photos = len(photos_enumerator)
start = total_photos - oldest
photos_enumerator = itertools.islice(photos_enumerator, start, total_photos)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am curious how that works on large collections in iCloud.com. I think we are processing images as a stream and any filtering is done on a fly instead of memory.


if until_found is not None:
photos_count = None
# ensure photos iterator doesn't have a known length
Expand Down
2 changes: 2 additions & 0 deletions src/icloudpd/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def __init__(
primary_sizes: Sequence[AssetVersionSize],
live_photo_size: LivePhotoVersionSize,
recent: Optional[int],
oldest: Optional[int],
until_found: Optional[int],
album: str,
list_albums: bool,
Expand Down Expand Up @@ -59,6 +60,7 @@ def __init__(
self.size = " ".join(str(e) for e in primary_sizes)
self.live_photo_size = live_photo_size
self.recent = recent
self.oldest = oldest
self.until_found = until_found
self.album = album
self.list_albums = list_albums
Expand Down