fix micro smarcio
This commit is contained in:
parent
ec36ea3feb
commit
cbf90d1d0a
2 changed files with 16 additions and 4 deletions
|
@ -210,6 +210,9 @@ def add_song():
|
||||||
## song not found, downolad from YT
|
## song not found, downolad from YT
|
||||||
yt_video_id = yt_search_song(info.title, info.artist)
|
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
|
## add in DB
|
||||||
song = Song(
|
song = Song(
|
||||||
uuid=str(uuid.uuid4()),
|
uuid=str(uuid.uuid4()),
|
||||||
|
|
|
@ -18,7 +18,7 @@ class SongInfo:
|
||||||
tags: list[str]
|
tags: list[str]
|
||||||
|
|
||||||
|
|
||||||
def _lastfm_search(query: str) -> tuple[str, str]:
|
def _lastfm_search(query: str) -> tuple[str, str] | None:
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
url="https://ws.audioscrobbler.com/2.0/?method=track.search&format=json",
|
url="https://ws.audioscrobbler.com/2.0/?method=track.search&format=json",
|
||||||
params={"limit": 5, "track": query, "api_key": os.environ["LASTFM_API_KEY"]},
|
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
|
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"]
|
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:
|
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)
|
img_id, tags = _lastfm_getinfo(name, artist)
|
||||||
|
|
||||||
return SongInfo(artist=artist, title=name, img_id=img_id, tags=tags)
|
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 = {
|
ydl_opts = {
|
||||||
"format": "bestaudio",
|
"format": "bestaudio",
|
||||||
"default_search": "ytsearch1",
|
"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:
|
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
||||||
info = ydl.extract_info(f"{name!r} - {artist!r}", download=False)
|
info = ydl.extract_info(f"{name!r} - {artist!r}", download=False)
|
||||||
|
|
||||||
|
if len(info["entries"]) == 0:
|
||||||
|
return None
|
||||||
|
|
||||||
return info["entries"][0]["id"]
|
return info["entries"][0]["id"]
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue