mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-03 20:13:02 +00:00
add last fm similar artists to db table
+ add db methods for the above + try and discard last fm store
This commit is contained in:
+14
-5
@@ -1,24 +1,33 @@
|
||||
"""
|
||||
Requests related to artists
|
||||
"""
|
||||
from pprint import pprint
|
||||
import requests
|
||||
|
||||
from app import settings
|
||||
from app.utils.hashing import create_hash
|
||||
from requests import ConnectionError
|
||||
import urllib.parse
|
||||
|
||||
|
||||
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}&format=json&limit=250"
|
||||
url = f"https://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist={urllib.parse.quote_plus(name, safe='')}&api_key={settings.Keys.LASTFM_API}&format=json&limit=250"
|
||||
|
||||
response = requests.get(url, timeout=10)
|
||||
response.raise_for_status()
|
||||
try:
|
||||
response = requests.get(url, timeout=10)
|
||||
response.raise_for_status()
|
||||
except ConnectionError:
|
||||
return []
|
||||
|
||||
data = response.json()
|
||||
artists = data["similarartists"]["artist"]
|
||||
|
||||
try:
|
||||
artists = data["similarartists"]["artist"]
|
||||
except KeyError:
|
||||
return []
|
||||
|
||||
for artist in artists:
|
||||
yield create_hash(artist["name"])
|
||||
|
||||
Reference in New Issue
Block a user