This commit is contained in:
2025-08-21 15:35:10 -04:00
parent dd8d07b2f0
commit a8d089c0fe
5 changed files with 57 additions and 49 deletions

View File

@@ -10,6 +10,7 @@ from .constructors import (
ValidRadioSongRequest,
ValidRadioTypeaheadRequest,
ValidRadioQueueRequest,
Station
)
from utils import radio_util
from typing import Optional
@@ -43,23 +44,19 @@ class Radio(FastAPI):
"radio/reshuffle": self.radio_reshuffle,
"radio/queue_remove": self.radio_queue_remove,
"radio/ls._next_": self.radio_get_next,
"radio/album_art": self.album_art_handler,
}
for endpoint, handler in self.endpoints.items():
methods: list[str] = ["POST"]
if endpoint == "radio/album_art":
methods = ["GET"]
app.add_api_route(
f"/{endpoint}", handler, methods=["POST"], include_in_schema=True,
f"/{endpoint}", handler, methods=methods, include_in_schema=False,
dependencies=[Depends(
RateLimiter(times=10, seconds=2))] if not endpoint == "radio/np" else None,
)
# NOTE: Not in loop because method is GET for this endpoint
app.add_api_route(
"/radio/album_art",
self.album_art_handler,
methods=["GET"],
include_in_schema=True,
)
app.add_event_handler("startup", self.on_start)
async def on_start(self) -> None:
@@ -135,21 +132,35 @@ class Radio(FastAPI):
"""
Get current play queue (paged, 20 results per page)
"""
if not (data and data.station):
return JSONResponse(status_code=500,
content={
"err": True,
"errorText": "Invalid request.",
})
search: Optional[str] = None
draw: int = 0
if isinstance(data, ValidRadioQueueRequest):
search = data.search
draw = data.draw
start: int = int(data.start)
draw = data.draw or 0
start: int = int(data.start or 0)
end: int = start + 20
else:
start: int = 0
end: int = 20
orig_queue: list[dict] = self.radio_util.active_playlist[data.station]
if not search:
queue_full: list = orig_queue
queue_full: Optional[list] = orig_queue
else:
queue_full: list = self.radio_util.datatables_search(data.search, data.station)
queue_full = self.radio_util.datatables_search(search, data.station)
if not queue_full:
return JSONResponse(
status_code=500,
content={
"err": True,
"errorText": "No queue found.",
}
)
queue: list = queue_full[start:end]
queue_out: list[dict] = []
for x, item in enumerate(queue):
@@ -240,7 +251,7 @@ class Radio(FastAPI):
async def album_art_handler(
self, request: Request, track_id: Optional[int] = None,
station: Optional[str] = "main"
station: Station = "main"
) -> Response:
"""
Get album art, optional parameter track_id may be specified.
@@ -251,6 +262,13 @@ class Radio(FastAPI):
try:
if not track_id:
track_id = self.radio_util.now_playing[station].get("id")
if not track_id:
# Still no track ID
return JSONResponse(status_code=500,
content={
"err": True,
"errorText": "Invalid request",
})
logging.debug("Seeking album art with trackId: %s", track_id)
album_art: Optional[bytes] = self.radio_util.get_album_art(
track_id=track_id
@@ -269,7 +287,7 @@ class Radio(FastAPI):
)
async def radio_now_playing(self, request: Request,
station: Optional[str] = "main") -> JSONResponse:
station: Station = "main") -> JSONResponse:
"""
Get currently playing track info
- **station**: default "main"