| 1 | """dayu 的命令参数与执行逻辑。""" |
| 2 | from __future__ import annotations |
| 3 | import json |
| 4 | from patchright.async_api import async_playwright |
| 5 | from conf import LOCAL_CHROME_PATH |
| 6 | from utils.article_render import ARTICLE_THEMES |
| 7 | from uploader.dayu_uploader.article import DayuArticle |
| 8 | from uploader.dayu_uploader.constants import ( |
| 9 | DAYU_SOURCE_TYPES, |
| 10 | DAYU_VIDEO_CATEGORIES, |
| 11 | DAYU_SCHEDULE_MIN_HOURS, |
| 12 | DAYU_SCHEDULE_MAX_DAYS, |
| 13 | DAYU_SCHEDULE_STEP_MINUTES, |
| 14 | ) |
| 15 | from uploader.dayu_uploader.main import DayuVideo |
| 16 | from uploader.dayu_uploader.session import ( |
| 17 | DAYU_HOME_URL, |
| 18 | check_dayu_account, |
| 19 | load_dayu_identity, |
| 20 | login_dayu_account, |
| 21 | read_dayu_identity, |
| 22 | ) |
| 23 | from uploader.dayu_uploader.verification import verify_dayu_article, verify_dayu_video |
| 24 | from sau_commands.arguments import ( |
| 25 | SCHEDULE_FORMAT, |
| 26 | add_runtime_flags, |
| 27 | existing_file_path, |
| 28 | parse_tags, |
| 29 | resolve_account_file, |
| 30 | schedule_value, |
| 31 | ) |
| 32 | |
| 33 | |
| 34 | def register(platform_parsers): |
| 35 | """声明本平台命令、参数及合法选项。""" |
| 36 | schedule_help = SCHEDULE_FORMAT.replace("%", "%%") |
| 37 | dayu_parser = platform_parsers.add_parser("dayu", help="Dayu (大鱼号) video publishing and verification") |
| 38 | dayu_actions = dayu_parser.add_subparsers(dest="action", required=True) |
| 39 | dayu_login = dayu_actions.add_parser("login", help="Log in manually with visible Chrome") |
| 40 | dayu_login.add_argument("--account", required=True, help="Local account alias") |
| 41 | dayu_login.add_argument("--expected-name", help="Expected name displayed by Dayu") |
| 42 | dayu_login.add_argument("--timeout", type=int, default=600, help="Login timeout in seconds") |
| 43 | dayu_check = dayu_actions.add_parser("check", help="Check login and bound Dayu identity") |
| 44 | dayu_check.add_argument("--account", required=True) |
| 45 | dayu_upload = dayu_actions.add_parser("upload-video", help="Publish a video; succeed when Dayu confirms submission") |
| 46 | dayu_upload.add_argument("--account", required=True) |
| 47 | dayu_upload.add_argument("--file", required=True, type=existing_file_path) |
| 48 | dayu_upload.add_argument("--title", required=True, help="Title, 5-60 Chinese character units") |
| 49 | dayu_upload.add_argument("--desc", default="") |
| 50 | dayu_upload.add_argument("--tags", default="", help="Comma-separated tags") |
| 51 | dayu_upload.add_argument("--category", required=True, choices=DAYU_VIDEO_CATEGORIES, help="视频分类,按页面选项选择") |
| 52 | dayu_upload.add_argument("--source", required=True, choices=DAYU_SOURCE_TYPES, help="按实际内容选择信息来源") |
| 53 | dayu_upload.add_argument("--source-url", default="", help="转载时必填的 http/https 原文链接") |
| 54 | dayu_upload.add_argument("--thumbnail", required=True, type=existing_file_path) |
| 55 | dayu_upload.add_argument("--schedule", type=schedule_value, |
| 56 | help=f"{schedule_help},北京时间,{DAYU_SCHEDULE_MIN_HOURS}小时后至{DAYU_SCHEDULE_MAX_DAYS}天内,分钟为{DAYU_SCHEDULE_STEP_MINUTES}的倍数") |
| 57 | dayu_upload.add_argument("--prepare-only", action="store_true", help="Upload and fill only; do not click publish") |
| 58 | dayu_upload.add_argument("--upload-timeout", type=int, default=600, help="Upload/processing timeout in seconds") |
| 59 | add_runtime_flags(dayu_upload) |
| 60 | dayu_article = dayu_actions.add_parser("upload-article", help="从正文文件发表图文,平台确认发表后返回成功") |
| 61 | dayu_article.add_argument("--account", required=True, help="已登录的本地账号名称") |
| 62 | dayu_article.add_argument("--title", required=True, help="图文标题,5–50字") |
| 63 | dayu_article.add_argument("--content-file", required=True, type=existing_file_path, help="UTF-8 HTML、Markdown 或 TXT 正文文件") |
| 64 | dayu_article.add_argument("--theme", choices=ARTICLE_THEMES, help="Markdown 渲染主题,默认 default;按大鱼号兼容排版发表") |
| 65 | dayu_article.add_argument("--source", required=True, choices=DAYU_SOURCE_TYPES, help="按实际内容选择信息来源") |
| 66 | dayu_article.add_argument("--source-url", default="", help="转载时必填的 http/https 原文链接") |
| 67 | dayu_article.add_argument("--author", help="选填作者名称;省略时沿用网页已有值") |
| 68 | dayu_article.add_argument("--cover-indices", nargs="+", type=int, help="选填1或3个正文图片序号(从1开始),对应单封面或三封面") |
| 69 | dayu_article.add_argument("--schedule", type=schedule_value, |
| 70 | help=f"{schedule_help},北京时间,{DAYU_SCHEDULE_MIN_HOURS}小时后至{DAYU_SCHEDULE_MAX_DAYS}天内,分钟为{DAYU_SCHEDULE_STEP_MINUTES}的倍数") |
| 71 | dayu_article.add_argument("--prepare-only", action="store_true", help="上传和填写后结束,不发表") |
| 72 | dayu_article.add_argument("--upload-timeout", type=int, default=600, help="每张图片上传等待秒数") |
| 73 | add_runtime_flags(dayu_article) |
| 74 | dayu_verify = dayu_actions.add_parser("verify", help="Check an existing publication without uploading again") |
| 75 | dayu_verify.add_argument("--account", required=True) |
| 76 | dayu_verify.add_argument("--type", choices=("video", "article"), help="作品类型;使用结果文件时自动识别,其余情况默认为video") |
| 77 | dayu_target = dayu_verify.add_mutually_exclusive_group(required=True) |
| 78 | dayu_target.add_argument("--content-id", help="Published content_id returned by Dayu") |
| 79 | dayu_target.add_argument("--origin-id", help="Draft ID captured before confirming publication") |
| 80 | dayu_target.add_argument("--result", type=existing_file_path, help="Saved Dayu result JSON to recover") |
| 81 | dayu_verify.add_argument("--public", action="store_true", help="额外等待审核后检查公开正文或播放;不影响发表命令的成功判定") |
| 82 | dayu_verify.add_argument("--timeout", type=int, default=60) |
| 83 | add_runtime_flags(dayu_verify) |
| 84 | |
| 85 | |
| 86 | async def run(args): |
| 87 | """执行本平台命令;发布结果由平台实现核验。""" |
| 88 | account_file = resolve_account_file("dayu", args.account) |
| 89 | if args.action == "login": |
| 90 | if args.timeout <= 0: |
| 91 | raise ValueError("登录期限必须大于 0 秒") |
| 92 | result = await login_dayu_account(account_file, args.expected_name, args.timeout) |
| 93 | elif args.action == "check": |
| 94 | valid = await check_dayu_account(account_file) |
| 95 | print("valid" if valid else "invalid") |
| 96 | return 0 if valid else 1 |
| 97 | elif args.action == "upload-video": |
| 98 | app = DayuVideo( |
| 99 | title=args.title, file_path=args.file, tags=parse_tags(args.tags), |
| 100 | account_file=account_file, thumbnail_path=args.thumbnail, category=args.category, |
| 101 | source=args.source, desc=args.desc, headless=args.headless, debug=args.debug, |
| 102 | prepare_only=args.prepare_only, upload_timeout=args.upload_timeout, |
| 103 | source_url=args.source_url, schedule=args.schedule, |
| 104 | ) |
| 105 | result = await app.main() |
| 106 | elif args.action == "upload-article": |
| 107 | app = DayuArticle( |
| 108 | title=args.title, content_file=args.content_file, account_file=account_file, |
| 109 | source=args.source, source_url=args.source_url, author=args.author, |
| 110 | theme=args.theme, schedule=args.schedule, |
| 111 | cover_indices=args.cover_indices, headless=args.headless, debug=args.debug, |
| 112 | prepare_only=args.prepare_only, upload_timeout=args.upload_timeout, |
| 113 | ) |
| 114 | result = await app.main() |
| 115 | elif args.action == "verify": |
| 116 | if args.timeout <= 0: |
| 117 | raise ValueError("核验期限必须大于 0 秒") |
| 118 | saved = json.loads(args.result.read_text(encoding="utf-8")) if args.result else {} |
| 119 | expected = load_dayu_identity(account_file) |
| 120 | if saved and saved.get("identity", {}).get("wmid") != expected["wmid"]: |
| 121 | raise RuntimeError("结果文件与所选大鱼号主体不一致") |
| 122 | kind = args.type or saved.get("type", "video") |
| 123 | if saved.get("type") and saved["type"] != kind: |
| 124 | raise ValueError("指定作品类型与结果文件不一致") |
| 125 | async with async_playwright() as playwright: |
| 126 | browser = await playwright.chromium.launch( |
| 127 | executable_path=LOCAL_CHROME_PATH or None, channel="chrome", headless=args.headless, |
| 128 | ) |
| 129 | try: |
| 130 | context = await browser.new_context(storage_state=str(account_file)) |
| 131 | page = await context.new_page() |
| 132 | await page.goto(DAYU_HOME_URL, wait_until="domcontentloaded", timeout=45000) |
| 133 | identity = await read_dayu_identity(page) |
| 134 | if identity["wmid"] != expected["wmid"]: |
| 135 | raise RuntimeError("核验时大鱼号主体与账号绑定不一致") |
| 136 | if kind == "article": |
| 137 | result = await verify_dayu_article( |
| 138 | context, browser, identity, |
| 139 | content_id=args.content_id or saved.get("content_id"), |
| 140 | origin_id=args.origin_id or saved.get("origin_id"), |
| 141 | title=saved.get("title"), timeout=args.timeout, manifest=saved.get("manifest"), public=args.public, |
| 142 | schedule=saved.get("schedule"), |
| 143 | ) |
| 144 | else: |
| 145 | result = await verify_dayu_video( |
| 146 | context, browser, identity, |
| 147 | content_id=args.content_id or saved.get("content_id"), |
| 148 | origin_id=args.origin_id or saved.get("origin_id"), |
| 149 | title=saved.get("title"), timeout=args.timeout, public=args.public, |
| 150 | schedule=saved.get("schedule"), |
| 151 | ) |
| 152 | if args.result: |
| 153 | saved.update(result) |
| 154 | saved.pop("error", None) |
| 155 | args.result.write_text(json.dumps(saved, ensure_ascii=False, indent=2), encoding="utf-8") |
| 156 | finally: |
| 157 | await browser.close() |
| 158 | else: |
| 159 | raise RuntimeError(f"Unsupported Dayu action: {args.action}") |
| 160 | print(json.dumps(result, ensure_ascii=False, indent=2)) |
| 161 | return 0 |
| 162 |