| 1 | """tencent 的命令参数与执行逻辑。""" |
| 2 | from __future__ import annotations |
| 3 | from dataclasses import dataclass |
| 4 | from datetime import datetime |
| 5 | from pathlib import Path |
| 6 | from uploader.tencent_uploader.main import ( |
| 7 | TENCENT_PUBLISH_STRATEGY_IMMEDIATE, |
| 8 | TENCENT_PUBLISH_STRATEGY_SCHEDULED, |
| 9 | TencentVideo, |
| 10 | cookie_auth as tencent_cookie_auth, |
| 11 | tencent_setup, |
| 12 | ) |
| 13 | from sau_commands.arguments import ( |
| 14 | SCHEDULE_FORMAT, |
| 15 | add_runtime_flags, |
| 16 | existing_file_path, |
| 17 | parse_tags, |
| 18 | resolve_account_file, |
| 19 | schedule_value, |
| 20 | ) |
| 21 | |
| 22 | |
| 23 | def register(platform_parsers): |
| 24 | """声明本平台命令、参数及合法选项。""" |
| 25 | schedule_help = SCHEDULE_FORMAT.replace("%", "%%") |
| 26 | tencent_parser = platform_parsers.add_parser("tencent", help="Tencent/WeChat Channels operations") |
| 27 | tencent_actions = tencent_parser.add_subparsers(dest="action", required=True) |
| 28 | |
| 29 | for action_name in ("login", "check"): |
| 30 | action_parser = tencent_actions.add_parser(action_name, help=f"Tencent/WeChat Channels {action_name}") |
| 31 | action_parser.add_argument("--account", required=True, help="Tencent user-defined account_name") |
| 32 | if action_name == "login": |
| 33 | add_runtime_flags(action_parser) |
| 34 | |
| 35 | tencent_upload_video_parser = tencent_actions.add_parser("upload-video", help="Upload one video to WeChat Channels") |
| 36 | tencent_upload_video_parser.add_argument("--account", required=True, help="Tencent user-defined account_name") |
| 37 | tencent_upload_video_parser.add_argument("--file", required=True, type=existing_file_path, help="Video file path") |
| 38 | tencent_upload_video_parser.add_argument("--title", required=True, help="Video title") |
| 39 | tencent_upload_video_parser.add_argument("--desc", default="", help="Optional video description") |
| 40 | tencent_upload_video_parser.add_argument("--tags", default="", help="Comma-separated tags, such as tag1,tag2") |
| 41 | tencent_upload_video_parser.add_argument("--schedule", type=schedule_value, help=f"Schedule time in {schedule_help}") |
| 42 | tencent_upload_video_parser.add_argument("--thumbnail", type=existing_file_path, help="Optional 3:4 portrait thumbnail path") |
| 43 | tencent_upload_video_parser.add_argument("--thumbnail-landscape", type=existing_file_path, help="Optional 4:3 landscape thumbnail path") |
| 44 | tencent_upload_video_parser.add_argument("--thumbnail-portrait", type=existing_file_path, help="Optional 3:4 portrait thumbnail path") |
| 45 | tencent_upload_video_parser.add_argument("--short-title", help="Optional WeChat Channels short title") |
| 46 | tencent_upload_video_parser.add_argument("--category", help="Optional original content category") |
| 47 | tencent_upload_video_parser.add_argument("--draft", action="store_true", help="Save as draft instead of publishing") |
| 48 | tencent_upload_video_parser.add_argument("--collection", default=None, help="Optional collection name to add the work into (must already exist)") |
| 49 | add_runtime_flags(tencent_upload_video_parser) |
| 50 | |
| 51 | |
| 52 | @dataclass(slots=True) |
| 53 | class TencentVideoUploadRequest: |
| 54 | account_name: str |
| 55 | video_file: Path |
| 56 | title: str |
| 57 | description: str |
| 58 | tags: list[str] |
| 59 | publish_date: datetime | int |
| 60 | thumbnail_file: Path | None = None |
| 61 | thumbnail_landscape_file: Path | None = None |
| 62 | thumbnail_portrait_file: Path | None = None |
| 63 | short_title: str | None = None |
| 64 | category: str | None = None |
| 65 | is_draft: bool = False |
| 66 | publish_strategy: str = TENCENT_PUBLISH_STRATEGY_IMMEDIATE |
| 67 | debug: bool = True |
| 68 | headless: bool = True |
| 69 | collection_name: str | None = None |
| 70 | |
| 71 | |
| 72 | async def login_tencent_account(account_name: str, headless: bool = True) -> dict: |
| 73 | account_file = resolve_account_file("tencent", account_name) |
| 74 | return await tencent_setup(str(account_file), handle=True, return_detail=True, headless=headless) |
| 75 | |
| 76 | |
| 77 | async def check_tencent_account(account_name: str) -> bool: |
| 78 | account_file = resolve_account_file("tencent", account_name) |
| 79 | if not account_file.exists(): |
| 80 | return False |
| 81 | return await tencent_cookie_auth(str(account_file)) |
| 82 | |
| 83 | |
| 84 | async def upload_tencent_video(request: TencentVideoUploadRequest) -> Path: |
| 85 | account_file = resolve_account_file("tencent", request.account_name) |
| 86 | is_ready = await tencent_setup(str(account_file), handle=False) |
| 87 | if not is_ready: |
| 88 | raise RuntimeError( |
| 89 | f"Tencent/WeChat Channels cookie is missing or expired: {account_file}. " |
| 90 | f"Run `sau tencent login --account {request.account_name}` first." |
| 91 | ) |
| 92 | |
| 93 | app = TencentVideo( |
| 94 | title=request.title, |
| 95 | file_path=str(request.video_file), |
| 96 | tags=request.tags, |
| 97 | publish_date=request.publish_date, |
| 98 | account_file=str(account_file), |
| 99 | category=request.category, |
| 100 | is_draft=request.is_draft, |
| 101 | desc=request.description, |
| 102 | thumbnail_path=str(request.thumbnail_file) if request.thumbnail_file else None, |
| 103 | thumbnail_landscape_path=( |
| 104 | str(request.thumbnail_landscape_file) if request.thumbnail_landscape_file else None |
| 105 | ), |
| 106 | thumbnail_portrait_path=( |
| 107 | str(request.thumbnail_portrait_file) if request.thumbnail_portrait_file else None |
| 108 | ), |
| 109 | short_title=request.short_title, |
| 110 | publish_strategy=request.publish_strategy, |
| 111 | debug=request.debug, |
| 112 | headless=request.headless, |
| 113 | collection_name=request.collection_name, |
| 114 | ) |
| 115 | await app.tencent_upload_video() |
| 116 | return account_file |
| 117 | |
| 118 | |
| 119 | async def run(args): |
| 120 | """执行本平台命令;发布结果由平台实现核验。""" |
| 121 | if args.action == "login": |
| 122 | result = await login_tencent_account(args.account, headless=args.headless) |
| 123 | if not result["success"]: |
| 124 | raise RuntimeError(result["message"]) |
| 125 | print(f"Tencent/WeChat Channels login flow completed: {result['account_file']}") |
| 126 | return 0 |
| 127 | |
| 128 | if args.action == "check": |
| 129 | is_valid = await check_tencent_account(args.account) |
| 130 | print("valid" if is_valid else "invalid") |
| 131 | return 0 if is_valid else 1 |
| 132 | |
| 133 | publish_strategy = TENCENT_PUBLISH_STRATEGY_SCHEDULED if args.schedule else TENCENT_PUBLISH_STRATEGY_IMMEDIATE |
| 134 | |
| 135 | if args.action == "upload-video": |
| 136 | request = TencentVideoUploadRequest( |
| 137 | account_name=args.account, |
| 138 | video_file=args.file, |
| 139 | title=args.title, |
| 140 | description=args.desc, |
| 141 | tags=parse_tags(args.tags), |
| 142 | publish_date=args.schedule or 0, |
| 143 | thumbnail_file=args.thumbnail, |
| 144 | thumbnail_landscape_file=args.thumbnail_landscape, |
| 145 | thumbnail_portrait_file=args.thumbnail_portrait, |
| 146 | short_title=args.short_title, |
| 147 | category=args.category, |
| 148 | is_draft=args.draft, |
| 149 | publish_strategy=publish_strategy, |
| 150 | debug=args.debug, |
| 151 | headless=args.headless, |
| 152 | collection_name=args.collection, |
| 153 | ) |
| 154 | await upload_tencent_video(request) |
| 155 | print(f"Tencent/WeChat Channels video upload submitted: {request.video_file}") |
| 156 | return 0 |
| 157 | |
| 158 | raise RuntimeError(f"Unsupported Tencent/WeChat Channels action: {args.action}") |
| 159 |