Skip to content

Commit 2d80b02

Browse files
committed
Support to set and show account name
1 parent 29b4658 commit 2d80b02

1 file changed

Lines changed: 42 additions & 19 deletions

File tree

baidupcs_py/app/app.py

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ def _user_ids(ctx) -> Optional[List[int]]:
146146
"""Select use_ids by their name probes"""
147147

148148
am = ctx.obj.account_manager
149-
user_name_probes = ctx.obj.users
149+
account_name_probes = ctx.obj.accounts
150150

151151
user_ids = []
152152
for user_id, account in am._accounts.items():
153-
user_name = account.user.user_name
154-
for probe in user_name_probes:
155-
if probe in user_name:
153+
account_name = account.account_name
154+
for probe in account_name_probes:
155+
if probe in account_name:
156156
user_ids.append(user_id)
157157
break
158158
return user_ids
@@ -166,7 +166,7 @@ def _change_account(ctx, user_id: int):
166166

167167

168168
def multi_user_do(func):
169-
"""Run command on multi users"""
169+
"""Run command on multi accounts"""
170170

171171
@wraps(func)
172172
def wrap(*args, **kwargs):
@@ -181,10 +181,10 @@ def wrap(*args, **kwargs):
181181
if not accout:
182182
continue
183183

184-
user_name = accout.user.user_name
184+
account_name = accout.account_name
185185
print(
186186
"[i yellow]@Do[/i yellow]: "
187-
f"user_name: [b]{user_name}[/b], "
187+
f"account_name: [b]{account_name}[/b], "
188188
f"user_id: [b]{user_id}[/b]"
189189
)
190190
_change_account(ctx, user_id)
@@ -270,6 +270,7 @@ def _rapiduploadinfo_file(ctx) -> str:
270270
"ua": "useradd",
271271
"ud": "userdel",
272272
"ep": "encryptpwd",
273+
"an": "accountname",
273274
# File Operations
274275
"l": "ls",
275276
"f": "search",
@@ -348,12 +349,12 @@ def list_commands(self, ctx):
348349
default=RAPIDUPLOADINFO_PATH,
349350
help="秒传 sqlite3 文件",
350351
)
351-
@click.option("--users", "-u", type=str, default=None, help="用户名片段,用“,”分割")
352+
@click.option("--accounts", "-u", type=str, default=None, help="帐号名片段,用“,”分割")
352353
@click.pass_context
353-
def app(ctx, account_data, rapiduploadinfo_file, users):
354+
def app(ctx, account_data, rapiduploadinfo_file, accounts):
354355
ctx.obj.account_manager = AccountManager.load_data(account_data)
355356
ctx.obj.rapiduploadinfo_file = str(Path(rapiduploadinfo_file).expanduser())
356-
ctx.obj.users = [] if users is None else users.split(",")
357+
ctx.obj.accounts = [] if accounts is None else accounts.split(",")
357358

358359

359360
# Account
@@ -411,7 +412,7 @@ def su(ctx, user_index):
411412
"""切换当前用户"""
412413

413414
am = ctx.obj.account_manager
414-
ls = sorted([(a.user, a.pwd) for a in am.accounts])
415+
ls = sorted([(a.user, a.pwd, a.account_name) for a in am.accounts])
415416
display_user_infos(*ls, recent_user_id=am._who)
416417

417418
if user_index:
@@ -438,18 +439,21 @@ def userlist(ctx):
438439
"""显示所有用户"""
439440

440441
am = ctx.obj.account_manager
441-
ls = sorted([(a.user, a.pwd) for a in am.accounts])
442+
ls = sorted([(a.user, a.pwd, a.account_name) for a in am.accounts])
442443
display_user_infos(*ls, recent_user_id=am._who)
443444

444445

445446
@app.command()
446-
@click.option("--bduss", prompt="bduss", hide_input=True, default="", help="用户 bduss")
447447
@click.option(
448-
"--cookies", prompt="cookies", hide_input=True, default="", help="用户 cookies"
448+
"--account_name", prompt="Account Name", hide_input=False, default="", help="账号名"
449+
)
450+
@click.option("--bduss", prompt="BDUSS", hide_input=True, default="", help="用户 BDUSS")
451+
@click.option(
452+
"--cookies", prompt="Cookies", hide_input=True, default="", help="用户 Cookies"
449453
)
450454
@click.pass_context
451455
@handle_error
452-
def useradd(ctx, bduss, cookies):
456+
def useradd(ctx, account_name, bduss, cookies):
453457
"""添加一个用户并设置为当前用户"""
454458

455459
if cookies:
@@ -459,9 +463,9 @@ def useradd(ctx, bduss, cookies):
459463
cookies = {}
460464
if not bduss:
461465
raise ValueError("bduss must be specified or be included in cookie")
462-
account = Account.from_bduss(bduss, cookies=cookies)
466+
account = Account.from_bduss(bduss, cookies=cookies, account_name=account_name)
463467
am = ctx.obj.account_manager
464-
am.useradd(account.user)
468+
am.add_account(account)
465469
am.su(account.user.user_id)
466470
am.save()
467471

@@ -473,7 +477,7 @@ def userdel(ctx):
473477
"""删除一个用户"""
474478

475479
am = ctx.obj.account_manager
476-
ls = sorted([(a.user, a.pwd) for a in am.accounts])
480+
ls = sorted([(a.user, a.pwd, a.account_name) for a in am.accounts])
477481
display_user_infos(*ls, recent_user_id=am._who)
478482

479483
indexes = list(str(idx) for idx in range(1, len(ls) + 1))
@@ -482,7 +486,7 @@ def userdel(ctx):
482486
return
483487

484488
user_id = ls[int(i) - 1][0].user_id
485-
am.userdel(user_id)
489+
am.delete_account(user_id)
486490
am.save()
487491

488492
print(f"Delete user {user_id}")
@@ -511,6 +515,25 @@ def encryptpwd(ctx, encrypt_password, salt):
511515
am.save()
512516

513517

518+
@app.command()
519+
@click.option(
520+
"--account-name",
521+
"--an",
522+
prompt="Account Name",
523+
hide_input=False,
524+
help="设置账号名,用于指定运行帐号和显示",
525+
)
526+
@click.option("--user-id", "-i", type=int, help="指定用户id")
527+
@click.pass_context
528+
@handle_error
529+
def accountname(ctx, account_name, user_id):
530+
"""设置账号名"""
531+
532+
am = ctx.obj.account_manager
533+
am.set_account_name(account_name, user_id)
534+
am.save()
535+
536+
514537
@app.command()
515538
@click.argument("remotedir", type=str, default="/", required=False)
516539
@click.pass_context

0 commit comments

Comments
 (0)