Skip to content

Conversation

@lucemia
Copy link

@lucemia lucemia commented Feb 15, 2023

Proposed change

Add .upload_file method to PhotosService.
The PR is based on https://github.com/picklepete/pyicloud/pull/354/files work but:

  1. fix KeyError: 'dsid' error
  2. focus on upload media

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:

api.photos.upload_file("./test.png")

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

@lucemia
Copy link
Author

lucemia commented Feb 21, 2023

You can upload JPEG images from your computer to your Personal Library using iCloud.com.

based on the https://support.apple.com/en-ca/guide/icloud/mmb5dbaf376f/icloud
only jpeg is support by this API :(

so I switch to this solutions
https://github.com/RhetTbull/osxphotos

@tboehme
Copy link

tboehme commented Sep 24, 2023

Thanks a lot - works for me, however, I can only upload to the main "Recents" album. Any idea if it is possible to select the album for the upload?

@pcace
Copy link

pcace commented Oct 9, 2023

Thanks a lot - works for me, however, I can only upload to the main "Recents" album. Any idea if it is possible to select the album for the upload?

Hi there, sorry for this little offtopic here, but do i understand correctly that you manage to upload directly to icloud Photos with https://github.com/RhetTbull/osxphotos ? if so, how?
Thanks a lot!

@jummo
Copy link

jummo commented Mar 29, 2024

Thanks a lot - works for me, however, I can only upload to the main "Recents" album. Any idea if it is possible to select the album for the upload?

Hi Thomas, did you found a way to add photos to a specific album?

@hyacz
Copy link

hyacz commented Jul 23, 2024

Thanks a lot - works for me, however, I can only upload to the main "Recents" album. Any idea if it is possible to select the album for the upload?

Hi Thomas, did you found a way to add photos to a specific album?

I made some new progress, I found that uploading photos and adding them to a specific album are two steps. A simple code example is as follows:

class MyService(PyiCloudService):
    def upload_photo(self, photo_path, album_name="MyAlbum"):
        # Skip the upload process, See https://github.com/lucemia/pyicloud/blob/96d9f26553ebdfb47f3cfbc8ec13b76e70ff0cff/pyicloud/services/photos.py#L231
        # ...

        photo_id = [x["recordName"] for x in request.json()["records"] if x["recordType"] == "CPLAsset"][0]

        # Query the target album properties (Especially ZoneID, I don't quite understand the meaning of this field, but the existing Python API cannot get it.)
        for folder in self.photos._fetch_folders():
            folder_name = base64.b64decode(
                folder.get("fields", {}).get("albumNameEnc", {}).get("value", "")
            ).decode("utf-8")

            if folder_name == album_name:
                album_id = folder["recordName"]
                zone_id = folder["zoneID"]["ownerRecordName"]
                album_len = len(self.photos.albums[album_name])
        
        # Put photos into the specified album
        url = f"{self.photos.service_endpoint}/records/modify?{urlencode(self.params)}"
        json_data = {
            "atomic": True,
            "zoneID": {
                "zoneName": "PrimarySync",
                "ownerRecordName": zone_id,
                "zoneType": "REGULAR_CUSTOM_ZONE"
            },
            "operations": [
                {
                    "operationType": "create",
                    "record": {
                        "fields": {
                            "itemId": {
                                "value": photo_id
                            },
                            "position": {
                                "value": 1024 * (1 + album_len)
                            },
                            "containerId": {
                                "value": album_id
                            }
                        },
                        "recordType": "CPLContainerRelation",
                        "recordName": f"{photo_id}-IN-{album_id}"
                    }
                }
            ]
        }
        request = self.session.post(
            url, data=json.dumps(json_data), headers={"Content-type": "text/plain"}
        )

@fxuniverse
Copy link

any chance to merge this?

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.

Can you upload photos to albums?

6 participants