Skip to content

Commit

Permalink
fix picklepete#192 add upload photo feature
Browse files Browse the repository at this point in the history
  • Loading branch information
G8s Bot committed Feb 15, 2023
1 parent 332cc9f commit 60526b0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
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
2 changes: 1 addition & 1 deletion pyicloud/services/ubiquity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit 60526b0

Please sign in to comment.