Merge branch 'main' of https://repos.hackathon.bz.it/2025-summer/team-1
This commit is contained in:
commit
db31dda495
5 changed files with 47 additions and 6 deletions
|
@ -9,7 +9,7 @@ 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, get_song_by_uuid
|
||||
from .song_fetch import lastfm_query_search, yt_get_audio_url, yt_search_song
|
||||
from .song_fetch import query_search, yt_get_audio_url, yt_search_song
|
||||
from .qrcode_gen import generate_qr
|
||||
|
||||
|
||||
|
@ -182,7 +182,8 @@ def add_song():
|
|||
if (query := request.args.get("query")) is None:
|
||||
return error("Missing query")
|
||||
|
||||
info = lastfm_query_search(query)
|
||||
if (info := query_search(query)) is None:
|
||||
return error("Search failed")
|
||||
|
||||
if (song := get_song_by_title_artist(info.title, info.artist)) is None:
|
||||
## song not found, downolad from YT
|
||||
|
|
|
@ -51,7 +51,23 @@ def _lastfm_getinfo(name: str, artist: str) -> tuple[str, list[str]]: # ( image
|
|||
)
|
||||
|
||||
|
||||
def lastfm_query_search(query: str) -> SongInfo:
|
||||
def _yt_search(query: str) -> tuple[str, str]:
|
||||
ydl_opts = {
|
||||
"format": "bestaudio",
|
||||
"default_search": "ytsearch1",
|
||||
"outtmpl": "%(title)s.%(ext)s",
|
||||
"skip_download": True,
|
||||
}
|
||||
|
||||
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
||||
info = ydl.extract_info(query, download=False)
|
||||
|
||||
first = info["entries"][0]
|
||||
|
||||
return first["track"], first["artists"][0]
|
||||
|
||||
|
||||
def query_search(query: str) -> SongInfo | None:
|
||||
name, artist = _lastfm_search(query)
|
||||
|
||||
img_id, tags = _lastfm_getinfo(name, artist)
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
export type FetchError = { code: number, message: string }
|
|
@ -65,3 +65,11 @@ export const triggerPlayNext = async function (roomId: string): Promise<[FetchEr
|
|||
})
|
||||
return [null, songs, json["index"]]
|
||||
}
|
||||
|
||||
export const getStreamingUrl = async function (uuid: string) {
|
||||
let resp = await fetch("/api/song/audio?song=" + uuid)
|
||||
|
||||
let json = await resp.json()
|
||||
|
||||
return json["url"]
|
||||
}
|
||||
|
|
|
@ -3,21 +3,37 @@
|
|||
import { type Song } from "$lib/types"
|
||||
import { onMount } from "svelte"
|
||||
import type { FetchError } from "$lib/types"
|
||||
import { getQueueSongs, triggerPlayNext } from "$lib/utils.js"
|
||||
import { getQueueSongs, getStreamingUrl, triggerPlayNext } from "$lib/utils.js"
|
||||
import Error from "$lib/components/Error.svelte"
|
||||
|
||||
let { data } = $props()
|
||||
|
||||
let queueSongs = $state<Song[]>([])
|
||||
let playingIndex = $state<number>()
|
||||
let playingIndex = $state<number>(0)
|
||||
let returnError = $state<FetchError | null>()
|
||||
|
||||
let currentPlaying = $derived<Song>(queueSongs[playingIndex])
|
||||
let audioController = $state<HTMLAudioElement>()
|
||||
|
||||
async function playOnEnd() {
|
||||
let url = await getStreamingUrl(currentPlaying.uuid)
|
||||
if (audioController) {
|
||||
audioController.src = url
|
||||
audioController.play()
|
||||
}
|
||||
}
|
||||
|
||||
onMount(async () => {
|
||||
let songs, index
|
||||
;[returnError, songs, index] = await getQueueSongs(data.roomId)
|
||||
|
||||
queueSongs = songs
|
||||
playingIndex = index
|
||||
|
||||
if (audioController) {
|
||||
audioController.src = await getStreamingUrl(currentPlaying.uuid)
|
||||
audioController.play()
|
||||
}
|
||||
})
|
||||
|
||||
async function playNext() {
|
||||
|
@ -37,5 +53,6 @@
|
|||
<div class="flex w-full flex-col items-center justify-center p-4 lg:p-10">
|
||||
<QueueSlider {queueSongs} {playingIndex} />
|
||||
<button onclick={playNext}>Next</button>
|
||||
<audio controls autoplay bind:this={audioController} onended={playOnEnd}></audio>
|
||||
</div>
|
||||
{/if}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue