Files
get_artist_art/get_artist_art.c
20xd6 c7faeb6bc8 get_mb_id now taking artist name as input variable.
The function can now be fed an artist name to be placed in the
MusicBrainz query string. This will allow it to be used in a loop in the
future.
2024-10-04 14:16:04 -04:00

146 lines
3.5 KiB
C

#include <curl/curl.h>
#include <libconfig.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define version_str "1.0"
#define conf_file "config.ini"
#define mb_url \
"https://musicbrainz.org/ws/2/artist?query=artist:\"%s\"&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(char *artist_name) {
int response_code;
const char *mb_id;
CURL *curl;
CURLcode res;
char *buffer;
long res_len;
char mb_url_full[100];
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
char *artist_name_esc = curl_easy_escape(curl, artist_name, 0);
printf("%s", artist_name_esc);
snprintf(mb_url_full, sizeof(mb_url_full), mb_url, artist_name_esc);
// snprintf(mb_url_full, sizeof(mb_url_full), "%s", curl_easy_escape(curl, mb_url_full, 0));
printf("%s\n", mb_url_full);
curl_easy_setopt(curl, CURLOPT_URL, mb_url_full);
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("The Beatles");
return EXIT_SUCCESS;
}