Compare commits
13 Commits
accept_swi
...
config_opt
| Author | SHA1 | Date | |
|---|---|---|---|
| c7e66ccbab | |||
| 320bdfaa92 | |||
| e73e27b954 | |||
| d7fb3f78f9 | |||
| 811979cea7 | |||
| 5856541a22 | |||
| 26dfe543a1 | |||
| 36a1f0ffab | |||
| cbde52eb59 | |||
| 883f594f4f | |||
| c5ffadd1f0 | |||
| 485b565a3f | |||
| 386f17779b |
36
api_calls.py
36
api_calls.py
@@ -1,5 +1,6 @@
|
|||||||
import requests
|
import requests
|
||||||
import os
|
import os
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
def get_mb_id(artist_name, mb_confidence):
|
def get_mb_id(artist_name, mb_confidence):
|
||||||
artist_name = artist_name.strip('_')
|
artist_name = artist_name.strip('_')
|
||||||
@@ -10,15 +11,19 @@ def get_mb_id(artist_name, mb_confidence):
|
|||||||
mb_data = response.json()
|
mb_data = response.json()
|
||||||
if mb_data['count'] > 0:
|
if mb_data['count'] > 0:
|
||||||
if mb_data['artists'][0]['score'] > mb_confidence:
|
if mb_data['artists'][0]['score'] > mb_confidence:
|
||||||
return True, mb_data['artists'][0]['id']
|
return True, mb_data['artists'][0]['id'], True
|
||||||
else:
|
else:
|
||||||
print("No artist found of hight enough confidance.")
|
print("No artist found of hight enough confidance.")
|
||||||
|
return False, "", True
|
||||||
else:
|
else:
|
||||||
print("No artist found.")
|
print("No artist found.")
|
||||||
return False, ""
|
return False, "", True
|
||||||
|
elif (response.status_code == 503):
|
||||||
|
sleep(1)
|
||||||
|
return False, "", False
|
||||||
else:
|
else:
|
||||||
print(f"Error: {response.status_code}")
|
print(f"MB Error: {response.status_code}")
|
||||||
return False, ""
|
return False, "", True
|
||||||
|
|
||||||
def get_image(mb_id, ftv_api_key, artist_path):
|
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}'
|
ftv_api_url = f'https://webservice.fanart.tv/v3/music/{mb_id}?api_key={ftv_api_key}'
|
||||||
@@ -32,16 +37,35 @@ def get_image(mb_id, ftv_api_key, artist_path):
|
|||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
with open(os.path.join(artist_path, 'artist.jpg'), 'wb') as f:
|
with open(os.path.join(artist_path, 'artist.jpg'), 'wb') as f:
|
||||||
f.write(response.content)
|
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):
|
elif ('hdmusiclogo' in ftv_data):
|
||||||
art_url = ftv_data['hdmusiclogo'][0]['url']
|
art_url = ftv_data['hdmusiclogo'][0]['url']
|
||||||
response = requests.get(art_url)
|
response = requests.get(art_url)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
with open(os.path.join(artist_path, 'artist.png'), 'wb') as f:
|
with open(os.path.join(artist_path, 'artist.png'), 'wb') as f:
|
||||||
f.write(response.content)
|
f.write(response.content)
|
||||||
|
return True
|
||||||
else:
|
else:
|
||||||
print("Error downloading: ", response.status_code)
|
print("Error downloading: ", response.status_code)
|
||||||
|
return True
|
||||||
else:
|
else:
|
||||||
print("Thumb not found.")
|
print("Thumb not found.")
|
||||||
|
return True
|
||||||
|
elif (response.status_code == 503):
|
||||||
|
sleep(1)
|
||||||
|
return False
|
||||||
else:
|
else:
|
||||||
error_msg = ftv_data['error message']
|
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
|
||||||
@@ -1,18 +1,20 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import configparser
|
import argparse
|
||||||
import os
|
import os
|
||||||
from time import sleep
|
from time import sleep
|
||||||
import dir_activities
|
import dir_activities
|
||||||
import api_calls
|
import api_calls
|
||||||
|
from prog_conf import gaa_conf
|
||||||
|
|
||||||
config = configparser.ConfigParser()
|
gaa_version = "2024.10.15.2"
|
||||||
if (os.path.exists('config.ini')):
|
|
||||||
conf_path = 'config.ini'
|
option_set = argparse.ArgumentParser(description=f"An automatic downloader of artist art using MusicBrainz and Fanart.TV.\nVersion: {gaa_version}")
|
||||||
else:
|
jls_extract_var = f'%(prog)s {gaa_version}'
|
||||||
conf_path = os.path.join(os.path.expanduser("~"), ".local/share/get_artist_art/config.ini")
|
option_set.add_argument('--version', '-v', action='version', version=jls_extract_var)
|
||||||
|
cmd_options = option_set.parse_args()
|
||||||
config.read(conf_path)
|
|
||||||
|
config = gaa_conf().conf
|
||||||
|
|
||||||
music_path = config['music']['dir']
|
music_path = config['music']['dir']
|
||||||
ftv_api_key = config['fanart_tv']['api_key']
|
ftv_api_key = config['fanart_tv']['api_key']
|
||||||
@@ -24,19 +26,24 @@ dir_list.sort()
|
|||||||
for artist in dir_list:
|
for artist in dir_list:
|
||||||
artist_path = os.path.join(music_path, artist)
|
artist_path = os.path.join(music_path, artist)
|
||||||
if (not(dir_activities.has_artist_art(artist_path))):
|
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('_'))
|
print(str(count) + ": " + artist.strip('_'))
|
||||||
try:
|
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)
|
# print("Getting ", artist_image)
|
||||||
if found_status:
|
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:
|
else:
|
||||||
print(f"{artist} returned no results.")
|
print(f"{artist} returned no results.")
|
||||||
# api_requests.get_art(artist_image, artist, music_path)
|
# api_requests.get_art(artist_image, artist, music_path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Artist or art not found.")
|
print("Artist or art not found.")
|
||||||
print(e)
|
print(e)
|
||||||
|
print("---------")
|
||||||
count += 1
|
count += 1
|
||||||
sleep(1)
|
#sleep(1)
|
||||||
|
|||||||
19
prog_conf.py
Normal file
19
prog_conf.py
Normal 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
|
||||||
Reference in New Issue
Block a user