Skip to content

Commit

Permalink
[Fix] #60 #61
Browse files Browse the repository at this point in the history
  • Loading branch information
wxy1343 committed Oct 29, 2021
1 parent 2a25672 commit 10a0613
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 28 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ python main.py -h
<td>-w, --whitelist</td>
<td>使用白名单过滤文件</td>
</tr>
<tr>
<td>-m, --match</td>
<td>指定使用正则匹配文件</td>
</tr>
</tbody>
</table>
</details>
Expand Down Expand Up @@ -301,13 +305,18 @@ python main.py COMMAND -h
</tr>
<tr>
<td>sync</td>
<td>--sync-time</td>
<td>-st, --sync-time</td>
<td>同步间隔时间</td>
</tr>
<tr>
<td>sync</td>
<td>--no-delete, -n</td>
<td>不删除云盘文件</td>
<td>不删除云盘文件(默认)</td>
</tr>
<tr>
<td>sync</td>
<td>-d, --delete</td>
<td>允许删除云盘文件</td>
</tr>
<tr>
<td>token</td>
Expand Down
2 changes: 1 addition & 1 deletion aliyunpan/about.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.9.1'
__version__ = '2.9.2'
56 changes: 38 additions & 18 deletions aliyunpan/cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import functools
import os
import platform
import re
from typing import List
from typing import List, Union

import aria2p
import requests
Expand All @@ -22,6 +23,7 @@

class Commander:
def __init__(self, init=True, *args, **kwargs):
self.match = False
self.whitelist = False
self._disk = AliyunPan()
self._path_list = PathList(self._disk)
Expand Down Expand Up @@ -49,14 +51,15 @@ def __del__(self):
pass

def init(self, config_file=None, refresh_token=None, username=None, password=None, depth=3, timeout=None,
drive_id=None, album=False, share_id='', share_pwd='', filter_file=None, whitelist=False):
drive_id=None, album=False, share_id='', share_pwd='', filter_file=None, whitelist=False, match=False):
self._path_list.depth = depth
self._req.timeout = timeout
self._disk.drive_id = drive_id
self._disk.album = album
self._disk._share = Share(share_id, share_pwd)
self.filter_set.update(filter_file)
self.whitelist = whitelist
self.match = match
config_file_list = list(
filter(lambda x: get_real_path(x).is_file(), map(lambda x: get_real_path(x), self._config_set)))
if config_file:
Expand Down Expand Up @@ -112,6 +115,8 @@ def ls(self, path='root', l=False, query=None):
file_info_list = self._path_list.get_file_info(self._disk.search(query))
else:
file_info_list = self._path_list.get_path_list(path, update=False)
if self.filter_set:
file_info_list = [i for i in file_info_list if self.file_filter(i.name)]
for i, j in enumerate(file_info_list):
if l:
if j.type:
Expand Down Expand Up @@ -228,24 +233,37 @@ def mkdir(self, path, name=None, parent_file_id=None):
file_id_list.append((file_id, path))
return file_id_list

def file_filter(self, path):
path = Path(path)
def file_filter(self, path, whitelist=None):
if not path:
return False
if whitelist is None:
whitelist = self.whitelist
if isinstance(path, FileInfo):
name = path.name
else:
name = Path(path)
for pattern in self.filter_set:
if re.match(pattern, path.name):
return self.whitelist
return not self.whitelist
if re.match(pattern, str(name)):
return whitelist
return not whitelist

def upload(self, path, upload_path='root', timeout=10.0, retry=3, force=False, share=False, chunk_size=None,
c=False, ignore=False):
if isinstance(path, (str, AliyunpanPath, Path)):
path_list = (path,)
path_list = {path}
else:
path_list = path
path_list = filter(self.file_filter, path_list)
if self.match:
[self.filter_set.add(i) for i in path_list]
if self.whitelist:
path_list = set(filter(functools.partial(self.file_filter, whitelist=False), Path().iterdir()))
else:
path_list = set(filter(functools.partial(self.file_filter, whitelist=True), Path().iterdir()))
else:
path_list = filter(self.file_filter, path_list)
result_list = []
for path in path_list:
path: Union[str, AliyunpanPath, Path]
if self._share_link in str(path):
share_list = []
if share:
Expand Down Expand Up @@ -308,6 +326,7 @@ def upload(self, path, upload_path='root', timeout=10.0, retry=3, force=False, s
upload_path = '/'
upload_path = Path(upload_path)
upload_file_list = self.upload_dir(path, upload_path)
upload_file_list = [i for i in upload_file_list if self.file_filter(i[1])]
for file in upload_file_list:
try:
parent_file_id = self._path_list.get_path_fid(file[0], update=False)
Expand Down Expand Up @@ -394,16 +413,17 @@ def upload_share(self, share_info_list: List[ShareInfo], upload_path='root', for
self._print.upload_info(p, status=False)
return folder_list, file_list

def download(self, path, save_path=None, single_file=False, share=False, chunk_size=1048576, aria2=False, **kwargs):
if not self.file_filter(path):
return False
def download(self, path, save_path=None, single_file=False, share=False, chunk_size=1048576, aria2=False,
first=True, **kwargs):
if not save_path:
save_path = Path().cwd()
save_path = Path(save_path)
if isinstance(path, str):
path_list = (path,)
path_list = {path}
else:
path_list = path
if not first:
path_list = [i for i in path_list if self.file_filter(i)]
kwargs.setdefault('referer', self._host_url)
for path in path_list:
if str(path).startswith(self._share_link) or share:
Expand Down Expand Up @@ -461,7 +481,7 @@ def download(self, path, save_path=None, single_file=False, share=False, chunk_s
self._print.print_line()
else:
self.download(self._path_list.get_fid_list(file_node.id), save_path=save_path / p.name,
chunk_size=chunk_size, aria2=aria2, **kwargs)
chunk_size=chunk_size, aria2=aria2, first=False, **kwargs)

def download_file(self, path, url, chunk_size=1048576):
if not self.file_filter(path):
Expand Down Expand Up @@ -579,15 +599,15 @@ def tui(self):
aliyunpan_tui = AliyunpanTUI(self)
aliyunpan_tui.run()

def sync(self, path, upload_path, sync_time, time_out, chunk_size, retry, no_delete, first=True):
def sync(self, path, upload_path, sync_time, time_out, chunk_size, retry, delete, first=True):
if first and path == 'root':
self._print.print_info(
'Do you really want to synchronize the root? This operation may delete all your files.', error=True)
input('\nEnter to continue.')
path = AliyunpanPath(path)
relative_path = AliyunpanPath(path.name)
if str(relative_path) == '.':
return self.sync(path.absolute(), upload_path, sync_time, time_out, chunk_size, retry, no_delete,
return self.sync(path.absolute(), upload_path, sync_time, time_out, chunk_size, retry, delete,
first=False)
upload_path = AliyunpanPath(upload_path)
p = upload_path / relative_path
Expand All @@ -608,12 +628,12 @@ def sync(self, path, upload_path, sync_time, time_out, chunk_size, retry, no_del
if first:
self._print.upload_info(path_, status=False)
self._print.print_line()
elif not no_delete:
elif delete:
self.rm(upload_path / relative_path)
if sync_time:
self._print.wait_info('等待{time}秒后再次同步', t=sync_time, refresh_line=True)
self._print.refresh_line()
self.sync(path, upload_path, sync_time, time_out, chunk_size, retry, no_delete=no_delete, first=False)
self.sync(path, upload_path, sync_time, time_out, chunk_size, retry, delete=delete, first=False)

def share_link(self, path_list, file_id_list=None, expiration=None):
path_list = filter(self.file_filter, path_list)
Expand Down
16 changes: 9 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@
@click.option('-sp', '--share-pwd', type=click.STRING, help='Specify share_pwd.')
@click.option('-f', '--filter-file', multiple=True, type=click.STRING, help='Filter files.')
@click.option('-w', '--whitelist', is_flag=True, help='Filter files using whitelist.')
@click.option('-m', '--match', is_flag=True, help='Specify to use regular matching files.')
def cli(config_file, refresh_token, username, password, depth, debug, timeout, drive_id, album, share_id, share_pwd,
filter_file, whitelist):
filter_file, whitelist, match):
logger.info(f'Version:{__version__}')
if debug:
logger.setLevel('DEBUG')
commander.init(config_file=None if refresh_token or username else config_file,
refresh_token=None if username else refresh_token, username=username, password=password, depth=depth,
timeout=timeout, drive_id=drive_id, album=album, share_id=share_id, share_pwd=share_pwd,
filter_file=set(filter_file), whitelist=whitelist)
filter_file=set(filter_file), whitelist=whitelist, match=match)


@cli.command(aliases=['l', 'list', 'dir'], help='List files.')
Expand Down Expand Up @@ -90,7 +91,7 @@ def upload(path, file, upload_path, time_out, retry, force, share, chunk_size, c
if not path and not file:
raise click.MissingParameter(param=click.get_current_context().command.params[2])
else:
path_list = {*file, path}
path_list = set(filter(None, {*file, path}))
commander.upload(path_list, upload_path, time_out, retry, force, share, chunk_size, c)


Expand Down Expand Up @@ -170,10 +171,11 @@ def cat(path, encoding):
@click.option('-cs', '--chunk-size', type=click.INT, help='Chunk size(byte).')
@click.option('-t', '--time-out', type=click.FLOAT, help='Chunk upload timeout(sec).', default=10.0, show_default=True)
@click.option('-r', '--retry', type=click.INT, help='number of retries.', default=3, show_default=True)
@click.option('--sync-time', type=click.FLOAT, help='Synchronization interval time(sec).')
@click.option('--no-delete', '-n', is_flag=True, help='Do not delete the cloud drive files.')
def sync(path, upload_path, time_out, chunk_size, retry, sync_time, no_delete):
commander.sync(path, upload_path, sync_time, time_out, chunk_size, retry, no_delete)
@click.option('-st', '--sync-time', type=click.FLOAT, help='Synchronization interval time(sec).')
@click.option('-n', '--no-delete', is_flag=True, help='Do not delete the cloud drive files.')
@click.option('-d', '--delete', is_flag=True, help='Allow deletion of cloud drive files.')
def sync(path, upload_path, time_out, chunk_size, retry, sync_time, no_delete, delete):
commander.sync(path, upload_path, sync_time, time_out, chunk_size, retry, delete)


@cli.command(aliases=['tui'], help='Text-based User Interface.')
Expand Down

0 comments on commit 10a0613

Please sign in to comment.