Skip to content

Commit 29b4658

Browse files
committed
Handle account name
1 parent efc7c5e commit 29b4658

1 file changed

Lines changed: 38 additions & 6 deletions

File tree

baidupcs_py/app/account.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class Account(NamedTuple):
2121
# Here it useless.
2222
salt: Optional[str] = None
2323

24+
# Account Name which can be set by hand
25+
account_name: str = ""
26+
2427
def pcsapi(self) -> BaiduPCSApi:
2528
auth = self.user.auth
2629
assert auth, f"{self}.user.auth is None"
@@ -33,10 +36,14 @@ def pcsapi(self) -> BaiduPCSApi:
3336
)
3437

3538
@staticmethod
36-
def from_bduss(bduss: str, cookies: Dict[str, Optional[str]] = {}) -> "Account":
39+
def from_bduss(
40+
bduss: str,
41+
cookies: Dict[str, Optional[str]] = {},
42+
account_name: str = "",
43+
) -> "Account":
3744
api = BaiduPCSApi(bduss=bduss, cookies=cookies)
3845
user = api.user_info()
39-
return Account(user=user)
46+
return Account(user=user, account_name=account_name or user.user_name or "")
4047

4148

4249
class AccountManager:
@@ -66,6 +73,20 @@ def accounts(self) -> List[Account]:
6673

6774
return list(self._accounts.values())
6875

76+
def set_account_name(self, account_name: str, user_id: int = None):
77+
"""Set account name"""
78+
79+
user_id = user_id or self._who
80+
81+
assert user_id, "No recent user"
82+
83+
account = self._accounts.get(user_id)
84+
85+
assert account
86+
87+
account = account._replace(account_name=account_name)
88+
self._accounts[user_id] = account
89+
6990
def set_encrypt_password(
7091
self, encrypt_password: Optional[str] = None, salt: Optional[str] = None
7192
):
@@ -139,12 +160,12 @@ def su(self, user_id: int):
139160

140161
self._who = user_id
141162

142-
def useradd(self, user: PcsUser):
143-
"""Add an user to data"""
163+
def add_account(self, account: Account):
164+
"""Add an account to the manager"""
144165

145-
self._accounts[user.user_id] = Account(user=user)
166+
self._accounts[account.user.user_id] = account
146167

147-
def userdel(self, user_id: int):
168+
def delete_account(self, user_id: int):
148169
"""Delete a user
149170
150171
Args:
@@ -172,6 +193,7 @@ def save(self, data_path: Optional[PathLike] = None):
172193
def _compat_account_manager(am: AccountManager):
173194
"""Update stored account manager to compat current version"""
174195
_compat_v0_5_9(am)
196+
_set_account_names(am)
175197

176198

177199
def _compat_v0_5_9(am: AccountManager):
@@ -189,3 +211,13 @@ def _compat_v0_5_9(am: AccountManager):
189211
am._data_path = ACCOUNT_DATA_PATH
190212

191213
am.save()
214+
215+
216+
def _set_account_names(am: AccountManager):
217+
"""Set account's name with its user's name if that is empty"""
218+
219+
for user_id, account in am._accounts.items():
220+
if not account.account_name:
221+
am._accounts[user_id] = account._replace(
222+
account_name=account.user.user_name or ""
223+
)

0 commit comments

Comments
 (0)