Spaces:
Running
Running
Revert to pre-ScoreBat version (FPT YouTube highlights + shorts, iframe audio fix)
Browse files
main.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
"""VNEWS - FastAPI backend with livescore +
|
| 2 |
import hashlib, re, time, subprocess, json
|
| 3 |
from datetime import datetime
|
| 4 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
@@ -61,30 +61,6 @@ def _parse_match_from_li(li, status_type="live"):
|
|
| 61 |
league=league_el.get_text(strip=True) if league_el else ""
|
| 62 |
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}
|
| 63 |
|
| 64 |
-
def scrape_scorebat_highlights():
|
| 65 |
-
"""Get highlight videos from ScoreBat free API."""
|
| 66 |
-
try:
|
| 67 |
-
r=requests.get("https://www.scorebat.com/video-api/v3/",headers=HEADERS,timeout=15)
|
| 68 |
-
if r.status_code!=200:return[]
|
| 69 |
-
data=r.json()
|
| 70 |
-
response=data.get("response",[])
|
| 71 |
-
videos=[]
|
| 72 |
-
for item in response:
|
| 73 |
-
title=item.get("title","")
|
| 74 |
-
thumb=item.get("thumbnail","")
|
| 75 |
-
competition=item.get("competition","")
|
| 76 |
-
matchview=item.get("matchviewUrl","")
|
| 77 |
-
vids=item.get("videos",[])
|
| 78 |
-
embed=""
|
| 79 |
-
if vids:
|
| 80 |
-
embed_html=vids[0].get("embed","")
|
| 81 |
-
m=re.search(r'src=["\']([^"\']+)["\']',embed_html)
|
| 82 |
-
if m:embed=m.group(1)
|
| 83 |
-
if title and (embed or matchview):
|
| 84 |
-
videos.append({"title":title,"img":thumb,"link":embed or matchview,"source":"scorebat","embed":embed,"competition":competition})
|
| 85 |
-
return videos[:30]
|
| 86 |
-
except:return[]
|
| 87 |
-
|
| 88 |
def _yt_playlist(channel_path, count=15):
|
| 89 |
try:
|
| 90 |
result=subprocess.run(["yt-dlp","--flat-playlist","--dump-json","--no-download","--playlist-end",str(count),f"https://www.youtube.com/@fptbongdaofficial/{channel_path}"],capture_output=True,text=True,timeout=60)
|
|
@@ -104,6 +80,8 @@ def _yt_playlist(channel_path, count=15):
|
|
| 104 |
|
| 105 |
def scrape_fpt_shorts():
|
| 106 |
return _yt_playlist("shorts", 15)
|
|
|
|
|
|
|
| 107 |
|
| 108 |
@app.get("/api/livescore/live")
|
| 109 |
def api_livescore_live():
|
|
@@ -166,7 +144,7 @@ def api_shorts():
|
|
| 166 |
return JSONResponse(_cached("fpt_shorts",scrape_fpt_shorts,ttl=_cache_ttl_yt))
|
| 167 |
@app.get("/api/highlights")
|
| 168 |
def api_highlights():
|
| 169 |
-
return JSONResponse(_cached("
|
| 170 |
@app.get("/api/video_url")
|
| 171 |
def api_video_url(url:str=Query(...)):
|
| 172 |
if "youtube.com" in url or "youtu.be" in url:
|
|
@@ -174,8 +152,6 @@ def api_video_url(url:str=Query(...)):
|
|
| 174 |
if m:
|
| 175 |
vid=m.group(1)
|
| 176 |
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"})
|
| 177 |
-
if "scorebat.com" in url:
|
| 178 |
-
return JSONResponse({"src":url,"poster":"","type":"youtube"})
|
| 179 |
if "bongdaplus.vn" in url:
|
| 180 |
try:
|
| 181 |
m=re.search(r'-(\d{6,})\.html',url)
|
|
|
|
| 1 |
+
"""VNEWS - FastAPI backend with livescore + YouTube FPT B贸ng 膼谩"""
|
| 2 |
import hashlib, re, time, subprocess, json
|
| 3 |
from datetime import datetime
|
| 4 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
|
|
| 61 |
league=league_el.get_text(strip=True) if league_el else ""
|
| 62 |
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}
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def _yt_playlist(channel_path, count=15):
|
| 65 |
try:
|
| 66 |
result=subprocess.run(["yt-dlp","--flat-playlist","--dump-json","--no-download","--playlist-end",str(count),f"https://www.youtube.com/@fptbongdaofficial/{channel_path}"],capture_output=True,text=True,timeout=60)
|
|
|
|
| 80 |
|
| 81 |
def scrape_fpt_shorts():
|
| 82 |
return _yt_playlist("shorts", 15)
|
| 83 |
+
def scrape_fpt_highlights():
|
| 84 |
+
return _yt_playlist("videos", 15)
|
| 85 |
|
| 86 |
@app.get("/api/livescore/live")
|
| 87 |
def api_livescore_live():
|
|
|
|
| 144 |
return JSONResponse(_cached("fpt_shorts",scrape_fpt_shorts,ttl=_cache_ttl_yt))
|
| 145 |
@app.get("/api/highlights")
|
| 146 |
def api_highlights():
|
| 147 |
+
return JSONResponse(_cached("fpt_highlights",scrape_fpt_highlights,ttl=_cache_ttl_yt))
|
| 148 |
@app.get("/api/video_url")
|
| 149 |
def api_video_url(url:str=Query(...)):
|
| 150 |
if "youtube.com" in url or "youtu.be" in url:
|
|
|
|
| 152 |
if m:
|
| 153 |
vid=m.group(1)
|
| 154 |
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"})
|
|
|
|
|
|
|
| 155 |
if "bongdaplus.vn" in url:
|
| 156 |
try:
|
| 157 |
m=re.search(r'-(\d{6,})\.html',url)
|