feat: add more config options

This commit is contained in:
Viktor Varland 2025-03-28 12:44:38 +01:00
parent 43709549dc
commit 6f77fddea1
Signed by: varl
GPG key ID: 7459F0B410115EE8
5 changed files with 91 additions and 58 deletions

4
.gitignore vendored
View file

@ -1,3 +1,5 @@
.direnv .direnv/
vids/
config.toml config.toml
subs-opml.xml subs-opml.xml
subsyt

36
internal/config/config.go Normal file
View file

@ -0,0 +1,36 @@
package config
import (
"os"
toml "github.com/pelletier/go-toml/v2"
)
type Provider struct {
Url string
Throttle int
Video_items int
After_date string
}
type Config struct {
Out_dir string
Provider map[string]Provider
Dry_run bool
}
func Load(filepath string) (Config, error) {
data, err := os.ReadFile(filepath)
if err != nil {
panic(err)
}
cfg := Config{}
err = toml.Unmarshal(data, &cfg)
if err != nil {
panic(err)
}
return cfg, err
}

View file

@ -6,37 +6,60 @@ import (
"net/url" "net/url"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strconv"
"strings"
"sync" "sync"
"git.meatbag.se/varl/subsyt/internal/config"
) )
type Download struct { type Download struct {
Url *url.URL Url string
OutDir string OutDir string
AfterDate string
Name string Name string
DryRun bool
} }
func Get(d Download) { func Get(d Download, p config.Provider) {
output := filepath.Join("%(upload_date>%Y)s/%(upload_date)s-%(title)s-%(id)s.%(ext)s") output := filepath.Join("%(upload_date>%Y)s/%(upload_date)s-%(title)s-%(id)s.%(ext)s")
archive := filepath.Join(d.OutDir, d.Name, "archive.txt") archive := filepath.Join(d.OutDir, d.Name, "archive.txt")
outdir := filepath.Join(d.OutDir, d.Name) 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)
}
var simulate string
if d.DryRun == true {
simulate = "--simulate"
log.Printf("/!\\ DRY RUN ENABLED /!\\")
} else {
simulate = "--no-simulate"
}
cmd := exec.Command("yt-dlp", cmd := exec.Command("yt-dlp",
d.Url.String(), fullUrl.String(),
"--no-simulate", simulate,
"--no-progress", "--no-progress",
"--sleep-interval", "1", "--sleep-interval", strconv.Itoa(p.Throttle),
"--sleep-subtitles", "3", "--sleep-subtitles", strconv.Itoa(p.Throttle),
"--sleep-requests", "1", "--sleep-requests", strconv.Itoa(p.Throttle),
"--prefer-free-formats", "--prefer-free-formats",
"--write-subs", "--write-subs",
"--no-write-automatic-subs", "--no-write-automatic-subs",
"--sub-langs", "en", "--sub-langs", "en",
"--dateafter", d.AfterDate,
"--paths", outdir, "--paths", outdir,
"--output", output, "--output", output,
"--download-archive", archive, "--download-archive", archive,
"--break-on-existing", "--break-on-existing",
"--playlist-items", strconv.Itoa(p.Video_items),
"--restrict-filenames", "--restrict-filenames",
) )
@ -44,6 +67,7 @@ func Get(d Download) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
stderr, err := cmd.StderrPipe() stderr, err := cmd.StderrPipe()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View file

@ -22,7 +22,7 @@ type OPML struct {
Body `xml:"opml>body"` Body `xml:"opml>body"`
} }
func Unmarshal(path string) OPML { func Load(path string) (OPML, error) {
data, err := os.ReadFile(path) data, err := os.ReadFile(path)
if err != nil { if err != nil {
@ -31,7 +31,10 @@ func Unmarshal(path string) OPML {
opml := OPML{} opml := OPML{}
xml.Unmarshal(data, &opml) err = xml.Unmarshal(data, &opml)
if err != nil {
return opml panic(err)
}
return opml, err
} }

50
main.go
View file

@ -1,68 +1,36 @@
package main package main
import ( import (
"fmt" "log"
"net/url"
"os"
"strings"
"git.meatbag.se/varl/subsyt/internal/config"
"git.meatbag.se/varl/subsyt/internal/dl" "git.meatbag.se/varl/subsyt/internal/dl"
"git.meatbag.se/varl/subsyt/internal/opml" "git.meatbag.se/varl/subsyt/internal/opml"
toml "github.com/pelletier/go-toml/v2"
) )
type Provider struct {
Url string
}
type Config struct {
Out_dir string
Provider map[string]Provider
}
func main() { func main() {
data, err := os.ReadFile("./config.toml") cfg, err := config.Load("./config.toml")
if err != nil { if err != nil {
panic(err) panic(err)
} }
cfg := Config{} opml, err := opml.Load("./subs-opml.xml")
err = toml.Unmarshal(data, &cfg)
if err != nil { if err != nil {
panic(err) panic(err)
} }
pUrl := cfg.Provider["youtube"].Url provider := cfg.Provider["youtube"]
fmt.Printf("provider url: %s", pUrl)
opml := opml.Unmarshal("./subs-opml.xml")
fmt.Printf("XMLName: %#v\n", opml.XMLName)
for _, outlines := range opml.Body.Outline { for _, outlines := range opml.Body.Outline {
fmt.Printf("%s\n", outlines.Title) log.Printf("Archiving videos from OPML: %s", outlines.Title)
for _, outline := range outlines.Outlines { for _, outline := range outlines.Outlines {
curl := strings.TrimPrefix(outline.XmlUrl, "/feed/")
furl, err := url.JoinPath(pUrl, curl)
if err != nil {
panic(err)
}
fmt.Printf("%s - %s\n", outline.Text, furl)
fullUrl, err := url.Parse(furl)
if err != nil {
panic(err)
}
dl.Get(dl.Download{ dl.Get(dl.Download{
Url: outline.XmlUrl,
Name: outline.Title, Name: outline.Title,
Url: fullUrl,
OutDir: cfg.Out_dir, OutDir: cfg.Out_dir,
AfterDate: "20250326", DryRun: cfg.Dry_run,
}) }, provider)
} }
} }
} }