Skip to content

Commit

Permalink
feat: add support for saving shared links with a path prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
himono committed Jun 1, 2024
1 parent e81e9b6 commit e1d1a13
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion baidupcs_py/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1597,11 +1597,12 @@ def listsharedpaths(ctx, shared_url, password, no_show_vcode):
@click.argument("shared_url", nargs=1, type=str)
@click.argument("remotedir", nargs=1, type=str)
@click.option("--password", "-p", type=str, help="链接密码,如果没有不用设置")
@click.option("--path-prefix", "-pre", type=str, help="需要保存的路径前缀")
@click.option("--no-show-vcode", "--NV", is_flag=True, help="不显示验证码")
@click.pass_context
@handle_error
@multi_user_do
def save(ctx, shared_url, remotedir, password, no_show_vcode):
def save(ctx, shared_url, remotedir, password, path_prefix ,no_show_vcode):
"""保存其他用户分享的链接"""

assert not password or len(password) == 4, "`password` must be 4 letters"
Expand All @@ -1618,6 +1619,7 @@ def save(ctx, shared_url, remotedir, password, no_show_vcode):
shared_url,
remotedir,
password=password,
path_prefix=path_prefix,
show_vcode=not no_show_vcode,
)

Expand Down
21 changes: 21 additions & 0 deletions baidupcs_py/commands/share.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def save_shared(
shared_url: str,
remotedir: str,
password: Optional[str] = None,
path_prefix: str|None = None,
show_vcode: bool = True,
):
assert remotedir.startswith("/"), "`remotedir` must be an absolute path"
Expand All @@ -86,6 +87,26 @@ def save_shared(
while shared_paths:
shared_path = shared_paths.popleft()
rd = _remotedirs[shared_path]
if path_prefix:
real_path = shared_path.path
if shared_path.path.startswith("/sharelink"):
# delete "/sharelinkxxxxxx
real_path = shared_path.path[shared_path.path.find("/", 1):]
if shared_path.is_dir:
if real_path.startswith(path_prefix):
pass
elif path_prefix.startswith(real_path):
sub_paths = list_all_sub_paths(api, shared_path.path, shared_path.uk, shared_path.share_id, shared_path.bdstoken)
rd = (Path(rd) / os.path.basename(shared_path.path)).as_posix()
for sp in sub_paths:
_remotedirs[sp] = rd
shared_paths.extendleft(sub_paths[::-1])
continue
else:
continue
else:
if not real_path.startswith(path_prefix):
continue

# Make sure remote dir exists
if rd not in _dir_exists:
Expand Down

0 comments on commit e1d1a13

Please sign in to comment.