102 lines
2.2 KiB
Go
102 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"git.meatbag.se/varl/subsyt/internal/config"
|
|
"git.meatbag.se/varl/subsyt/internal/dl"
|
|
"git.meatbag.se/varl/subsyt/internal/format"
|
|
"git.meatbag.se/varl/subsyt/internal/metadata"
|
|
"git.meatbag.se/varl/subsyt/internal/scheduler"
|
|
)
|
|
|
|
func run(cfg config.Config) {
|
|
provider := cfg.Provider["youtube"]
|
|
|
|
if err := dl.UpgradeYtDlp(provider.Cmd); err != nil {
|
|
log.Fatalf("failed to ensure yt-dlp is up to date: %v", err)
|
|
}
|
|
|
|
opml, err := format.OpmlLoad(provider.Opml_file)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
for _, outlines := range opml.Body.Outline {
|
|
log.Printf("Archiving videos from OPML: %s\n", outlines.Title)
|
|
|
|
for _, outline := range outlines.Outlines {
|
|
rssData, err := dl.RssDownloader(outline.XmlUrl)
|
|
if err != nil {
|
|
log.Printf("Failed to download RSS for %s: %v", outline.Title, err)
|
|
continue
|
|
}
|
|
|
|
feed, err := format.RssLoad(rssData)
|
|
if err != nil {
|
|
log.Printf("Failed to parse RSS for %s: %v", feed.Title, err)
|
|
continue
|
|
}
|
|
|
|
dl.Youtube(dl.Download{
|
|
Url: feed.Author.Uri,
|
|
OutDir: filepath.Join(cfg.Out_dir, outline.Title),
|
|
DryRun: cfg.Dry_run,
|
|
Metadata: true,
|
|
}, provider)
|
|
|
|
log.Printf("Downloaded RSS feed for %s with %d entries", feed.Title, len(feed.Entries))
|
|
|
|
for _, entry := range feed.Entries {
|
|
url := fmt.Sprintf("%s/watch?v=%s", provider.Url, entry.VideoId)
|
|
|
|
log.Printf("Entry: %#v", entry)
|
|
dl.Youtube(dl.Download{
|
|
Url: url,
|
|
OutDir: filepath.Join(cfg.Out_dir, feed.Title),
|
|
DryRun: cfg.Dry_run,
|
|
Metadata: false,
|
|
}, provider)
|
|
}
|
|
|
|
metadata.Generate(cfg.Out_dir, feed.Title, cfg.Dry_run)
|
|
}
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
configPtr := flag.String("config", "", "path to config file")
|
|
flag.Parse()
|
|
|
|
configEnv := os.Getenv("CONFIG")
|
|
|
|
var configPath string
|
|
if *configPtr != "" {
|
|
configPath = *configPtr
|
|
} else if configEnv != "" {
|
|
configPath = configEnv
|
|
} else {
|
|
configPath = "./config.json"
|
|
}
|
|
|
|
log.Println("resolved config file", configPath)
|
|
|
|
cfg, err := config.Load(configPath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if cfg.Daemon {
|
|
log.Println("running with scheduler")
|
|
s := scheduler.Scheduler{}
|
|
s.Start(run, cfg)
|
|
} else {
|
|
log.Println("running standalone")
|
|
run(cfg)
|
|
}
|
|
}
|