"""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''' ''' # 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 "" 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 '' in html: html = html.replace('', body + '\n') else: html += body return HTMLResponse(html)