| 1 | """通过 sau CLI 发布微信公众号图文的最小模板。 |
| 2 | |
| 3 | 用法:python wechat_cli_template.py --account creator --title "标题" --content-file article.md --prepare-only |
| 4 | 移除 --prepare-only 会进入真实发表流程;发表最后一步需管理员微信扫码验证。 |
| 5 | """ |
| 6 | import argparse |
| 7 | import json |
| 8 | import subprocess |
| 9 | |
| 10 | |
| 11 | def main(): |
| 12 | parser = argparse.ArgumentParser() |
| 13 | parser.add_argument("--account", required=True) |
| 14 | parser.add_argument("--title", required=True) |
| 15 | parser.add_argument("--content-file", required=True) |
| 16 | parser.add_argument("--digest") |
| 17 | parser.add_argument("--cover") |
| 18 | parser.add_argument("--originality", choices=("none", "original"), default="none") |
| 19 | parser.add_argument("--no-broadcast", action="store_true") |
| 20 | parser.add_argument("--prepare-only", action="store_true") |
| 21 | args = parser.parse_args() |
| 22 | |
| 23 | command = ["sau", "wechat", "upload-article", "--account", args.account, |
| 24 | "--title", args.title, "--content-file", args.content_file, |
| 25 | "--originality", args.originality] |
| 26 | if args.digest: |
| 27 | command += ["--digest", args.digest] |
| 28 | if args.cover: |
| 29 | command += ["--cover", args.cover] |
| 30 | if args.no_broadcast: |
| 31 | command.append("--no-broadcast") |
| 32 | if args.prepare_only: |
| 33 | command.append("--prepare-only") |
| 34 | |
| 35 | completed = subprocess.run(command, text=True, capture_output=True) |
| 36 | print(completed.stdout) |
| 37 | if completed.returncode: |
| 38 | print(completed.stderr) |
| 39 | raise SystemExit(completed.returncode) |
| 40 | # 结果 JSON 也为每行前缀,如需结构化处理可逐行解析。 |
| 41 | for line in reversed(completed.stdout.splitlines()): |
| 42 | line = line.strip() |
| 43 | if line.startswith("{"): |
| 44 | print("result:", json.loads(line).get("status")) |
| 45 | break |
| 46 | |
| 47 | |
| 48 | if __name__ == "__main__": |
| 49 | main() |
| 50 |