move download() to hdbd.c and refactor it

it also wont compile because it is missing the config stuff
This commit is contained in:
2025-09-18 23:36:59 +02:00
parent e52388311e
commit 34495bccbb
2 changed files with 67 additions and 94 deletions

View File

@@ -78,68 +78,6 @@ int h_config_parser(Config *config) {
return 0;
}
int h_download(char *line, Config config) {
int ret;
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(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;
}
args[6] = NULL;
printf("Starting download using %s\n", args[0]);
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]);
}
*/
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) {
break;
}
fprintf(stderr, "Command exited with non-zero code. code = %d\n", ret);
}
free(url);
free(artist_name);
free(website_name);
free(tmp);
return 0;
}
int main(int argc, char **argv) {
int ret;
Config config = { 0 };

View File

@@ -22,58 +22,79 @@ int sockaddr_len;
void parse_message(char *buffer, char *operation, char *operand1, char *operand2, char *operand3) {
int len_to_ws = 0;
int offset = 0;
char log_line[1000] = {0};
sprintf(log_line, "Beginning of parsing message: '%s'", buffer);
syslog(LOG_DEBUG, log_line);
len_to_ws = strcspn(buffer, " ");
strncpy(operation, buffer, len_to_ws);
offset = len_to_ws + 1;
sprintf(log_line, "operation = '%s'", operation);
syslog(LOG_DEBUG, log_line);
syslog(LOG_DEBUG, "parse_message(): operation = '%s'", operation);
if (strlen(buffer) == offset - 1) { return; }
if (strlen(buffer) == offset - 1) {
syslog(LOG_DEBUG, "Finished parsing message.");
return;
}
len_to_ws = strcspn(buffer + offset, " ");
strncpy(operand1, buffer + offset, len_to_ws);
offset += len_to_ws + 1;
sprintf(log_line, "operand1 = '%s'", operand1);
syslog(LOG_DEBUG, log_line);
syslog(LOG_DEBUG, "parse_message(): operand1 = '%s'", operand1);
if (strlen(buffer) == offset -1) {
syslog(LOG_DEBUG, "Finished parsing message.");
return;
}
if (strlen(buffer) == offset -1) { return; }
len_to_ws = strcspn(buffer + offset, " ");
strncpy(operand2, buffer + offset, len_to_ws);
offset += len_to_ws + 1;
sprintf(log_line, "operand2 = '%s'", operand2);
syslog(LOG_DEBUG, log_line);
syslog(LOG_DEBUG, "parse_message(): operand2 = '%s'", operand2);
if (strlen(buffer) == offset -1) { return; }
if (strlen(buffer) == offset -1) {
syslog(LOG_DEBUG, "Finished parsing message.");
return;
}
len_to_ws = strcspn(buffer + offset, " ");
strncpy(operand3, buffer + offset, len_to_ws);
offset += len_to_ws + 1;
sprintf(log_line, "operand3 = '%s'", operand3);
syslog(LOG_DEBUG, log_line);
syslog(LOG_DEBUG, "parse_message(): operand3 = '%s'", operand3);
if (strlen(buffer) == offset -1) {
syslog(LOG_DEBUG, "Finished parsing message.");
return;
if (strlen(buffer) == offset -1) { return; }
}
int download(char *url, char *artist_name, char *website_name) {
char *args[7];
char download_path[10000] = {0};
if (strstr(website_name, "Iwara") != NULL) {
sprintf(download_path, "%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";
} else {
sprintf(download_path, "%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] = download_path;
args[5] = url;
args[6] = NULL;
syslog(LOG_DEBUG, "download(): Starting download using %s", args[0]);
int ret;
pid_t pid = fork();
if (pid == -1) {
syslog(LOG_ERR, "download(): Couldn't fork");
return 1;
} else if (pid > 0) {
waitpid(pid, &ret, 0);
} else {
execvp(args[0], args);
}
if (ret != 0) {
syslog(LOG_ERR, "download(): Downloader exited with non-zero code. code = %d", ret);
}
return 0;
}
int main() {
@@ -132,7 +153,6 @@ int main() {
int data_length = 0;
char receive_buf[1000] = {0};
char send_buf[1000] = {0};
char log_line[10000] = {0};
char operation[1000] = {0};
char operand1[1000] = {0};
char operand2[1000] = {0};
@@ -153,8 +173,7 @@ int main() {
continue;
}
sprintf(log_line, "Received from Client: '%s'", receive_buf);
syslog(LOG_DEBUG, log_line);
syslog(LOG_DEBUG, "Received from Client: '%s'", receive_buf);
parse_message(receive_buf, operation, operand1, operand2, operand3);
@@ -165,6 +184,22 @@ int main() {
close(client_sock);
break;
}
if (strcmp(operation, "dl") == 0) {
}
if (strcmp(operation, "add") == 0) {
}
if (strcmp(operation, "add_custom") == 0) {
}
if (strcmp(operation, "delete") == 0) {
}
if (strcmp(operation, "update") == 0) {
}
syslog(LOG_INFO, "Closing connection.");
close(client_sock);