sync update: moving to ./sync.py as entry point
This commit is contained in:
1 parent
6f144fb6de
commit
13a3a02613
8 files changed
+456
-83
No files matched your search
@@ -0,0 +1,64 @@
|
||||
import os
|
||||
|
||||
import re
|
||||
|
||||
import io
|
||||
import requests
|
||||
|
||||
from sync.data import Provider
|
||||
from utils.fn import lmap
|
||||
from yandexdisk import YDClient
|
||||
|
||||
|
||||
class YDProvider(Provider):
|
||||
def __init__(self, root, access_token):
|
||||
super().__init__(root)
|
||||
self.api = YDClient(access_token)
|
||||
|
||||
def read(self, path):
|
||||
remote = self.api.get_download_link(self.get_abs(path))
|
||||
url = remote['href']
|
||||
req = requests.get(url)
|
||||
data = req.content
|
||||
return io.BytesIO(data)
|
||||
|
||||
def scan(self, path):
|
||||
res = self.api.get_content(self.get_abs(path))
|
||||
files = []
|
||||
for item in res['_embedded']['items']:
|
||||
files.append(item['path'])
|
||||
|
||||
files = map(
|
||||
lambda fp: re.sub('disk:', '', fp),
|
||||
files
|
||||
)
|
||||
return lmap(self.get_rel, files)
|
||||
|
||||
def glob(self, pattr):
|
||||
return []
|
||||
|
||||
def type_filter(self, path, ext):
|
||||
return list(filter(
|
||||
lambda fn: os.path.splitext(fn)[1] == ext,
|
||||
self.scan(path)
|
||||
))
|
||||
|
||||
def hash(self, path):
|
||||
res = self.api.get_content(self.get_abs(path))
|
||||
return res['sha256']
|
||||
|
||||
def copy(self, path, out):
|
||||
data = self.read(path)
|
||||
f = open(out, 'wb')
|
||||
f.write(data.read())
|
||||
f.close()
|
||||
return out
|
||||
|
||||
def size(self, path):
|
||||
res = self.api.get_content(self.get_abs(path))
|
||||
return res['size']
|
||||
|
||||
def is_dir(self, path):
|
||||
res = self.api.get_content(self.get_abs(path))
|
||||
return res['type'] == 'dir'
|
||||
|
||||
Reference in new issue
Block a user