Add some apis
This commit is contained in:
parent
3b927522e3
commit
5a7d661629
2 changed files with 52 additions and 7 deletions
|
@ -2,6 +2,7 @@ import dotenv
|
|||
from flask import Flask, Response, jsonify, request
|
||||
from flask_cors import CORS
|
||||
from flask_socketio import SocketIO, emit
|
||||
import uuid
|
||||
|
||||
from .state import State
|
||||
from .connect import get_connection
|
||||
|
@ -162,19 +163,62 @@ def add_song():
|
|||
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:
|
||||
return error("Cannot get info from YT")
|
||||
yt_id, _ = res
|
||||
## song not found, downolad from YT
|
||||
if (res := download_song_mp3(info.title, info.artist)) is None:
|
||||
return error("Cannot get info from YT")
|
||||
|
||||
## add in DB
|
||||
add_song_in_db(info.artist, info.title, info.tags, info.img_id, yt_id)
|
||||
song = Song(
|
||||
uuid=str(uuid.uuid4()),
|
||||
title=info.title,
|
||||
artist=info.artist,
|
||||
tags=info.tags,
|
||||
image_id=info.img_id,
|
||||
youtube_id=res[0],
|
||||
)
|
||||
|
||||
add_song_in_db(song)
|
||||
|
||||
# if song.uuid
|
||||
|
||||
room.songs # TODO: add in room list
|
||||
|
||||
## HERE: add song in the room list
|
||||
|
||||
return {"artist": info.artist, "title": info.title, "tags": info.tags, "image_id": info.img_id}
|
||||
|
||||
|
||||
@app.get("/api/room/suggestions")
|
||||
def get_room_suggestion():
|
||||
if (room_id := request.args.get("room")) is None:
|
||||
return error("Missing room id")
|
||||
|
||||
if (room := state.rooms.get(int(room_id))) is None:
|
||||
return error("Invalid room id")
|
||||
|
||||
return {"success": True, "songs": room.songs}
|
||||
|
||||
|
||||
@app.post("/api/song/voting")
|
||||
def post_song_vote():
|
||||
if (room_id := request.args.get("room")) is None:
|
||||
return error("Missing room id")
|
||||
|
||||
if (room := state.rooms.get(int(room_id))) is None:
|
||||
return error("Invalid room id")
|
||||
|
||||
if (song_id := request.args.get("song")) is None:
|
||||
return error("Missing song id")
|
||||
|
||||
if (song_info := room.songs.get(song_id)) is None:
|
||||
return error("Invalid song id")
|
||||
|
||||
## update the song
|
||||
room.songs[song_id] = (song_info[0], song_info[1] + int(request.args["increment"]))
|
||||
|
||||
return {"success": True}
|
||||
|
||||
|
||||
@app.get("/api/room/qrcode")
|
||||
def room_qrcode():
|
||||
if (room_id := request.args.get("room")) is None:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue