feat: convert live redirects to direct streaming proxy to bypass Mopidy timeouts

This commit is contained in:
2026-09-18 23:09:20 +02:00
parent de887f9884
commit 09a56382d0
+14 -22
View File
@@ -612,10 +612,15 @@ def podcast_rss(station: str, slug_with_ext: str, request: Request, db: Session
return Response(status_code=404)
return Response(content=xml, media_type="application/xml")
def iter_stream(target_url):
import requests
with requests.get(target_url, stream=True, timeout=10) as r:
for chunk in r.iter_content(chunk_size=8192):
if chunk:
yield chunk
@app.get("/live/{station}")
def live_stream_redirect(station: str, db: Session = Depends(db_session)):
is_m3u = False
# Usuwamy sztuczne rozszerzenia dodane dla Mopidy
if station.endswith(".aac"):
station = station[:-4]
@@ -623,32 +628,19 @@ def live_stream_redirect(station: str, db: Session = Depends(db_session)):
station = station[:-4]
elif station.endswith(".m3u"):
station = station[:-4]
is_m3u = True
from fastapi.responses import StreamingResponse
if station == "radio357":
config_url = db.query(Config).filter_by(key="r357_live_url").first()
target_url = config_url.value if config_url else "https://stream.rcs.revma.com/ye5kghkgcm0uv"
# Oszukujemy Mopidy rozszerzeniem .mp3, ponieważ .aac nie figuruje na jego whiteliscie w starszych wersjach
if "#" not in target_url:
target_url += "#.mp3"
if is_m3u:
m3u_content = f"#EXTM3U\n#EXTINF:-1,Radio 357\n{target_url}\n"
from fastapi.responses import PlainTextResponse
return PlainTextResponse(content=m3u_content, media_type="audio/x-mpegurl")
return RedirectResponse(url=target_url, status_code=302)
# Proxy strumienia! Zwracamy jako audio/mpeg żeby ominąć unwrapper
return StreamingResponse(iter_stream(target_url), media_type="audio/mpeg")
elif station == "rns":
config_url = db.query(Config).filter_by(key="rns_live_url").first()
target_url = config_url.value if config_url else "https://stream.rcs.revma.com/4md4m0a0fs8uv"
if "#" not in target_url:
target_url += "#.mp3"
if is_m3u:
m3u_content = f"#EXTM3U\n#EXTINF:-1,Radio Nowy Świat\n{target_url}\n"
from fastapi.responses import PlainTextResponse
return PlainTextResponse(content=m3u_content, media_type="audio/x-mpegurl")
return RedirectResponse(url=target_url, status_code=302)
return StreamingResponse(iter_stream(target_url), media_type="audio/mpeg")
else:
raise HTTPException(status_code=404, detail="Brak streamu dla podanej stacji")