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

Fetch albums in folders for iCloud Photos #297

Closed
wants to merge 14 commits into from

Conversation

noizwaves
Copy link

Proposed change

This PR enhances the PhotosService to traverse sub-folders when it retrieves albums. The initial implementation only shallowly iterated over items in the root folder. The code has been changed to first determine if an item is actually a folder, and if so to query iCloud for any albums inside of it.

The ancestry or "lineage" of an album is also tracked in a new field PhotoAlbum.lineage; the names of each parent directory are included in this list. The definition of PhotoAlbum.__unicode__ has been enhanced to include this information in a path-esque way. The aim is to better represent the actual "path" within an iCloud Library.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New service (thank you!)
  • New feature (which adds functionality to an existing service)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests
  • Documentation or code sample

Example of code:

for name, album in api.photos.albums.items():
    print(name, album.lineage, str(album))

Additionally, I used a local build of this package in this script to download the details of all of my albums. My library consists of albums at the root level, albums in a single folder, and albums nested within multiple levels of folders.

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

  • Documentation added/updated to README

@noizwaves noizwaves changed the title Albums in folders Fetch albums in folders for iCloud Photos Aug 21, 2020
pyicloud/services/photos.py Outdated Show resolved Hide resolved
@@ -474,7 +514,8 @@ def _list_query_gen(self, offset, list_type, direction, query_filter=None):
return query

def __unicode__(self):
return self.title
names = self.lineage + [self.title]
return '/'.join(names)
Copy link
Contributor

Choose a reason for hiding this comment

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

I do not believe, this is a good logic here. In iCloud Photos, a folder name can contain a "/".

So either you need to escape the / character in foldernames (and possible also Album and Filenames) or you skip the whole thing.
Considering that none of the current services (ubuiquity, files and photos) support Unix-Styled foldernames/paths but instead do it via chaining of list items I'm thinking that maybe dropping this kind of feature completely is not the worst idea.

But let#s see what @Quentame thinks...

Copy link
Author

Choose a reason for hiding this comment

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

I don't feel strongly either that this change adds value. It was added initially to aid debugging/development, but ended up being unused for my use case. I've removed it to make the PR smaller & simpler.

pyicloud/services/photos.py Outdated Show resolved Hide resolved
@noizwaves
Copy link
Author

Thanks for the feedback @ixs! I've just fixed up the failing CI checks (formatting issues), so I think we're technically mergeable now. Do you still want a review from @Quentame?

@ixs
Copy link
Contributor

ixs commented Aug 29, 2020

@noizwaves I saw the changes. I liked them. Thanks for that. But yes, @Quentame has to review. I got no commit rights on this repo. And he's on vacation for a few more days. So patience. 😁

Copy link
Collaborator

@Quentame Quentame left a comment

Choose a reason for hiding this comment

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

A little change about the "magical numbers" 😄

Then, it seems nice to me.

pyicloud/services/photos.py Outdated Show resolved Hide resolved
for sub_folder in self._fetch_sub_folders(folder_id):
if sub_folder["recordName"] == "----Root-Folder----" or (
sub_folder["fields"].get("isDeleted")
and sub_folder["fields"]["isDeleted"]["value"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why checking the value here ?

Should we fetch sub folders if the parent is deleted ?

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

I'm inclined to leave this logic in, in other words, I'm included to not fetch sub folders / albums for deleted parent folders.

In the iCloud GUI, if you delete a folder that contains sub folders, it will delete both the folder and the sub-folder. I think it may be confusing/inconsistent if some deleted folders are traversed when looking for albums, as the results generated by pyicloud will be different than what is visible in the UI. This also remains aligned with the existing code where deleted albums were not returned.

I could see it being valuable to allow the user to choose how deleted folders/abums should be treated when fetching albums in a future PR that extends this functionality.

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK but I think we should add a quick comment to explain how it is.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also we can extract the code here in a little private function as, as you pointed out, the logic exists already at L178

Copy link
Author

Choose a reason for hiding this comment

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

@Quentame I've implemented both of your suggestions, thank you for making them. Sorry for the slow delay in implementing them!

I think all of the feedback has been addressed and this can be merged. Please let me know if you have other feedback.

pyicloud/services/photos.py Show resolved Hide resolved
@kpiwko
Copy link

kpiwko commented Nov 1, 2020

Hello @Quentame, what is the the status on this PR?

@Roelio81
Copy link

Roelio81 commented Feb 16, 2021

Hello @Quentame, what is the the status on this PR?

@noizwaves and @Quentame, is there still something that needs to be done for this PR or do you need help on anything?
I'm looking forward to getting this PR completed.

@mm21
Copy link

mm21 commented Oct 9, 2022

Hi, can this be pulled please? This is a crucial feature for a project I'm working on. Thanks!

@noizwaves noizwaves closed this Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Get Photo-Album within Folder
6 participants