Reimpliment delay to only trigger on 503 response
The code has been refactored to only trigger a one second delay when one of the APIs have returned a 503. This will allow the script to finish faster.
This commit is contained in:
parent
36a1f0ffab
commit
26dfe543a1
30
api_calls.py
30
api_calls.py
@ -1,5 +1,6 @@
|
||||
import requests
|
||||
import os
|
||||
from time import sleep
|
||||
|
||||
def get_mb_id(artist_name, mb_confidence):
|
||||
artist_name = artist_name.strip('_')
|
||||
@ -10,15 +11,19 @@ def get_mb_id(artist_name, mb_confidence):
|
||||
mb_data = response.json()
|
||||
if mb_data['count'] > 0:
|
||||
if mb_data['artists'][0]['score'] > mb_confidence:
|
||||
return True, mb_data['artists'][0]['id']
|
||||
return True, mb_data['artists'][0]['id'], True
|
||||
else:
|
||||
print("No artist found of hight enough confidance.")
|
||||
return False, "", True
|
||||
else:
|
||||
print("No artist found.")
|
||||
return False, ""
|
||||
return False, "", True
|
||||
elif (response.status_code == 503):
|
||||
sleep(1)
|
||||
return False, "", False
|
||||
else:
|
||||
print(f"Error: {response.status_code}")
|
||||
return False, ""
|
||||
print(f"MB Error: {response.status_code}")
|
||||
return False, "", True
|
||||
|
||||
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}'
|
||||
@ -32,22 +37,35 @@ def get_image(mb_id, ftv_api_key, artist_path):
|
||||
if response.status_code == 200:
|
||||
with open(os.path.join(artist_path, 'artist.jpg'), 'wb') as f:
|
||||
f.write(response.content)
|
||||
return True
|
||||
elif ('artistbackground' in ftv_data):
|
||||
art_url = ftv_data['artistbackground'][0]['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)
|
||||
return True
|
||||
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)
|
||||
f.write(response.content)
|
||||
return True
|
||||
else:
|
||||
print("Error downloading: ", response.status_code)
|
||||
return True
|
||||
else:
|
||||
print("Thumb not found.")
|
||||
return True
|
||||
elif (response.status_code == 503):
|
||||
sleep(1)
|
||||
return False
|
||||
else:
|
||||
error_msg = ftv_data['error message']
|
||||
print(f"Error: {error_msg}")
|
||||
if (error_msg == "503"):
|
||||
sleep(1)
|
||||
return False
|
||||
else:
|
||||
print(f"FTV Error: {error_msg}")
|
||||
return True
|
@ -24,19 +24,24 @@ dir_list.sort()
|
||||
for artist in dir_list:
|
||||
artist_path = os.path.join(music_path, artist)
|
||||
if (not(dir_activities.has_artist_art(artist_path))):
|
||||
print(dir_activities.has_artist_art(artist_path))
|
||||
# print(dir_activities.has_artist_art(artist_path))
|
||||
print(str(count) + ": " + artist.strip('_'))
|
||||
try:
|
||||
found_status, mb_id = api_calls.get_mb_id(artist, mb_confidence)
|
||||
ftv_response = False
|
||||
mb_exit = False
|
||||
while not mb_exit:
|
||||
found_status, mb_id, mb_exit = api_calls.get_mb_id(artist, mb_confidence)
|
||||
# print("Getting ", artist_image)
|
||||
if found_status:
|
||||
artist_image = api_calls.get_image(mb_id, ftv_api_key, artist_path)
|
||||
while not ftv_response:
|
||||
ftv_response = api_calls.get_image(mb_id, ftv_api_key, artist_path)
|
||||
print(ftv_response)
|
||||
else:
|
||||
print(f"{artist} returned no results.")
|
||||
# api_requests.get_art(artist_image, artist, music_path)
|
||||
except Exception as e:
|
||||
print("Artist or art not found.")
|
||||
print(e)
|
||||
|
||||
print("---------")
|
||||
count += 1
|
||||
sleep(1)
|
||||
#sleep(1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user