ytback/main.go

63 lines
1.3 KiB
Go

package main
import (
"context"
"flag"
"log"
"os"
"os/exec"
"strings"
"sync"
"github.com/mkmik/stringlist"
"within.website/confyg/flagconfyg"
)
var (
config = flag.String("cfg", "dl.config", "file to look for channel configurations in")
channels = stringlist.Flag("channel", "the channels to archive")
)
func runCommand(ctx context.Context, command, dir string, args ...string) error {
cmd := exec.CommandContext(ctx, command, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = dir
err := cmd.Start()
if err != nil {
return err
}
return cmd.Wait()
}
func main() {
flag.Parse()
flagconfyg.CmdParse(*config)
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for _, ch := range *channels {
sp := strings.SplitN(ch, " ", 2)
name := sp[0]
url := sp[1][1 : len(sp[1])-1]
log.Printf("name: %s, url: %s", name, url)
wg.Add(1)
go backupChannel(ctx, &wg, name, url)
}
wg.Wait()
log.Println("everything backed up")
}
func backupChannel(ctx context.Context, wg *sync.WaitGroup, name, url string) {
defer wg.Done()
os.MkdirAll(name, 0777)
err := runCommand(ctx, "youtube-dl", name, "--add-metadata", "--quiet", "--sleep-interval", "30", "--max-sleep-interval", "60", "--force-ipv6", url)
if err != nil {
log.Printf("can't backup %s: %v", name, err)
}
}