fix #192 support upload media to albums
Proposed change
Add .upload_file method to PhotosService.
The PR is based on https://github.com/picklepete/pyicloud/pull/354/files work but:
- fix
KeyError: 'dsid'error - focus on upload media
Type of change
- [ ] Dependency upgrade
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New service (thank you!)
- [x] 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
- This PR fixes or closes issue: fixes #192
- This PR is related to issue: #354
Checklist
- [x] The code change is tested and works locally.
- [x] Local tests pass. Your PR cannot be merged unless tests pass
- [x] 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:
- [x] Documentation added/updated to README
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
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?
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!
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?
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"}
)