| 1 | """baijiahao 的命令参数与执行逻辑。""" |
| 2 | from __future__ import annotations |
| 3 | from dataclasses import dataclass |
| 4 | from pathlib import Path |
| 5 | from uploader.baijiahao_uploader.main import ( |
| 6 | BaiJiaHaoVideo, |
| 7 | baijiahao_setup, |
| 8 | cookie_auth as baijiahao_cookie_auth, |
| 9 | ) |
| 10 | from sau_commands.arguments import ( |
| 11 | SCHEDULE_FORMAT, |
| 12 | add_runtime_flags, |
| 13 | existing_file_path, |
| 14 | parse_tags, |
| 15 | resolve_account_file, |
| 16 | ) |
| 17 | |
| 18 | |
| 19 | def register(platform_parsers): |
| 20 | """声明本平台命令、参数及合法选项。""" |
| 21 | schedule_help = SCHEDULE_FORMAT.replace("%", "%%") |
| 22 | baijiahao_parser = platform_parsers.add_parser("baijiahao", help="Baidu Baijiahao operations") |
| 23 | baijiahao_actions = baijiahao_parser.add_subparsers(dest="action", required=True) |
| 24 | |
| 25 | for action_name in ("login", "check"): |
| 26 | action_parser = baijiahao_actions.add_parser(action_name, help=f"Baijiahao {action_name}") |
| 27 | action_parser.add_argument("--account", required=True, help="Baijiahao user-defined account_name") |
| 28 | if action_name == "login": |
| 29 | add_runtime_flags(action_parser) |
| 30 | |
| 31 | baijiahao_upload_video_parser = baijiahao_actions.add_parser("upload-video", help="Upload one video to Baijiahao") |
| 32 | baijiahao_upload_video_parser.add_argument("--account", required=True, help="Baijiahao user-defined account_name") |
| 33 | baijiahao_upload_video_parser.add_argument("--file", required=True, type=existing_file_path, help="Video file path") |
| 34 | baijiahao_upload_video_parser.add_argument("--title", required=True, help="Video title") |
| 35 | baijiahao_upload_video_parser.add_argument("--desc", default="", help="Optional video description") |
| 36 | baijiahao_upload_video_parser.add_argument("--tags", default="", help="Comma-separated tags, such as tag1,tag2") |
| 37 | baijiahao_upload_video_parser.add_argument("--thumbnail", type=existing_file_path, help="Optional cover image path") |
| 38 | baijiahao_upload_video_parser.add_argument("--collection", default=None, help="Optional collection name") |
| 39 | add_runtime_flags(baijiahao_upload_video_parser) |
| 40 | |
| 41 | |
| 42 | @dataclass(slots=True) |
| 43 | class BaijiahaoVideoUploadRequest: |
| 44 | account_name: str |
| 45 | video_file: Path |
| 46 | title: str |
| 47 | description: str |
| 48 | tags: list[str] |
| 49 | thumbnail_file: Path | None = None |
| 50 | collection_name: str | None = None |
| 51 | debug: bool = True |
| 52 | headless: bool = True |
| 53 | |
| 54 | |
| 55 | async def login_baijiahao_account(account_name: str, headless: bool = True, qrcode_callback=None) -> dict: |
| 56 | account_file = resolve_account_file("baijiahao", account_name) |
| 57 | return await baijiahao_setup(str(account_file), handle=True, return_detail=True, headless=headless, qrcode_callback=qrcode_callback) |
| 58 | |
| 59 | |
| 60 | async def check_baijiahao_account(account_name: str) -> bool: |
| 61 | account_file = resolve_account_file("baijiahao", account_name) |
| 62 | if not account_file.exists(): |
| 63 | return False |
| 64 | return await baijiahao_cookie_auth(str(account_file)) |
| 65 | |
| 66 | |
| 67 | async def upload_baijiahao_video(request: BaijiahaoVideoUploadRequest) -> Path: |
| 68 | account_file = resolve_account_file("baijiahao", request.account_name) |
| 69 | is_ready = await baijiahao_setup(str(account_file), handle=False) |
| 70 | if not is_ready: |
| 71 | raise RuntimeError( |
| 72 | f"Baijiahao cookie is missing or expired: {account_file}. Run `sau baijiahao login --account {request.account_name}` first." |
| 73 | ) |
| 74 | |
| 75 | app = BaiJiaHaoVideo( |
| 76 | title=request.title, |
| 77 | file_path=str(request.video_file), |
| 78 | tags=request.tags, |
| 79 | account_file=str(account_file), |
| 80 | desc=request.description, |
| 81 | thumbnail_path=str(request.thumbnail_file) if request.thumbnail_file else None, |
| 82 | collection_name=request.collection_name, |
| 83 | debug=request.debug, |
| 84 | headless=request.headless, |
| 85 | ) |
| 86 | await app.main() |
| 87 | return account_file |
| 88 | |
| 89 | |
| 90 | async def run(args): |
| 91 | """执行本平台命令;发布结果由平台实现核验。""" |
| 92 | if args.action == "login": |
| 93 | result = await login_baijiahao_account(args.account, headless=args.headless) |
| 94 | if not result["success"]: |
| 95 | raise RuntimeError(result["message"]) |
| 96 | print(f"Baijiahao login flow completed: {result['account_file']}") |
| 97 | return 0 |
| 98 | |
| 99 | if args.action == "check": |
| 100 | is_valid = await check_baijiahao_account(args.account) |
| 101 | print("valid" if is_valid else "invalid") |
| 102 | return 0 if is_valid else 1 |
| 103 | |
| 104 | if args.action == "upload-video": |
| 105 | request = BaijiahaoVideoUploadRequest( |
| 106 | account_name=args.account, |
| 107 | video_file=args.file, |
| 108 | title=args.title, |
| 109 | description=args.desc, |
| 110 | tags=parse_tags(args.tags), |
| 111 | thumbnail_file=args.thumbnail, |
| 112 | collection_name=args.collection, |
| 113 | debug=args.debug, |
| 114 | headless=args.headless, |
| 115 | ) |
| 116 | await upload_baijiahao_video(request) |
| 117 | print(f"Baijiahao video upload submitted: {request.video_file}") |
| 118 | return 0 |
| 119 | |
| 120 | raise RuntimeError(f"Unsupported Baijiahao action: {args.action}") |
| 121 |