diff --git a/pyicloud/base.py b/pyicloud/base.py index 6ac8bdbd..67fbbec9 100644 --- a/pyicloud/base.py +++ b/pyicloud/base.py @@ -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 diff --git a/pyicloud/services/photos.py b/pyicloud/services/photos.py index 06b3dd32..8d61e6c2 100644 --- a/pyicloud/services/photos.py +++ b/pyicloud/services/photos.py @@ -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: @@ -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 @@ -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}) @@ -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.""" diff --git a/pyicloud/services/ubiquity.py b/pyicloud/services/ubiquity.py index f45a78bc..4873af5d 100644 --- a/pyicloud/services/ubiquity.py +++ b/pyicloud/services/ubiquity.py @@ -21,7 +21,7 @@ def root(self): def get_node_url(self, node_id, variant="item"): """Returns a node URL.""" - return self._node_url % (self.params["dsid"], variant, node_id) + return self._node_url % (self.params[""], variant, node_id) def get_node(self, node_id): """Returns a node."""