Add song fetch test
This commit is contained in:
parent
fa73c6de66
commit
8bb582148d
6 changed files with 76 additions and 9 deletions
63
backend/src/song_fetch.py
Normal file
63
backend/src/song_fetch.py
Normal file
|
@ -0,0 +1,63 @@
|
|||
import requests
|
||||
import urllib.parse
|
||||
import os.path
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.append("/yt-dlp")
|
||||
import yt_dlp
|
||||
|
||||
|
||||
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"]},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
track_info = response.json()["results"]["trackmatches"]["track"][0]
|
||||
|
||||
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 )
|
||||
response = requests.get(
|
||||
url="https://ws.audioscrobbler.com/2.0/?method=track.getInfo&format=json",
|
||||
params={
|
||||
"track": name,
|
||||
"artist": artist,
|
||||
"api_key": os.environ["LASTFM_API_KEY"],
|
||||
},
|
||||
)
|
||||
|
||||
track_info = response.json()["track"]
|
||||
|
||||
image_url = urllib.parse.urlparse(track_info["album"]["image"][0]["#text"])
|
||||
|
||||
return (
|
||||
track_info["mbid"],
|
||||
[t["name"] for t in track_info["toptags"]["tag"]],
|
||||
os.path.splitext(os.path.basename(image_url.path))[0],
|
||||
)
|
||||
|
||||
|
||||
print(yt_dlp, flush=True)
|
||||
|
||||
# # def get_yt_mp3link(name: str, artist: str) -> str: ...
|
||||
# # os.popen("/yt-dlp ")
|
||||
|
||||
# # /yt-dlp/yt-dlp.sh "ytsearch1:Never gonna give you up" --get-url -f "ba"
|
||||
|
||||
# import json
|
||||
|
||||
# print(json.dumps(lastfm_getinfo(*lastfm_search("money")), indent=2))
|
||||
# exit(1)
|
||||
|
||||
|
||||
# # def
|
||||
|
||||
|
||||
# ## query ==> lastfm ==> list of songs ==> take first ==> request song info ==> get YT link ==> save in DB ==>
|
Loading…
Add table
Add a link
Reference in a new issue