ytva/ytva.go

155 lines
4.3 KiB
Go
Raw Normal View History

2024-12-31 16:33:46 -05:00
package main
import (
//"flag"
"context"
2024-12-31 20:28:07 -05:00
"fmt"
2024-12-31 16:33:46 -05:00
"os"
//"io"
//"reflect"
//"google.golang.org/api/option"
//"google.golang.org/api/youtube/v3"
"github.com/wader/goutubedl"
"github.com/BurntSushi/toml"
"github.com/charmbracelet/log"
"github.com/alexflint/go-arg"
)
type _flags struct {
Quiet bool `arg:"-q,--quiet" help:"Output only warnings and errors."`
Verbose bool `arg:"-v,--verbose" help:"Enables debug output."`
2024-12-31 20:28:07 -05:00
Version bool `arg:"--version" help:"Prints version of ytva and exits."`
2024-12-31 16:33:46 -05:00
ConfigFile string `arg:"-c,--config,env:YTVA_CONFIG" help:"Specifies the path to a TOML formatted configuration file." default:"config.toml"`
}
func (_flags) Description() string {
return "YouTube Video Archive - Downloads videos in bulk automatically"
}
var Flags _flags
2024-12-31 18:59:03 -05:00
type _config struct {
TempDir string
Group map[string]GroupConfig
2024-12-31 16:33:46 -05:00
}
2024-12-31 18:59:03 -05:00
type GroupConfig struct {
URL []string
OutputDir string
FileName string
CFormat string
VideoFormat string
AudioFormat string
NumVideos uint
SizeLimit float64
}
2024-12-31 16:33:46 -05:00
2024-12-31 18:59:03 -05:00
var MainConfig _config
2024-12-31 16:33:46 -05:00
2024-12-31 20:28:07 -05:00
var gitCommit string
var buildDate string
2024-12-31 16:33:46 -05:00
func main() {
_ = arg.MustParse(&Flags)
if Flags.Verbose {
log.SetReportCaller(true)
log.SetLevel(log.DebugLevel)
if Flags.Quiet {
log.Warn("Verbose and quiet specified. Defaulting to verbose.")
}
} else if Flags.Quiet {
log.SetLevel(log.WarnLevel)
}
2024-12-31 20:28:07 -05:00
if Flags.Version {
if gitCommit == "" {
gitCommit = "unknown"
}
if buildDate == "" {
buildDate = "unknown"
}
fmt.Printf("Commit %s built on %s\n", gitCommit, buildDate)
os.Exit(0)
}
2024-12-31 16:33:46 -05:00
confFile, err := os.ReadFile(Flags.ConfigFile)
if(err != nil) {
log.Fatal(err)
}
confContent := string(confFile)
_, err = toml.Decode(confContent, &MainConfig)
log.Debug("", "config", MainConfig)
goutubedl.Path = "yt-dlp"
2024-12-31 18:59:03 -05:00
for name, group := range MainConfig.Group {
log.Info("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
log.Infof("Started group \"%s\"", name)
for _, channel := range group.URL {
log.Info("================================================================")
log.Infof("Channel URL: \"%s\"", channel)
optType := goutubedl.TypeFromString["channel"]
channel, err := goutubedl.New(context.Background(), channel,
goutubedl.Options{
Type: optType,
PlaylistEnd: group.NumVideos,
},
)
if err != nil {
log.Fatalf("Error creating YouTube service: %v", err)
2024-12-31 16:33:46 -05:00
}
2024-12-31 18:59:03 -05:00
log.Infof("Channel Name: %s", channel.Info.Channel)
for _, v := range channel.Info.Entries {
log.Debugf("-------------------------------------")
log.Debugf("Title: %s", v.Title)
log.Debugf("URL: %s", v.WebpageURL)
log.Debugf("Playlist: %s", v.Playlist)
log.Debugf("PublishedAt: %s", v.UploadDate)
log.Debugf("IsLive: %t", v.IsLive)
//log.Debugf("Description: %s\n", item.Snippet.Description)
dirPath := fillPlaceholders(group.OutputDir, v)
fileName := fillPlaceholders(group.FileName, v)
path := dirPath+"/"+fileName
log.Debugf("Output: %s", path)
if _, err := os.Stat(dirPath); err != nil {
if os.IsNotExist(err) {
log.Debugf("Directory \"%s\" does not exist, creating.", dirPath)
if err := os.MkdirAll(dirPath, 0755); err != nil {
log.Error(err)
continue
}
} else {
log.Error(err)
continue
2024-12-31 16:33:46 -05:00
}
}
2024-12-31 18:59:03 -05:00
log.Infof("Downloading video: %s", v.Title)
2024-12-31 19:48:37 -05:00
if err := downloadVideo(v.WebpageURL, path, group); err != nil {
log.Error(err)
}
2024-12-31 18:59:03 -05:00
}
2024-12-31 16:33:46 -05:00
}
}
}