Esempio n. 1
0
def save_shared(
    api: BaiduPCSApi,
    shared_url: str,
    remotedir: str,
    password=Optional[str],
    show_vcode: bool = True,
):
    assert remotedir.startswith("/"), "`remotedir` must be an absolute path"

    # Vertify with password
    if password:
        api.access_shared(shared_url, password, show_vcode=show_vcode)

    shared_paths = deque(api.shared_paths(shared_url))

    # Record the remotedir of each shared_path
    _remotedirs: Dict[PcsSharedPath, str] = {}
    for sp in shared_paths:
        _remotedirs[sp] = remotedir

    _dir_exists: Set[str] = set()

    while shared_paths:
        shared_path = shared_paths.popleft()
        uk, share_id, bdstoken = (
            shared_path.uk,
            shared_path.share_id,
            shared_path.bdstoken,
        )
        assert uk
        assert share_id
        assert bdstoken

        rd = _remotedirs[shared_path]
        if rd not in _dir_exists and not api.exists(rd):
            api.makedir(rd)
            _dir_exists.add(rd)

        # rd = (Path(_remotedirs[shared_path]) / os.path.basename(shared_path.path)).as_posix()
        try:
            api.transfer_shared_paths(rd, [shared_path.fs_id], uk, share_id,
                                      bdstoken, shared_url)
            print(f"save: {shared_path.path} to {rd}")
            continue
        except BaiduPCSError as err:
            if err.error_code not in (12, -33):
                raise err

            if err.error_code == 12:  # -33: '一次支持操作999个,减点试试吧'
                print(f"[yellow]WARNING[/]: {shared_path.path} has be in {rd}")
            if err.error_code == -33:  # -33: '一次支持操作999个,减点试试吧'
                print(f"[yellow]WARNING[/]: {shared_path.path} "
                      "has more items and need to transfer one by one")

        sub_paths = api.list_shared_paths(shared_path.path, uk, share_id,
                                          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])
Esempio n. 2
0
def list_shared_paths(
    api: BaiduPCSApi,
    shared_url: str,
    password: Optional[str] = None,
    show_vcode: bool = True,
):
    # Vertify with password
    if password:
        api.access_shared(shared_url, password, show_vcode=show_vcode)

    all_shared_paths: List[PcsSharedPath] = []

    shared_paths = deque(api.shared_paths(shared_url))
    all_shared_paths += shared_paths

    while shared_paths:
        shared_path = shared_paths.popleft()

        uk, share_id, bdstoken = (
            shared_path.uk,
            shared_path.share_id,
            shared_path.bdstoken,
        )
        assert uk
        assert share_id
        assert bdstoken

        if shared_path.is_dir:
            # Take all sub paths
            sub_paths = list_all_sub_paths(api, shared_path.path, uk, share_id,
                                           bdstoken)
            all_shared_paths += sub_paths
            shared_paths.extendleft(sub_paths[::-1])

    display_shared_paths(*all_shared_paths)