From 4808c0ab8e05d606d47642822cdceabff2afb1e7 Mon Sep 17 00:00:00 2001 From: AustrianToast Date: Mon, 5 May 2025 00:57:01 +0200 Subject: [PATCH] make it work Ain't pretty but it is getting the job done for now --- src/main.c | 201 +++++++++++++++++++++++++---------------------------- 1 file changed, 94 insertions(+), 107 deletions(-) diff --git a/src/main.c b/src/main.c index 8dc5919..b930ca3 100644 --- a/src/main.c +++ b/src/main.c @@ -9,11 +9,12 @@ #include #include #include +#include +#include typedef struct { - char *data_dir; - char *log_dir; char *tmp_dir; + char *data_dir; char *gallery_dl_conf; char *yt_dlp_conf; } Config; @@ -25,7 +26,7 @@ int h_config_parser(Config *config) { fprintf(stderr, "Couldn't read env variable USER\n"); return 1; } - sprintf(config_location, "/home/%s/.config/Hentai.conf", user_name); + sprintf(config_location, "/home/%s/.config/HDB.conf", user_name); FILE *fptr = fopen(config_location, "r"); if (fptr == NULL) { @@ -61,12 +62,6 @@ int h_config_parser(Config *config) { if (strcmp(left, "data_dir") == 0) { config->data_dir = malloc(10000); memcpy(config->data_dir, right, strlen(right)); - } else if (strcmp(left, "log_dir") == 0) { - config->log_dir = malloc(10000); - memcpy(config->log_dir, right, strlen(right)); - } else if (strcmp(left, "tmp_dir") == 0) { - config->tmp_dir = malloc(10000); - memcpy(config->tmp_dir, right, strlen(right)); } else if (strcmp(left, "gallery-dl_conf") == 0) { config->gallery_dl_conf = malloc(10000); memcpy(config->gallery_dl_conf, right, strlen(right)); @@ -83,49 +78,6 @@ int h_config_parser(Config *config) { return 0; } -int h_command_builder(char *command, char **args) { - char *tmp_command = malloc(strlen(command)); - memcpy(tmp_command, command, strlen(command)); - char *tmp_token; - size_t index = 0; - - if (tmp_command == NULL) { return 1; } - - tmp_token = strtok(tmp_command, " "); - while(tmp_token != NULL) { - args[index] = malloc(strlen(tmp_token)); - memcpy(args[index], tmp_token, strlen(tmp_token)); - tmp_token = strtok(NULL, " "); - index++; - } - free(tmp_command); - args[index++] = NULL; - return 0; -} - -int h_run_cmd_and_wait(char *command) { - char **args = malloc(strlen(command)); - int ret = h_command_builder(command, args); - if (ret != 0) { - return 1; - } - - pid_t pid = fork(); - int status; - - if (pid == -1) { - printf("Couldn't fork'\n"); - return 1; - } else if (pid > 0) { - waitpid(pid, &status, 0); - } else { - execvp(args[0], args); - } - free(args); - - return status; -} - int h_has_internet() { int ret; @@ -173,41 +125,75 @@ int h_has_internet() { return 0; } -int h_download(char *db_row, Config config) { +int h_download(char *line, Config config) { int ret; - char *url = malloc(strlen(db_row)); - char *artist_name = malloc(strlen(db_row)); - char *website_name = malloc(strlen(db_row)); - sscanf(db_row, "%s %s %s", url, artist_name, website_name); - char *path = malloc(strlen(artist_name) + strlen(website_name) + 2); - sprintf(path, "%s/%s", artist_name, website_name); - char *command = malloc(10000); - if (strstr(website_name, "Iwara") == 0) { - sprintf(command, "yt-dlp_linux --config-locations %s -o %s/%(id)s.%(ext)s %s", config.yt_dlp_conf, path, url); + char *url = malloc(strlen(line)); + char *artist_name = malloc(strlen(line)); + char *website_name = malloc(strlen(line)); + sscanf(line, "%s %s %s", url, artist_name, website_name); + char *args[7]; + char *tmp = malloc(10000); + if (strstr(website_name, "Iwara") != NULL) { + sprintf(tmp, "%s/%s/%s/%(id)s.%(ext)s", config.data_dir, artist_name, website_name); + args[0] = "yt-dlp_linux"; + args[1] = "--config-locations"; + args[2] = config.yt_dlp_conf; + args[3] = "-o"; + args[4] = tmp; + args[5] = url; } else { - sprintf(command, "gallery-dl --config %s --directory %s/%s %s", config.gallery_dl_conf, config.data_dir, path, url); + sprintf(tmp, "%s/%s/%s", config.data_dir, artist_name, website_name); + args[0] = "gallery-dl"; + args[1] = "--config"; + args[2] = config.gallery_dl_conf; + args[3] = "--directory"; + args[4] = tmp; + args[5] = url; } - free(url); - free(artist_name); - free(website_name); + args[6] = NULL; + + size_t i; + for (i = 5; i > 0; i--) { + /* Keep for later debugging or logging + size_t j; + for (j = 0; args[j] != NULL; j++) { + printf("args[%d] = %s\n", j, args[j]); + } + */ - size_t i = 5; - for (; i > 0; i--) { - printf("%s\n", command); - /* h_run_cmd_and_wait(command) */ + pid_t pid = fork(); + + if (pid == -1) { + printf("Couldn't fork'\n"); + return 1; + } else if (pid > 0) { + waitpid(pid, &ret, 0); + } else { + execvp(args[0], args); + } + + if (ret != 0) { + fprintf(stderr, "Command exited with non-zero code. code = %d\n", ret); + } + + /* TODO: move contents of h_has_internet here */ ret = h_has_internet(); if (ret != 0) { fprintf(stderr, "No internet\n"); return 1; } } - free(command); + free(url); + free(artist_name); + free(website_name); + free(tmp); return 0; } int main(int argc, char **argv) { int ret; Config config = { 0 }; + config.tmp_dir = "/tmp/HDB"; ret = h_config_parser(&config); if (ret != 0) { @@ -215,12 +201,17 @@ int main(int argc, char **argv) { return 1; } + if(mkdir(config.tmp_dir, 0777) && errno != EEXIST) { + fprintf(stderr, "'%s' %s\n", config.tmp_dir, strerror(errno)); + return 1; + } + if (argc < 2) { fprintf(stderr, "Incorrect amount of arguments\n"); return 1; } - ret = h_db_open(":memory:"); + ret = h_db_open("HDB.db"); if (ret != 0) { return 1; } @@ -253,45 +244,41 @@ int main(int argc, char **argv) { return 1; } } else if (strcmp(argv[1], "download_website") == 0) { - char *website_name = argv[2]; - char *file_name = malloc(10000); - if (argc == 3) { - sprintf(file_name, "%s_generated.txt", website_name); - FILE *fptr = fopen(file_name, "w+"); - free(file_name); - if (fptr == NULL) { - fprintf(stderr, "Couldn't open file\n"); - h_db_close(); - return 1; - } - - ret = h_db_generate_file(fptr, argv[2]); - fclose(fptr); - if (ret != 0) { - h_db_close(); - return 1; - } - } else if (argc == 4) { - sprintf(file_name, "%s/%s_generated.txt", argv[3], website_name); - FILE *fptr = fopen(file_name, "w+"); - free(file_name); - if (fptr == NULL) { - fprintf(stderr, "Couldn't open file\n"); - h_db_close(); - return 1; - } - - ret = h_db_generate_file(fptr, argv[2]); - fclose(fptr); - if (ret != 0) { - h_db_close(); - return 1; - } - } else { + if (argc != 3) { fprintf(stderr, "Incorrect amount of arguments\n"); h_db_close(); return 1; } + char *file_name = malloc(10000); + sprintf(file_name, "%s/%s_generated.txt", config.tmp_dir, argv[2]); + FILE *fptr = fopen(file_name, "w+"); + if (fptr == NULL) { + fprintf(stderr, "Couldn't open file\n"); + h_db_close(); + return 1; + } + + ret = h_db_generate_file(fptr, argv[2]); + fclose(fptr); + if (ret != 0) { + h_db_close(); + return 1; + } + + fptr = fopen(file_name, "r"); + char *line = malloc(10000); + while(fgets(line, 10000, fptr) != NULL) { + ret = h_download(line, config); + if (ret != 0) { + fclose(fptr); + h_db_close(); + return 1; + } + } + + free(line); + fclose(fptr); + free(file_name); } else { fprintf(stderr, "Invalid argument\n"); h_db_close();