Spaces:
Running
Running
fix share: redirect via ?open= query param (no localStorage), fix comment: click outside closes panel
Browse files
main.py
CHANGED
|
@@ -107,10 +107,7 @@ def scrape_24h_highlights():
|
|
| 107 |
arts.append({"title":title,"link":href,"img":img_src,"source":"24h"})
|
| 108 |
def _og(art):
|
| 109 |
if art["img"]:return
|
| 110 |
-
try:
|
| 111 |
-
r=requests.get(art["link"],headers=HEADERS,timeout=8);r.encoding="utf-8"
|
| 112 |
-
og=BeautifulSoup(r.text,"lxml").find("meta",property="og:image")
|
| 113 |
-
if og:art["img"]=og.get("content","")
|
| 114 |
except:pass
|
| 115 |
need=[a for a in arts if not a["img"]][:10]
|
| 116 |
if need:
|
|
@@ -128,8 +125,7 @@ def scrape_24h_shorts():
|
|
| 128 |
href=a.get("href","")
|
| 129 |
if not href.startswith("http"):href=BASE_24H+href
|
| 130 |
if href in seen:continue
|
| 131 |
-
img_tag=art.find("img")
|
| 132 |
-
title=(img_tag.get("alt","") if img_tag else "") or a.get("title","") or a.get_text(strip=True)
|
| 133 |
if not title or len(title)<10:continue
|
| 134 |
img_src=""
|
| 135 |
if img_tag:img_src=img_tag.get("data-original") or img_tag.get("data-src") or img_tag.get("src","")
|
|
@@ -296,12 +292,13 @@ def api_article(url:str=Query(...)):
|
|
| 296 |
else:data=None
|
| 297 |
return JSONResponse(data if data else{"error":"not supported"})
|
| 298 |
|
| 299 |
-
# Share page: SEO
|
| 300 |
@app.get("/s")
|
| 301 |
async def share_redirect(url:str=Query(default=""),title:str=Query(default="VNEWS"),img:str=Query(default="")):
|
| 302 |
og_image=unquote(img) if img else "https://s1.vnecdn.net/vnexpress/restruct/i/v9505/logo_default.jpg"
|
| 303 |
decoded_url=unquote(url)
|
| 304 |
-
|
|
|
|
| 305 |
return HTMLResponse(f'''<!DOCTYPE html><html><head>
|
| 306 |
<meta charset="utf-8"><title>{unquote(title)}</title>
|
| 307 |
<meta property="og:title" content="{unquote(title)}">
|
|
@@ -313,7 +310,7 @@ async def share_redirect(url:str=Query(default=""),title:str=Query(default="VNEW
|
|
| 313 |
<meta name="twitter:card" content="summary_large_image">
|
| 314 |
<meta name="twitter:title" content="{unquote(title)}">
|
| 315 |
<meta name="twitter:image" content="{og_image}">
|
| 316 |
-
</head><body>{
|
| 317 |
|
| 318 |
@app.get("/")
|
| 319 |
async def index():
|
|
|
|
| 107 |
arts.append({"title":title,"link":href,"img":img_src,"source":"24h"})
|
| 108 |
def _og(art):
|
| 109 |
if art["img"]:return
|
| 110 |
+
try:r=requests.get(art["link"],headers=HEADERS,timeout=8);r.encoding="utf-8";og=BeautifulSoup(r.text,"lxml").find("meta",property="og:image");art["img"]=og.get("content","") if og else ""
|
|
|
|
|
|
|
|
|
|
| 111 |
except:pass
|
| 112 |
need=[a for a in arts if not a["img"]][:10]
|
| 113 |
if need:
|
|
|
|
| 125 |
href=a.get("href","")
|
| 126 |
if not href.startswith("http"):href=BASE_24H+href
|
| 127 |
if href in seen:continue
|
| 128 |
+
img_tag=art.find("img");title=(img_tag.get("alt","") if img_tag else "") or a.get("title","") or a.get_text(strip=True)
|
|
|
|
| 129 |
if not title or len(title)<10:continue
|
| 130 |
img_src=""
|
| 131 |
if img_tag:img_src=img_tag.get("data-original") or img_tag.get("data-src") or img_tag.get("src","")
|
|
|
|
| 292 |
else:data=None
|
| 293 |
return JSONResponse(data if data else{"error":"not supported"})
|
| 294 |
|
| 295 |
+
# Share page: SEO og:image for crawlers. Redirect users via ?open= query param (NOT localStorage)
|
| 296 |
@app.get("/s")
|
| 297 |
async def share_redirect(url:str=Query(default=""),title:str=Query(default="VNEWS"),img:str=Query(default="")):
|
| 298 |
og_image=unquote(img) if img else "https://s1.vnecdn.net/vnexpress/restruct/i/v9505/logo_default.jpg"
|
| 299 |
decoded_url=unquote(url)
|
| 300 |
+
# Redirect to /?open=ENCODED_URL — frontend reads query param directly (no localStorage needed)
|
| 301 |
+
redirect_target=f"{SPACE_URL}/?open={quote(decoded_url,'')}" if decoded_url else SPACE_URL
|
| 302 |
return HTMLResponse(f'''<!DOCTYPE html><html><head>
|
| 303 |
<meta charset="utf-8"><title>{unquote(title)}</title>
|
| 304 |
<meta property="og:title" content="{unquote(title)}">
|
|
|
|
| 310 |
<meta name="twitter:card" content="summary_large_image">
|
| 311 |
<meta name="twitter:title" content="{unquote(title)}">
|
| 312 |
<meta name="twitter:image" content="{og_image}">
|
| 313 |
+
</head><body><script>window.location.replace("{redirect_target}");</script></body></html>''')
|
| 314 |
|
| 315 |
@app.get("/")
|
| 316 |
async def index():
|