Add main Download() function and its test
// It downloads given URL using the given options and filter (usually a format id or quality designator). If filter is empty, then youtube-dl will use its default format selector.
This commit is contained in:
11
goutubedl.go
11
goutubedl.go
@ -231,6 +231,17 @@ func Version(ctx context.Context) (string, error) {
|
|||||||
return strings.TrimSpace(string(versionBytes)), nil
|
return strings.TrimSpace(string(versionBytes)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Downloads given URL using the given options and filter (usually a format id or quality designator).
|
||||||
|
// If filter is empty, then youtube-dl will use its default format selector.
|
||||||
|
func Download(ctx context.Context, rawURL string, options Options, filter string) (*DownloadResult, error) {
|
||||||
|
options.NoInfoDownload = true
|
||||||
|
d, err := New(ctx, rawURL, options)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return d.Download(ctx, filter)
|
||||||
|
}
|
||||||
|
|
||||||
// New downloads metadata for URL
|
// New downloads metadata for URL
|
||||||
func New(ctx context.Context, rawURL string, options Options) (result Result, err error) {
|
func New(ctx context.Context, rawURL string, options Options) (result Result, err error) {
|
||||||
if options.DebugLog == nil {
|
if options.DebugLog == nil {
|
||||||
|
@ -99,6 +99,38 @@ func TestDownload(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDownloadWithoutInfo(t *testing.T) {
|
||||||
|
defer leakChecks(t)()
|
||||||
|
|
||||||
|
stderrBuf := &bytes.Buffer{}
|
||||||
|
dr, err := goutubedl.Download(context.Background(), testVideoRawURL, goutubedl.Options{
|
||||||
|
StderrFn: func(cmd *exec.Cmd) io.Writer {
|
||||||
|
return stderrBuf
|
||||||
|
},
|
||||||
|
}, "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
downloadBuf := &bytes.Buffer{}
|
||||||
|
n, err := io.Copy(downloadBuf, dr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
dr.Close()
|
||||||
|
|
||||||
|
if n != int64(downloadBuf.Len()) {
|
||||||
|
t.Errorf("copy n not equal to download buffer: %d!=%d", n, downloadBuf.Len())
|
||||||
|
}
|
||||||
|
|
||||||
|
if n < 10000 {
|
||||||
|
t.Errorf("should have copied at least 10000 bytes: %d", n)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(stderrBuf.String(), "Destination") {
|
||||||
|
t.Errorf("did not find expected log message on stderr: %q", stderrBuf.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestParseInfo(t *testing.T) {
|
func TestParseInfo(t *testing.T) {
|
||||||
for _, c := range []struct {
|
for _, c := range []struct {
|
||||||
url string
|
url string
|
||||||
|
Reference in New Issue
Block a user