Make response type an/single/playlist

Make it possible to assert what response to get
This commit is contained in:
Mattias Wadman
2019-11-01 00:01:13 +01:00
parent 941a4092e2
commit 5b6ef4f1f2
3 changed files with 54 additions and 21 deletions

View File

@ -12,12 +12,14 @@ import (
) )
var dumpFlag = flag.Bool("J", false, "Dump JSON") var dumpFlag = flag.Bool("J", false, "Dump JSON")
var typeFlag = flag.String("t", "any", "Type")
func main() { func main() {
log.SetFlags(0) log.SetFlags(0)
flag.Parse() flag.Parse()
result, err := goutubedl.New(context.Background(), flag.Arg(0), goutubedl.Options{}) optType, _ := goutubedl.TypeFromString[*typeFlag]
result, err := goutubedl.New(context.Background(), flag.Arg(0), goutubedl.Options{Type: optType})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -173,9 +173,27 @@ func (f Format) String() string {
) )
} }
// Type of response you want
type Type int
const (
// TypeAny single or playlist (default)
TypeAny Type = iota
// TypeSingle single track, file etc
TypeSingle
// TypePlaylist playlist with multiple tracks, files etc
TypePlaylist
)
var TypeFromString = map[string]Type{
"any": TypeAny,
"single": TypeSingle,
"playlist": TypePlaylist,
}
// Options for New() // Options for New()
type Options struct { type Options struct {
YesPlaylist bool // --yes-playlist Type Type
PlaylistStart uint // --playlist-start PlaylistStart uint // --playlist-start
PlaylistEnd uint // --playlist-end PlaylistEnd uint // --playlist-end
DownloadThumbnail bool DownloadThumbnail bool
@ -234,7 +252,7 @@ func infoFromURL(ctx context.Context, rawURL string, options Options) (info Info
"--batch-file", "-", "--batch-file", "-",
"-J", "-J",
) )
if options.YesPlaylist { if options.Type == TypePlaylist {
cmd.Args = append(cmd.Args, "--yes-playlist") cmd.Args = append(cmd.Args, "--yes-playlist")
if options.PlaylistStart > 0 { if options.PlaylistStart > 0 {
@ -300,8 +318,15 @@ func infoFromURL(ctx context.Context, rawURL string, options Options) (info Info
return Info{}, nil, infoErr return Info{}, nil, infoErr
} }
if options.YesPlaylist && (info.Type != "playlist" || info.Type == "multi_video") { isPlaylist := info.Type == "playlist" || info.Type == "multi_video"
switch {
case options.Type == TypePlaylist && !isPlaylist:
return Info{}, nil, fmt.Errorf("not a playlist") return Info{}, nil, fmt.Errorf("not a playlist")
case options.Type == TypeSingle && isPlaylist:
return Info{}, nil, fmt.Errorf("not a single")
default:
// any type
}
} }
// TODO: use headers from youtube-dl info for thumbnail and subtitle download? // TODO: use headers from youtube-dl info for thumbnail and subtitle download?
@ -334,7 +359,7 @@ func infoFromURL(ctx context.Context, rawURL string, options Options) (info Info
} }
// as we ignore errors some entries might show up as null // as we ignore errors some entries might show up as null
if options.YesPlaylist { if options.Type == TypePlaylist {
var filteredEntrise []Info var filteredEntrise []Info
for _, e := range info.Entries { for _, e := range info.Entries {
if e.ID == "" { if e.ID == "" {

View File

@ -157,7 +157,7 @@ func TestPlaylist(t *testing.T) {
defer leakChecks(t)() defer leakChecks(t)()
ydlResult, ydlResultErr := New(context.Background(), playlistRawURL, Options{ ydlResult, ydlResultErr := New(context.Background(), playlistRawURL, Options{
YesPlaylist: true, Type: TypePlaylist,
DownloadThumbnail: false, DownloadThumbnail: false,
}) })
@ -186,7 +186,7 @@ func TestPlaylistWithPrivateVideo(t *testing.T) {
playlistRawURL := "https://www.youtube.com/playlist?list=PLX0g748fkegS54oiDN4AXKl7BR7mLIydP" playlistRawURL := "https://www.youtube.com/playlist?list=PLX0g748fkegS54oiDN4AXKl7BR7mLIydP"
ydlResult, ydlResultErr := New(context.Background(), playlistRawURL, Options{ ydlResult, ydlResultErr := New(context.Background(), playlistRawURL, Options{
YesPlaylist: true, Type: TypePlaylist,
DownloadThumbnail: false, DownloadThumbnail: false,
}) })
@ -201,20 +201,6 @@ func TestPlaylistWithPrivateVideo(t *testing.T) {
} }
} }
func TestPlaylistBadURL(t *testing.T) {
defer leakChecks(t)()
// using a non-playlist url
_, ydlResultErr := New(context.Background(), testVideoRawURL, Options{
YesPlaylist: true,
DownloadThumbnail: false,
})
if ydlResultErr == nil {
t.Error("expected error")
}
}
func TestSubtitles(t *testing.T) { func TestSubtitles(t *testing.T) {
defer leakChecks(t)() defer leakChecks(t)()
@ -246,3 +232,23 @@ func TestSubtitles(t *testing.T) {
} }
} }
} }
func TestErrorNotAPlaylist(t *testing.T) {
_, ydlResultErr := New(context.Background(), testVideoRawURL, Options{
Type: TypePlaylist,
DownloadThumbnail: false,
})
if ydlResultErr.Error() != "not a playlist" {
t.Errorf("expected is playlist error")
}
}
func TestErrorNotASingle(t *testing.T) {
_, ydlResultErr := New(context.Background(), playlistRawURL, Options{
Type: TypeSingle,
DownloadThumbnail: false,
})
if ydlResultErr.Error() != "not a single" {
t.Errorf("expected is playlist error")
}
}