From e1d1a130f6d6412d73f86534b1f5020358d8726d Mon Sep 17 00:00:00 2001 From: himono <1418730576@qq.com> Date: Sun, 2 Jun 2024 02:05:41 +0800 Subject: [PATCH 1/3] feat: add support for saving shared links with a path prefix --- baidupcs_py/app/app.py | 4 +++- baidupcs_py/commands/share.py | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/baidupcs_py/app/app.py b/baidupcs_py/app/app.py index d3260ff..2c2e9c0 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..659a08a 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,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: From 8dd30e8bff83393658fb2fb3540e99fba339e5d2 Mon Sep 17 00:00:00 2001 From: shengzu-xue Date: Tue, 18 Jun 2024 14:16:54 +0800 Subject: [PATCH 2/3] format style --- baidupcs_py/app/app.py | 2 +- baidupcs_py/commands/share.py | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/baidupcs_py/app/app.py b/baidupcs_py/app/app.py index 2c2e9c0..7954dd6 100644 --- a/baidupcs_py/app/app.py +++ b/baidupcs_py/app/app.py @@ -1602,7 +1602,7 @@ def listsharedpaths(ctx, shared_url, password, no_show_vcode): @click.pass_context @handle_error @multi_user_do -def save(ctx, shared_url, remotedir, password, path_prefix ,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" diff --git a/baidupcs_py/commands/share.py b/baidupcs_py/commands/share.py index 659a08a..797be39 100644 --- a/baidupcs_py/commands/share.py +++ b/baidupcs_py/commands/share.py @@ -64,7 +64,7 @@ def save_shared( shared_url: str, remotedir: str, password: Optional[str] = None, - path_prefix: str|None = None, + path_prefix: str | None = None, show_vcode: bool = True, ): assert remotedir.startswith("/"), "`remotedir` must be an absolute path" @@ -91,12 +91,14 @@ def save_shared( real_path = shared_path.path if shared_path.path.startswith("/sharelink"): # delete "/sharelinkxxxxxx - real_path = shared_path.path[shared_path.path.find("/", 1):] + 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) + 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 From 5c2df158960b39c2ad6c7b8fc452c7df1053af76 Mon Sep 17 00:00:00 2001 From: shengzu-xue Date: Tue, 18 Jun 2024 14:29:34 +0800 Subject: [PATCH 3/3] feat: add support for saving shared links with a path prefix --- README.md | 1 + 1 file changed, 1 insertion(+) 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 | 需要保存的路径前缀 | ## 添加离线下载任务