Skip to content
Open
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
8 changes: 8 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,14 @@ To download a specific version of the photo asset, pass the version to ``downloa
with open(photo.versions['thumb']['filename'], 'wb') as thumb_file:
thumb_file.write(download.raw.read())

To upload an image

.. code-block:: python

api.photos.upload_file(file_path)

Note: Only limited media type is accepted, upload not support types (e.g. png) will get TYPE_UNSUPPORTED error.


Code samples
============
Expand Down
5 changes: 4 additions & 1 deletion pyicloud/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,10 @@ def photos(self):
"""Gets the 'Photo' service."""
if not self._photos:
service_root = self._get_webservice_url("ckdatabasews")
self._photos = PhotosService(service_root, self.session, self.params)
upload_url = self._get_webservice_url("uploadimagews")
self.params["dsid"] = self.data["dsInfo"]["dsid"]

self._photos = PhotosService(service_root, self.session, self.params, upload_url)
return self._photos

@property
Expand Down
23 changes: 21 additions & 2 deletions pyicloud/services/photos.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Photo service."""
import json
import os
import base64
from urllib.parse import urlencode

from datetime import datetime, timezone
from pyicloud.exceptions import PyiCloudServiceNotActivatedException
from pyicloud.exceptions import PyiCloudServiceNotActivatedException, PyiCloudAPIResponseException


class PhotosService:
Expand Down Expand Up @@ -121,7 +122,7 @@ class PhotosService:
},
}

def __init__(self, service_root, session, params):
def __init__(self, service_root, session, params, upload_url):
self.session = session
self.params = dict(params)
self._service_root = service_root
Expand All @@ -130,6 +131,7 @@ def __init__(self, service_root, session, params):
% self._service_root
)

self._upload_url = upload_url
self._albums = None

self.params.update({"remapEnums": True, "getCurrentSyncToken": True})
Expand Down Expand Up @@ -226,6 +228,23 @@ def all(self):
"""Returns all photos."""
return self.albums["All Photos"]

def upload_file(self, path):
''' Upload a photo from path, returns a recordName'''

filename = os.path.basename(path)
url = '{}/upload'.format(self._upload_url)

with open(path, 'rb') as file_obj:
request = self.session.post(url, data=file_obj.read(), params={
'filename': filename,
'dsid': self.params['dsid'],
})

if 'errors' in request.json():
raise PyiCloudAPIResponseException('', request.json()['errors'])

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


class PhotoAlbum:
"""A photo album."""
Expand Down