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