add method and route to fetch similar artists from lastfm

This commit is contained in:
geoffrey45 committed 2023-03-12 19:28:41 +03:00
1 parent e3ec9db989
commit fed62bf7f4
6 files changed
+62 -45

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
"""
All network requests are defined here.
"""
+24
View File
@@ -0,0 +1,24 @@
"""
Requests related to artists
"""
import requests
from app import settings
from app.utils.hashing import create_hash
def fetch_similar_artists(name: str):
"""
Fetches similar artists from Last.fm
"""
url = f"https://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist={name}&api_key=" \
f"{settings.Keys.LASTFM_API_KEY}&format=json&limit=100"
response = requests.get(url, timeout=10)
response.raise_for_status()
data = response.json()
artists = data["similarartists"]["artist"]
for artist in artists:
yield create_hash(artist["name"])