sync: yandex disk update
This commit is contained in:
1 parent
b85f4ca6dc
commit
6c99192122
2 files changed
+62
-34
No files matched your search
@@ -25,8 +25,8 @@ class YDProvider(Provider):
|
|||||||
def scan(self, path):
|
def scan(self, path):
|
||||||
res = self.api.get_content(self.get_abs(path))
|
res = self.api.get_content(self.get_abs(path))
|
||||||
files = []
|
files = []
|
||||||
for item in res['_embedded']['items']:
|
for item in res.children:
|
||||||
files.append(item['path'])
|
files.append(item.path)
|
||||||
|
|
||||||
files = map(
|
files = map(
|
||||||
lambda fp: re.sub('disk:', '', fp),
|
lambda fp: re.sub('disk:', '', fp),
|
||||||
@@ -45,7 +45,7 @@ class YDProvider(Provider):
|
|||||||
|
|
||||||
def hash(self, path):
|
def hash(self, path):
|
||||||
res = self.api.get_content(self.get_abs(path))
|
res = self.api.get_content(self.get_abs(path))
|
||||||
return res['sha256']
|
return res.sha256
|
||||||
|
|
||||||
def copy(self, path, out):
|
def copy(self, path, out):
|
||||||
data = self.read(path)
|
data = self.read(path)
|
||||||
@@ -60,5 +60,4 @@ class YDProvider(Provider):
|
|||||||
|
|
||||||
def is_dir(self, path):
|
def is_dir(self, path):
|
||||||
res = self.api.get_content(self.get_abs(path))
|
res = self.api.get_content(self.get_abs(path))
|
||||||
return res['type'] == 'dir'
|
return res.type == 'dir'
|
||||||
|
|
||||||
@@ -47,8 +47,7 @@ class YDClient:
|
|||||||
self.check_code(r)
|
self.check_code(r)
|
||||||
|
|
||||||
json_dict = r.json()
|
json_dict = r.json()
|
||||||
return json_dict
|
return Directory(**json_dict)
|
||||||
# return Directory(**json_dict)
|
|
||||||
|
|
||||||
def create_folder(self, path_to_folder):
|
def create_folder(self, path_to_folder):
|
||||||
"""
|
"""
|
||||||
@@ -106,13 +105,11 @@ class YDClient:
|
|||||||
json_dict = self._get_dictionary_of_published_files()
|
json_dict = self._get_dictionary_of_published_files()
|
||||||
|
|
||||||
elements = []
|
elements = []
|
||||||
|
for item in json_dict['items']:
|
||||||
# for item in json_dict["items"]:
|
if item['type'] == 'dir':
|
||||||
# if item["type"] == "dir":
|
elements.append(Directory(**item))
|
||||||
# elements.append(Directory(**item))
|
elif item['type'] == 'file':
|
||||||
# elif item["type"] == "file":
|
elements.append(File(**item))
|
||||||
# elements.append(File(**item))
|
|
||||||
|
|
||||||
return elements
|
return elements
|
||||||
|
|
||||||
def get_public_link_to_folder_or_file(self, path):
|
def get_public_link_to_folder_or_file(self, path):
|
||||||
@@ -120,26 +117,24 @@ class YDClient:
|
|||||||
:param path: path
|
:param path: path
|
||||||
:return: public link to folder or file
|
:return: public link to folder or file
|
||||||
"""
|
"""
|
||||||
url = self.base_url + "/resources/publish"
|
url = '{0}/resources/publish'.format(self.base_url)
|
||||||
|
|
||||||
payload = {'path': path}
|
payload = {'path': path}
|
||||||
r = requests.put(url, headers=self.base_headers, params=payload)
|
r = requests.put(url, headers=self.base_headers, params=payload)
|
||||||
self.check_code(r)
|
self.check_code(r)
|
||||||
|
|
||||||
files = self._get_dictionary_of_published_files()
|
files = self._get_dictionary_of_published_files()
|
||||||
|
for file in files['items']:
|
||||||
for file in files["items"]:
|
if str(file['path']).endswith(path):
|
||||||
if str(file["path"]).endswith(path):
|
return file['public_url']
|
||||||
return file["public_url"]
|
return ''
|
||||||
|
|
||||||
return ""
|
|
||||||
|
|
||||||
def unpublish_folder_or_file(self, path):
|
def unpublish_folder_or_file(self, path):
|
||||||
"""
|
"""
|
||||||
Unpublish folder of file
|
Unpublish folder of file
|
||||||
:param path: path to file or folder
|
:param path: path to file or folder
|
||||||
"""
|
"""
|
||||||
url = self.base_url + "/resources/unpublish"
|
url = '{0}/resources/unpublish'.format(self.base_url)
|
||||||
|
|
||||||
payload = {'path': path}
|
payload = {'path': path}
|
||||||
r = requests.put(url, headers=self.base_headers, params=payload)
|
r = requests.put(url, headers=self.base_headers, params=payload)
|
||||||
@@ -149,7 +144,7 @@ class YDClient:
|
|||||||
"""
|
"""
|
||||||
:return: List of all files
|
:return: List of all files
|
||||||
"""
|
"""
|
||||||
url = self.base_url + "/resources/files"
|
url = '{0}/resources/files'.format(self.base_url)
|
||||||
|
|
||||||
r = requests.get(url, headers=self.base_headers)
|
r = requests.get(url, headers=self.base_headers)
|
||||||
self.check_code(r)
|
self.check_code(r)
|
||||||
@@ -157,11 +152,9 @@ class YDClient:
|
|||||||
json_dict = r.json()
|
json_dict = r.json()
|
||||||
|
|
||||||
files = []
|
files = []
|
||||||
|
for item in json_dict['items']:
|
||||||
# for item in json_dict["items"]:
|
f = File(**item)
|
||||||
# f = File(**item)
|
files.append(f)
|
||||||
# files.append(f)
|
|
||||||
|
|
||||||
return files
|
return files
|
||||||
|
|
||||||
def move_folder_or_file(self, path_from, path_to):
|
def move_folder_or_file(self, path_from, path_to):
|
||||||
@@ -170,7 +163,7 @@ class YDClient:
|
|||||||
:param path_from: path from
|
:param path_from: path from
|
||||||
:param path_to: path to
|
:param path_to: path to
|
||||||
"""
|
"""
|
||||||
url = self.base_url + "/resources/move"
|
url = '{0}/resources/move'.format(self.base_url)
|
||||||
|
|
||||||
payload = {'path': path_to, 'from': path_from}
|
payload = {'path': path_to, 'from': path_from}
|
||||||
r = requests.post(url, headers=self.base_headers, params=payload)
|
r = requests.post(url, headers=self.base_headers, params=payload)
|
||||||
@@ -182,7 +175,7 @@ class YDClient:
|
|||||||
:param path_from: path from
|
:param path_from: path from
|
||||||
:param path_to: path to yandex disk
|
:param path_to: path to yandex disk
|
||||||
"""
|
"""
|
||||||
url = self.base_url + "/resources/upload"
|
url = '{0}/resources/upload'.format(self.base_url)
|
||||||
|
|
||||||
payload = {'path': path_to}
|
payload = {'path': path_to}
|
||||||
r = requests.get(url, headers=self.base_headers, params=payload)
|
r = requests.get(url, headers=self.base_headers, params=payload)
|
||||||
@@ -203,14 +196,14 @@ class YDClient:
|
|||||||
:param from_url: URL path from
|
:param from_url: URL path from
|
||||||
:param path_to: path to yandex disk
|
:param path_to: path to yandex disk
|
||||||
"""
|
"""
|
||||||
url = self.base_url + "/resources/upload"
|
url = '{0}/resources/upload'.format(self.base_url)
|
||||||
|
|
||||||
payload = {'path': path_to, 'url': from_url}
|
payload = {'path': path_to, 'url': from_url}
|
||||||
r = requests.post(url, headers=self.base_headers, params=payload)
|
r = requests.post(url, headers=self.base_headers, params=payload)
|
||||||
self.check_code(r)
|
self.check_code(r)
|
||||||
|
|
||||||
def _get_dictionary_of_published_files(self):
|
def _get_dictionary_of_published_files(self):
|
||||||
url = self.base_url + "/resources/public"
|
url = '{0}/resources/public'.format(self.base_url)
|
||||||
|
|
||||||
r = requests.get(url, headers=self.base_headers)
|
r = requests.get(url, headers=self.base_headers)
|
||||||
self.check_code(r)
|
self.check_code(r)
|
||||||
@@ -218,7 +211,7 @@ class YDClient:
|
|||||||
return r.json()
|
return r.json()
|
||||||
|
|
||||||
def check_code(self, req):
|
def check_code(self, req):
|
||||||
if not str(req.status_code).startswith("2"):
|
if not str(req.status_code).startswith('2'):
|
||||||
raise YDException(req.status_code, req.text)
|
raise YDException(req.status_code, req.text)
|
||||||
|
|
||||||
|
|
||||||
@@ -230,4 +223,40 @@ class YDException(Exception):
|
|||||||
self.code = code
|
self.code = code
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "%d. %s" % (self.code, super(YDException, self).__str__())
|
return "%d. %s" % (self.code, super(YDException, self).__str__())
|
||||||
|
|
||||||
|
|
||||||
|
class Directory:
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
self.children = []
|
||||||
|
|
||||||
|
for key in kwargs:
|
||||||
|
if key is not '_embedded':
|
||||||
|
setattr(self, key, kwargs[key])
|
||||||
|
|
||||||
|
if "_embedded" in kwargs:
|
||||||
|
for item in kwargs['_embedded']['items']:
|
||||||
|
if item['type'] == 'dir':
|
||||||
|
d = Directory(**item)
|
||||||
|
self.children.append(d)
|
||||||
|
|
||||||
|
if item['type'] == 'file':
|
||||||
|
f = File(**item)
|
||||||
|
self.children.append(f)
|
||||||
|
|
||||||
|
def get_children(self):
|
||||||
|
return self.children
|
||||||
|
|
||||||
|
|
||||||
|
class File:
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
for key in kwargs:
|
||||||
|
setattr(self, key, kwargs[key])
|
||||||
|
|
||||||
|
|
||||||
|
class Disk:
|
||||||
|
def __init__(self, trash_size, total_space, used_space, system_folders):
|
||||||
|
self.trash_size = trash_size
|
||||||
|
self.total_space = total_space
|
||||||
|
self.used_space = used_space
|
||||||
|
self.system_folders = system_folders
|
||||||
Reference in new issue
Block a user