Skip to content

Commit

Permalink
[+]本地同步 #62
Browse files Browse the repository at this point in the history
  • Loading branch information
wxy1343 committed Nov 3, 2021
1 parent 10a0613 commit e4577bf
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 14 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,17 @@ python main.py COMMAND -h
<tr>
<td>sync</td>
<td>--no-delete, -n</td>
<td>不删除云盘文件(默认)</td>
<td>不删除(云盘/本地)文件(默认)</td>
</tr>
<tr>
<td>sync</td>
<td>-d, --delete</td>
<td>允许删除云盘文件</td>
<td>允许删除(云盘/本地)文件</td>
</tr>
<tr>
<td>sync</td>
<td>-l, --local</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.2'
__version__ = '2.10.1'
3 changes: 3 additions & 0 deletions aliyunpan/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,6 @@ def __sub__(self, other: Path):
else:
parts.append(j)
return AliyunpanPath('/'.join(parts))

def __add__(self, other):
return Path(self.__str__()) / Path(str(other))
35 changes: 33 additions & 2 deletions aliyunpan/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import platform
import re
import shutil
from typing import List, Union

import aria2p
Expand Down Expand Up @@ -57,7 +58,7 @@ def init(self, config_file=None, refresh_token=None, username=None, password=Non
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.filter_set.update(filter_file) if filter_file else None
self.whitelist = whitelist
self.match = match
config_file_list = list(
Expand Down Expand Up @@ -413,8 +414,10 @@ 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,
def download(self, path, save_path=None, single_file=False, share=False, chunk_size=None, aria2=False,
first=True, **kwargs):
if not chunk_size:
chunk_size = 1048576
if not save_path:
save_path = Path().cwd()
save_path = Path(save_path)
Expand Down Expand Up @@ -635,6 +638,34 @@ def sync(self, path, upload_path, sync_time, time_out, chunk_size, retry, delete
self._print.refresh_line()
self.sync(path, upload_path, sync_time, time_out, chunk_size, retry, delete=delete, first=False)

def sync_local(self, sync_path, save_path, sync_time, chunk_size, delete, **kwargs):
if not save_path:
save_path = '.'
path = AliyunpanPath(save_path) + AliyunpanPath(sync_path)
if not path.exists():
self.download(sync_path, save_path)
file_id = self.path_list.get_path_fid(sync_path, update=False)
if not file_id:
raise FileNotFoundError(sync_path)
self._path_list.update_path_list(sync_path, is_fid=False)
path_ = self._path_list._tree.to_dict(file_id, with_data=True)[str(AliyunpanPath(sync_path))]
change_file_list = self._path_list.check_path_diff(path, path_['children'] if 'children' in path_ else [])
for path_ in change_file_list:
p = str(AliyunpanPath(path_) - AliyunpanPath(save_path))
file_node = self.path_list.get_path_node(p)
if not file_node:
if delete:
Path(path_).unlink() if path_.is_file() else shutil.rmtree(path_)
self._print.remove_info(path_, True)
self._print.print_line()
else:
self.download(p, str(save_path / Path(p).parent), chunk_size=chunk_size, **kwargs)
if sync_time:
self._print.wait_info('等待{time}秒后再次同步', t=sync_time, refresh_line=True)
self._print.refresh_line()
return self.sync_local(sync_path, save_path, sync_time=sync_time, chunk_size=chunk_size, delete=delete,
**kwargs)

def share_link(self, path_list, file_id_list=None, expiration=None):
path_list = filter(self.file_filter, path_list)
t = '' if expiration is None else time.time() + expiration
Expand Down
4 changes: 3 additions & 1 deletion aliyunpan/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def __init__(self):
self._print = self._output_gen()
self._print.__next__()
self.print_line = False
self.count = 0

def __del__(self):
pass
Expand All @@ -157,7 +158,7 @@ def _output_gen(self):
while True:
info = yield
with self._lock:
if self.print_line:
if self.print_line and self.count:
self._stdout.write('\n')
self.print_line = False
if last_info:
Expand All @@ -170,6 +171,7 @@ def _output_gen(self):
self._stdout.write(str(info))
self._stderr.flush()
self._stdout.flush()
self.count += 1
last_info = info


Expand Down
31 changes: 23 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ def download(ctx, path, file, save_path, share, chunk_size, aria2):
file_list = {*file, path}
kwargs = {}
for i in ctx.args:
kwargs[i.split('=')[0]] = i.split('=')[1]
if '=' in i:
kwargs[i.split('=')[0]] = i.split('=')[1]
else:
kwargs[i] = True
commander.download(file_list, save_path=save_path, share=share, chunk_size=chunk_size, aria2=aria2, **kwargs)


Expand Down Expand Up @@ -164,18 +167,30 @@ def cat(path, encoding):
click.echo(commander.cat(path, encoding))


@cli.command(aliases=['sync'], help='Synchronize files.')
@cli.command(aliases=['sync'], help='Synchronize files.',
context_settings=dict(ignore_unknown_options=True, allow_extra_args=True))
@click.help_option('-h', '--help')
@click.argument('path', type=click.Path())
@click.argument('upload_path', default='root')
@click.argument('local_path', type=click.Path())
@click.argument('remote_path', default='root')
@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('-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)
@click.option('-n', '--no-delete', is_flag=True, help='Do not delete the cloud/local files.')
@click.option('-d', '--delete', is_flag=True, help='Allow deletion of cloud/local files.')
@click.option('-l', '--local', is_flag=True, help='Sync cloud drive files to local.')
@click.pass_context
def sync(ctx, local_path, remote_path, time_out, chunk_size, retry, sync_time, no_delete, delete, local):
kwargs = {}
for i in ctx.args:
if '=' in i:
kwargs[i.split('=')[0]] = i.split('=')[1]
else:
kwargs[i] = True
if local:
commander.sync_local(remote_path, local_path, sync_time, chunk_size, delete, **kwargs)
else:
commander.sync(local_path, remote_path, sync_time, time_out, chunk_size, retry, delete)


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

0 comments on commit e4577bf

Please sign in to comment.