Spaces:
Running
Running
revert: restore index.html + main.py to ed6aeb52 (stable version before comment bug)
Browse files
main.py
CHANGED
|
@@ -15,18 +15,15 @@ BASE_24H = "https://www.24h.com.vn"
|
|
| 15 |
SPACE_URL = "https://bep40-vnews.hf.space"
|
| 16 |
_cache = {}
|
| 17 |
_cache_ttl = 300
|
| 18 |
-
|
| 19 |
def _cached(key, fn):
|
| 20 |
now=time.time()
|
| 21 |
if key in _cache and now-_cache[key]["t"]<_cache_ttl:return _cache[key]["d"]
|
| 22 |
try:data=fn()
|
| 23 |
except:data=_cache.get(key,{}).get("d",[])
|
| 24 |
_cache[key]={"d":data,"t":now};return data
|
| 25 |
-
|
| 26 |
def _get(url,headers=None):
|
| 27 |
h=headers or HEADERS;r=requests.get(url,headers=h,timeout=15);r.encoding="utf-8"
|
| 28 |
return BeautifulSoup(r.text,"lxml")
|
| 29 |
-
|
| 30 |
def scrape_vne(cat_url):
|
| 31 |
try:
|
| 32 |
soup=_get(cat_url);arts=[]
|
|
@@ -43,7 +40,6 @@ def scrape_vne(cat_url):
|
|
| 43 |
arts.append({"title":t,"link":lk,"img":img,"summary":(desc.get_text(strip=True)[:150] if desc else ""),"source":"vne"})
|
| 44 |
return arts
|
| 45 |
except:return[]
|
| 46 |
-
|
| 47 |
def scrape_vne_article(url):
|
| 48 |
try:
|
| 49 |
soup=_get(url);h1=soup.select_one("h1.title-detail");desc=soup.select_one("p.description")
|
|
@@ -59,45 +55,6 @@ def scrape_vne_article(url):
|
|
| 59 |
elif ch.name in("h2","h3"):body.append({"type":"heading","text":ch.get_text(strip=True)})
|
| 60 |
return{"title":h1.get_text(strip=True) if h1 else "","summary":desc.get_text(strip=True) if desc else "","og_image":og_img,"body":body,"source":"vne","url":url}
|
| 61 |
except:return None
|
| 62 |
-
|
| 63 |
-
def scrape_bdp_article(url):
|
| 64 |
-
try:
|
| 65 |
-
soup=_get(url);h1=soup.select_one(".lead-title h1") or soup.select_one("h1")
|
| 66 |
-
og=soup.find("meta",property="og:image");og_img=og.get("content","") if og else ""
|
| 67 |
-
se=soup.select_one("div.summary");body=[]
|
| 68 |
-
cd=soup.select_one("div.content#postContent") or soup.select_one("div.content")
|
| 69 |
-
if cd:
|
| 70 |
-
for ch in cd.children:
|
| 71 |
-
if not hasattr(ch,'name') or not ch.name:continue
|
| 72 |
-
if ch.name=="p":
|
| 73 |
-
im=ch.find("img")
|
| 74 |
-
if im:
|
| 75 |
-
s=im.get("src") or im.get("data-src","")
|
| 76 |
-
if s:body.append({"type":"img","src":s,"alt":""})
|
| 77 |
-
t=ch.get_text(strip=True)
|
| 78 |
-
if t:body.append({"type":"p","text":t})
|
| 79 |
-
elif ch.name in("h2","h3"):body.append({"type":"heading","text":ch.get_text(strip=True)})
|
| 80 |
-
return{"title":h1.get_text(strip=True) if h1 else "","summary":se.get_text(strip=True) if se else "","og_image":og_img,"body":body,"source":"bdp","url":url}
|
| 81 |
-
except:return None
|
| 82 |
-
|
| 83 |
-
def scrape_24h_article(url):
|
| 84 |
-
try:
|
| 85 |
-
r=requests.get(url,headers=HEADERS,timeout=15);r.encoding="utf-8"
|
| 86 |
-
soup=BeautifulSoup(r.text,"lxml");h1=soup.select_one("h1")
|
| 87 |
-
og=soup.find("meta",property="og:image");og_img=og.get("content","") if og else ""
|
| 88 |
-
desc=soup.find("meta",property="og:description");summary=desc.get("content","")[:200] if desc else ""
|
| 89 |
-
body=[]
|
| 90 |
-
for sel in["div.the-article-body","div.nws-mainContent","div.nwsCt","div.detail-content"]:
|
| 91 |
-
cd=soup.select_one(sel)
|
| 92 |
-
if cd:
|
| 93 |
-
for ch in cd.children:
|
| 94 |
-
if not hasattr(ch,'name') or not ch.name:continue
|
| 95 |
-
if ch.name=="p":t=ch.get_text(strip=True);(body.append({"type":"p","text":t}) if t else None)
|
| 96 |
-
elif ch.name in("h2","h3"):body.append({"type":"heading","text":ch.get_text(strip=True)})
|
| 97 |
-
break
|
| 98 |
-
return{"title":h1.get_text(strip=True) if h1 else "","summary":summary,"og_image":og_img,"body":body,"source":"24h","url":url}
|
| 99 |
-
except:return None
|
| 100 |
-
|
| 101 |
def scrape_bdp(url):
|
| 102 |
try:
|
| 103 |
soup=_get(url);arts=[];seen=set()
|
|
@@ -113,7 +70,6 @@ def scrape_bdp(url):
|
|
| 113 |
seen.add(lk);arts.append({"title":t,"link":lk,"img":img,"source":"bdp","summary":""})
|
| 114 |
return arts[:15]
|
| 115 |
except:return[]
|
| 116 |
-
|
| 117 |
def scrape_bdp_videos():
|
| 118 |
try:
|
| 119 |
soup=_get(f"{BASE_BDP}/video");arts=[];seen=set()
|
|
@@ -129,7 +85,6 @@ def scrape_bdp_videos():
|
|
| 129 |
seen.add(href);arts.append({"title":title,"link":href,"img":img,"source":"bdp"})
|
| 130 |
return arts[:20]
|
| 131 |
except:return[]
|
| 132 |
-
|
| 133 |
def scrape_24h_highlights():
|
| 134 |
try:
|
| 135 |
soup=_get(f"{BASE_24H}/video-highlight-c953.html");arts=[];seen=set()
|
|
@@ -152,7 +107,6 @@ def scrape_24h_highlights():
|
|
| 152 |
with ThreadPoolExecutor(5) as ex:list(ex.map(_og,need))
|
| 153 |
return arts[:30]
|
| 154 |
except:return[]
|
| 155 |
-
|
| 156 |
def scrape_24h_shorts():
|
| 157 |
try:
|
| 158 |
r=requests.get(f"{BASE_24H}/video/video-tin-tuc-cvd769.html",headers=HEADERS,timeout=15);r.encoding="utf-8"
|
|
@@ -184,7 +138,6 @@ def scrape_24h_shorts():
|
|
| 184 |
seen.add(href);arts.append({"title":title,"link":href,"img":img_src,"source":"24h-shorts"})
|
| 185 |
return arts[:20]
|
| 186 |
except:return[]
|
| 187 |
-
|
| 188 |
def scrape_bbc_vietnamese():
|
| 189 |
try:
|
| 190 |
bbc_h={"User-Agent":"Mozilla/5.0","Accept-Language":"en-GB,en;q=0.9"}
|
|
@@ -208,7 +161,6 @@ def scrape_bbc_vietnamese():
|
|
| 208 |
if len(arts)>=15:break
|
| 209 |
return arts
|
| 210 |
except:return[]
|
| 211 |
-
|
| 212 |
def scrape_bbc_article(url):
|
| 213 |
try:
|
| 214 |
r=requests.get(url,headers={"User-Agent":"Mozilla/5.0","Accept-Language":"en-GB"},timeout=15);r.encoding="utf-8"
|
|
@@ -223,7 +175,6 @@ def scrape_bbc_article(url):
|
|
| 223 |
if src and("ichef" in src or"bbci" in src):body.append({"type":"img","src":src,"alt":img.get("alt","")})
|
| 224 |
return{"title":h1.get_text(strip=True) if h1 else "","summary":"","og_image":og_img,"body":body,"source":"bbc","url":url}
|
| 225 |
except:return None
|
| 226 |
-
|
| 227 |
def extract_video_url(article_url):
|
| 228 |
try:
|
| 229 |
r=requests.get(article_url,headers={**HEADERS,"Referer":"https://www.24h.com.vn/"},timeout=10);r.encoding="utf-8"
|
|
@@ -233,8 +184,7 @@ def extract_video_url(article_url):
|
|
| 233 |
primary=full or p720
|
| 234 |
if not primary:return None
|
| 235 |
soup=BeautifulSoup(r.text,"lxml");og=soup.find("meta",property="og:image");poster=og.get("content","") if og else ""
|
| 236 |
-
base_url=primary[0];parts=[base_url]
|
| 237 |
-
is_720p='_720p.m3u8' in base_url;name=base_url.replace('_720p.m3u8','').replace('.m3u8','')
|
| 238 |
m=re.search(r'(\D)(\d{1,2})$',name)
|
| 239 |
if m:
|
| 240 |
cur=int(m.group(2));w=len(m.group(2));bn=name[:m.start(2)]
|
|
@@ -260,7 +210,6 @@ def extract_video_url(article_url):
|
|
| 260 |
if len(parts)>1:return{"src":parts[0],"poster":poster,"all_parts":parts}
|
| 261 |
return{"src":primary[0],"poster":poster}
|
| 262 |
except:return None
|
| 263 |
-
|
| 264 |
def extract_bdp_video(url):
|
| 265 |
try:
|
| 266 |
m=re.search(r'-(\d{6,})\.html',url)
|
|
@@ -271,11 +220,8 @@ def extract_bdp_video(url):
|
|
| 271 |
source=video.find("source")
|
| 272 |
return{"src":source.get("src","") if source else "","poster":video.get("poster","")}
|
| 273 |
except:return None
|
| 274 |
-
|
| 275 |
-
# API
|
| 276 |
VNE_CATS={"thoi-su":("https://vnexpress.net/thoi-su","Thời Sự"),"the-gioi":("https://vnexpress.net/the-gioi","Thế Giới"),"kinh-doanh":("https://vnexpress.net/kinh-doanh","Kinh Doanh"),"cong-nghe":("https://vnexpress.net/so-hoa","Công Nghệ"),"the-thao":("https://vnexpress.net/the-thao","Thể Thao"),"giai-tri":("https://vnexpress.net/giai-tri","Giải Trí"),"suc-khoe":("https://vnexpress.net/suc-khoe","Sức Khỏe")}
|
| 277 |
BDP_CATS={"ngoai-hang-anh":("https://bongdaplus.vn/ngoai-hang-anh","Ngoại Hạng Anh"),"la-liga":("https://bongdaplus.vn/la-liga","La Liga"),"champions-league":("https://bongdaplus.vn/champions-league-cup-c1","Champions League"),"bong-da-vn":("https://bongdaplus.vn/bong-da-viet-nam","Bóng Đá VN")}
|
| 278 |
-
|
| 279 |
@app.get("/api/homepage")
|
| 280 |
def api_homepage():
|
| 281 |
def _f():
|
|
@@ -292,7 +238,6 @@ def api_homepage():
|
|
| 292 |
except:pass
|
| 293 |
return articles
|
| 294 |
return JSONResponse(_cached("homepage",_f))
|
| 295 |
-
|
| 296 |
@app.get("/api/category/{cat_id}")
|
| 297 |
def api_category(cat_id:str):
|
| 298 |
def _f():
|
|
@@ -301,43 +246,35 @@ def api_category(cat_id:str):
|
|
| 301 |
if cat_id in BDP_CATS:arts=scrape_bdp(BDP_CATS[cat_id][0]);[a.update({"group":BDP_CATS[cat_id][1]}) for a in arts];return arts
|
| 302 |
return[]
|
| 303 |
return JSONResponse(_cached(f"cat_{cat_id}",_f))
|
| 304 |
-
|
| 305 |
@app.get("/api/categories")
|
| 306 |
def api_categories():
|
| 307 |
cats=[{"id":"bbc","name":"BBC Tiếng Việt","source":"bbc"}]
|
| 308 |
for k,(u,n) in VNE_CATS.items():cats.append({"id":k,"name":n,"source":"vne"})
|
| 309 |
for k,(u,n) in BDP_CATS.items():cats.append({"id":k,"name":n,"source":"bdp"})
|
| 310 |
return JSONResponse(cats)
|
| 311 |
-
|
| 312 |
@app.get("/api/highlights")
|
| 313 |
def api_highlights():return JSONResponse(_cached("highlights",scrape_24h_highlights))
|
| 314 |
@app.get("/api/shorts")
|
| 315 |
def api_shorts():return JSONResponse(_cached("shorts",scrape_24h_shorts))
|
| 316 |
@app.get("/api/bdp_videos")
|
| 317 |
def api_bdp_videos():return JSONResponse(_cached("bdp_videos",scrape_bdp_videos))
|
| 318 |
-
|
| 319 |
@app.get("/api/video_url")
|
| 320 |
def api_video_url(url:str=Query(...)):
|
| 321 |
if"24h.com.vn" in url:v=extract_video_url(url)
|
| 322 |
elif"bongdaplus.vn" in url:v=extract_bdp_video(url)
|
| 323 |
else:v=None
|
| 324 |
return JSONResponse(v if v else{"error":"not found"})
|
| 325 |
-
|
| 326 |
-
# ★ FIX: supports ALL sources now
|
| 327 |
@app.get("/api/article")
|
| 328 |
def api_article(url:str=Query(...)):
|
| 329 |
-
data=None
|
| 330 |
if"vnexpress.net" in url:data=scrape_vne_article(url)
|
| 331 |
elif"bbc.com" in url:data=scrape_bbc_article(url)
|
| 332 |
-
|
| 333 |
-
elif"24h.com.vn" in url:data=scrape_24h_article(url)
|
| 334 |
return JSONResponse(data if data else{"error":"not supported"})
|
| 335 |
-
|
| 336 |
@app.get("/s")
|
| 337 |
async def share_redirect(url:str=Query(default=""),title:str=Query(default="VNEWS"),img:str=Query(default="")):
|
| 338 |
og_image=unquote(img) if img else "https://s1.vnecdn.net/vnexpress/restruct/i/v9505/logo_default.jpg"
|
| 339 |
decoded_url=unquote(url)
|
| 340 |
-
|
| 341 |
return HTMLResponse(f'''<!DOCTYPE html><html><head>
|
| 342 |
<meta charset="utf-8"><title>{unquote(title)}</title>
|
| 343 |
<meta property="og:title" content="{unquote(title)}">
|
|
@@ -349,10 +286,8 @@ async def share_redirect(url:str=Query(default=""),title:str=Query(default="VNEW
|
|
| 349 |
<meta name="twitter:card" content="summary_large_image">
|
| 350 |
<meta name="twitter:title" content="{unquote(title)}">
|
| 351 |
<meta name="twitter:image" content="{og_image}">
|
| 352 |
-
</head><body>
|
| 353 |
-
|
| 354 |
@app.get("/")
|
| 355 |
async def index():
|
| 356 |
with open("/app/static/index.html","r",encoding="utf-8") as f:return HTMLResponse(content=f.read())
|
| 357 |
-
|
| 358 |
app.mount("/static",StaticFiles(directory="/app/static"),name="static")
|
|
|
|
| 15 |
SPACE_URL = "https://bep40-vnews.hf.space"
|
| 16 |
_cache = {}
|
| 17 |
_cache_ttl = 300
|
|
|
|
| 18 |
def _cached(key, fn):
|
| 19 |
now=time.time()
|
| 20 |
if key in _cache and now-_cache[key]["t"]<_cache_ttl:return _cache[key]["d"]
|
| 21 |
try:data=fn()
|
| 22 |
except:data=_cache.get(key,{}).get("d",[])
|
| 23 |
_cache[key]={"d":data,"t":now};return data
|
|
|
|
| 24 |
def _get(url,headers=None):
|
| 25 |
h=headers or HEADERS;r=requests.get(url,headers=h,timeout=15);r.encoding="utf-8"
|
| 26 |
return BeautifulSoup(r.text,"lxml")
|
|
|
|
| 27 |
def scrape_vne(cat_url):
|
| 28 |
try:
|
| 29 |
soup=_get(cat_url);arts=[]
|
|
|
|
| 40 |
arts.append({"title":t,"link":lk,"img":img,"summary":(desc.get_text(strip=True)[:150] if desc else ""),"source":"vne"})
|
| 41 |
return arts
|
| 42 |
except:return[]
|
|
|
|
| 43 |
def scrape_vne_article(url):
|
| 44 |
try:
|
| 45 |
soup=_get(url);h1=soup.select_one("h1.title-detail");desc=soup.select_one("p.description")
|
|
|
|
| 55 |
elif ch.name in("h2","h3"):body.append({"type":"heading","text":ch.get_text(strip=True)})
|
| 56 |
return{"title":h1.get_text(strip=True) if h1 else "","summary":desc.get_text(strip=True) if desc else "","og_image":og_img,"body":body,"source":"vne","url":url}
|
| 57 |
except:return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
def scrape_bdp(url):
|
| 59 |
try:
|
| 60 |
soup=_get(url);arts=[];seen=set()
|
|
|
|
| 70 |
seen.add(lk);arts.append({"title":t,"link":lk,"img":img,"source":"bdp","summary":""})
|
| 71 |
return arts[:15]
|
| 72 |
except:return[]
|
|
|
|
| 73 |
def scrape_bdp_videos():
|
| 74 |
try:
|
| 75 |
soup=_get(f"{BASE_BDP}/video");arts=[];seen=set()
|
|
|
|
| 85 |
seen.add(href);arts.append({"title":title,"link":href,"img":img,"source":"bdp"})
|
| 86 |
return arts[:20]
|
| 87 |
except:return[]
|
|
|
|
| 88 |
def scrape_24h_highlights():
|
| 89 |
try:
|
| 90 |
soup=_get(f"{BASE_24H}/video-highlight-c953.html");arts=[];seen=set()
|
|
|
|
| 107 |
with ThreadPoolExecutor(5) as ex:list(ex.map(_og,need))
|
| 108 |
return arts[:30]
|
| 109 |
except:return[]
|
|
|
|
| 110 |
def scrape_24h_shorts():
|
| 111 |
try:
|
| 112 |
r=requests.get(f"{BASE_24H}/video/video-tin-tuc-cvd769.html",headers=HEADERS,timeout=15);r.encoding="utf-8"
|
|
|
|
| 138 |
seen.add(href);arts.append({"title":title,"link":href,"img":img_src,"source":"24h-shorts"})
|
| 139 |
return arts[:20]
|
| 140 |
except:return[]
|
|
|
|
| 141 |
def scrape_bbc_vietnamese():
|
| 142 |
try:
|
| 143 |
bbc_h={"User-Agent":"Mozilla/5.0","Accept-Language":"en-GB,en;q=0.9"}
|
|
|
|
| 161 |
if len(arts)>=15:break
|
| 162 |
return arts
|
| 163 |
except:return[]
|
|
|
|
| 164 |
def scrape_bbc_article(url):
|
| 165 |
try:
|
| 166 |
r=requests.get(url,headers={"User-Agent":"Mozilla/5.0","Accept-Language":"en-GB"},timeout=15);r.encoding="utf-8"
|
|
|
|
| 175 |
if src and("ichef" in src or"bbci" in src):body.append({"type":"img","src":src,"alt":img.get("alt","")})
|
| 176 |
return{"title":h1.get_text(strip=True) if h1 else "","summary":"","og_image":og_img,"body":body,"source":"bbc","url":url}
|
| 177 |
except:return None
|
|
|
|
| 178 |
def extract_video_url(article_url):
|
| 179 |
try:
|
| 180 |
r=requests.get(article_url,headers={**HEADERS,"Referer":"https://www.24h.com.vn/"},timeout=10);r.encoding="utf-8"
|
|
|
|
| 184 |
primary=full or p720
|
| 185 |
if not primary:return None
|
| 186 |
soup=BeautifulSoup(r.text,"lxml");og=soup.find("meta",property="og:image");poster=og.get("content","") if og else ""
|
| 187 |
+
base_url=primary[0];parts=[base_url];is_720p='_720p.m3u8' in base_url;name=base_url.replace('_720p.m3u8','').replace('.m3u8','')
|
|
|
|
| 188 |
m=re.search(r'(\D)(\d{1,2})$',name)
|
| 189 |
if m:
|
| 190 |
cur=int(m.group(2));w=len(m.group(2));bn=name[:m.start(2)]
|
|
|
|
| 210 |
if len(parts)>1:return{"src":parts[0],"poster":poster,"all_parts":parts}
|
| 211 |
return{"src":primary[0],"poster":poster}
|
| 212 |
except:return None
|
|
|
|
| 213 |
def extract_bdp_video(url):
|
| 214 |
try:
|
| 215 |
m=re.search(r'-(\d{6,})\.html',url)
|
|
|
|
| 220 |
source=video.find("source")
|
| 221 |
return{"src":source.get("src","") if source else "","poster":video.get("poster","")}
|
| 222 |
except:return None
|
|
|
|
|
|
|
| 223 |
VNE_CATS={"thoi-su":("https://vnexpress.net/thoi-su","Thời Sự"),"the-gioi":("https://vnexpress.net/the-gioi","Thế Giới"),"kinh-doanh":("https://vnexpress.net/kinh-doanh","Kinh Doanh"),"cong-nghe":("https://vnexpress.net/so-hoa","Công Nghệ"),"the-thao":("https://vnexpress.net/the-thao","Thể Thao"),"giai-tri":("https://vnexpress.net/giai-tri","Giải Trí"),"suc-khoe":("https://vnexpress.net/suc-khoe","Sức Khỏe")}
|
| 224 |
BDP_CATS={"ngoai-hang-anh":("https://bongdaplus.vn/ngoai-hang-anh","Ngoại Hạng Anh"),"la-liga":("https://bongdaplus.vn/la-liga","La Liga"),"champions-league":("https://bongdaplus.vn/champions-league-cup-c1","Champions League"),"bong-da-vn":("https://bongdaplus.vn/bong-da-viet-nam","Bóng Đá VN")}
|
|
|
|
| 225 |
@app.get("/api/homepage")
|
| 226 |
def api_homepage():
|
| 227 |
def _f():
|
|
|
|
| 238 |
except:pass
|
| 239 |
return articles
|
| 240 |
return JSONResponse(_cached("homepage",_f))
|
|
|
|
| 241 |
@app.get("/api/category/{cat_id}")
|
| 242 |
def api_category(cat_id:str):
|
| 243 |
def _f():
|
|
|
|
| 246 |
if cat_id in BDP_CATS:arts=scrape_bdp(BDP_CATS[cat_id][0]);[a.update({"group":BDP_CATS[cat_id][1]}) for a in arts];return arts
|
| 247 |
return[]
|
| 248 |
return JSONResponse(_cached(f"cat_{cat_id}",_f))
|
|
|
|
| 249 |
@app.get("/api/categories")
|
| 250 |
def api_categories():
|
| 251 |
cats=[{"id":"bbc","name":"BBC Tiếng Việt","source":"bbc"}]
|
| 252 |
for k,(u,n) in VNE_CATS.items():cats.append({"id":k,"name":n,"source":"vne"})
|
| 253 |
for k,(u,n) in BDP_CATS.items():cats.append({"id":k,"name":n,"source":"bdp"})
|
| 254 |
return JSONResponse(cats)
|
|
|
|
| 255 |
@app.get("/api/highlights")
|
| 256 |
def api_highlights():return JSONResponse(_cached("highlights",scrape_24h_highlights))
|
| 257 |
@app.get("/api/shorts")
|
| 258 |
def api_shorts():return JSONResponse(_cached("shorts",scrape_24h_shorts))
|
| 259 |
@app.get("/api/bdp_videos")
|
| 260 |
def api_bdp_videos():return JSONResponse(_cached("bdp_videos",scrape_bdp_videos))
|
|
|
|
| 261 |
@app.get("/api/video_url")
|
| 262 |
def api_video_url(url:str=Query(...)):
|
| 263 |
if"24h.com.vn" in url:v=extract_video_url(url)
|
| 264 |
elif"bongdaplus.vn" in url:v=extract_bdp_video(url)
|
| 265 |
else:v=None
|
| 266 |
return JSONResponse(v if v else{"error":"not found"})
|
|
|
|
|
|
|
| 267 |
@app.get("/api/article")
|
| 268 |
def api_article(url:str=Query(...)):
|
|
|
|
| 269 |
if"vnexpress.net" in url:data=scrape_vne_article(url)
|
| 270 |
elif"bbc.com" in url:data=scrape_bbc_article(url)
|
| 271 |
+
else:data=None
|
|
|
|
| 272 |
return JSONResponse(data if data else{"error":"not supported"})
|
|
|
|
| 273 |
@app.get("/s")
|
| 274 |
async def share_redirect(url:str=Query(default=""),title:str=Query(default="VNEWS"),img:str=Query(default="")):
|
| 275 |
og_image=unquote(img) if img else "https://s1.vnecdn.net/vnexpress/restruct/i/v9505/logo_default.jpg"
|
| 276 |
decoded_url=unquote(url)
|
| 277 |
+
redirect_script=f'<script>localStorage.setItem("pending_article","{decoded_url}");location.href="{SPACE_URL}";</script>' if decoded_url else f'<script>location.href="{SPACE_URL}";</script>'
|
| 278 |
return HTMLResponse(f'''<!DOCTYPE html><html><head>
|
| 279 |
<meta charset="utf-8"><title>{unquote(title)}</title>
|
| 280 |
<meta property="og:title" content="{unquote(title)}">
|
|
|
|
| 286 |
<meta name="twitter:card" content="summary_large_image">
|
| 287 |
<meta name="twitter:title" content="{unquote(title)}">
|
| 288 |
<meta name="twitter:image" content="{og_image}">
|
| 289 |
+
</head><body>{redirect_script}</body></html>''')
|
|
|
|
| 290 |
@app.get("/")
|
| 291 |
async def index():
|
| 292 |
with open("/app/static/index.html","r",encoding="utf-8") as f:return HTMLResponse(content=f.read())
|
|
|
|
| 293 |
app.mount("/static",StaticFiles(directory="/app/static"),name="static")
|