Skip to content

Commit

Permalink
feat: implemented format select (with config file mentions)
Browse files Browse the repository at this point in the history
  • Loading branch information
pspiagicw committed Jun 5, 2024
1 parent f652dc7 commit 174251c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/argparse/argparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Opts struct {
Days int
NoSync bool
MarkWatched bool
Format bool
}

func Parse(version string) *Opts {
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Config struct {
VideoFolder string `toml:"videoFolder"`
URLS []string `toml:"urls"`
Feeds map[string]Feed `toml:"feed"`
Quality string `toml:"quality"`
}
type Feed struct {
URL string `toml:"url"`
Expand Down Expand Up @@ -59,6 +60,10 @@ func checkConfig(conf *Config) {
help.HelpConfig()
goreland.LogFatal("No URLs set in config file")
}
if conf.Quality == "" {
help.HelpConfig()
goreland.LogFatal("Quality not set in config file")
}
}

func readConfig(path string) *Config {
Expand Down
2 changes: 1 addition & 1 deletion pkg/help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func HelpUpdate() {
func HelpDownload() {
pelp.Print("Download a video")
pelp.HeaderWithDescription("Usage", []string{"sinister download"})
pelp.Flags("flag", []string{"no-spinner"}, []string{"Disable spinner"})
pelp.Flags("flag", []string{"no-spinner", "select-format"}, []string{"Disable spinner", "Select format manually"})
pelp.HeaderWithDescription("Description",
[]string{
"Prompt for a creator and a video to download.",
Expand Down
30 changes: 26 additions & 4 deletions pkg/tui/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tui

import (
"flag"
"fmt"
"io"
"os"
"path/filepath"
Expand All @@ -21,6 +22,7 @@ import (
func parseDownloadArgs(opts *argparse.Opts) {
flag := flag.NewFlagSet("sinister download", flag.ExitOnError)
flag.BoolVar(&opts.NoSpinner, "no-spinner", false, "Disable spinner")
flag.BoolVar(&opts.Format, "select-format", false, "Select format manually")
flag.Usage = help.HelpDownload
flag.Parse(opts.Args[1:])
}
Expand Down Expand Up @@ -75,7 +77,7 @@ func selectEntry() *feed.Entry {

func startSpinner(opts *argparse.Opts) func() {

if opts.NoSpinner {
if opts.NoSpinner || opts.Format {
return func() {}
}

Expand Down Expand Up @@ -113,11 +115,31 @@ func getVideo(entry *feed.Entry) (*youtube.Client, *youtube.Video) {

return &client, video
}
func selectFormat(video *youtube.Video) *youtube.Format {
formats := video.Formats.Quality("720p").WithAudioChannels()
func formatListString(formats youtube.FormatList) []string {
results := []string{}
for _, format := range formats {
line := fmt.Sprintf("Quality: %s, Bitrate: %d", format.Quality, format.Bitrate)
results = append(results, line)
}

return results
}
func selectFormat(video *youtube.Video, opts *argparse.Opts) *youtube.Format {
formats := video.Formats

formats.Sort()

if opts.Format {
index := promptSelection("Select format", formatListString(formats))
return &formats[index]
}

conf := config.ParseConfig(opts)

formatQuery := conf.Quality

formats = formats.Quality(formatQuery).WithAudioChannels()

if len(formats) == 0 {
goreland.LogFatal("No suitable formats found")
}
Expand All @@ -138,7 +160,7 @@ func downloadVideo(entry *feed.Entry, opts *argparse.Opts) {

client, video := getVideo(entry)

format := selectFormat(video)
format := selectFormat(video, opts)

stream := getStream(client, video, format)

Expand Down

0 comments on commit 174251c

Please sign in to comment.