Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for saving shared links with a path prefix #127

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,7 @@ BaiduPCS-Py save [OPTIONS] SHARED_URL REMOTEDIR
| --------------------- | ---------------------------------- |
| -p, --password TEXT | 链接密码,如果没有不用设置 |
| --no-show-vcode, --NV | 不显示验证码,如果需要验证码则报错 |
| --path-prefix, -pre TEXT | 需要保存的路径前缀 |

## 添加离线下载任务

Expand Down
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
23 changes: 23 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,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:
Expand Down