Spaces:
Running
Running
File size: 3,235 Bytes
07e8c38 | 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 | """PATCH AI: prepend AI topics to hot list + homepage route fix"""
import re, json, time
from fastapi.responses import HTMLResponse
# Import at runtime to avoid circular
try:
from main import app, rt
import ai_runtime_final6 as f6
from ai_runtime_final6 import f5
except:
f6, f5, rt = None, None, None
# Patch hot_topics to prepend AI topics
if f6 and hasattr(f6, '_HOT_CACHE') and hasattr(f6, '_hot_topics'):
_orig_hot = f6._hot_topics
def _hot_topics_patched():
topics = _orig_hot()
# Prepend AI topics to front
for ai in ['Công nghệ AI', 'World Cup 2026', 'Kinh tế Việt Nam']:
if not any(ai.lower() == t.get('topic','').lower() for t in topics):
topics.insert(0, {'label': f'#{ai.replace(" ", "")}', 'topic': ai, 'count': 0})
return topics[:24]
f6._hot_topics = f6._HOT_CACHE['d'] = _hot_topics_patched()
f6._HOT_CACHE['t'] = time.time()
PATCH_INJECT = r'''
<script>
const AI_HOT_TOPICS = ['Công nghệ AI', 'World Cup 2026', 'Kinh tế Việt Nam', 'Bóng đá châu Âu'];
async function ensureHotTopics(){let i=document.getElementById('ai-topic-input-final5');if(!i||document.getElementById('ai-hot-row'))return;let r=document.createElement('div');r.id='ai-hot-row';r.style.cssText='display:flex;gap:6px;overflow-x:auto;padding:4px 0;margin:6px 0';let t=[];try{let j=await fetch('/api/hot_topics').then(r=>r.json()).catch(()=>({topics:[]}));t=j.topics||[];}catch(e){}AI_HOT_TOPICS.forEach(ai=>{if(!t.find(x=>(x.topic||'').toLowerCase()===ai.toLowerCase())){t.unshift({label:'#'+ai.replace(/\s+/g,''),topic:ai});}});r.innerHTML=t.slice(0,14).map(x=>`<button class="hot-chip" style="flex:0 0 auto;background:#222;border:1px solid #333;color:#ddd;border-radius:16px;padding:5px 10px;font-size:11px;cursor:pointer" onclick="document.getElementById('ai-topic-input-final5').value='${x.topic.replace(/'/g,'\\''}';document.getElementById('ai-topic-input-final5').focus();searchTopic('${x.topic.replace(/'/g,'\\''}')">${x.label}</button>`).join('');i.insertAdjacentElement('afterend',r);}
setInterval(ensureHotTopics,1500);
</script>
'''
# Register homepage route
if app and f5 and f6:
# Remove old / route
app.router.routes = [r for r in app.router.routes if not (getattr(r,'path',None)=='/' and 'GET' in getattr(r,'methods',set()))]
@app.get('/')
async def patch_homepage():
html = f5.f4.f3.f2.f1._load_index_html() if f5 else "<html><body></body></html>"
body = (getattr(rt.old,'PATCH_INJECT','') if hasattr(rt,'old') else '') + \
(getattr(f5.f4.f3.f2.f1,'FINAL_INJECT','') if f5 else '') + \
(getattr(f5.f4.f3,'FINAL3_INJECT','') if f5 else '') + \
(getattr(f5.f4,'FINAL4_INJECT','') if f5 else '') + \
(getattr(f5,'FINAL5_INJECT','') if f5 else '') + \
(getattr(f6,'FINAL6_INJECT','') or '') + \
(getattr(f6,'FINAL6_FAST_HOME_INJECT','') or '') + \
(getattr(f6,'FINAL6E_INJECT','') or '') + \
PATCH_INJECT
if '</body>' in html:
html = html.replace('</body>', body + '\n</body>')
else:
html += body
return HTMLResponse(html) |