返回 Social Auto Upload
wechat.py
根目录 / sau_commands / wechat.py
1 """wechat 的命令参数与执行逻辑(微信公众号图文发布)。"""
2 from __future__ import annotations
3 import json
4 from uploader.wechat_uploader.article import WechatArticle
5 from uploader.wechat_uploader.constants import WECHAT_ORIGINALITY
6 from uploader.wechat_uploader.session import check_wechat_account, login_wechat_account
7 from sau_commands.arguments import (
8 add_runtime_flags, existing_file_path, resolve_account_file,
9 )
10
11
12 def register(platform_parsers):
13 """声明本平台命令、参数及合法选项。"""
14 wechat_parser = platform_parsers.add_parser("wechat", help="微信公众平台图文文章发布")
15 wechat_actions = wechat_parser.add_subparsers(dest="action", required=True)
16
17 wechat_login = wechat_actions.add_parser("login", help="在可见 Chrome 中扫码登录并保存本地会话")
18 wechat_login.add_argument("--account", required=True, help="本地会话别名,可与公众号昵称不同")
19 wechat_login.add_argument("--timeout", type=int, default=600, help="扫码登录等待秒数")
20
21 wechat_check = wechat_actions.add_parser("check", help="检查微信公众号登录态与账号绑定")
22 wechat_check.add_argument("--account", required=True, help="本地会话别名")
23 add_runtime_flags(wechat_check)
24 wechat_check.set_defaults(headless=True)
25
26 wechat_article = wechat_actions.add_parser("upload-article", help="发布微信公众号图文;正文图片保留原稿位置")
27 wechat_article.add_argument("--account", required=True, help="本地会话别名")
28 wechat_article.add_argument("--title", required=True, help="文章标题,最多 64 字")
29 wechat_article.add_argument("--content-file", required=True, type=existing_file_path,
30 help="UTF-8 HTML/Markdown/TXT,图片路径写在原稿对应段落中")
31 wechat_article.add_argument("--digest", help="可选摘要,最多 120 字")
32 wechat_article.add_argument("--author", help="可选作者名,最多 8 字")
33 wechat_article.add_argument("--cover", type=existing_file_path, help="可选独立封面图片")
34 wechat_article.add_argument("--originality", choices=WECHAT_ORIGINALITY, default="none",
35 help="创作来源声明:none 不声明,original 声明原创")
36 wechat_article.add_argument("--no-broadcast", action="store_true",
37 help="发表但不群发通知;默认开启群发")
38 wechat_article.add_argument("--prepare-only", action="store_true", help="填写并检查后结束,不点击发表")
39 wechat_article.add_argument("--qr-timeout", type=int, default=600,
40 help="等待管理员扫码验证的秒数")
41 add_runtime_flags(wechat_article)
42 # 公众号填表与扫码验证需要可见窗口,默认有头;可显式 --headless 关闭。
43 wechat_article.set_defaults(headless=False)
44
45
46 async def run(args):
47 """执行本平台命令;发布结果由平台实现核验。"""
48 account_file = resolve_account_file("wechat", args.account)
49 if args.action == "login":
50 result = await login_wechat_account(account_file, args.timeout)
51 elif args.action == "check":
52 valid = await check_wechat_account(account_file, args.headless)
53 print("valid" if valid else "invalid")
54 return 0 if valid else 1
55 else:
56 app = WechatArticle(
57 title=args.title, content_file=args.content_file, account_file=account_file,
58 digest=args.digest, author=args.author, cover=args.cover,
59 originality=args.originality, broadcast=not args.no_broadcast,
60 prepare_only=args.prepare_only, headless=args.headless, debug=args.debug,
61 qr_timeout=args.qr_timeout,
62 )
63 result = await app.main()
64 print(json.dumps(result, ensure_ascii=False, indent=2))
65 return 0
66
66 lines PYTHON