""" VNEWS Unified Patch v2 ====================== Single file replacing app_entry.py + patch_extra.py functionality. No conflicts, no duplicate slides, no DOM destruction. Features: 1. Tường AI persistent (fix FINAL6E destroying DOM) 2. Source details with image + description + "Xem trên VNEWS" 3. Highlight = TikTok fullheight 1:1 crop center with interaction buttons 4. Rewrite auto-title, no "xem trên VNEWS" junk 5. Topic post uses source og:image instead of AI image 6. Fast homepage load (non-blocking) """ from ai_runtime_patch_fast import * from ai_runtime_patch_fast import app, f5, f6, rt, PATCH_INJECT, _scrape, _domain, clean, _bg, _bg_home, _bg_shorts from fastapi.responses import HTMLResponse, JSONResponse from fastapi import Request, Query import asyncio, re, threading, time DEFAULT_IMG = "https://s1.vnecdn.net/vnexpress/restruct/i/v9505/logo_default.jpg" # ============================================================ # REMOVE ALL CONFLICTING ROUTES — we redefine them cleanly # ============================================================ _OVERRIDE_PATHS = {'/api/homepage','/api/shorts','/api/topic_post','/api/topic/rewrite','/api/rewrite_share','/api/url_wall','/'} app.router.routes = [r for r in app.router.routes if not (getattr(r,'path',None) in _OVERRIDE_PATHS and any(m in getattr(r,'methods',set()) for m in ('GET','POST')))] # ============================================================ # FAST HOMEPAGE + SHORTS (non-blocking) # ============================================================ @app.get('/api/homepage') def _homepage(): if _bg_home['d']: if time.time()-_bg_home['t']>300:threading.Thread(target=_bg,daemon=True).start() return JSONResponse(_bg_home['d']) threading.Thread(target=_bg,daemon=True).start() return JSONResponse([]) @app.get('/api/shorts') def _shorts(refresh:int=Query(default=0)): if _bg_shorts['d']: if time.time()-_bg_shorts['t']>600:threading.Thread(target=_bg,daemon=True).start() return JSONResponse(_bg_shorts['d']) threading.Thread(target=_bg,daemon=True).start() return JSONResponse([]) # ============================================================ # HELPERS # ============================================================ def _extract_title(text): if not text:return 'Bài viết AI' lines=[l.strip() for l in text.strip().split('\n') if l.strip()] if lines: first=re.sub(r'^[#*\-•\d\.\)\s]+','',lines[0]).strip() if 10<=len(first)<=120:return first return lines[0][:100] if lines else 'Bài viết AI' def _clean_text(text): if not text:return text for junk in ['xem trên VNEWS','Xem trên VNEWS','📖 Xem trên VNEWS','đọc trên VNEWS','Đọc trên VNEWS','Mở nguồn gốc','mở nguồn gốc','📖 Đọc trên']: text=text.replace(junk,'') return re.sub(r'\n{3,}','\n\n',text).strip() def _source_image(sources, details): for s in (details or [])+(sources or []): url=s.get('url','') if not url:continue try:_,_,img=_scrape(url,500) except:img='' if img and 'pollinations' not in img and len(img)>20:return img return '' def _ensure_img(img): return img if (img and len(img)>20 and img.startswith('http')) else DEFAULT_IMG # ============================================================ # TOPIC POST (source image instead of AI image) # ============================================================ @app.post('/api/topic_post') async def _topic(request:Request): b=await request.json();topic=clean(b.get('topic','')) if not topic:return JSONResponse({'error':'missing topic'},status_code=400) research=f6._fast_context(topic) if hasattr(f6,'_fast_context') else f6._web_research_context(topic) ctx=research.get('context','');src=research.get('sources',[]) det=f6._extract_source_details_from_context(ctx,src) if hasattr(f6,'_extract_source_details_from_context') else [] if not ctx or not src:return JSONResponse({'error':'Không tìm được nội dung.'},status_code=422) img=_ensure_img(_source_image(src,det) or f6._topic_image(topic)) sb='\n\n'.join([f"[{i+1}] {d.get('title','')} ({d.get('via','')})\n{d.get('content','')[:1400]}" for i,d in enumerate(det)]) if det else ctx[:18000] text=None try:text=await asyncio.wait_for(f5.base.qwen_generate(f'Viết bài tiếng Việt VỀ: "{topic}"\nNGUỒN:\n{sb[:18000]}\nCHỈ viết về "{topic}". 5-8 đoạn. Cuối có nguồn.',image_url=img,max_tokens=1700),timeout=35) except:pass if not text or len(text)<300: text=f"{topic}: tổng hợp\n\n"+'\n'.join([f"• {d['title']}: {d.get('content','')[:300]}" for d in (det or [])[:6]])+"\n\nNguồn: "+', '.join(sorted({d.get('via','') for d in (det or []) if d.get('via')})) text=_clean_text(text) post=f5.base.make_post(topic,text,img,'','topic_focused',sources=[s for s in src if s.get('url')]) post['images']=[img];post['source_details']=det ps=f5.base._load_ai_wall();ps.insert(0,post);f5.base._save_ai_wall(ps) return JSONResponse({'post':post}) # ============================================================ # REWRITE (auto-title, clean text) # ============================================================ @app.post('/api/rewrite_share') @app.post('/api/url_wall') async def _rewrite(request:Request): b=await request.json();url=clean(b.get('url',''));ctx=clean(b.get('context','')) if not url.startswith('http'):return JSONResponse({'error':'URL không hợp lệ'},status_code=400) title,raw,img=_scrape(url,14000) if len(raw)<50:raw=ctx[:14000] if len(raw)<50:return JSONResponse({'error':'Không đọc được bài'},status_code=422) img=_ensure_img(img) prompt=f"""Tóm tắt bài viết thành bản tin ngắn. Dòng đầu tiên là tiêu đề mới hấp dẫn (tự đặt, không copy gốc). Tiêu đề gốc: {title} Nội dung: {raw[:14000]} Yêu cầu: - Dòng 1: Tiêu đề MỚI ngắn gọn hấp dẫn. - Tiếp: 4-6 ý chính. - Cuối: nguồn. - KHÔNG viết bất kỳ cụm điều hướng nào.""" text=None try:text=await asyncio.wait_for(f5.base.qwen_generate(prompt,image_url=img,max_tokens=1000),timeout=30) except:pass if not text or len(text)<80:text=f"{title}\n\n{raw[:1200]}\n\nNguồn: {_domain(url)}" text=_clean_text(text) ai_title=_extract_title(text) lines=text.strip().split('\n') body='\n'.join(lines[1:]).strip() if lines and lines[0].strip()==ai_title else text post=f5.base.make_post(ai_title,_clean_text(body),img,url,'rewrite',sources=[{'title':title,'url':url,'via':_domain(url)}]) ps=f5.base._load_ai_wall();ps.insert(0,post);f5.base._save_ai_wall(ps) return JSONResponse({'post':post}) @app.post('/api/topic/rewrite') async def _topic_rewrite(request:Request): b=await request.json();pid=str(b.get('post_id','')).strip() if not pid:return JSONResponse({'error':'missing post_id'},status_code=400) ps=f5.base._load_ai_wall();p=next((x for x in ps if str(x.get('id'))==pid),None) if not p:return JSONResponse({'error':'Bài không tồn tại'},status_code=404) urls=list(dict.fromkeys([s['url'] for s in (p.get('source_details') or []) if s.get('url')]+[s['url'] for s in (p.get('sources') or []) if s.get('url')]))[:5] parts=[];best_img='' for u in urls: t,r,uimg=_scrape(u,6000) if r and len(r)>150:parts.append(f"[{_domain(u)}] {t}\n{r}") if not best_img and uimg and len(uimg)>20:best_img=uimg ac='\n---\n'.join(parts) if parts else (p.get('text') or '') img=_ensure_img(best_img or p.get('img','')) prompt=f"""Viết lại thành bản tóm tắt mới. Dòng đầu là tiêu đề mới hấp dẫn. Chủ đề: {p.get('title','')} Nguồn: {ac[:16000]} Yêu cầu: Dòng 1 = tiêu đề mới. Tiếp: 4-6 ý. Cuối: nguồn. KHÔNG viết cụm điều hướng.""" text=None try:text=await asyncio.wait_for(f5.base.qwen_generate(prompt,image_url=img,max_tokens=1200),timeout=35) except:pass if not text or len(text)<100:text=f"Tóm tắt: {p.get('title','')}\n\n{ac[:1500]}\n\nNguồn: VNEWS AI" text=_clean_text(text) ai_title=_extract_title(text) lines=text.strip().split('\n') body='\n'.join(lines[1:]).strip() if lines and lines[0].strip()==ai_title else text np=f5.base.make_post(ai_title,_clean_text(body),img,'','rewrite_topic',sources=p.get('sources',[]));np['images']=[img] all_p=f5.base._load_ai_wall();all_p.insert(0,np);f5.base._save_ai_wall(all_p) return JSONResponse({'post':np}) # ============================================================ # UNIFIED INJECT: everything in one clean block # ============================================================ UNIFIED_INJECT = r'''
''' # ============================================================ # ROOT ROUTE: inject order matters # ============================================================ @app.get('/') async def _index(): html = f5.f4.f3.f2.f1._load_index_html() # Inject order: PRE_KILL (in UNIFIED) → old injects → PATCH_INJECT → UNIFIED body = '' body += getattr(rt.old,'PATCH_INJECT','') body += f5.f4.f3.f2.f1.FINAL_INJECT + f5.f4.f3.FINAL3_INJECT + f5.f4.FINAL4_INJECT + f5.FINAL5_INJECT body += getattr(f6,'FINAL6_INJECT','') body += getattr(f6,'FINAL6_FAST_HOME_INJECT','') body += getattr(f6,'FINAL6E_INJECT','') # Keep it — our PRE_KILL in UNIFIED neutralizes its destructive parts body += PATCH_INJECT body += UNIFIED_INJECT # This goes LAST and contains PRE_KILL at the TOP (runs first in browser) return HTMLResponse(html.replace('