72 lines
3.1 KiB
Python
72 lines
3.1 KiB
Python
|
import logging
|
||
|
from fastapi import FastAPI, Request, Response, Depends
|
||
|
from fastapi_throttle import RateLimiter
|
||
|
from fastapi.responses import JSONResponse
|
||
|
from utils.hifi_wrapper import HifiUtil
|
||
|
|
||
|
logging.getLogger().setLevel(logging.INFO)
|
||
|
|
||
|
class RIP(FastAPI):
|
||
|
"""
|
||
|
Ripping Endpoints
|
||
|
"""
|
||
|
|
||
|
def __init__(self, app: FastAPI, my_util, constants) -> None:
|
||
|
self.app: FastAPI = app
|
||
|
self.util = my_util
|
||
|
self.trip_util = HifiUtil()
|
||
|
self.constants = constants
|
||
|
self.endpoints: dict = {
|
||
|
"trip/get_artists_by_name": self.artists_by_name_handler,
|
||
|
"trip/get_albums_by_artist_id/{artist_id:path}": self.albums_by_artist_id_handler,
|
||
|
"trip/get_tracks_by_artist_song": self.tracks_by_artist_song_handler,
|
||
|
"trip/get_tracks_by_album_id/{album_id:path}": self.tracks_by_album_id_handler,
|
||
|
"trip/get_track_by_id/{track_id:path}": self.track_by_id_handler,
|
||
|
}
|
||
|
|
||
|
for endpoint, handler in self.endpoints.items():
|
||
|
dependencies = [Depends(RateLimiter(times=8, seconds=2))] # Do not rate limit image retrievals (cached)
|
||
|
app.add_api_route(
|
||
|
f"/{endpoint}",
|
||
|
handler,
|
||
|
methods=["GET"],
|
||
|
include_in_schema=True,
|
||
|
dependencies=dependencies,
|
||
|
)
|
||
|
|
||
|
async def artists_by_name_handler(self, artist: str, request: Request) -> Response:
|
||
|
"""Get artists by name"""
|
||
|
artists = await self.trip_util.get_artists_by_name(artist)
|
||
|
if not artists:
|
||
|
return Response(status_code=404, content="Not found")
|
||
|
return JSONResponse(content=artists)
|
||
|
|
||
|
async def albums_by_artist_id_handler(self, artist_id: int, request: Request) -> Response:
|
||
|
"""Get albums by artist ID"""
|
||
|
albums = await self.trip_util.get_albums_by_artist_id(artist_id)
|
||
|
if not albums:
|
||
|
return Response(status_code=404, content="Not found")
|
||
|
return JSONResponse(content=albums)
|
||
|
|
||
|
async def tracks_by_album_id_handler(self, album_id: int, request: Request) -> Response:
|
||
|
"""Get tracks by album id"""
|
||
|
tracks = await self.trip_util.get_tracks_by_album_id(album_id)
|
||
|
if not tracks:
|
||
|
return Response(status_code=404, content="Not Found")
|
||
|
return JSONResponse(content=tracks)
|
||
|
|
||
|
async def tracks_by_artist_song_handler(self, artist: str, song: str, request: Request) -> Response:
|
||
|
"""Get tracks by artist and song name"""
|
||
|
logging.critical("Searching for tracks by artist: %s, song: %s", artist, song)
|
||
|
tracks = await self.trip_util.get_tracks_by_artist_song(artist, song)
|
||
|
if not tracks:
|
||
|
return Response(status_code=404, content="Not found")
|
||
|
return JSONResponse(content=tracks)
|
||
|
|
||
|
async def track_by_id_handler(self, track_id: int, request: Request) -> Response:
|
||
|
"""Get track by ID"""
|
||
|
track = await self.trip_util.get_stream_url_by_track_id(track_id)
|
||
|
if not track:
|
||
|
return Response(status_code=404, content="Not found")
|
||
|
return JSONResponse(content={"stream_url": track})
|