返回 Social Auto Upload
hupu.py
根目录 / sau_commands / hupu.py
1 """hupu 的命令参数与执行逻辑。"""
2 from __future__ import annotations
3 from dataclasses import dataclass
4 from pathlib import Path
5 from uploader.hupu_uploader.main import HuPuVideo, hupu_setup, cookie_auth as hupu_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 hupu_parser = platform_parsers.add_parser("hupu", help="Hupu (虎扑) operations")
19 hupu_actions = hupu_parser.add_subparsers(dest="action", required=True)
20
21 for action_name in ("login", "check"):
22 action_parser = hupu_actions.add_parser(action_name, help=f"Hupu {action_name}")
23 action_parser.add_argument("--account", required=True, help="Hupu user-defined account_name")
24 if action_name == "login":
25 add_runtime_flags(action_parser)
26
27 hupu_upload_video_parser = hupu_actions.add_parser("upload-video", help="Upload one video to Hupu")
28 hupu_upload_video_parser.add_argument("--account", required=True, help="Hupu user-defined account_name")
29 hupu_upload_video_parser.add_argument("--file", required=True, type=existing_file_path, help="Video file path")
30 hupu_upload_video_parser.add_argument("--title", required=True, help="Video title (4-40 chars)")
31 hupu_upload_video_parser.add_argument("--desc", default="", help="Optional video description")
32 hupu_upload_video_parser.add_argument("--tags", default="", help="Comma-separated tags, such as tag1,tag2")
33 hupu_upload_video_parser.add_argument("--thumbnail", type=existing_file_path, help="Optional cover image path")
34 add_runtime_flags(hupu_upload_video_parser)
35
36
37 @dataclass(slots=True)
38 class HupuVideoUploadRequest:
39 account_name: str
40 video_file: Path
41 title: str
42 description: str
43 tags: list[str]
44 thumbnail_file: Path | None = None
45 debug: bool = True
46 headless: bool = True
47
48
49 async def login_hupu_account(account_name: str, headless: bool = False, qrcode_callback=None) -> dict:
50 account_file = resolve_account_file("hupu", account_name)
51 return await hupu_setup(str(account_file), handle=True, return_detail=True, headless=headless, qrcode_callback=qrcode_callback)
52
53
54 async def check_hupu_account(account_name: str) -> bool:
55 account_file = resolve_account_file("hupu", account_name)
56 if not account_file.exists():
57 return False
58 return await hupu_cookie_auth(str(account_file))
59
60
61 async def upload_hupu_video(request: HupuVideoUploadRequest) -> Path:
62 account_file = resolve_account_file("hupu", request.account_name)
63 is_ready = await hupu_setup(str(account_file), handle=False)
64 if not is_ready:
65 raise RuntimeError(
66 f"Hupu cookie is missing or expired: {account_file}. Run `sau hupu login --account {request.account_name}` first."
67 )
68
69 app = HuPuVideo(
70 title=request.title,
71 file_path=str(request.video_file),
72 tags=request.tags,
73 account_file=str(account_file),
74 desc=request.description,
75 thumbnail_path=str(request.thumbnail_file) if request.thumbnail_file else None,
76 debug=request.debug,
77 headless=request.headless,
78 )
79 await app.main()
80 return account_file
81
82
83 async def run(args):
84 """执行本平台命令;发布结果由平台实现核验。"""
85 if args.action == "login":
86 result = await login_hupu_account(args.account, headless=args.headless)
87 if not result["success"]:
88 raise RuntimeError(result["message"])
89 print(f"Hupu login flow completed: {result['account_file']}")
90 return 0
91
92 if args.action == "check":
93 is_valid = await check_hupu_account(args.account)
94 print("valid" if is_valid else "invalid")
95 return 0 if is_valid else 1
96
97 if args.action == "upload-video":
98 request = HupuVideoUploadRequest(
99 account_name=args.account,
100 video_file=args.file,
101 title=args.title,
102 description=args.desc,
103 tags=parse_tags(args.tags),
104 thumbnail_file=args.thumbnail,
105 debug=args.debug,
106 headless=args.headless,
107 )
108 await upload_hupu_video(request)
109 print(f"Hupu video upload submitted: {request.video_file}")
110 return 0
111
112 raise RuntimeError(f"Unsupported Hupu action: {args.action}")
113
113 lines PYTHON