Merge remote-tracking branch 'refs/remotes/origin/main'
This commit is contained in:
commit
a0360f821a
2 changed files with 16 additions and 4 deletions
|
@ -210,6 +210,9 @@ def add_song():
|
|||
## song not found, downolad from YT
|
||||
yt_video_id = yt_search_song(info.title, info.artist)
|
||||
|
||||
if yt_video_id is None:
|
||||
return error("No video found on youtube")
|
||||
|
||||
## add in DB
|
||||
song = Song(
|
||||
uuid=str(uuid.uuid4()),
|
||||
|
|
|
@ -18,7 +18,7 @@ class SongInfo:
|
|||
tags: list[str]
|
||||
|
||||
|
||||
def _lastfm_search(query: str) -> tuple[str, str]:
|
||||
def _lastfm_search(query: str) -> tuple[str, str] | None:
|
||||
response = requests.get(
|
||||
url="https://ws.audioscrobbler.com/2.0/?method=track.search&format=json",
|
||||
params={"limit": 5, "track": query, "api_key": os.environ["LASTFM_API_KEY"]},
|
||||
|
@ -26,7 +26,10 @@ def _lastfm_search(query: str) -> tuple[str, str]:
|
|||
|
||||
assert response.status_code == 200
|
||||
|
||||
track_info = response.json()["results"]["trackmatches"]["track"][0]
|
||||
tracks = response.json()["results"]["trackmatches"]["track"]
|
||||
if len(tracks) == 0:
|
||||
return None
|
||||
track_info = tracks[0]
|
||||
|
||||
return track_info["name"], track_info["artist"]
|
||||
|
||||
|
@ -74,14 +77,17 @@ def _yt_search(query: str) -> tuple[str, str]:
|
|||
|
||||
|
||||
def query_search(query: str) -> SongInfo | None:
|
||||
name, artist = _lastfm_search(query)
|
||||
res = _lastfm_search(query)
|
||||
if res is None:
|
||||
return None
|
||||
|
||||
name, artist = res
|
||||
img_id, tags = _lastfm_getinfo(name, artist)
|
||||
|
||||
return SongInfo(artist=artist, title=name, img_id=img_id, tags=tags)
|
||||
|
||||
|
||||
def yt_search_song(name: str, artist: str) -> str: # video id
|
||||
def yt_search_song(name: str, artist: str) -> str | None: # video id
|
||||
ydl_opts = {
|
||||
"format": "bestaudio",
|
||||
"default_search": "ytsearch1",
|
||||
|
@ -92,6 +98,9 @@ def yt_search_song(name: str, artist: str) -> str: # video id
|
|||
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
||||
info = ydl.extract_info(f"{name!r} - {artist!r}", download=False)
|
||||
|
||||
if len(info["entries"]) == 0:
|
||||
return None
|
||||
|
||||
return info["entries"][0]["id"]
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue