Files
katelyatv/patches/patch-home-hkdrama.py
T

78 lines
4.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""首页注入「最新港剧」板块(热门电影上方,5条,聚合接口t=902 by=time)
并移除顶部导航「直播」菜单项。幂等可重跑。"""
import subprocess, tempfile, os
PC = "/app/.next/static/chunks/app/page-1a01a64271e1f47e.js"
NC = "/app/.next/static/chunks/402-42ae253cc67eee98.js"
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]
def check(src, tag):
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, f"{tag}: {r.stderr[:500]}"
COMPONENT = r'''var __HKDrama__=function(){var R=o,useState=R.useState,useEffect=R.useEffect;
var st=useState([]),d=st[0],setD=st[1];
var img=(window.RUNTIME_CONFIG&&window.RUNTIME_CONFIG.IMAGE_PROXY)||"";
useEffect(function(){
fetch("/api/tvbox/all?ac=videolist&t=902&pg=1&by=time").then(function(r){return r.json()}).then(function(j){
var arr=((j&&j.list)||[]).slice();
arr.sort(function(a,b){var ya=parseInt(a.vod_year)||0,yb=parseInt(b.vod_year)||0;
if(yb!==ya)return yb-ya;
return String(b.vod_time||"").localeCompare(String(a.vod_time||""))});
setD(arr.slice(0,10))}).catch(function(){})},[]);
if(!d.length)return null;
return(0,s.jsxs)("section",{className:"mb-8",children:[
(0,s.jsxs)("div",{className:"mb-4 flex items-center justify-between",children:[
(0,s.jsx)("h2",{className:"text-xl font-bold text-gray-800 dark:text-gray-200",children:"最新港剧"}),
(0,s.jsxs)(i.default,{href:"/filter?t=%E9%A6%99%E6%B8%AF%E5%89%A7",className:"flex items-center text-sm text-gray-500 hover:text-purple-700 dark:text-gray-400 dark:hover:text-purple-300 transition-colors",children:["查看更多",(0,s.jsx)("svg",{xmlns:"http://www.w3.org/2000/svg",width:"24",height:"24",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",className:"lucide lucide-chevron-right w-4 h-4 ml-1",children:(0,s.jsx)("path",{d:"m9 18 6-6-6-6"})})]})
]}),
(0,s.jsx)("div",{className:"grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 gap-4 relative",children:d.map(function(v){
var key=String(v.vod_id||""),sep=key.indexOf(":"),ks=sep>0?key.slice(0,sep):"",kid=sep>0?key.slice(sep+1):key;
var pic=v.vod_pic?(img+encodeURIComponent(v.vod_pic)):"";
return(0,s.jsxs)(i.default,{href:"/play?source="+encodeURIComponent(ks)+"&id="+encodeURIComponent(kid)+"&stitle="+encodeURIComponent(v.vod_name||"")+"&stype=tv",className:"group block",children:[
(0,s.jsxs)("div",{className:"relative aspect-[2/3] w-full overflow-hidden rounded-lg bg-gray-200 dark:bg-gray-800",children:[
pic?(0,s.jsx)("img",{src:pic,alt:v.vod_name||"",loading:"lazy",className:"absolute inset-0 w-full h-full object-cover group-hover:scale-105 transition-transform duration-300"}):null,
v.vod_remarks?(0,s.jsx)("span",{className:"absolute bottom-1 right-1 px-1.5 py-0.5 text-xs bg-black/65 text-white rounded",children:v.vod_remarks}):null
]}),
(0,s.jsx)("div",{className:"mt-2 text-sm font-medium text-gray-800 dark:text-gray-200 truncate",children:v.vod_name})
]},key)})})
]})};'''
# ---- 1) page chunk:组件定义 + 使用点 ----
pc = read(PC)
ST = "var __HKDrama__="
END = "var s=a(4393)"
if ST in pc and END in pc:
a0 = pc.find(ST); b0 = pc.find(END, a0)
assert b0 > a0, "old comp bounds"
pc = pc[:a0] + COMPONENT.strip() + pc[b0:]
check(pc, "page chunk (re)")
write(PC, pc)
print("HKDrama replaced")
elif "__HKDrama__" not in pc:
anchor = 'a.d(t,{default:function(){return j}});'
assert pc.count(anchor)==1, "def anchor"
pc = pc.replace(anchor, anchor + COMPONENT, 1)
use = '(0,s.jsx)(p,{}),'
assert pc.count(use)==1, "use anchor"
pc = pc.replace(use, use + '(0,s.jsx)(__HKDrama__,{}),', 1)
check(pc, "page chunk")
write(PC, pc)
print("HKDrama injected")
else:
print("HKDrama exists")
# ---- 3) 菜单:不碰(用户要求保留顶部「直播」入口) ----
print("DONE - restart to apply")