119 lines
2.1 KiB
Go
119 lines
2.1 KiB
Go
package dl
|
|
|
|
import (
|
|
"bufio"
|
|
"log"
|
|
"net/url"
|
|
"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 Get(d Download, p config.Provider) {
|
|
output := filepath.Join("%(channel)s-%(title)s-%(id)s.%(ext)s")
|
|
archive := filepath.Join(d.OutDir, d.Name, "archive.txt")
|
|
outdir := filepath.Join(d.OutDir, d.Name)
|
|
|
|
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,
|
|
"--prefer-free-formats",
|
|
"--write-subs",
|
|
"--no-write-automatic-subs",
|
|
"--sub-langs", "en",
|
|
"--paths", outdir,
|
|
"--output", output,
|
|
"--download-archive", archive,
|
|
"--break-on-existing",
|
|
"--playlist-items", p.Range,
|
|
"--restrict-filenames",
|
|
"--embed-metadata",
|
|
}
|
|
|
|
if d.DryRun == true {
|
|
args = append(args, "--simulate")
|
|
log.Printf("/!\\ 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", d.Name, 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.Name, scanner.Text())
|
|
}
|
|
}()
|
|
|
|
go func() {
|
|
defer wg.Done()
|
|
scanner := bufio.NewScanner(stderr)
|
|
for scanner.Scan() {
|
|
log.Printf("[%s] %s\n", d.Name, 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)
|
|
}
|
|
}
|