| 1 | """zhihu 的命令参数与执行逻辑。""" |
| 2 | from __future__ import annotations |
| 3 | import json |
| 4 | from uploader.zhihu_uploader.article import ZhihuArticle |
| 5 | from uploader.zhihu_uploader.constants import ( |
| 6 | ZHIHU_SCHEDULE_MAX_HOURS, |
| 7 | ZHIHU_SCHEDULE_MINUTES, |
| 8 | ZHIHU_ORIGINALITY, |
| 9 | ZHIHU_VIDEO_CATEGORIES, |
| 10 | ZHIHU_VIDEO_MARKS, |
| 11 | ) |
| 12 | from uploader.zhihu_uploader.session import check_zhihu_account, login_zhihu_account |
| 13 | from uploader.zhihu_uploader.verification import verify_zhihu_result |
| 14 | from uploader.zhihu_uploader.video import ZhihuVideo |
| 15 | from sau_commands.arguments import ( |
| 16 | SCHEDULE_FORMAT, |
| 17 | add_runtime_flags, |
| 18 | existing_file_path, |
| 19 | resolve_account_file, |
| 20 | schedule_value, |
| 21 | ) |
| 22 | |
| 23 | |
| 24 | def register(platform_parsers): |
| 25 | """声明本平台命令、参数及合法选项。""" |
| 26 | schedule_help = SCHEDULE_FORMAT.replace("%", "%%") |
| 27 | zhihu_parser = platform_parsers.add_parser("zhihu", help="知乎创作中心文章与视频发布") |
| 28 | zhihu_actions = zhihu_parser.add_subparsers(dest="action", required=True) |
| 29 | zhihu_login = zhihu_actions.add_parser("login", help="在可见 Chrome 中登录并保存本地会话") |
| 30 | zhihu_login.add_argument("--account", required=True, help="本地会话别名,可与知乎展示名不同") |
| 31 | zhihu_login.add_argument("--timeout", type=int, default=600, help="登录等待秒数") |
| 32 | zhihu_check = zhihu_actions.add_parser("check", help="检查知乎登录态和页头账号绑定") |
| 33 | zhihu_check.add_argument("--account", required=True, help="本地会话别名") |
| 34 | add_runtime_flags(zhihu_check) |
| 35 | zhihu_check.set_defaults(headless=False) |
| 36 | zhihu_verify = zhihu_actions.add_parser("verify", help="按已保存的作品 URL 只读核对提交结果") |
| 37 | zhihu_verify.add_argument("--account", required=True, help="本地会话别名") |
| 38 | zhihu_verify.add_argument("--result", required=True, type=existing_file_path) |
| 39 | add_runtime_flags(zhihu_verify) |
| 40 | zhihu_verify.set_defaults(headless=False) |
| 41 | zhihu_article = zhihu_actions.add_parser("upload-article", help="发布知乎文章;正文配图由原稿位置决定") |
| 42 | zhihu_article.add_argument("--account", required=True, help="本地会话别名") |
| 43 | zhihu_article.add_argument("--title", required=True, help="文章标题,最多 100 字") |
| 44 | zhihu_article.add_argument("--content-file", required=True, type=existing_file_path, help="UTF-8 HTML/Markdown/TXT,图片路径写在原稿对应段落中") |
| 45 | zhihu_article.add_argument("--schedule", type=schedule_value, |
| 46 | help=f"实验性定时发布,格式 {schedule_help},北京时间 {ZHIHU_SCHEDULE_MINUTES} 分钟后至 {ZHIHU_SCHEDULE_MAX_HOURS} 小时内") |
| 47 | zhihu_article.add_argument("--prepare-only", action="store_true", help="填写并检查编辑器后结束,不点击发布") |
| 48 | zhihu_article.add_argument("--upload-timeout", type=int, default=600, help="每张正文图片上传等待秒数") |
| 49 | add_runtime_flags(zhihu_article) |
| 50 | zhihu_article.set_defaults(headless=False) |
| 51 | zhihu_video = zhihu_actions.add_parser("upload-video", help="上传并发布一个知乎视频") |
| 52 | zhihu_video.add_argument("--account", required=True, help="本地会话别名") |
| 53 | zhihu_video.add_argument("--file", required=True, type=existing_file_path, help="视频文件,大于 100KB、最大 20GB、最长 4 小时") |
| 54 | zhihu_video.add_argument("--title", required=True, help="视频标题,5–50 字") |
| 55 | zhihu_video.add_argument("--desc", default="", help="可选视频简介,最多 300 字") |
| 56 | zhihu_video.add_argument("--category", choices=ZHIHU_VIDEO_CATEGORIES, |
| 57 | help="所属领域;省略时在终端按编号选择,提交前核对实时菜单") |
| 58 | zhihu_video.add_argument("--video-mark", choices=ZHIHU_VIDEO_MARKS, |
| 59 | help="视频声明;省略时在终端按编号选择") |
| 60 | zhihu_video.add_argument("--show-options", action="store_true", help="上传素材后列出全部领域和视频标记,保存到结果 JSON 后退出,不发布") |
| 61 | zhihu_video.add_argument("--originality", choices=ZHIHU_ORIGINALITY, required=True, |
| 62 | help="内容为原创或转载") |
| 63 | zhihu_video.add_argument("--schedule", type=schedule_value, |
| 64 | help=f"实验性定时发布,格式 {schedule_help},北京时间 {ZHIHU_SCHEDULE_MINUTES} 分钟后至 {ZHIHU_SCHEDULE_MAX_HOURS} 小时内") |
| 65 | zhihu_video.add_argument("--prepare-only", action="store_true", help="上传并填写后结束,不点击发布") |
| 66 | zhihu_video.add_argument("--upload-timeout", type=int, default=1200, help="视频上传等待秒数") |
| 67 | add_runtime_flags(zhihu_video) |
| 68 | zhihu_video.set_defaults(headless=False) |
| 69 | |
| 70 | |
| 71 | async def run(args): |
| 72 | """执行本平台命令;发布结果由平台实现核验。""" |
| 73 | account_file = resolve_account_file("zhihu", args.account) |
| 74 | if args.action == "login": |
| 75 | result = await login_zhihu_account(account_file, args.timeout) |
| 76 | elif args.action == "check": |
| 77 | valid = await check_zhihu_account(account_file, args.headless) |
| 78 | print("valid" if valid else "invalid") |
| 79 | return 0 if valid else 1 |
| 80 | elif args.action == "verify": |
| 81 | result = await verify_zhihu_result(account_file, args.result, args.headless) |
| 82 | elif args.action == "upload-article": |
| 83 | app = ZhihuArticle( |
| 84 | title=args.title, content_file=args.content_file, account_file=account_file, |
| 85 | schedule=args.schedule, prepare_only=args.prepare_only, |
| 86 | upload_timeout=args.upload_timeout, headless=args.headless, debug=args.debug, |
| 87 | ) |
| 88 | result = await app.main() |
| 89 | else: |
| 90 | app = ZhihuVideo( |
| 91 | title=args.title, file_path=args.file, account_file=account_file, |
| 92 | description=args.desc, category=args.category, |
| 93 | topics=[], video_mark=args.video_mark, show_options=args.show_options, |
| 94 | originality=args.originality, schedule=args.schedule, |
| 95 | prepare_only=args.prepare_only, upload_timeout=args.upload_timeout, |
| 96 | headless=args.headless, debug=args.debug, |
| 97 | ) |
| 98 | result = await app.main() |
| 99 | print(json.dumps(result, ensure_ascii=False, indent=2)) |
| 100 | return 0 |
| 101 |