Compare commits

..

2 Commits

Author SHA1 Message Date
92b65aacc6 switch to libstrops and execlp 2025-05-28 20:16:24 +02:00
e0756a714d Portable Makefile 2025-05-28 20:15:56 +02:00
2 changed files with 28 additions and 35 deletions

View File

@ -1,2 +1,11 @@
yt_download: .POSIX:
gcc -ansi -I . -L. -ggdb -o yt_download yt_download.c -Wl,-Bstatic -lstrops -Wl,-Bdynamic yt_download: yt_download.c
gcc -ansi -O2 -o yt_download yt_download.c -lstrops
.PHONY: install uninstall
install: yt_download
sudo mv yt_download /usr/local/bin
uninstall:
sudo rm /usr/local/bin/yt_download

View File

@ -1,32 +1,12 @@
#include <strops.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
int download(char *path, char *url, char is_audio) { int download(char *path, char *url, char is_audio) {
int ret; int ret;
char *args[10]; char *downloaded = malloc(strops_length(path) + 1);
args[0] = "yt-dlp"; is_audio ? sprintf(downloaded, "%s/downloaded_audio.txt", path)
args[1] = "--config-locations"; : sprintf(downloaded, "%s/downloaded_video.txt", path);
if (is_audio) {
args[2] = "/mnt/youtube/yt-dlp_audio.conf";
} else {
args[2] = "/mnt/youtube/yt-dlp_video.conf";
}
args[3] = "--download-archive";
if (is_audio) {
char *buf = malloc(strlen(path) + 1);
sprintf(buf, "%s/downloaded_audio.txt", path);
args[4] = buf;
} else {
char *buf = malloc(strlen(path) + 1);
sprintf(buf, "%s/downloaded_video.txt", path);
args[4] = buf;
}
args[5] = "--paths";
args[6] = path;
args[7] = "--yes-playlist";
args[8] = url;
args[9] = 0;
int pid = fork(); int pid = fork();
@ -36,32 +16,36 @@ int download(char *path, char *url, char is_audio) {
} else if (pid > 0) { } else if (pid > 0) {
waitpid(pid, &ret, 0); waitpid(pid, &ret, 0);
} else { } else {
execvp(args[0], args); execlp("yt-dlp", "yt-dlp",
"--config-locations", is_audio ? "/mnt/youtube/yt-dlp_audio.conf" : "/mnt/youtube/yt-dlp_video.conf",
"--download-archive", downloaded,
"--paths", path,
"--yes-playlist",
url,
NULL);
} }
free(buf); free(downloaded);
return ret; return ret;
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
if (argc != 4) { if (argc != 4) {
fprintf(stderr, "Incorred amount of arguments\n"); fprintf(stderr, "Incorred amount of arguments\n");
return 1;
} }
char *path; char *path, *url, is_audio;
char *url;
char is_audio;
size_t i; size_t i;
for (i = 1; i < 4; i++) { for (i = 1; i < 4; i++) {
if (argv[i][0] == '-') { if (argv[i][0] == '-') {
if (strcmp(argv[i], "-audio") == 0) { if (strops_equals(argv[i], "-audio")) {
is_audio = 1; is_audio = 1;
} else if (strcmp(argv[i], "-video") == 0) { } else if (strops_equals(argv[i], "-video")) {
is_audio = 0; is_audio = 0;
} else { } else {
fprintf(stderr, "Invalid option: %s\n", argv[i]); fprintf(stderr, "Invalid option: %s\n", argv[i]);
return 1; return 1;
} }
} else if (strstr(argv[i], "https://")) { } else if (strops_starts_with(argv[i], "https://")) {
url = argv[i]; url = argv[i];
} else { } else {
path = argv[i]; path = argv[i];