#!/usr/bin/env python3
"""Пробуем достать данные устройств с yealink.com и support.yealink.com."""
import json
import re
import subprocess
import sys
UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120 Safari/537.36"
def fetch(url):
return subprocess.run(["curl", "-sL", "-m", "45", "-A", UA, url],
capture_output=True, text=True, timeout=90).stdout
def nuxt_text(html):
m = re.search(r'', html, re.S)
if not m:
return ""
try:
data = json.loads(m.group(1).strip())
except Exception:
return ""
texts = []
def walk(o):
if isinstance(o, str):
if o.strip().startswith('{"type":"doc"'):
try:
d = json.loads(o)
def t(n):
if isinstance(n, dict):
if n.get("type") == "text":
texts.append(n.get("text", ""))
for c in n.get("content") or []:
t(c)
t(d)
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)
return "\n".join(texts)
def raw_text(html):
html = re.sub(r"", " ", html, flags=re.S | re.I)
html = re.sub(r"", " ", html, flags=re.S | re.I)
txt = re.sub(r"<[^>]+>", "\n", html)
txt = re.sub(r" ?", " ", txt)
txt = re.sub(r"\n{2,}", "\n", txt)
return txt
KEYS = ("power", "poe", "vch", "pickup", "consumption", "watt", "Вт",
"interface", "port", "microphone", "cable")
def scan(text, name):
print("\n=== %s ===" % name)
lines = [ln.strip() for ln in text.splitlines() if ln.strip()]
hits = [ln for ln in lines if any(k.lower() in ln.lower() for k in KEYS)]
for ln in hits[:14]:
print(" ", ln[:150])
if not hits:
print(" (нет совпадений)")
if __name__ == "__main__":
urls = sys.argv[1:]
for u in urls:
html = fetch(u)
name = u.split("/")[-2] if u.endswith("/") else u.rsplit("/", 1)[-1]
print("\n" + "#" * 70)
print("URL:", u, "| размер:", len(html))
t = nuxt_text(html)
if t:
scan(t, "NUXT: " + name[:40])
if len(t) < 200:
scan(raw_text(html), "HTML: " + name[:40])