add api docs for streaming routes

+ add trackhash schema
This commit is contained in:
mungai-njoroge
2024-03-04 02:22:25 +03:00
committed by Mungai Njoroge
parent 7d064a8562
commit fb635ff35f
7 changed files with 87 additions and 45 deletions
+43 -15
View File
@@ -1,30 +1,43 @@
"""
Contains all the track routes.
"""
import os
from flask import Blueprint, send_file, request
from flask_openapi3 import APIBlueprint, Tag
from pydantic import BaseModel, Field
from app.api.apischemas import TrackHashSchema
from app.lib.trackslib import get_silence_paddings
from app.store.tracks import TrackStore
api = Blueprint("track", __name__, url_prefix="/")
bp_tag = Tag(name="File", description="Single artist")
api = APIBlueprint("track", __name__, url_prefix="/file", abp_tags=[bp_tag])
@api.route("/file/<trackhash>")
def send_track_file(trackhash: str):
class SendTrackFileQuery(BaseModel):
filepath: str = Field(
description="The filepath to play (if available)", default=None
)
@api.get("/<trackhash>")
def send_track_file(path: TrackHashSchema, query: SendTrackFileQuery):
"""
Returns an audio file that matches the passed id to the client.
Falls back to track hash if id is not found.
Get file
Returns a playable audio file that corresponds to the given filepath. Falls back to track hash if filepath is not found.
"""
trackhash = path.trackhash
filepath = query.filepath
msg = {"msg": "File Not Found"}
def get_mime(filename: str) -> str:
ext = filename.rsplit(".", maxsplit=1)[-1]
return f"audio/{ext}"
filepath = request.args.get("filepath")
# If filepath is provide, try to send that
if filepath is not None:
try:
track = TrackStore.get_tracks_by_filepaths([filepath])[0]
@@ -37,9 +50,7 @@ def send_track_file(trackhash: str):
audio_type = get_mime(filepath)
return send_file(filepath, mimetype=audio_type)
if trackhash is None:
return msg, 404
# Else, find file by trackhash
tracks = TrackStore.get_tracks_by_trackhashes([trackhash])
for track in tracks:
@@ -56,11 +67,28 @@ def send_track_file(trackhash: str):
return msg, 404
@api.route("/file/silence", methods=["POST"])
def get_audio_silence():
data = request.get_json()
ending_file = data.get("end", None) # ending file's filepath
starting_file = data.get("start", None) # starting file's filepath
class GetAudioSilenceBody(BaseModel):
ending_file: str = Field(
description="The ending file's path",
example="/home/cwilvx/Music/Made in Kenya/Sol generation/Bensoul - Salama.mp3",
)
starting_file: str = Field(
description="The beginning file's path",
example="/home/cwilvx/Music/Tidal/Albums/Bensoul - Qwarantunes/Bensoul - Peddi.m4a",
)
@api.post("/silence")
def get_audio_silence(body: GetAudioSilenceBody):
"""
Get silence paddings
Returns the duration of silence at the end of the current ending track and the duration of silence at the beginning of the next track.
NOTE: Durations are in milliseconds.
"""
ending_file = body.ending_file # ending file's filepath
starting_file = body.starting_file # starting file's filepath
if ending_file is None or starting_file is None:
return {"msg": "No filepath provided"}, 400