Add song fetch final function
This commit is contained in:
parent
f26275766a
commit
b86461cf2d
1 changed files with 21 additions and 4 deletions
|
@ -3,12 +3,21 @@ import urllib.parse
|
|||
import os.path
|
||||
import os
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
|
||||
sys.path.append("/yt-dlp")
|
||||
import yt_dlp
|
||||
|
||||
|
||||
def lastfm_search(query: str) -> tuple[str, str]:
|
||||
@dataclass
|
||||
class SongInfo:
|
||||
artist: str
|
||||
title: str
|
||||
img_id: str
|
||||
tags: list[str]
|
||||
|
||||
|
||||
def _lastfm_search(query: str) -> tuple[str, str]:
|
||||
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"]},
|
||||
|
@ -21,7 +30,7 @@ def lastfm_search(query: str) -> tuple[str, str]:
|
|||
return track_info["name"], track_info["artist"]
|
||||
|
||||
|
||||
def lastfm_getinfo(name: str, artist: str) -> tuple[str, str, str, str, list[str]]: # ( id, image_id, tags )
|
||||
def _lastfm_getinfo(name: str, artist: str) -> tuple[str, list[str]]: # ( image_id, tags )
|
||||
response = requests.get(
|
||||
url="https://ws.audioscrobbler.com/2.0/?method=track.getInfo&format=json",
|
||||
params={
|
||||
|
@ -36,12 +45,20 @@ def lastfm_getinfo(name: str, artist: str) -> tuple[str, str, str, str, list[str
|
|||
image_url = urllib.parse.urlparse(track_info["album"]["image"][0]["#text"])
|
||||
|
||||
return (
|
||||
track_info["mbid"],
|
||||
[t["name"] for t in track_info["toptags"]["tag"]],
|
||||
# track_info["mbid"],
|
||||
os.path.splitext(os.path.basename(image_url.path))[0],
|
||||
[t["name"] for t in track_info["toptags"]["tag"]],
|
||||
)
|
||||
|
||||
|
||||
def lastfm_query_search(query: str) -> SongInfo:
|
||||
name, artist = _lastfm_search(query)
|
||||
|
||||
img_id, tags = _lastfm_getinfo(name, artist)
|
||||
|
||||
return SongInfo(artist=artist, title=name, img_id=img_id, tags=tags)
|
||||
|
||||
|
||||
def download_song_mp3(name: str, artist: str) -> tuple[str, str] | None: # ( id, audio )
|
||||
ydl_opts = {
|
||||
"format": "bestaudio",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue