"""Final wrapper with complete highlight override including interaction buttons. PLUS: Hashtag inline sources on homepage with rewrite button.""" import json, os, time from app_patch_unified import * from app_patch_unified import app, UNIFIED_INJECT, f5, f6, rt, PATCH_INJECT from fastapi.responses import HTMLResponse, JSONResponse from fastapi import Request, Query DATA_DIR="/data" if os.path.isdir('/data') else "/app/data" os.makedirs(DATA_DIR,exist_ok=True) HL_STATS_FILE=os.path.join(DATA_DIR,'highlight_stats.json') def _load_hl(): try: if os.path.exists(HL_STATS_FILE):return json.load(open(HL_STATS_FILE,'r',encoding='utf-8')) except:pass return {} def _save_hl(db): try:open(HL_STATS_FILE+'.tmp','w',encoding='utf-8').write(json.dumps(db,ensure_ascii=False));os.replace(HL_STATS_FILE+'.tmp',HL_STATS_FILE) except:pass app.router.routes=[r for r in app.router.routes if not ( (getattr(r,'path',None)=='/api/highlight/interact' and 'POST' in getattr(r,'methods',set())) or (getattr(r,'path',None)=='/api/highlight/stats' and 'GET' in getattr(r,'methods',set())) or (getattr(r,'path',None)=='/api/hashtag/sources' and 'GET' in getattr(r,'methods',set())) or (getattr(r,'path',None)=='/' and 'GET' in getattr(r,'methods',set())) )] @app.post('/api/highlight/interact') async def _hl_act(request:Request): b=await request.json();vid=str(b.get('id','')).strip();action=str(b.get('action','')).strip() if not vid or action not in ('view','like','share'):return JSONResponse({'error':'invalid'},status_code=400) db=_load_hl();st=db.get(vid,{'views':0,'likes':0,'shares':0}) st[action+'s']=st.get(action+'s',0)+1 db[vid]=st;_save_hl(db);return JSONResponse({'stats':st}) @app.get('/api/highlight/stats') def _hl_stats(ids:str=Query(default='')): db=_load_hl();out={} for vid in ids.split(','): vid=vid.strip() if vid:out[vid]=db.get(vid,{'views':0,'likes':0,'shares':0}) return JSONResponse({'stats':out}) @app.get('/api/hashtag/sources') def _hashtag_sources(topic:str=Query(...)): """Return sources for a hashtag topic to display inline on homepage.""" research=f6._fast_context(topic) if hasattr(f6,'_fast_context') else f6._web_research_context(topic) sources=research.get('sources',[]) # Add og:image for each source from ai_runtime_patch_fast import _scrape for s in sources[:6]: if s.get('url') and not s.get('img'): try:_,_,img=_scrape(s['url'],500) except:img='' s['img']=img if img and len(img)>20 else '' return JSONResponse({'sources':sources[:6],'topic':topic}) # PRE_KILL fix UNIFIED_INJECT_FIXED = UNIFIED_INJECT.replace( """Object.defineProperty(window,'renderAIShorts7',{get:function(){return function(){}},set:function(){},configurable:true});""", """Object.defineProperty(window,'renderAIShorts7',{get:function(){return function(){}},set:function(){},configurable:true}); Object.defineProperty(window,'renderPatchedWall',{get:function(){return function(){}},set:function(){},configurable:true}); Object.defineProperty(window,'renderAiShorts',{get:function(){return function(){}},set:function(){},configurable:true}); Object.defineProperty(window,'renderWall',{get:function(){return function(){}},set:function(){},configurable:true}); Object.defineProperty(window,'renderAIShorts',{get:function(){return function(){}},set:function(){},configurable:true}); Object.defineProperty(window,'loadPatchedWall',{get:function(){return function(){}},set:function(){},configurable:true}); Object.defineProperty(window,'refreshFinalWall3',{get:function(){return function(){}},set:function(){},configurable:true});""" ) # Fix highlight fetch UNIFIED_INJECT_FIXED = UNIFIED_INJECT_FIXED.replace( "var articles=(window._hlLeagueData||{})[league]||[];\n if(!articles.length){el.innerHTML=", "var articles=(window._hlLeagueData||{})[league]||[];\n if(!articles.length){try{var _r=await fetch('/api/highlights/'+league);articles=await _r.json();if(!Array.isArray(articles))articles=[];}catch(e){articles=[];}}\n if(!articles.length){el.innerHTML=" ) # Highlight full override (same as 5a5b626) HIGHLIGHT_FULL_OVERRIDE = r'''
''' EXTRA_WALL_FIX = r''' ''' @app.get('/') async def _index_fixed(): html=f5.f4.f3.f2.f1._load_index_html() 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','') body+=PATCH_INJECT body+=UNIFIED_INJECT_FIXED body+=HIGHLIGHT_FULL_OVERRIDE body+=EXTRA_WALL_FIX return HTMLResponse(html.replace('',body+'\n') if '' in html else html+body)