From 386f17779b6561d299cf848c353d53bf72887ebf Mon Sep 17 00:00:00 2001 From: 20xd6 <20xd6@airmail.cc> Date: Thu, 3 Oct 2024 14:53:52 -0400 Subject: [PATCH] Changes the internal ratelimiting scheme. Rather than ratelimit to one request per-second, attempt to detect the MusicBrainz API ratelimiting. The API will return HTTP 503 errors once ratelimiting has begun. Once this has been detected, the script will wait 1 second before continuing processing. --- api_calls.py | 47 ----------------------------------------------- get_artist_art.py | 2 +- 2 files changed, 1 insertion(+), 48 deletions(-) diff --git a/api_calls.py b/api_calls.py index aa1bcbd..e69de29 100644 --- a/api_calls.py +++ b/api_calls.py @@ -1,47 +0,0 @@ -import requests -import os - -def get_mb_id(artist_name, mb_confidence): - artist_name = artist_name.strip('_') - mb_url = f'https://musicbrainz.org/ws/2/artist?query=artist:"{artist_name}"&fmt=json' - header = {'User-Agent': 'get_artist_art.py/1.0'} - response = requests.get(mb_url, headers=header) - if response.status_code == 200: - mb_data = response.json() - if mb_data['count'] > 0: - if mb_data['artists'][0]['score'] > mb_confidence: - return True, mb_data['artists'][0]['id'] - else: - print("No artist found of hight enough confidance.") - else: - print("No artist found.") - return False, "" - else: - print(f"Error: {response.status_code}") - return False, "" - -def get_image(mb_id, ftv_api_key, artist_path): - ftv_api_url = f'https://webservice.fanart.tv/v3/music/{mb_id}?api_key={ftv_api_key}' - response = requests.get(ftv_api_url) - ftv_data =response.json() - if not ('status' in ftv_data): - if ('artistthumb' in ftv_data): - art_url = ftv_data['artistthumb'][0]['url'] - print(art_url) - response = requests.get(art_url) - if response.status_code == 200: - with open(os.path.join(artist_path, 'artist.jpg'), 'wb') as f: - f.write(response.content) - elif ('hdmusiclogo' in ftv_data): - art_url = ftv_data['hdmusiclogo'][0]['url'] - response = requests.get(art_url) - if response.status_code == 200: - with open(os.path.join(artist_path, 'artist.png'), 'wb') as f: - f.write(response.content) - else: - print("Error downloading: ", response.status_code) - else: - print("Thumb not found.") - else: - error_msg = ftv_data['error message'] - print(f"Error: {error_msg}") \ No newline at end of file diff --git a/get_artist_art.py b/get_artist_art.py index 4293a09..46c0807 100755 --- a/get_artist_art.py +++ b/get_artist_art.py @@ -39,4 +39,4 @@ for artist in dir_list: print(e) count += 1 - sleep(1) + #sleep(1)