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 import Flask, Response, jsonify, request
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
from flask_socketio import SocketIO, emit
|
from flask_socketio import SocketIO, emit
|
||||||
|
import uuid
|
||||||
|
|
||||||
from .state import State
|
from .state import State
|
||||||
from .connect import get_connection
|
from .connect import get_connection
|
||||||
|
@ -162,19 +163,62 @@ def add_song():
|
||||||
info = lastfm_query_search(query)
|
info = lastfm_query_search(query)
|
||||||
|
|
||||||
if (song := get_song_by_title_artist(info.title, info.artist)) is None:
|
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
|
## 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 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
|
## HERE: add song in the room list
|
||||||
|
|
||||||
return {"artist": info.artist, "title": info.title, "tags": info.tags, "image_id": info.img_id}
|
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")
|
@app.get("/api/room/qrcode")
|
||||||
def room_qrcode():
|
def room_qrcode():
|
||||||
if (room_id := request.args.get("room")) is None:
|
if (room_id := request.args.get("room")) is None:
|
||||||
|
|
|
@ -56,7 +56,7 @@ def get_song_by_title_artist(title: str, artist: str) -> Song | None:
|
||||||
return song
|
return song
|
||||||
|
|
||||||
|
|
||||||
def add_song_in_db(title: str, artist: str, tags: list[str], image_id: str, yt_id: str) -> None:
|
def add_song_in_db(song: Song) -> None:
|
||||||
conn = get_connection()
|
conn = get_connection()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
@ -65,7 +65,8 @@ def add_song_in_db(title: str, artist: str, tags: list[str], image_id: str, yt_i
|
||||||
INSERT OR REPLACE INTO songs (uuid, title, artist, tags, lastfm_image_id, youtube_id)
|
INSERT OR REPLACE INTO songs (uuid, title, artist, tags, lastfm_image_id, youtube_id)
|
||||||
VALUES (?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?)
|
||||||
""",
|
""",
|
||||||
(str(uuid.uuid4()), title, artist, ",".join(tags), image_id, yt_id),
|
(song.uuid, song.title, song.artist, ",".join(song.tags), song.image_id, song.youtube_id),
|
||||||
) # Updates song info if it already exists
|
) # Updates song info if it already exists
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue