205 lines
4 KiB
Go
205 lines
4 KiB
Go
package dl
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
"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) {
|
|
|
|
if p.Bgutil_server != "" && p.Po_token != "" {
|
|
log.Fatal("please only provide bgutil_server OR po_token, not both")
|
|
}
|
|
|
|
archive := filepath.Join(d.OutDir, "archive.txt")
|
|
outdir := d.OutDir
|
|
|
|
opmlUrl, err := url.Parse(d.Url)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
q := opmlUrl.Query()
|
|
cid := q.Get("channel_id")
|
|
|
|
if cid == "" {
|
|
log.Fatal("no channel !")
|
|
}
|
|
|
|
if p.Url == "" {
|
|
p.Url = "https://www.youtube.com"
|
|
}
|
|
|
|
fullUrl, err := url.Parse(p.Url)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
channelUrl := fullUrl.JoinPath("channel", cid, "videos")
|
|
|
|
throttle := strconv.Itoa(p.Throttle)
|
|
|
|
args := []string{
|
|
"--no-progress",
|
|
"--sleep-interval", throttle,
|
|
"--sleep-subtitles", throttle,
|
|
"--sleep-requests", throttle,
|
|
"--max-sleep-interval", "90",
|
|
"--prefer-free-formats",
|
|
"--write-subs",
|
|
"--no-write-automatic-subs",
|
|
"--sub-langs", "en",
|
|
"--paths", outdir,
|
|
"--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.Verbose == true {
|
|
args = append(args, "--verbose")
|
|
}
|
|
|
|
if p.Cookies_file != "" {
|
|
args = append(args, "--cookies")
|
|
args = append(args, p.Cookies_file)
|
|
} else {
|
|
args = append(args, "--no-cookies")
|
|
}
|
|
|
|
if p.After_date != "" {
|
|
args = append(args, "--dateafter")
|
|
args = append(args, p.After_date)
|
|
}
|
|
|
|
if p.Po_token != "" {
|
|
args = append(args, "--extractor-args")
|
|
args = append(args, fmt.Sprintf("youtube:po_token=web.gvs+%s", p.Po_token))
|
|
}
|
|
|
|
if p.Bgutil_server != "" {
|
|
args = append(args, "--extractor-args")
|
|
args = append(args, fmt.Sprintf("youtube:getpot_bgutil_baseurl=%s", p.Bgutil_server))
|
|
}
|
|
|
|
if p.Player_client != "" {
|
|
args = append(args, "--extractor-args")
|
|
args = append(args, fmt.Sprintf("youtube:player_client=%s", p.Player_client))
|
|
}
|
|
|
|
args = append(args, "--format-sort")
|
|
if p.Quality != "" {
|
|
args = append(args, p.Quality)
|
|
} else {
|
|
args = append(args, "res:1080")
|
|
}
|
|
|
|
args = append(args, "--output")
|
|
if p.Output_path_template != "" {
|
|
args = append(args, p.Output_path_template)
|
|
} else {
|
|
args = append(args, "s%(upload_date>%Y)s/%(channel)s.s%(upload_date>%Y)Se%(upload_date>%m%d)S.%(title)s.%(id)s-1080p.%(ext)s")
|
|
}
|
|
|
|
args = append(args, channelUrl.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 with args: %v\n", d.OutDir, args)
|
|
|
|
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")
|
|
}
|
|
}
|