Files

88 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""Извлекает текст ProseMirror-документа со страницы support.yealink.com."""
import json
import re
import subprocess
import sys
UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120"
def get_text(url):
html = subprocess.run(["curl", "-sL", "-m", "40", "-A", UA, url],
capture_output=True, text=True, timeout=90).stdout
m = re.search(r'<script[^>]*id="__NUXT_DATA__"[^>]*>(.*?)</script>', html, re.S)
if not m:
return [], []
data = json.loads(m.group(1).strip())
docs = []
def walk(o):
if isinstance(o, str):
if o.strip().startswith('{"type":"doc"'):
try:
docs.append(json.loads(o))
except Exception:
pass
elif isinstance(o, list):
for v in o:
walk(v)
elif isinstance(o, dict):
for v in o.values():
walk(v)
walk(data)
lines, images = [], []
def t(n, depth=0):
if not isinstance(n, dict):
return
typ = n.get("type")
if typ == "text":
lines.append(n.get("text", ""))
elif typ == "image":
src = (n.get("attrs") or {}).get("src")
if src:
images.append(src)
elif typ == "heading":
txt = "".join(x for x in _collect(n))
lines.append("\n## " + txt)
return
elif typ in ("paragraph", "tableRow"):
txt = "".join(x for x in _collect(n))
if txt.strip():
lines.append(txt)
return
for c in n.get("content") or []:
t(c, depth + 1)
def _collect(n):
out = []
def w(x):
if isinstance(x, dict):
if x.get("type") == "text":
out.append(x.get("text", ""))
for c in x.get("content") or []:
w(c)
w(n)
return out
for d in docs:
for c in d.get("content") or []:
t(c)
return lines, images
if __name__ == "__main__":
for url in sys.argv[1:]:
print("\n" + "=" * 70)
print("URL:", url)
print("=" * 70)
lines, images = get_text(url)
for ln in lines[:60]:
print(" ", ln[:160])
if images:
print("\n изображения (%d):" % len(images))
for i in images[:10]:
print(" ", i[:140])