Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making drive work properly #385

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 18 additions & 16 deletions pyicloud/services/drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ def _get_token_from_cookie(self):
return {"token": match.group(1)}
raise Exception("Token cookie not found")

def get_node_data(self, node_id):
def get_node_data(self, drivewsid):
"""Returns the node data."""
request = self.session.post(
self._service_root + "/retrieveItemDetailsInFolders",
params=self.params,
data=json.dumps(
[
{
"drivewsid": "FOLDER::com.apple.CloudDocs::%s" % node_id,
"drivewsid": drivewsid,
"partialData": False,
}
]
Expand All @@ -51,12 +51,12 @@ def get_node_data(self, node_id):
self._raise_if_error(request)
return request.json()[0]

def get_file(self, file_id, **kwargs):
def get_file(self, file_id, zone="com.apple.CloudDocs", **kwargs):
"""Returns iCloud Drive file."""
file_params = dict(self.params)
file_params.update({"document_id": file_id})
response = self.session.get(
self._document_root + "/ws/com.apple.CloudDocs/download/by_id",
self._document_root + "/ws/%s/download/by_id" % zone,
params=file_params,
)
self._raise_if_error(response)
Expand All @@ -77,7 +77,7 @@ def get_app_data(self):
self._raise_if_error(request)
return request.json()["items"]

def _get_upload_contentws_url(self, file_object):
def _get_upload_contentws_url(self, file_object, zone="com.apple.CloudDocs"):
"""Get the contentWS endpoint URL to add a new file."""
content_type = mimetypes.guess_type(file_object.name)[0]
if content_type is None:
Expand All @@ -93,7 +93,7 @@ def _get_upload_contentws_url(self, file_object):
file_params.update(self._get_token_from_cookie())

request = self.session.post(
self._document_root + "/ws/com.apple.CloudDocs/upload/web",
self._document_root + "/ws/%s/upload/web" % zone,
params=file_params,
headers={"Content-Type": "text/plain"},
data=json.dumps(
Expand All @@ -108,7 +108,9 @@ def _get_upload_contentws_url(self, file_object):
self._raise_if_error(request)
return (request.json()[0]["document_id"], request.json()[0]["url"])

def _update_contentws(self, folder_id, sf_info, document_id, file_object):
def _update_contentws(
self, folder_id, sf_info, document_id, file_object, zone="com.apple.CloudDocs"
):
data = {
"data": {
"signature": sf_info["fileChecksum"],
Expand All @@ -121,7 +123,7 @@ def _update_contentws(self, folder_id, sf_info, document_id, file_object):
"document_id": document_id,
"path": {
"starting_document_id": folder_id,
"path": file_object.name,
"path": os.path.basename(file_object.name),
},
"allow_conflict": True,
"file_flags": {
Expand All @@ -138,22 +140,22 @@ def _update_contentws(self, folder_id, sf_info, document_id, file_object):
data["data"].update({"receipt": sf_info["receipt"]})

request = self.session.post(
self._document_root + "/ws/com.apple.CloudDocs/update/documents",
self._document_root + "/ws/%s/update/documents" % zone,
params=self.params,
headers={"Content-Type": "text/plain"},
data=json.dumps(data),
)
self._raise_if_error(request)
return request.json()

def send_file(self, folder_id, file_object):
def send_file(self, folder_id, file_object, zone="com.apple.CloudDocs"):
"""Send new file to iCloud Drive."""
document_id, content_url = self._get_upload_contentws_url(file_object)
document_id, content_url = self._get_upload_contentws_url(file_object, zone)

request = self.session.post(content_url, files={file_object.name: file_object})
self._raise_if_error(request)
content_response = request.json()["singleFile"]
self._update_contentws(folder_id, content_response, document_id, file_object)
self._update_contentws(folder_id, content_response, document_id, file_object, zone)

def create_folders(self, parent, name):
"""Creates a new iCloud Drive folder"""
Expand Down Expand Up @@ -220,7 +222,7 @@ def move_items_to_trash(self, node_id, etag):
def root(self):
"""Returns the root node."""
if not self._root:
self._root = DriveNode(self, self.get_node_data("root"))
self._root = DriveNode(self, self.get_node_data("FOLDER::com.apple.CloudDocs::root"))
return self._root

def __getattr__(self, attr):
Expand Down Expand Up @@ -263,7 +265,7 @@ def get_children(self):
"""Gets the node children."""
if not self._children:
if "items" not in self.data:
self.data.update(self.connection.get_node_data(self.data["docwsid"]))
self.data.update(self.connection.get_node_data(self.data["drivewsid"]))
if "items" not in self.data:
raise KeyError("No items in folder, status: %s" % self.data["status"])
self._children = [
Expand Down Expand Up @@ -302,11 +304,11 @@ def open(self, **kwargs):
response = Response()
response.raw = io.BytesIO()
return response
return self.connection.get_file(self.data["docwsid"], **kwargs)
return self.connection.get_file(self.data["docwsid"], zone=self.data["zone"], **kwargs)

def upload(self, file_object, **kwargs):
"""Upload a new file."""
return self.connection.send_file(self.data["docwsid"], file_object, **kwargs)
return self.connection.send_file(self.data["docwsid"], file_object, zone=self.data["zone"], **kwargs)

def dir(self):
"""Gets the node list of directories."""
Expand Down