Compare commits
13 Commits
6189a8b17f
...
config_opt
| Author | SHA1 | Date | |
|---|---|---|---|
| c7e66ccbab | |||
| 320bdfaa92 | |||
| e73e27b954 | |||
| d7fb3f78f9 | |||
| 811979cea7 | |||
| 5856541a22 | |||
| 26dfe543a1 | |||
| 36a1f0ffab | |||
| cbde52eb59 | |||
| 883f594f4f | |||
| c5ffadd1f0 | |||
| 485b565a3f | |||
| 386f17779b |
71
api_calls.py
Normal file
71
api_calls.py
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
import requests
|
||||||
|
import os
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
|
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'], True
|
||||||
|
else:
|
||||||
|
print("No artist found of hight enough confidance.")
|
||||||
|
return False, "", True
|
||||||
|
else:
|
||||||
|
print("No artist found.")
|
||||||
|
return False, "", True
|
||||||
|
elif (response.status_code == 503):
|
||||||
|
sleep(1)
|
||||||
|
return False, "", False
|
||||||
|
else:
|
||||||
|
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']
|
||||||
|
if (error_msg == "503"):
|
||||||
|
sleep(1)
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
print(f"FTV Error: {error_msg}")
|
||||||
|
return True
|
||||||
10
dir_activities.py
Normal file
10
dir_activities.py
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
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):
|
||||||
|
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
|
||||||
139
get_artist_art.c
139
get_artist_art.c
@@ -1,139 +0,0 @@
|
|||||||
#include <curl/curl.h>
|
|
||||||
#include <libconfig.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#define conf_file "config.ini"
|
|
||||||
#define mb_url \
|
|
||||||
"https://musicbrainz.org/ws/2/artist?query=artist:\"Slayer\"&fmt=json"
|
|
||||||
|
|
||||||
const char *get_conf_str(char set_key[]) {
|
|
||||||
const char *prog_conf = malloc(10 * sizeof(char));
|
|
||||||
config_t cfg;
|
|
||||||
config_setting_t *setting;
|
|
||||||
|
|
||||||
config_init(&cfg);
|
|
||||||
|
|
||||||
if (!config_read_file(&cfg, conf_file)) {
|
|
||||||
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
|
|
||||||
config_error_line(&cfg), config_error_text(&cfg));
|
|
||||||
config_destroy(&cfg);
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
setting = config_lookup(&cfg, set_key);
|
|
||||||
|
|
||||||
prog_conf = config_setting_get_string(setting);
|
|
||||||
|
|
||||||
config_destroy(&cfg);
|
|
||||||
return prog_conf;
|
|
||||||
}
|
|
||||||
|
|
||||||
int get_conf_int(char set_key[]) {
|
|
||||||
int set_int;
|
|
||||||
config_t cfg;
|
|
||||||
config_setting_t *setting;
|
|
||||||
|
|
||||||
config_init(&cfg);
|
|
||||||
|
|
||||||
if (!config_read_file(&cfg, conf_file)) {
|
|
||||||
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
|
|
||||||
config_error_line(&cfg), config_error_text(&cfg));
|
|
||||||
config_destroy(&cfg);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
setting = config_lookup(&cfg, set_key);
|
|
||||||
set_int = config_setting_get_int(setting);
|
|
||||||
|
|
||||||
config_destroy(&cfg);
|
|
||||||
|
|
||||||
return set_int;
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_conf(const char m_dir[], const int mb_conf,
|
|
||||||
const char ftv_api_key[32]) {
|
|
||||||
if (m_dir != NULL) {
|
|
||||||
printf("The music directory is: %s\n", m_dir);
|
|
||||||
}
|
|
||||||
if (mb_conf != 1) {
|
|
||||||
printf("The lower limit for MB confidence is: %d\n", mb_conf);
|
|
||||||
}
|
|
||||||
if (ftv_api_key != NULL) {
|
|
||||||
printf("Your API key is: %s\n", ftv_api_key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *get_mb_id() {
|
|
||||||
int response_code;
|
|
||||||
const char *mb_id;
|
|
||||||
CURL *curl;
|
|
||||||
CURLcode res;
|
|
||||||
char *buffer;
|
|
||||||
long res_len;
|
|
||||||
|
|
||||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
|
||||||
|
|
||||||
curl = curl_easy_init();
|
|
||||||
|
|
||||||
if (curl) {
|
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, mb_url);
|
|
||||||
curl_easy_setopt(curl, CURLOPT_USERAGENT, "get_artist_art.py/1.0");
|
|
||||||
|
|
||||||
res = curl_easy_perform(curl);
|
|
||||||
|
|
||||||
if (res != CURLE_OK) {
|
|
||||||
fprintf(stderr, "curl_easy_perform() failed with error: %s\n",
|
|
||||||
curl_easy_strerror(res));
|
|
||||||
return "1";
|
|
||||||
}
|
|
||||||
|
|
||||||
curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &res_len);
|
|
||||||
|
|
||||||
buffer = malloc(res_len);
|
|
||||||
|
|
||||||
res = curl_easy_recv(curl, buffer, res_len, 0);
|
|
||||||
|
|
||||||
if (res != CURLE_OK) {
|
|
||||||
fprintf(stderr, "curl_easy_recv() failed with error: %s\n",
|
|
||||||
curl_easy_strerror(res));
|
|
||||||
return "1";
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Response body: %s\n", buffer);
|
|
||||||
|
|
||||||
free(buffer);
|
|
||||||
|
|
||||||
curl_easy_cleanup(curl);
|
|
||||||
}
|
|
||||||
|
|
||||||
return mb_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
|
||||||
int mb_conf, opt;
|
|
||||||
const char *m_dir, *ftv_api_key;
|
|
||||||
|
|
||||||
m_dir = get_conf_str("dir");
|
|
||||||
mb_conf = get_conf_int("confidence");
|
|
||||||
ftv_api_key = get_conf_str("api_key");
|
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "ps:")) != -1) {
|
|
||||||
switch (opt) {
|
|
||||||
case 'p':
|
|
||||||
print_conf(m_dir, mb_conf, ftv_api_key);
|
|
||||||
break;
|
|
||||||
case 's':
|
|
||||||
printf("This doesn't do anything yet but you searched for %s\n", optarg);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
printf("So long and thanks for all the fish.\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
get_mb_id();
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
|
||||||
}
|
|
||||||
49
get_artist_art.py
Executable file
49
get_artist_art.py
Executable file
@@ -0,0 +1,49 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
from time import sleep
|
||||||
|
import dir_activities
|
||||||
|
import api_calls
|
||||||
|
from prog_conf import gaa_conf
|
||||||
|
|
||||||
|
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']
|
||||||
|
mb_confidence = int(config['musicbrainz']['confidence'])
|
||||||
|
|
||||||
|
count = 1
|
||||||
|
dir_list = dir_activities.get_all(music_path)
|
||||||
|
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.strip('_'))
|
||||||
|
try:
|
||||||
|
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:
|
||||||
|
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
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