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:
Nonoo
2023-08-17 11:11:27 +02:00
parent ddaf5ad2fa
commit a230313e47
2 changed files with 43 additions and 0 deletions

View File

@ -231,6 +231,17 @@ func Version(ctx context.Context) (string, error) {
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
func New(ctx context.Context, rawURL string, options Options) (result Result, err error) {
if options.DebugLog == nil {

View File

@ -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) {
for _, c := range []struct {
url string