diff --git a/README.md b/README.md index b4120fe..2fb2677 100644 --- a/README.md +++ b/README.md @@ -989,6 +989,7 @@ BaiduPCS-Py save [OPTIONS] SHARED_URL REMOTEDIR | --------------------- | ---------------------------------- | | -p, --password TEXT | 链接密码,如果没有不用设置 | | --no-show-vcode, --NV | 不显示验证码,如果需要验证码则报错 | +| --path-prefix, -pre TEXT | 需要保存的路径前缀 | ## 添加离线下载任务 diff --git a/baidupcs_py/app/app.py b/baidupcs_py/app/app.py index d3260ff..7954dd6 100644 --- a/baidupcs_py/app/app.py +++ b/baidupcs_py/app/app.py @@ -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" @@ -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, ) diff --git a/baidupcs_py/commands/share.py b/baidupcs_py/commands/share.py index a93e8bc..797be39 100644 --- a/baidupcs_py/commands/share.py +++ b/baidupcs_py/commands/share.py @@ -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" @@ -86,6 +87,28 @@ 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: