Spaces:
Paused
Paused
File size: 11,360 Bytes
23aa7a2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | """Runtime patch layer for VNEWS.
Keeps the current large app intact, but replaces fragile AI wall endpoints with
stable JSON endpoints and injects frontend safeJson wrappers.
"""
import hashlib
import time
import os
from urllib.parse import quote
import requests
from bs4 import BeautifulSoup
from fastapi import Request
from fastapi.responses import JSONResponse, HTMLResponse
import main as _main
app = _main.app
DEFAULT_IMG = "https://s1.vnecdn.net/vnexpress/restruct/i/v9505/logo_default.jpg"
def _remove_routes(paths):
app.router.routes = [r for r in app.router.routes if getattr(r, "path", None) not in set(paths)]
def _safe_text(v):
return (v or "").strip()
def _ensure_article(url: str):
data = None
try:
if hasattr(_main, "_article_by_url"):
data = _main._article_by_url(url)
except Exception:
data = None
if not data:
try:
data = _main._scrape_generic_article(url) if hasattr(_main, "_scrape_generic_article") else None
except Exception:
data = None
if not data:
data = {"title": "", "summary": "", "og_image": "", "body": [], "url": url, "source": "generic"}
title = _safe_text(data.get("title"))
summary = _safe_text(data.get("summary"))
img = _safe_text(data.get("og_image"))
body = data.get("body") or []
if not title or not summary or not img or not body:
try:
r = requests.get(url, headers=getattr(_main, "HEADERS", {}), timeout=15)
r.encoding = "utf-8"
soup = BeautifulSoup(r.text, "lxml")
if not title:
tag = soup.find("meta", property="og:title") or soup.find("title")
title = tag.get("content", "").strip() if tag and tag.name == "meta" else (tag.get_text(strip=True) if tag else "")
if not summary:
tag = soup.find("meta", property="og:description") or soup.find("meta", attrs={"name": "description"})
summary = tag.get("content", "").strip() if tag else ""
if not img:
tag = soup.find("meta", property="og:image") or soup.find("meta", attrs={"name": "twitter:image"})
img = tag.get("content", "").strip() if tag else ""
if not body:
ps = []
for p in soup.find_all("p"):
t = p.get_text(" ", strip=True)
if len(t) > 40:
ps.append({"type": "p", "text": t})
if len(ps) >= 30:
break
body = ps
except Exception:
pass
if not summary and body:
first = next((b.get("text", "") for b in body if b.get("type") == "p" and b.get("text")), "")
summary = first[:360]
if not title:
title = url
if not img:
img = DEFAULT_IMG
if not body and summary:
body = [{"type": "p", "text": summary}]
data.update({"title": title, "summary": summary, "og_image": img, "body": body, "url": url})
return data
def _rewrite(data, tone="tu-nhien"):
try:
if hasattr(_main, "_ai_rewrite_article"):
text = _main._ai_rewrite_article(data, tone=tone)
if text and len(text.strip()) > 50:
return text.strip()
except Exception:
pass
title = data.get("title", "")
summary = data.get("summary", "")
ps = [b.get("text", "") for b in data.get("body", []) if b.get("type") == "p" and b.get("text")]
lead = summary or (ps[0] if ps else "")
points = "\n".join(["• " + p[:220] + ("..." if len(p) > 220 else "") for p in ps[:5]])
body = "\n\n".join(ps[:10])
return (f"Bản tin AI viết lại: {title}\n\n{lead}\n\n{body}\n\nĐiểm chính:\n{points}").strip()
def _topic_image(topic):
try:
if hasattr(_main, "_image_for_topic"):
return _main._image_for_topic(topic)
except Exception:
pass
return "https://image.pollinations.ai/prompt/" + quote("editorial illustration Vietnamese news " + topic, safe="") + "?width=1024&height=576&nologo=true"
def _save_post(post):
try:
posts = _main._load_wall() if hasattr(_main, "_load_wall") else []
except Exception:
posts = []
posts.insert(0, post)
try:
if hasattr(_main, "_save_wall"):
_main._save_wall(posts)
except Exception:
pass
return post
_remove_routes(["/api/url_wall", "/api/topic_post", "/api/rewrite_share", "/"])
@app.post("/api/url_wall")
async def patched_url_wall(request: Request):
try:
body = await request.json()
except Exception:
body = {}
url = _safe_text(body.get("url"))
tone = _safe_text(body.get("tone")) or "tu-nhien"
if not url:
return JSONResponse({"error": "missing url"}, status_code=400)
try:
data = _ensure_article(url)
text = _rewrite(data, tone=tone)
post = {
"id": hashlib.md5((url + str(time.time())).encode()).hexdigest()[:12],
"url": url,
"title": data.get("title") or url,
"summary": data.get("summary") or "",
"img": data.get("og_image") or DEFAULT_IMG,
"text": text or (data.get("summary") or data.get("title") or url),
"source": data.get("source", "url"),
"ts": int(time.time()),
}
_save_post(post)
return JSONResponse({"post": post})
except Exception as e:
return JSONResponse({"error": "Không tạo được tóm tắt URL", "detail": str(e)[:300]}, status_code=500)
@app.post("/api/rewrite_share")
async def patched_rewrite_share(request: Request):
return await patched_url_wall(request)
@app.post("/api/topic_post")
async def patched_topic_post(request: Request):
try:
body = await request.json()
except Exception:
body = {}
topic = _safe_text(body.get("topic"))
tone = _safe_text(body.get("tone")) or "tu-nhien"
if not topic:
return JSONResponse({"error": "missing topic"}, status_code=400)
try:
context = ""
try:
if hasattr(_main, "_topic_article_context"):
context = _main._topic_article_context(topic)
if not context and hasattr(_main, "_web_context"):
context = _main._web_context(topic)
except Exception:
context = ""
if not context:
context = f"Chủ đề: {topic}"
data = {"title": topic, "summary": context[:420], "og_image": _topic_image(topic), "body": [{"type": "p", "text": context}], "source": "topic", "url": ""}
text = _rewrite(data, tone=tone)
post = {
"id": hashlib.md5((topic + str(time.time())).encode()).hexdigest()[:12],
"url": "",
"title": topic,
"summary": data["summary"],
"img": data["og_image"] or DEFAULT_IMG,
"text": text or context,
"source": "topic",
"ts": int(time.time()),
}
_save_post(post)
return JSONResponse({"post": post})
except Exception as e:
return JSONResponse({"error": "Không tạo được bài theo chủ đề", "detail": str(e)[:300]}, status_code=500)
_FRONTEND_PATCH = r'''
<script>
(function(){
async function safeJson(res){
const text = await res.text();
try { return JSON.parse(text); }
catch(e){ return { error: (text || 'Server không trả JSON').slice(0,500) }; }
}
window.safeJson = safeJson;
window.createUrlPost = function(){
let inp=document.getElementById('ai-url-input');
let url=(inp&&inp.value||'').trim();
if(!url){ alert('Dán URL trước'); return; }
fetch('/api/url_wall',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url})})
.then(safeJson).then(j=>{
if(j&&j.post){
if(!j.post.img) j.post.img='https://s1.vnecdn.net/vnexpress/restruct/i/v9505/logo_default.jpg';
if(!j.post.text) j.post.text=j.post.summary||j.post.title||'Không lấy được nội dung tóm tắt.';
if(typeof prependWallPost==='function') prependWallPost(j.post);
alert('Đã tóm tắt URL và đăng lên tường');
if(inp) inp.value='';
} else alert((j&&j.error)||'Lỗi URL');
}).catch(e=>alert('Lỗi URL: '+e.message));
};
window.createTopicPost = function(){
let inp=document.getElementById('ai-topic-input');
let topic=(inp&&inp.value||'').trim();
if(!topic){ alert('Nhập chủ đề trước'); return; }
fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic})})
.then(safeJson).then(j=>{
if(j&&j.post){
if(!j.post.img) j.post.img='https://s1.vnecdn.net/vnexpress/restruct/i/v9505/logo_default.jpg';
if(!j.post.text) j.post.text=j.post.summary||j.post.title||'Không lấy được nội dung.';
if(typeof prependWallPost==='function') prependWallPost(j.post);
alert('Đã tạo bài và đăng lên tường');
if(inp) inp.value='';
} else alert((j&&j.error)||'Lỗi tạo bài');
}).catch(e=>alert('Lỗi tạo bài: '+e.message));
};
window.rewriteCurrentArticle = function(){
if(!window._currentArticle && typeof _currentArticle!=='undefined') window._currentArticle=_currentArticle;
let ca = (typeof _currentArticle!=='undefined') ? _currentArticle : window._currentArticle;
if(!ca || !ca.url){ alert('Chưa có bài viết để rewrite'); return; }
let tone=document.getElementById('rewrite-tone')?.value||'nghiem-tuc';
let btn=document.querySelector('.article-actions button.primary');
if(btn){btn.textContent='Đang rewrite...';btn.disabled=true;}
fetch('/api/rewrite_share',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url:ca.url,tone})})
.then(safeJson).then(j=>{
if(j&&j.post){
if(!j.post.img) j.post.img='https://s1.vnecdn.net/vnexpress/restruct/i/v9505/logo_default.jpg';
if(!j.post.text) j.post.text=j.post.summary||j.post.title||'Không lấy được nội dung.';
let box=document.getElementById('rewrite-result');
if(box) box.innerHTML='<div class="rewrite-box"><div class="rewrite-title">Đã rewrite và đăng lên Tường AI</div><div class="rewrite-text">'+(j.post.text||'').replace(/[&<>"']/g,m=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[m]))+'</div></div>';
if(typeof prependWallPost==='function') prependWallPost(j.post);
alert('Đã đăng lên Tường AI');
} else alert((j&&j.error)||'Không tạo được bài AI');
}).catch(e=>alert('Lỗi tạo bài AI: '+e.message))
.finally(()=>{if(btn){btn.textContent='🤖 AI viết lại & đăng tường';btn.disabled=false;}});
};
})();
</script>
'''
@app.get("/")
async def patched_index():
try:
with open("/app/static/index.html", "r", encoding="utf-8") as f:
html = f.read()
if "window.safeJson" not in html:
html = html.replace("</body>", _FRONTEND_PATCH + "</body>")
return HTMLResponse(content=html)
except Exception as e:
return HTMLResponse(content=f"<pre>Index error: {str(e)}</pre>", status_code=500)
|