Spaces:
Running
Running
| """VNEWS - FastAPI backend with livescore + xemlaibongda highlights + VTV channels""" | |
| import re, time, subprocess, json, os, threading | |
| import html as html_lib | |
| from datetime import datetime, timezone, timedelta, date | |
| from collections import defaultdict | |
| VN_TZ = timezone(timedelta(hours=7)) | |
| from concurrent.futures import ThreadPoolExecutor, as_completed | |
| from fastapi import FastAPI, Query, Request | |
| from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse, Response | |
| from urllib.parse import quote | |
| import requests | |
| from bs4 import BeautifulSoup | |
| app = FastAPI() | |
| # ===== WORLD CUP 2026 SCRAPER ===== | |
| from wc2026_scraper import get_wc2026_all, scrape_fixtures, scrape_standings, scrape_stats, scrape_wc_news | |
| # ===== RATE LIMITING ===== | |
| _rate_limit_data = defaultdict(list) | |
| _rate_limit_lock = threading.Lock() | |
| RATE_LIMIT_MAX = 60 | |
| RATE_LIMIT_WINDOW = 60 | |
| def _check_rate_limit(ip: str) -> bool: | |
| with _rate_limit_lock: | |
| now = time.time() | |
| _rate_limit_data[ip] = [t for t in _rate_limit_data[ip] if now - t < RATE_LIMIT_WINDOW] | |
| if len(_rate_limit_data[ip]) >= RATE_LIMIT_MAX: return False | |
| _rate_limit_data[ip].append(now) | |
| return True | |
| async def rate_limit_middleware(request: Request, call_next): | |
| if request.url.path.startswith("/api/"): | |
| ip = request.client.host | |
| if not _check_rate_limit(ip): return JSONResponse({"error": "rate limit exceeded"}, status_code=429) | |
| return await call_next(request) | |
| # ===== VTV CHANNELS API ===== | |
| from vtv_api import router as vtv_router | |
| app.include_router(vtv_router) | |
| HEADERS = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36","Accept-Language":"vi-VN,vi;q=0.9,en;q=0.8"} | |
| BONGDA_HEADERS = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36","Accept-Language":"vi-VN,vi;q=0.9","Referer":"https://bongda.com.vn/lich-thi-dau","X-Requested-With":"XMLHttpRequest"} | |
| BASE_BDP = "https://bongdaplus.vn" | |
| _cache = {} | |
| _cache_ttl = 300 | |
| _cache_ttl_live = 60 | |
| _cache_ttl_yt = 1800 | |
| PRIORITY_LEAGUES = ["Ngoại Hạng Anh","FA Cup","Champions League","LaLiga","Copa del Rey","Serie A","Bundesliga","Ligue 1","V-League"] | |
| LEAGUE_IDS = {"nha":27110,"laliga":27233,"seriea":27044,"bundesliga":26891,"ligue1":27212} | |
| HL_LEAGUES = { | |
| "premier-league":{"path":"anh/premier-league","name":"Premier League","emoji":"🏴"}, | |
| "fa-cup":{"path":"anh/fa-cup","name":"FA Cup","emoji":"🏆"}, | |
| "bundesliga":{"path":"duc/bundesliga","name":"Bundesliga","emoji":"🇩🇪"}, | |
| "serie-a":{"path":"italy/serie-a","name":"Serie A","emoji":"🇮🇹"}, | |
| "la-liga":{"path":"tay-ban-nha/la-liga","name":"La Liga","emoji":"🇪🇸"}, | |
| "champions-league":{"path":"cup-chau-au/uefa-champions-league","name":"Champions League","emoji":"⭐"}, | |
| "europa-league":{"path":"cup-chau-au/uefa-europa-league","name":"Europa League","emoji":"🟠"}, | |
| "world-cup":{"path":"the-gioi/world-cup","name":"World Cup 2026","emoji":"🌍"}, | |
| } | |
| def _cached(key, fn, ttl=None): | |
| now=time.time(); t=ttl or _cache_ttl | |
| if key in _cache and now-_cache[key]["t"]<t: return _cache[key]["d"] | |
| try: data=fn() | |
| except: data=_cache.get(key,{}).get("d",[]) | |
| _cache[key]={"d":data,"t":now}; return data | |
| def _get(url, headers=None): | |
| h=headers or HEADERS; r=requests.get(url, headers=h, timeout=15); r.encoding="utf-8" | |
| return BeautifulSoup(r.text,"lxml") | |
| def fetch_bongda_api(endpoint): | |
| try: | |
| r=requests.get(f"https://bongda.com.vn{endpoint}", headers=BONGDA_HEADERS, timeout=10) | |
| if r.status_code==200: | |
| data=r.json() | |
| if data.get("status")=="success": return data.get("html","") | |
| return "" | |
| except: return "" | |
| def _parse_match_from_li(li, status_type="live"): | |
| match_div=li.select_one("div.match") | |
| if not match_div: return None | |
| home_el=match_div.select_one(".home-team .name"); away_el=match_div.select_one(".away-team .name") | |
| if not home_el or not away_el: return None | |
| status_el=match_div.select_one(".status a"); league_el=li.find_previous("strong"); time_el=match_div.select_one(".match-time") | |
| home_logo=match_div.select_one(".home-team .logo img"); away_logo=match_div.select_one(".away-team .logo img") | |
| event_id="" | |
| if status_el: | |
| href=status_el.get("href",""); m=re.search(r'/tran-dau/(\d+)/',href) | |
| if m: event_id=m.group(1) | |
| spans=status_el.find_all("span") if status_el else []; score=""; minute="" | |
| if len(spans)>=3: score=f"{spans[0].get_text(strip=True)} - {spans[2].get_text(strip=True)}" | |
| if len(spans)>=4: minute=spans[3].get_text(strip=True) | |
| if not score and status_el and status_el.select_one(".vs"): score="VS" | |
| league=league_el.get_text(strip=True) if league_el else "" | |
| return {"home":home_el.get_text(strip=True),"away":away_el.get_text(strip=True),"score":score or"VS","minute":minute,"league":league,"time":time_el.get_text(strip=True) if time_el else "","event_id":event_id,"home_logo":home_logo.get("src","") if home_logo else "","away_logo":away_logo.get("src","") if away_logo else "","status":status_type} | |
| # ===== VIDEO PROXY ===== | |
| def proxy_m3u8(url: str = Query(...)): | |
| try: | |
| r = requests.get(url, headers=HEADERS, timeout=15) | |
| if r.status_code != 200: return Response(status_code=502, content="upstream error") | |
| lines = r.text.strip().split('\n'); rewritten = [] | |
| for line in lines: | |
| if line.startswith('#') or not line.strip(): rewritten.append(line) | |
| else: rewritten.append("/api/proxy/seg?url=" + quote(line.strip(), safe="")) | |
| return Response(content='\n'.join(rewritten).encode('utf-8'), media_type="application/vnd.apple.mpegurl", headers={"Access-Control-Allow-Origin":"*","Cache-Control":"public, max-age=300"}) | |
| except: return Response(status_code=502, content="proxy error") | |
| def proxy_segment(url: str = Query(...)): | |
| try: | |
| r = requests.get(url, headers=HEADERS, timeout=30) | |
| if r.status_code != 200: return Response(status_code=502, content="upstream error") | |
| data = r.content | |
| if len(data) > 188 and data[0:4] == b'\x89PNG' and data[188] == 0x47: data = data[188:] | |
| return Response(content=data, media_type="video/mp2t", headers={"Access-Control-Allow-Origin":"*","Cache-Control":"public, max-age=3600"}) | |
| except: return Response(status_code=502, content="proxy error") | |
| def proxy_video(url: str = Query(...), request: Request = None): | |
| try: | |
| req_headers = dict(HEADERS) | |
| if request and request.headers.get("range"): req_headers["Range"] = request.headers["range"] | |
| r = requests.get(url, headers=req_headers, timeout=30, stream=True) | |
| resp_headers = {"Access-Control-Allow-Origin":"*","Accept-Ranges":"bytes","Content-Type":r.headers.get("Content-Type","video/mp4")} | |
| if "Content-Range" in r.headers: resp_headers["Content-Range"] = r.headers["Content-Range"] | |
| if "Content-Length" in r.headers: resp_headers["Content-Length"] = r.headers["Content-Length"] | |
| return StreamingResponse(r.iter_content(chunk_size=256*1024), status_code=r.status_code, headers=resp_headers) | |
| except: return Response(status_code=502, content="proxy error") | |
| def proxy_img(url: str = Query(...)): | |
| try: | |
| from urllib.parse import urlparse | |
| _u = urlparse(url); _host = _u.netloc.lower() | |
| _referer = "https://dantri.com.vn/" | |
| if "refooty" in _host or "xemlaibongda" in _host: _referer = "https://xemlaibongda.top/" | |
| elif "ytimg" in _host or "youtube" in _host: _referer = "https://www.youtube.com/" | |
| elif "vncecdn" in _host or "vnexpress" in _host: _referer = "https://vnexpress.net/" | |
| r = requests.get(url, headers={**HEADERS, "Referer": _referer}, timeout=10) | |
| if r.status_code != 200: return Response(status_code=502) | |
| return Response(content=r.content, media_type=r.headers.get("Content-Type", "image/jpeg"), headers={"Cache-Control": "public, max-age=86400", "Access-Control-Allow-Origin": "*"}) | |
| except: return Response(status_code=502) | |
| # ===== XEMLAIBONGDA HIGHLIGHTS ===== | |
| def _scrape_xemlaibongda_page(page_path, limit=20): | |
| try: | |
| url = f"https://xemlaibongda.top/{page_path}" if page_path else "https://xemlaibongda.top/" | |
| r = requests.get(url, headers=HEADERS, timeout=15) | |
| if r.status_code != 200: return [] | |
| r.encoding = "utf-8" | |
| soup = BeautifulSoup(r.text, "lxml") | |
| videos = []; seen = set() | |
| for a in soup.find_all("a", href=True): | |
| href = a.get("href", "") | |
| if "/video/" not in href and "/xem-lai/" not in href: continue | |
| if not href.startswith("http"): href = "https://xemlaibongda.top" + href | |
| clean_href = href.split("?")[0].split("#")[0] | |
| if clean_href in seen: continue | |
| seen.add(clean_href) | |
| img_src = "" | |
| img = a.find("img") | |
| if not img and a.parent: img = a.parent.find("img") | |
| if not img: | |
| p = a.parent | |
| for _ in range(4): | |
| if p and p.find("img"): img = p.find("img"); break | |
| p = p.parent if p else None | |
| if img: | |
| img_src = (img.get("data-src", "") or img.get("src", "") or img.get("data-lazy", "") or img.get("data-original", "") or img.get("data-thumb", "") or img.get("data-image", "")) | |
| if img_src.startswith("//"): img_src = "https:" + img_src | |
| elif img_src.startswith("/"): img_src = "https://xemlaibongda.top" + img_src | |
| if not img_src: | |
| p = a.parent | |
| for _ in range(5): | |
| if p is None: break | |
| style = p.get("style", "") | |
| bg_match = re.search(r'url\(["\']?(.*?)["\']?\)', style) | |
| if bg_match: | |
| img_src = bg_match.group(1) | |
| if img_src.startswith("//"): img_src = "https:" + img_src | |
| elif img_src.startswith("/"): img_src = "https://xemlaibongda.top" + img_src | |
| break | |
| p = p.parent if p else None | |
| title = "" | |
| for attr in ["title", "aria-label"]: | |
| val = a.get(attr, "") | |
| if val and len(val) >= 5: title = val; break | |
| if not title: | |
| for selector in ["h3", "h2", "h4", ".title", ".video-title", "strong"]: | |
| try: | |
| el = a.select_one(selector) | |
| if el: t = el.get_text(strip=True) | |
| if t and len(t) >= 5: title = t; break | |
| except: pass | |
| if not title: | |
| text = a.get_text(strip=True) | |
| if text and len(text) >= 5: title = text[:100] | |
| if not title or len(title) < 3: | |
| slug = clean_href.split("/video/")[-1].rstrip("/").split("/xem-lai/")[-1].rstrip("/") | |
| title = slug.replace("-", " ").replace("_", " ").title() | |
| title = re.sub(r'\d{4}-\d{2}-\d{2}', '', title).strip() | |
| if not title or len(title) < 3: continue | |
| if not img_src: | |
| slug = clean_href.split("/video/")[-1].rstrip("/").split("/xem-lai/")[-1].rstrip("/") | |
| img_src = f"https://xemlaibongda.top/uploads/thumb/{slug}.jpg" | |
| videos.append({"title": title[:100], "link": clean_href, "img": img_src, "source": "xemlaibongda"}) | |
| if len(videos) >= limit: break | |
| return videos | |
| except Exception as e: | |
| print(f"[xemlaibongda] Error: {e}"); return [] | |
| def scrape_xemlaibongda(): return _scrape_xemlaibongda_page("", 20) | |
| def scrape_highlights_by_league(league_key): | |
| if league_key not in HL_LEAGUES: return [] | |
| return _scrape_xemlaibongda_page(HL_LEAGUES[league_key]["path"], 20) | |
| def scrape_all_league_highlights(): | |
| results = {} | |
| def _fetch(key): return key, scrape_highlights_by_league(key) | |
| with ThreadPoolExecutor(8) as ex: | |
| futs = [ex.submit(_fetch, k) for k in HL_LEAGUES] | |
| for f in as_completed(futs, timeout=25): | |
| try: key, vids = f.result() | |
| except: continue | |
| if vids: results[key] = vids | |
| return results | |
| def extract_xemlaibongda_video(url): | |
| try: | |
| r=requests.get(url, headers=HEADERS, timeout=15) | |
| if r.status_code!=200: return None | |
| r.encoding="utf-8"; soup=BeautifulSoup(r.text,"lxml") | |
| og=soup.find("meta",property="og:image") | |
| og_poster=og.get("content","") if og else "" | |
| if og_poster.startswith("//"): og_poster="https:"+og_poster | |
| video=soup.find("video") | |
| if video: | |
| src=video.get("src",""); poster=video.get("poster","") | |
| if not src: | |
| source=video.find("source") | |
| if source: src=source.get("src","") | |
| if not poster: poster=og_poster | |
| if src: return{"src":src,"poster":poster,"type":"hls" if".m3u8" in src else"video"} | |
| m3u8s=re.findall(r'(https?://[^\s"\'<>]+\.m3u8)',r.text) | |
| if m3u8s: return{"src":m3u8s[0],"poster":og_poster,"type":"hls"} | |
| yt_iframe = soup.find("iframe", src=re.compile(r"youtube\.com/embed|youtube-nocookie\.com/embed")) | |
| if yt_iframe: return{"src":yt_iframe.get("src",""),"poster":og_poster,"type":"youtube"} | |
| return None | |
| except: return None | |
| # ===== LIVESCORE ===== | |
| def api_livescore_live(): return JSONResponse({"html":_cached("ls_live",lambda:fetch_bongda_api("/api/fixtures/live"),ttl=_cache_ttl_live)}) | |
| def api_livescore_incoming(): return JSONResponse({"html":_cached("ls_incoming",lambda:fetch_bongda_api("/api/fixtures/incoming"),ttl=_cache_ttl_live)}) | |
| def api_livescore_today(): | |
| today=datetime.now(VN_TZ).strftime("%Y-%m-%d");return JSONResponse({"html":_cached("ls_today",lambda:fetch_bongda_api(f"/api/fixtures/get-by-date?date={today}"),ttl=_cache_ttl)}) | |
| def api_livescore_results(): | |
| today=datetime.now(VN_TZ).strftime("%Y-%m-%d");return JSONResponse({"html":_cached("ls_results",lambda:fetch_bongda_api(f"/api/fixtures/get-by-date?date={today}&status=finished"),ttl=_cache_ttl)}) | |
| def api_livescore_standings(league:str): | |
| tid=LEAGUE_IDS.get(league,27110);return JSONResponse({"html":_cached(f"ls_bxh_{league}",lambda:fetch_bongda_api(f"/api/league-table/home?tournament_id={tid}&is_detail=True"),ttl=_cache_ttl)}) | |
| def api_livescore_date(date:str):return JSONResponse({"html":fetch_bongda_api(f"/api/fixtures/get-by-date?date={date}")}) | |
| def api_livescore_updates7d(): | |
| def _f(): | |
| from datetime import date as _date | |
| today = _date.today() | |
| all_html = [] | |
| for i in range(7, 0, -1): | |
| d = (today - timedelta(days=i)).strftime("%Y-%m-%d") | |
| html = fetch_bongda_api(f"/api/fixtures/get-by-date?date={d}&status=finished") | |
| if html and len(html) > 50: | |
| soup = BeautifulSoup(html, "lxml") | |
| day_label = (today - timedelta(days=i)).strftime("%d/%m") | |
| for match in soup.select(".match-detail"): | |
| dt = soup.new_tag("div", **{"class": "datetime"}) | |
| dt.string = f"📅 {day_label}" | |
| match.insert(0, dt) | |
| all_html.append(str(soup)) | |
| for i in range(7): | |
| d = (today + timedelta(days=i)).strftime("%Y-%m-%d") | |
| html = fetch_bongda_api(f"/api/fixtures/get-by-date?date={d}") | |
| if html and len(html) > 50: | |
| soup = BeautifulSoup(html, "lxml") | |
| day_label = (today + timedelta(days=i)).strftime("%d/%m") | |
| for match in soup.select(".match-detail"): | |
| dt = soup.new_tag("div", **{"class": "datetime"}) | |
| dt.string = f"📅 {day_label}" | |
| match.insert(0, dt) | |
| all_html.append(str(soup)) | |
| combined = "<div class='updates7d'>" + "".join(all_html) + "</div>" | |
| return combined if all_html else "" | |
| return JSONResponse({"html": _cached("ls_updates7d", _f, ttl=_cache_ttl)}) | |
| def api_match_commentaries(event_id:int):return JSONResponse({"html":fetch_bongda_api(f"/api/fixtures/commentaries?event_id={event_id}")}) | |
| def api_match_stats(event_id:int):return JSONResponse({"html":fetch_bongda_api(f"/api/event-standing/player-performance?event_id={event_id}")}) | |
| from match_detail_v2 import fetch_match_detail, fetch_match_detail_by_url | |
| def api_match_detail(event_id: int, url: str = Query(default="")): | |
| try: | |
| if url: data = fetch_match_detail_by_url(url) | |
| else: data = fetch_match_detail(event_id) | |
| return JSONResponse(data) | |
| except Exception as e: return JSONResponse({"event_id": event_id, "found": False, "error": str(e)}) | |
| def api_livescore_featured(): | |
| def _f(): | |
| sources=[("/api/fixtures/live","live"),("/api/fixtures/get-by-date?date="+datetime.now(VN_TZ).strftime("%Y-%m-%d"),"today"),("/api/fixtures/incoming","upcoming")] | |
| for endpoint, stype in sources: | |
| html=fetch_bongda_api(endpoint) | |
| if not html or len(html)<100:continue | |
| soup=BeautifulSoup(html,"lxml");all_matches=[] | |
| for li in soup.select("li.match-detail"): | |
| match=_parse_match_from_li(li, stype) | |
| if not match or not match["event_id"]:continue | |
| if stype=="today" and "KT" in match.get("minute",""):continue | |
| all_matches.append(match) | |
| if not all_matches:continue | |
| for pl in PRIORITY_LEAGUES: | |
| for match in all_matches: | |
| if pl in match["league"]:return match | |
| return all_matches[0] | |
| return None | |
| return JSONResponse(_cached("ls_featured",_f,ttl=30)) | |
| def api_highlights(): return JSONResponse(_cached("xemlaibongda_hl",scrape_xemlaibongda,ttl=_cache_ttl)) | |
| def api_highlights_leagues(): return JSONResponse(_cached("hl_leagues",scrape_all_league_highlights,ttl=_cache_ttl)) | |
| def api_highlights_league(league:str): | |
| if league not in HL_LEAGUES: return JSONResponse({"error":"league not found"}) | |
| return JSONResponse(_cached(f"hl_{league}",lambda:scrape_highlights_by_league(league),ttl=_cache_ttl)) | |
| def api_video_url(url:str=Query(...), img:str=Query(default="")): | |
| if "youtube.com" in url or "youtu.be" in url: | |
| m=re.search(r'(?:v=|shorts/|youtu\.be/)([a-zA-Z0-9_-]{11})',url) | |
| if m: vid=m.group(1); return JSONResponse({"src":f"https://www.youtube.com/embed/{vid}?autoplay=1&rel=0&enablejsapi=1","poster":f"https://i.ytimg.com/vi/{vid}/hqdefault.jpg","type":"youtube"}) | |
| if "xemlaibongda.top" in url: | |
| v=extract_xemlaibongda_video(url) | |
| if v: | |
| if v["type"]=="hls": v["src"]="/api/proxy/m3u8?url="+quote(v["src"],safe="") | |
| if not v.get("poster") and img: v["poster"] = img | |
| return JSONResponse(v) | |
| return JSONResponse({"error":"not found"}) | |
| # ===== WORLD CUP 2026 API ===== | |
| _wc_request_times = []; _wc_rate_limit_lock = threading.Lock() | |
| _WC_RATE_LIMIT = 10 | |
| def _wc_rate_limit(): | |
| global _wc_request_times | |
| with _wc_rate_limit_lock: | |
| now = time.time() | |
| _wc_request_times = [t for t in _wc_request_times if now - t < 60] | |
| if len(_wc_request_times) >= _WC_RATE_LIMIT: return False | |
| _wc_request_times.append(now) | |
| return True | |
| def api_wc2026(): | |
| return JSONResponse(_cached("wc2026", get_wc2026_all, ttl=_cache_ttl)) | |
| def api_wc2026_tab(tab: str): | |
| valid_tabs = ["news", "fixtures", "standings", "stats", "highlights"] | |
| if tab not in valid_tabs: return JSONResponse({"error": "invalid tab"}, status_code=400) | |
| def _fetch_tab(): | |
| if tab == "highlights": return scrape_highlights_by_league("world-cup") | |
| elif tab == "news": return scrape_wc_news() | |
| elif tab == "fixtures": return scrape_fixtures() | |
| elif tab == "standings": return scrape_standings() | |
| elif tab == "stats": return scrape_stats() | |
| return [] | |
| return JSONResponse(_cached(f"wc2026_{tab}", _fetch_tab, ttl=_cache_ttl)) | |
| def api_bdp_videos(): | |
| def _f(): | |
| try: | |
| soup=_get(f"{BASE_BDP}/video"); arts=[]; seen=set() | |
| for a in soup.find_all("a",href=True): | |
| href=a.get("href","") | |
| if"/video/" not in href or href in("/video/","/video/ban-thang-dep","/video/highlight"):continue | |
| if not href.startswith("http"): href=BASE_BDP+href | |
| if href in seen: continue | |
| title=re.sub(r'^\d{2}:\d{2}','',a.get_text(strip=True)).strip() | |
| if not title or len(title)<5: continue | |
| img_tag=a.find("img") or(a.parent.find("img") if a.parent else None) | |
| img=(img_tag.get("data-src") or img_tag.get("src","")) if img_tag else "" | |
| seen.add(href); arts.append({"title":title,"link":href,"img":img,"source":"bdp"}) | |
| return arts[:20] | |
| except: return [] | |
| return JSONResponse(_cached("bdp_videos",_f)) | |
| # ===== NEWS ===== | |
| 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"),"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"),"phap-luat":("https://vnexpress.net/phap-luat","Pháp Luật"),"giao-duc":("https://vnexpress.net/giao-duc","Giáo Dục"),"du-lich":("https://vnexpress.net/du-lich","Du Lịch"),"doi-song":("https://vnexpress.net/doi-song","Đời Sống")} | |
| def scrape_vne(cat_url): | |
| try: | |
| soup=_get(cat_url); arts=[] | |
| for it in soup.select("article.item-news")[:15]: | |
| a=it.select_one("h2.title-news a") or it.select_one("h3.title-news a") | |
| if not a: continue | |
| t=a.get("title","") or a.get_text(strip=True); lk=a.get("href","") | |
| if not t or not lk: continue | |
| im=it.find("img"); img=(im.get("data-src") or im.get("src","")) if im else "" | |
| if img and 'blank' in img: | |
| src=it.find("source") | |
| if src: img=src.get("srcset","").split(",")[0].strip().split(" ")[0] | |
| arts.append({"title":t,"link":lk,"img":img,"source":"vne"}) | |
| return arts | |
| except: return [] | |
| def scrape_genk_ai(): | |
| try: | |
| r=requests.get("https://genk.vn/ai.chn",headers=HEADERS,timeout=15) | |
| if r.status_code!=200: return [] | |
| r.encoding="utf-8"; soup=BeautifulSoup(r.text,"lxml"); articles=[]; seen=set() | |
| for a in soup.find_all("a",href=True): | |
| href=a.get("href","") | |
| if not href.endswith(".chn") or href=="/ai.chn": continue | |
| if href.startswith("/"): href="https://genk.vn"+href | |
| if href in seen or "genk.vn" not in href: continue | |
| title=a.get("title","") or a.get_text(strip=True) | |
| if not title or len(title)<20: continue | |
| container=a.parent; img_src="" | |
| for _ in range(6): | |
| if container is None: break | |
| for img in container.find_all("img"): | |
| s=img.get("data-src","") or img.get("src","") | |
| if s and "mediacdn" in s and "avatar" not in s and "logo" not in s: img_src=s; break | |
| if img_src: break; container=container.parent | |
| seen.add(href) | |
| if not img_src: | |
| try: | |
| og_r=requests.get(href,headers=HEADERS,timeout=8); og_r.encoding="utf-8" | |
| og_soup=BeautifulSoup(og_r.text,"lxml"); og_tag=og_soup.find("meta",property="og:image") | |
| if og_tag: img_src=og_tag.get("content","") | |
| except: pass | |
| articles.append({"title":title,"link":href,"img":img_src,"source":"genk"}) | |
| if len(articles)>=30: break | |
| return articles | |
| except: return [] | |
| def api_homepage(): | |
| def _f(): | |
| articles=[] | |
| with ThreadPoolExecutor(12) as ex: | |
| futs={ex.submit(scrape_vne,VNE_CATS[k][0]):VNE_CATS[k][1] for k in["thoi-su","the-gioi","kinh-doanh","the-thao","giai-tri","phap-luat","giao-duc","du-lich","doi-song"]} | |
| for f in as_completed(futs): | |
| try: | |
| for a in f.result(): a["group"]=futs[f]; articles.append(a) | |
| except: pass | |
| return articles | |
| return JSONResponse(_cached("homepage",_f)) | |
| def api_category(cat_id:str): | |
| def _f(): | |
| if cat_id=="cong-nghe": return scrape_genk_ai() | |
| if cat_id in VNE_CATS: | |
| arts=scrape_vne(VNE_CATS[cat_id][0]) | |
| [a.update({"group":VNE_CATS[cat_id][1]}) for a in arts] | |
| return arts | |
| return [] | |
| return JSONResponse(_cached(f"cat_{cat_id}",_f)) | |
| def api_categories(): | |
| cats=[{"id":"cong-nghe","name":"Công Nghệ","source":"genk"}] | |
| for k,(u,n) in VNE_CATS.items(): cats.append({"id":k,"name":n,"source":"vne"}) | |
| return JSONResponse(cats) | |
| def api_xlb(path: str = Query(default=""), limit: int = Query(default=20)): | |
| try: | |
| url = f"https://xemlaibongda.top/{path}" if path else "https://xemlaibongda.top/" | |
| r = requests.get(url, headers=HEADERS, timeout=15) | |
| if r.status_code != 200: return JSONResponse({"videos": []}) | |
| r.encoding = "utf-8" | |
| soup = BeautifulSoup(r.text, "lxml") | |
| videos, seen = [], set() | |
| for a in soup.find_all("a", href=True): | |
| href = a.get("href", "") | |
| if "/video/" not in href and "/xem-lai/" not in href: continue | |
| if not href.startswith("http"): href = "https://xemlaibongda.top" + href | |
| clean = href.split("?")[0].split("#")[0] | |
| if clean in seen: continue | |
| seen.add(clean) | |
| img_src = "" | |
| img = a.find("img") or (a.parent.find("img") if a.parent else None) | |
| if not img: | |
| p = a.parent | |
| for _ in range(5): | |
| if p and p.find("img"): img = p.find("img"); break | |
| p = p.parent if p else None | |
| if img: | |
| img_src = (img.get("data-src", "") or img.get("src", "") or img.get("data-lazy", "") or img.get("data-original", "")) | |
| if img_src.startswith("//"): img_src = "https:" + img_src | |
| elif img_src.startswith("/"): img_src = "https://xemlaibongda.top" + img_src | |
| title = a.find("h3") | |
| if not title: title = a.find("h2") | |
| if not title: title = a.find("strong") | |
| t = title.get_text(strip=True) if title else "" | |
| if not t: | |
| slug = clean.split("/video/")[-1].rstrip("/") | |
| t = slug.replace("-", " ").title() | |
| videos.append({"title": t[:100], "link": clean, "img": img_src, "source": "xemlaibongda"}) | |
| if len(videos) >= limit: break | |
| return JSONResponse({"videos": videos}) | |
| except Exception as e: | |
| return JSONResponse({"videos": [], "error": str(e)}) | |
| def api_article(url:str=Query(...)): | |
| try: | |
| r2 = requests.get(url, headers=HEADERS, timeout=10) | |
| if r2.status_code == 200: | |
| r2.encoding = "utf-8" | |
| soup = BeautifulSoup(r2.text, "lxml") | |
| og = soup.find("meta", property="og:image") | |
| return JSONResponse({"og_image": og.get("content", "") if og else ""}) | |
| except: pass | |
| return JSONResponse({"og_image": ""}) | |
| def api_storage_status(): | |
| return JSONResponse({"persistent":os.path.isdir("/data")}) | |
| def api_hot_topics(): | |
| return JSONResponse({"topics":[]}) | |
| # IMPORTANT: No root GET route here - app_v2_entry.py handles the homepage via serve_index() | |
| # This file only defines API routes and utilities |