20 Commits

Author SHA1 Message Date
c7e66ccbab Remove testing code from get_artist_art.py 2024-10-16 11:53:34 -04:00
320bdfaa92 Impiment set_conf.
This function updates the config file being used by the script.
2024-10-16 11:52:39 -04:00
e73e27b954 Remove old code.
Remove the old config setup code.
2024-10-16 11:22:04 -04:00
d7fb3f78f9 Simplify setup of gaa_conf.
Now done on one line in the main file.
2024-10-16 11:21:18 -04:00
811979cea7 Move configparser code to seperate module
This code is now in its own Class. This is to make it easier to do reporting and updating.
2024-10-16 11:20:14 -04:00
5856541a22 Display version number
The program will now output a version number with the use of either the 'version' or 'help' options.
2024-10-15 15:06:54 -04:00
26dfe543a1 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.
2024-10-15 08:52:22 -04:00
36a1f0ffab Reenable basic ratelimit.
A one second delay between requests to the fanart.tv servers. This is to prevent a 503 response that is given when requests are over the number per second allowed.
2024-10-14 23:33:33 -04:00
cbde52eb59 Merge branch 'master' into ratelimiting 2024-10-14 11:17:37 -04:00
883f594f4f Fix the output path of Artist Background.
The artist background had been saving to the folder the script was run from. This has now been resolved.
2024-10-14 11:08:17 -04:00
c5ffadd1f0 Get the artist background.
Adds the artist background as a possible source of artist image.
2024-10-14 10:34:43 -04:00
485b565a3f Replace working code in empty file.
api_calls.py had been emptied for some reason. Probably an acidential deletion. The old code has now been placed in the file.
2024-10-13 17:38:03 -04:00
386f17779b 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.
2024-10-03 14:53:52 -04:00
ea7396ff53 Sleep timer added.
A dely of 1 second has been added to keep the MusicBrainz API from dropping requests (503 errors).
2024-09-30 22:06:30 -04:00
c01b4cdcdd Send a useragent in the request header to MB.
MusicBrainz desires an identifying header in the requests made to its API. This is now included.
2024-09-30 22:05:31 -04:00
66ce579992 Add check for config in .local directory 2024-09-30 17:03:56 -04:00
6f7e69411b Update code to remove a trialing _ from the artist name. 2024-09-25 21:09:10 -04:00
fe8a4c2410 Add fetching of hdmusiclogo if artistthumb fails.
This will catch a few more artists and at least place something in their directory.
2024-09-20 14:12:54 -04:00
858d68b491 Fix if statement from previous commit. 2024-09-20 14:01:00 -04:00
f2d21185af Check for artist.png when looking for local art. 2024-09-20 13:58:58 -04:00
4 changed files with 94 additions and 23 deletions

View File

@@ -1,36 +1,71 @@
import requests
import os
from time import sleep
def get_mb_id(artist_name, mb_confidence):
mb_url = f'https://musicbrainz.org/ws/2/artist?query=artist:%22{artist_name}%22&fmt=json'
response = requests.get(mb_url)
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']
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}'
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)
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)
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

View File

@@ -4,4 +4,7 @@ def get_all(path):
return [name for name in os.listdir(path) if os.path.isdir(os.path.join(path, name))]
def has_artist_art(path):
return os.path.exists(os.path.join(path, "artist.jpg"))
if(os.path.exists(os.path.join(path, "artist.jpg")))or (os.path.exists(os.path.join(path, "artist.png"))):
return True
else:
return False

View File

@@ -1,12 +1,20 @@
#!/usr/bin/env python3
import configparser
import argparse
import os
from time import sleep
import dir_activities
import api_calls
from prog_conf import gaa_conf
config = configparser.ConfigParser()
config.read('config.ini')
gaa_version = "2024.10.15.2"
option_set = argparse.ArgumentParser(description=f"An automatic downloader of artist art using MusicBrainz and Fanart.TV.\nVersion: {gaa_version}")
jls_extract_var = f'%(prog)s {gaa_version}'
option_set.add_argument('--version', '-v', action='version', version=jls_extract_var)
cmd_options = option_set.parse_args()
config = gaa_conf().conf
music_path = config['music']['dir']
ftv_api_key = config['fanart_tv']['api_key']
@@ -18,18 +26,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(str(count) + ": " + artist)
# 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)

19
prog_conf.py Normal file
View File

@@ -0,0 +1,19 @@
import configparser
from os.path import exists,join,expanduser
class gaa_conf:
def __init__(self):
self.conf = configparser.ConfigParser()
if (exists('config.ini')):
self.conf_path = 'config.ini'
else:
self.conf_path = join(expanduser(
"~"), ".local/share/get_artist_art/config.ini")
self.conf.read(self.conf_path)
def get():
return gaa_conf
def set_conf(self, conf_path):
self.read(conf_path)
return self