Add endpoit to get audio url

This commit is contained in:
Leonardo Segala 2025-08-02 04:09:01 +02:00
parent 1383c0fbed
commit 64941061ea
2 changed files with 37 additions and 9 deletions

View file

@ -8,8 +8,8 @@ from dataclasses import asdict
from .state import State
from .connect import get_connection
from .room import Room
from .song import Song, init_db, get_song_by_title_artist, add_song_in_db
from .song_fetch import lastfm_query_search, download_song_mp3
from .song import Song, init_db, get_song_by_title_artist, add_song_in_db, get_song_by_uuid
from .song_fetch import lastfm_query_search, yt_get_audio_url, yt_search_song
from .qrcode_gen import generate_qr
from typing import Any
@ -171,8 +171,7 @@ def add_song():
if (song := get_song_by_title_artist(info.title, info.artist)) is None:
## song not found, downolad from YT
if (res := download_song_mp3(info.title, info.artist)) is None:
return error("Cannot get info from YT")
yt_video_id = yt_search_song(info.title, info.artist)
## add in DB
song = Song(
@ -181,7 +180,7 @@ def add_song():
artist=info.artist,
tags=info.tags,
image_id=info.img_id,
youtube_id=res[0],
youtube_id=yt_video_id,
)
add_song_in_db(song)
@ -241,5 +240,19 @@ def room_qrcode():
return Response(stream, content_type="image/jpeg")
@app.get("/api/song/audio")
def get_audio_url():
if (song_uuid := request.args.get("song")) is None:
return error("Missing song id")
if (song := get_song_by_uuid(song_uuid)) is None:
return error("Song not found")
if (url := yt_get_audio_url(song.youtube_id)) is None:
return error("Cannot get audio url")
return {"success": True, "url": url}
if __name__ == "__main__":
socketio.run(app, debug=True)