Add vip-course/lesson/ebook with source markdown seeds, VIP order API + admin offers, ban web-view for content, and update READMEs for weapp rebuild/extract scripts. Co-authored-by: Cursor <cursoragent@cursor.com>
100 lines
3.0 KiB
Python
100 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Extract cloudphone lesson markdown + videoUrl into weapp data."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import re
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[4]
|
|
SRC = ROOT / "src/modules/vip/app/cloudphone/config/lessons.ts"
|
|
OUT = ROOT / "apps/miniprogram/h5/src/data/vipLessonContent.ts"
|
|
|
|
SAMPLE = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
|
|
|
|
|
|
def extract_template_literals(text: str) -> dict[str, str]:
|
|
"""Parse key -> template-literal blocks; allow escaped backticks inside."""
|
|
out: dict[str, str] = {}
|
|
# Only scan LESSON_MARKDOWNS block to avoid LESSONS_MAP noise
|
|
block_m = re.search(
|
|
r"const LESSON_MARKDOWNS[^=]*=\s*\{(.*?)\n\};",
|
|
text,
|
|
re.S,
|
|
)
|
|
body = block_m.group(1) if block_m else text
|
|
i = 0
|
|
while True:
|
|
m = re.search(r'"(\d+-\d+)":\s*`', body[i:])
|
|
if not m:
|
|
break
|
|
key = m.group(1)
|
|
start = i + m.end()
|
|
j = start
|
|
chunks: list[str] = []
|
|
while j < len(body):
|
|
ch = body[j]
|
|
if ch == "\\" and j + 1 < len(body):
|
|
nxt = body[j + 1]
|
|
if nxt == "`":
|
|
chunks.append("`")
|
|
elif nxt == "\\":
|
|
chunks.append("\\")
|
|
elif nxt == "n":
|
|
chunks.append("\n")
|
|
elif nxt == "t":
|
|
chunks.append("\t")
|
|
else:
|
|
chunks.append(nxt)
|
|
j += 2
|
|
continue
|
|
if ch == "`":
|
|
break
|
|
chunks.append(ch)
|
|
j += 1
|
|
out[key] = "".join(chunks).strip("\n")
|
|
i = j + 1
|
|
return out
|
|
|
|
|
|
def extract_videos(text: str) -> dict[str, str]:
|
|
vids: dict[str, str] = {}
|
|
map_m = re.search(r"export const LESSONS_MAP[^=]*=\s*\{(.*?)\n\};", text, re.S)
|
|
body = map_m.group(1) if map_m else text
|
|
for m in re.finditer(
|
|
r'"(\d+-\d+)":\s*\{[^}]*?videoUrl:\s*(?:"([^"]+)"|SAMPLE_VIDEO)',
|
|
body,
|
|
re.S,
|
|
):
|
|
vids[m.group(1)] = m.group(2) or SAMPLE
|
|
return vids
|
|
|
|
|
|
def main() -> None:
|
|
text = SRC.read_text(encoding="utf-8")
|
|
mds = extract_template_literals(text)
|
|
vids = extract_videos(text)
|
|
keys = sorted(
|
|
set(mds) | set(vids),
|
|
key=lambda x: (int(x.split("-")[0]), int(x.split("-")[1])),
|
|
)
|
|
payload = {
|
|
k: {"markdown": mds.get(k, ""), "videoUrl": vids.get(k, SAMPLE)} for k in keys
|
|
}
|
|
OUT.write_text(
|
|
"/** Auto from src/modules/vip/app/cloudphone/config/lessons.ts */\n"
|
|
"export const VIP_LESSON_CONTENT: Record<\n"
|
|
" string,\n"
|
|
" { markdown: string; videoUrl: string }\n"
|
|
f"> = {json.dumps(payload, ensure_ascii=False, indent=2)}\n",
|
|
encoding="utf-8",
|
|
)
|
|
print("keys", len(payload), "bytes", OUT.stat().st_size)
|
|
empty = [k for k, v in payload.items() if len(v["markdown"]) < 80]
|
|
if empty:
|
|
print("WARN short markdown:", empty)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|