-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconvert_to_json.py
More file actions
51 lines (40 loc) · 1.66 KB
/
Copy pathconvert_to_json.py
File metadata and controls
51 lines (40 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""Convert an article URL with MagicMD and print a JSON payload."""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
from magicmd import MagicMDError, convert_article
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Convert an article URL to MagicMD JSON.")
parser.add_argument("url", help="Public article URL.")
parser.add_argument("--output-dir", help="Optional output directory for a content package.")
parser.add_argument("--config", help="Optional .magicmd.toml path.")
parser.add_argument("--platform", default="auto", help="Platform key, default: auto.")
parser.add_argument("--no-images", action="store_true", help="Skip image downloads.")
parser.add_argument("--compact", action="store_true", help="Print compact JSON.")
return parser.parse_args()
def main() -> int:
args = parse_args()
try:
result = convert_article(
url=args.url,
platform=args.platform,
output_dir=Path(args.output_dir) if args.output_dir else None,
download_images=not args.no_images,
config_path=Path(args.config) if args.config else None,
)
except MagicMDError as exc:
print(
json.dumps(
{"ok": False, "error": exc.__class__.__name__, "message": str(exc)},
ensure_ascii=False,
),
file=sys.stderr,
)
return 1
indent = None if args.compact else 2
print(json.dumps(result.model_dump(mode="json"), ensure_ascii=False, indent=indent))
return 0
if __name__ == "__main__":
raise SystemExit(main())