Add add song api endpoint
This commit is contained in:
parent
b86461cf2d
commit
0ebbc205a1
2 changed files with 31 additions and 6 deletions
|
@ -4,9 +4,8 @@ from flask_cors import CORS
|
|||
from flask_socketio import SocketIO, emit
|
||||
|
||||
from .room import Room
|
||||
from .song import Song, init_db
|
||||
|
||||
# from .song_fetch import *
|
||||
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
|
||||
|
||||
dotenv.load_dotenv()
|
||||
|
||||
|
@ -120,6 +119,31 @@ def room():
|
|||
]
|
||||
|
||||
|
||||
@app.post("/api/addsong")
|
||||
def add_song():
|
||||
if (room_id := request.args.get("room")) is None:
|
||||
return error("Missing room id")
|
||||
|
||||
if (room := ROOMS.get(int(room_id))) is None:
|
||||
return error("Invalid room")
|
||||
|
||||
if (query := request.args.get("query")) is None:
|
||||
return error("Missing query")
|
||||
|
||||
info = lastfm_query_search(query)
|
||||
|
||||
if (song := get_song_by_title_artist(info.title, info.artist)) is None:
|
||||
res = download_song_mp3(info.title, info.artist)
|
||||
if res is None:
|
||||
ops
|
||||
yt_id, _ = res
|
||||
## song not found, downolad from YT
|
||||
## add in DB
|
||||
add_song_in_db(info.artist, info.title, info.tags, info.img_id, yt_id)
|
||||
|
||||
return {"artist": info.artist, "title": info.title, "tags": info.tags, "image_id": info.img_id}
|
||||
|
||||
|
||||
init_db()
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue