Allow forcing updates from TADB
This commit is contained in:
@ -104,10 +104,18 @@ class Artist(TimeStampedModel):
|
|||||||
self.bandcamp_id = slug
|
self.bandcamp_id = slug
|
||||||
self.save(update_fields=["bandcamp_id"])
|
self.save(update_fields=["bandcamp_id"])
|
||||||
|
|
||||||
def fix_metadata(self):
|
def fix_metadata(self, force_update=False):
|
||||||
|
tadb_info = {}
|
||||||
|
if self.theaudiodb_id and force_update:
|
||||||
|
tadb_info = lookup_artist_from_tadb(self.theaudiodb_id)
|
||||||
|
|
||||||
|
if not self.theaudiodb_id or (force_update and not tadb_info):
|
||||||
tadb_info = lookup_artist_from_tadb(self.name)
|
tadb_info = lookup_artist_from_tadb(self.name)
|
||||||
|
|
||||||
if not tadb_info:
|
if not tadb_info:
|
||||||
logger.warn(f"No response from TADB for artist {self.name}")
|
logger.warn(
|
||||||
|
f"No response from TADB for artist {self.name}, try force_update=True"
|
||||||
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
self.biography = tadb_info["biography"]
|
self.biography = tadb_info["biography"]
|
||||||
|
|||||||
@ -7,23 +7,33 @@ from django.conf import settings
|
|||||||
|
|
||||||
THEAUDIODB_API_KEY = getattr(settings, "THEAUDIODB_API_KEY")
|
THEAUDIODB_API_KEY = getattr(settings, "THEAUDIODB_API_KEY")
|
||||||
ARTIST_SEARCH_URL = f"https://www.theaudiodb.com/api/v1/json/{THEAUDIODB_API_KEY}/search.php?s="
|
ARTIST_SEARCH_URL = f"https://www.theaudiodb.com/api/v1/json/{THEAUDIODB_API_KEY}/search.php?s="
|
||||||
|
ARTIST_FETCH_URL = f"https://www.theaudiodb.com/api/v1/json/{THEAUDIODB_API_KEY}/artist.php?i="
|
||||||
ALBUM_SEARCH_URL = f"https://www.theaudiodb.com/api/v1/json/{THEAUDIODB_API_KEY}/searchalbum.php?s="
|
ALBUM_SEARCH_URL = f"https://www.theaudiodb.com/api/v1/json/{THEAUDIODB_API_KEY}/searchalbum.php?s="
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def lookup_artist_from_tadb(name: str) -> dict:
|
def lookup_artist_from_tadb(name_or_id: str) -> dict:
|
||||||
artist_info = {}
|
artist_info = {}
|
||||||
name = urllib.parse.quote(name)
|
name = urllib.parse.quote(name_or_id)
|
||||||
response = requests.get(ARTIST_SEARCH_URL + name)
|
response = requests.get(ARTIST_SEARCH_URL + name)
|
||||||
|
|
||||||
|
found_by_name = True
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
logger.warn(f"Bad response from TADB: {response.status_code}")
|
logger.warn(f"Bad response from TADB: {response.status_code}")
|
||||||
return {}
|
found_by_name = False
|
||||||
|
|
||||||
if not response.content:
|
if not response.content:
|
||||||
logger.warn(f"Bad content from TADB: {response.content}")
|
logger.warn(f"Bad content from TADB: {response.content}")
|
||||||
return {}
|
found_by_name = False
|
||||||
|
|
||||||
|
if "null" in str(response.content):
|
||||||
|
logger.warn(f"Bad content from TADB: {response.content}")
|
||||||
|
found_by_name = False
|
||||||
|
|
||||||
|
if not found_by_name:
|
||||||
|
# Try using an TABD ID
|
||||||
|
response = requests.get(ARTIST_FETCH_URL + name_or_id)
|
||||||
|
|
||||||
results = json.loads(response.content)
|
results = json.loads(response.content)
|
||||||
if results["artists"]:
|
if results["artists"]:
|
||||||
|
|||||||
Reference in New Issue
Block a user