64 lines
2.5 KiB
Python
64 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
"""注入直播功能到运行中容器(幂等可重跑):LIVE 函数块 + 入口拦截 + /live rewrite + 菜单项"""
|
|
import subprocess, json, tempfile, os
|
|
|
|
P = "/app/.next/server/app/api/tvbox/route.js"
|
|
RM = "/app/.next/routes-manifest.json"
|
|
MC = "/app/.next/static/chunks/402-42ae253cc67eee98.js"
|
|
BASE = os.path.dirname(os.path.abspath(__file__))
|
|
MARK_S, MARK_E = "/* ==== LIVE:", "/* ==== LIVE END ==== */"
|
|
HOOK = 'try{let _lv=new URL(e.url).searchParams.get("live");if(_lv)return await LIVE(_lv,e)}catch(_le){console.log("[LIVE-HOOK]",_le&&_le.message)}'
|
|
|
|
def read(p):
|
|
return subprocess.run(["docker","exec","katelyatv","cat",p],capture_output=True,check=True).stdout.decode()
|
|
|
|
def write(p, data):
|
|
r=subprocess.run(["docker","exec","-i","katelyatv","sh","-c",f"cat > {p}"],input=data.encode(),capture_output=True)
|
|
assert r.returncode==0, r.stderr.decode()[:300]
|
|
|
|
# ---- 1) route.js: 删旧块+插新块+入口钩子 ----
|
|
src = read(P)
|
|
live = open(os.path.join(BASE, "live-inject.js")).read().strip()
|
|
i, j = src.find(MARK_S), src.find(MARK_E)
|
|
if i >= 0 and j > i:
|
|
src = src[:i] + src[j+len(MARK_E)+1:]
|
|
print("old LIVE block removed")
|
|
anchor = "let _aggC=new Map();"
|
|
assert src.count(anchor) == 1, "anchor A"
|
|
if MARK_S not in src:
|
|
src = src.replace(anchor, live + "\n" + anchor, 1)
|
|
print("LIVE block injected")
|
|
a = 'async function u(e){console.log("[TVBOX-REQ]",e.method,e.url);'
|
|
assert src.count(a) == 1, "anchor B"
|
|
if HOOK not in src:
|
|
src = src.replace(a, a + HOOK, 1)
|
|
print("entry hook injected")
|
|
with tempfile.NamedTemporaryFile('w', suffix='.js', delete=False) as f:
|
|
f.write(src); tmp = f.name
|
|
r = subprocess.run(['node', '--check', tmp], capture_output=True, text=True)
|
|
os.unlink(tmp)
|
|
assert r.returncode == 0, r.stderr[:600]
|
|
write(P, src)
|
|
print("route.js OK")
|
|
|
|
# ---- 2) rewrite /live ----
|
|
rm = json.loads(read(RM))
|
|
af = rm.get("rewrites", {}).get("afterFiles", [])
|
|
if not any(x.get("source") == "/live" for x in af):
|
|
af.append({"source": "/live", "destination": "/api/tvbox?live=page", "regex": "^/live$"})
|
|
rm["rewrites"]["afterFiles"] = af
|
|
write(RM, json.dumps(rm, ensure_ascii=False))
|
|
print("rewrite /live added")
|
|
|
|
# ---- 3) 菜单 ----
|
|
mc = read(MC)
|
|
if '"直播"' not in mc:
|
|
old = '{icon:o.Z,label:"综艺",href:"/douban?type=show"}'
|
|
n = mc.count(old)
|
|
assert n == 2, f"menu anchor {n}"
|
|
mc = mc.replace(old, old + ',{icon:o.Z,label:"直播",href:"/live"}')
|
|
write(MC, mc)
|
|
print(f"menu injected x{n}")
|
|
|
|
print("DONE - restart container to apply")
|