Showing
1 changed file
with
27 additions
and
4 deletions
| @@ -384,11 +384,19 @@ def cmd_login(args: argparse.Namespace) -> None: | @@ -384,11 +384,19 @@ def cmd_login(args: argparse.Namespace) -> None: | ||
| 384 | 384 | ||
| 385 | def cmd_phone_login(args: argparse.Namespace) -> None: | 385 | def cmd_phone_login(args: argparse.Namespace) -> None: |
| 386 | """手机号+验证码登录(适用于无界面服务器)。""" | 386 | """手机号+验证码登录(适用于无界面服务器)。""" |
| 387 | + from xhs.errors import RateLimitError | ||
| 387 | from xhs.login import send_phone_code, submit_phone_code | 388 | from xhs.login import send_phone_code, submit_phone_code |
| 388 | 389 | ||
| 389 | browser, page = _connect(args) | 390 | browser, page = _connect(args) |
| 390 | try: | 391 | try: |
| 391 | sent = send_phone_code(page, args.phone) | 392 | sent = send_phone_code(page, args.phone) |
| 393 | + except RateLimitError: | ||
| 394 | + # 频率限制——直接切换二维码登录 | ||
| 395 | + logger.info("验证码发送受限,切换为二维码登录") | ||
| 396 | + _qrcode_fallback(browser, page, args) | ||
| 397 | + return | ||
| 398 | + | ||
| 399 | + try: | ||
| 392 | if not sent: | 400 | if not sent: |
| 393 | _output({"logged_in": True, "message": "已登录,无需重新登录"}) | 401 | _output({"logged_in": True, "message": "已登录,无需重新登录"}) |
| 394 | return | 402 | return |
| @@ -396,7 +404,13 @@ def cmd_phone_login(args: argparse.Namespace) -> None: | @@ -396,7 +404,13 @@ def cmd_phone_login(args: argparse.Namespace) -> None: | ||
| 396 | # 输出提示,等待用户在终端输入验证码 | 404 | # 输出提示,等待用户在终端输入验证码 |
| 397 | print( | 405 | print( |
| 398 | json.dumps( | 406 | json.dumps( |
| 399 | - {"status": "code_sent", "message": f"验证码已发送至 {args.phone[:3]}****{args.phone[-4:]}"}, | 407 | + { |
| 408 | + "status": "code_sent", | ||
| 409 | + "message": ( | ||
| 410 | + f"验证码已发送至 " | ||
| 411 | + f"{args.phone[:3]}****{args.phone[-4:]}" | ||
| 412 | + ), | ||
| 413 | + }, | ||
| 400 | ensure_ascii=False, | 414 | ensure_ascii=False, |
| 401 | ), | 415 | ), |
| 402 | flush=True, | 416 | flush=True, |
| @@ -409,16 +423,25 @@ def cmd_phone_login(args: argparse.Namespace) -> None: | @@ -409,16 +423,25 @@ def cmd_phone_login(args: argparse.Namespace) -> None: | ||
| 409 | try: | 423 | try: |
| 410 | code = input("请输入验证码: ").strip() | 424 | code = input("请输入验证码: ").strip() |
| 411 | except EOFError: | 425 | except EOFError: |
| 412 | - _output({"success": False, "error": "未收到验证码输入"}, exit_code=2) | 426 | + _output( |
| 427 | + {"success": False, "error": "未收到验证码输入"}, | ||
| 428 | + exit_code=2, | ||
| 429 | + ) | ||
| 413 | return | 430 | return |
| 414 | 431 | ||
| 415 | if not code: | 432 | if not code: |
| 416 | - _output({"success": False, "error": "验证码不能为空"}, exit_code=2) | 433 | + _output( |
| 434 | + {"success": False, "error": "验证码不能为空"}, | ||
| 435 | + exit_code=2, | ||
| 436 | + ) | ||
| 417 | return | 437 | return |
| 418 | 438 | ||
| 419 | success = submit_phone_code(page, code) | 439 | success = submit_phone_code(page, code) |
| 420 | _output( | 440 | _output( |
| 421 | - {"logged_in": success, "message": "登录成功" if success else "验证码错误或超时"}, | 441 | + { |
| 442 | + "logged_in": success, | ||
| 443 | + "message": "登录成功" if success else "验证码错误或超时", | ||
| 444 | + }, | ||
| 422 | exit_code=0 if success else 2, | 445 | exit_code=0 if success else 2, |
| 423 | ) | 446 | ) |
| 424 | finally: | 447 | finally: |
-
Please register or login to post a comment