subsyt/internal/dl/dl.go
Viktor Varland dedb49f507
All checks were successful
build / build (push) Successful in 2m0s
feat: ignore live vids and duration below 60s
2025-04-04 14:22:53 +02:00

153 lines
2.8 KiB
Go

package dl
import (
"bufio"
"io"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"git.meatbag.se/varl/subsyt/internal/config"
)
type Download struct {
Url string
OutDir string
Name string
DryRun bool
}
func Youtube(d Download, p config.Provider) {
archive := filepath.Join(d.OutDir, "archive.txt")
outdir := d.OutDir
curl := strings.TrimPrefix(d.Url, "/feed/")
furl, err := url.JoinPath(p.Url, curl, "videos")
if err != nil {
panic(err)
}
fullUrl, err := url.Parse(furl)
if err != nil {
panic(err)
}
throttle := strconv.Itoa(p.Throttle)
args := []string{
"--no-progress",
"--sleep-interval", throttle,
"--sleep-subtitles", throttle,
"--sleep-requests", throttle,
"--format-sort", p.Quality,
"--prefer-free-formats",
"--write-subs",
"--no-write-automatic-subs",
"--sub-langs", "en",
"--paths", outdir,
"--output", p.Output_path_template,
"--download-archive", archive,
"--break-on-existing",
"--playlist-items", p.Range,
"--restrict-filenames",
"--embed-metadata",
"--write-thumbnail",
"--write-info-json",
"--match-filters", "!is_live & duration>?60",
"--convert-thumbnails", "jpg",
}
if d.DryRun == true {
args = append(args, "--simulate")
log.Println("/!\\ DRY RUN ENABLED /!\\")
} else {
args = append(args, "--no-simulate")
}
if p.Cookies == true {
args = append(args, "--cookies")
args = append(args, p.Cookies_file)
} else {
args = append(args, "--no-cookies")
}
args = append(args, fullUrl.String())
cmd := exec.Command(p.Cmd, args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
log.Printf("[%s] running yt-dlp for: %s\n", d.OutDir, d.Url)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
log.Printf("[%s] %s\n", d.OutDir, scanner.Text())
}
}()
go func() {
defer wg.Done()
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
log.Printf("[%s] %s\n", d.OutDir, scanner.Text())
}
}()
err = cmd.Start()
if err != nil {
log.Fatal(err)
}
wg.Wait()
err = cmd.Wait()
if err != nil {
log.Printf("Error: %s\n", err)
}
}
func Fetch(d Download) {
// Create output directory if it doesn't exist
if err := os.MkdirAll(d.OutDir, 0755); err != nil {
}
outputPath := filepath.Join(d.OutDir, d.Name)
out, err := os.Create(outputPath)
if err != nil {
log.Printf("failed to create '%s'\n", outputPath)
return
}
defer out.Close()
resp, err := http.Get(d.Url)
if err != nil {
log.Printf("failed to download '%s'\n", d.Url)
return
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
log.Printf("failed to write file")
}
}