make it work

Ain't pretty but it is getting the job done for now
This commit is contained in:
2025-05-05 00:57:01 +02:00
parent b60124ba82
commit 4808c0ab8e

View File

@ -9,11 +9,12 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h>
#include <errno.h>
typedef struct { typedef struct {
char *data_dir;
char *log_dir;
char *tmp_dir; char *tmp_dir;
char *data_dir;
char *gallery_dl_conf; char *gallery_dl_conf;
char *yt_dlp_conf; char *yt_dlp_conf;
} Config; } Config;
@ -25,7 +26,7 @@ int h_config_parser(Config *config) {
fprintf(stderr, "Couldn't read env variable USER\n"); fprintf(stderr, "Couldn't read env variable USER\n");
return 1; 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"); FILE *fptr = fopen(config_location, "r");
if (fptr == NULL) { if (fptr == NULL) {
@ -61,12 +62,6 @@ int h_config_parser(Config *config) {
if (strcmp(left, "data_dir") == 0) { if (strcmp(left, "data_dir") == 0) {
config->data_dir = malloc(10000); config->data_dir = malloc(10000);
memcpy(config->data_dir, right, strlen(right)); 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) { } else if (strcmp(left, "gallery-dl_conf") == 0) {
config->gallery_dl_conf = malloc(10000); config->gallery_dl_conf = malloc(10000);
memcpy(config->gallery_dl_conf, right, strlen(right)); memcpy(config->gallery_dl_conf, right, strlen(right));
@ -83,49 +78,6 @@ int h_config_parser(Config *config) {
return 0; 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 h_has_internet() {
int ret; int ret;
@ -173,41 +125,75 @@ int h_has_internet() {
return 0; return 0;
} }
int h_download(char *db_row, Config config) { int h_download(char *line, Config config) {
int ret; int ret;
char *url = malloc(strlen(db_row)); char *url = malloc(strlen(line));
char *artist_name = malloc(strlen(db_row)); char *artist_name = malloc(strlen(line));
char *website_name = malloc(strlen(db_row)); char *website_name = malloc(strlen(line));
sscanf(db_row, "%s %s %s", url, artist_name, website_name); sscanf(line, "%s %s %s", url, artist_name, website_name);
char *path = malloc(strlen(artist_name) + strlen(website_name) + 2); char *args[7];
sprintf(path, "%s/%s", artist_name, website_name); char *tmp = malloc(10000);
char *command = malloc(10000); if (strstr(website_name, "Iwara") != NULL) {
if (strstr(website_name, "Iwara") == 0) { sprintf(tmp, "%s/%s/%s/%(id)s.%(ext)s", config.data_dir, artist_name, website_name);
sprintf(command, "yt-dlp_linux --config-locations %s -o %s/%(id)s.%(ext)s %s", config.yt_dlp_conf, path, url); 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 { } 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); args[6] = NULL;
free(artist_name);
free(website_name);
size_t i = 5; size_t i;
for (; i > 0; i--) { for (i = 5; i > 0; i--) {
printf("%s\n", command); /* Keep for later debugging or logging
/* h_run_cmd_and_wait(command) */ size_t j;
for (j = 0; args[j] != NULL; j++) {
printf("args[%d] = %s\n", j, args[j]);
}
*/
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(); ret = h_has_internet();
if (ret != 0) { if (ret != 0) {
fprintf(stderr, "No internet\n"); fprintf(stderr, "No internet\n");
return 1; return 1;
} }
} }
free(command); free(url);
free(artist_name);
free(website_name);
free(tmp);
return 0; return 0;
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
int ret; int ret;
Config config = { 0 }; Config config = { 0 };
config.tmp_dir = "/tmp/HDB";
ret = h_config_parser(&config); ret = h_config_parser(&config);
if (ret != 0) { if (ret != 0) {
@ -215,12 +201,17 @@ int main(int argc, char **argv) {
return 1; 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) { if (argc < 2) {
fprintf(stderr, "Incorrect amount of arguments\n"); fprintf(stderr, "Incorrect amount of arguments\n");
return 1; return 1;
} }
ret = h_db_open(":memory:"); ret = h_db_open("HDB.db");
if (ret != 0) { if (ret != 0) {
return 1; return 1;
} }
@ -253,45 +244,41 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
} else if (strcmp(argv[1], "download_website") == 0) { } else if (strcmp(argv[1], "download_website") == 0) {
char *website_name = argv[2]; if (argc != 3) {
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 {
fprintf(stderr, "Incorrect amount of arguments\n"); fprintf(stderr, "Incorrect amount of arguments\n");
h_db_close(); h_db_close();
return 1; 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 { } else {
fprintf(stderr, "Invalid argument\n"); fprintf(stderr, "Invalid argument\n");
h_db_close(); h_db_close();