Add HTTPChunkSize option (--http-chunk-size)
Switch to _test test package to make sure to test external API. Also don't ignore download process wait error
This commit is contained in:
10
goutubedl.go
10
goutubedl.go
@ -208,6 +208,7 @@ type Options struct {
|
|||||||
DownloadSubtitles bool
|
DownloadSubtitles bool
|
||||||
DebugLog Printer
|
DebugLog Printer
|
||||||
StderrFn func(cmd *exec.Cmd) io.Writer // if not nil, function to get Writer for stderr
|
StderrFn func(cmd *exec.Cmd) io.Writer // if not nil, function to get Writer for stderr
|
||||||
|
HTTPChunkSize uint // --http-chunk-size
|
||||||
HTTPClient *http.Client // Client for download thumbnail and subtitles (nil use http.DefaultClient)
|
HTTPClient *http.Client // Client for download thumbnail and subtitles (nil use http.DefaultClient)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -454,6 +455,9 @@ func (result Result) Download(ctx context.Context, filter string) (*DownloadResu
|
|||||||
if !result.Info.Direct {
|
if !result.Info.Direct {
|
||||||
cmd.Args = append(cmd.Args, "-f", filter)
|
cmd.Args = append(cmd.Args, "-f", filter)
|
||||||
}
|
}
|
||||||
|
if result.Options.HTTPChunkSize != 0 {
|
||||||
|
cmd.Args = append(cmd.Args, "--http-chunk-size", fmt.Sprintf("%d", result.Options.HTTPChunkSize))
|
||||||
|
}
|
||||||
|
|
||||||
cmd.Dir = tempPath
|
cmd.Dir = tempPath
|
||||||
var w io.WriteCloser
|
var w io.WriteCloser
|
||||||
@ -472,14 +476,16 @@ func (result Result) Download(ctx context.Context, filter string) (*DownloadResu
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var waitErr error
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
cmd.Wait()
|
waitErr = cmd.Wait()
|
||||||
w.Close()
|
w.Close()
|
||||||
os.RemoveAll(tempPath)
|
os.RemoveAll(tempPath)
|
||||||
close(dr.waitCh)
|
close(dr.waitCh)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return dr, nil
|
return dr, waitErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dr *DownloadResult) Read(p []byte) (n int, err error) {
|
func (dr *DownloadResult) Read(p []byte) (n int, err error) {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package goutubedl
|
package goutubedl_test
|
||||||
|
|
||||||
// TODO: currently the tests only run on linux as they use osleaktest which only
|
// TODO: currently the tests only run on linux as they use osleaktest which only
|
||||||
// has linux support
|
// has linux support
|
||||||
@ -14,6 +14,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/fortytw2/leaktest"
|
"github.com/fortytw2/leaktest"
|
||||||
|
"github.com/wader/goutubedl"
|
||||||
"github.com/wader/osleaktest"
|
"github.com/wader/osleaktest"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -33,10 +34,10 @@ func leakChecks(t *testing.T) func() {
|
|||||||
|
|
||||||
func TestBinaryNotPath(t *testing.T) {
|
func TestBinaryNotPath(t *testing.T) {
|
||||||
defer leakChecks(t)()
|
defer leakChecks(t)()
|
||||||
defer func(orig string) { Path = orig }(Path)
|
defer func(orig string) { goutubedl.Path = orig }(goutubedl.Path)
|
||||||
Path = "/non-existing"
|
goutubedl.Path = "/non-existing"
|
||||||
|
|
||||||
_, versionErr := Version(context.Background())
|
_, versionErr := goutubedl.Version(context.Background())
|
||||||
if versionErr == nil || !strings.Contains(versionErr.Error(), "no such file or directory") {
|
if versionErr == nil || !strings.Contains(versionErr.Error(), "no such file or directory") {
|
||||||
t.Fatalf("err should be nil 'no such file or directory': %v", versionErr)
|
t.Fatalf("err should be nil 'no such file or directory': %v", versionErr)
|
||||||
}
|
}
|
||||||
@ -46,7 +47,7 @@ func TestVersion(t *testing.T) {
|
|||||||
defer leakChecks(t)()
|
defer leakChecks(t)()
|
||||||
|
|
||||||
versionRe := regexp.MustCompile(`^\d{4}\.\d{2}.\d{2}.*$`)
|
versionRe := regexp.MustCompile(`^\d{4}\.\d{2}.\d{2}.*$`)
|
||||||
version, versionErr := Version(context.Background())
|
version, versionErr := goutubedl.Version(context.Background())
|
||||||
|
|
||||||
if versionErr != nil {
|
if versionErr != nil {
|
||||||
t.Fatalf("err: %s", versionErr)
|
t.Fatalf("err: %s", versionErr)
|
||||||
@ -57,15 +58,21 @@ func TestVersion(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDownload(t *testing.T) {
|
func testDownload(t *testing.T, rawURL string, optionsFn func(options *goutubedl.Options)) {
|
||||||
defer leakChecks(t)()
|
defer leakChecks(t)()
|
||||||
|
|
||||||
stderrBuf := &bytes.Buffer{}
|
stderrBuf := &bytes.Buffer{}
|
||||||
r, err := New(context.Background(), testVideoRawURL, Options{
|
|
||||||
|
options := goutubedl.Options{
|
||||||
StderrFn: func(cmd *exec.Cmd) io.Writer {
|
StderrFn: func(cmd *exec.Cmd) io.Writer {
|
||||||
return stderrBuf
|
return stderrBuf
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
|
if optionsFn != nil {
|
||||||
|
optionsFn(&options)
|
||||||
|
}
|
||||||
|
|
||||||
|
r, err := goutubedl.New(context.Background(), rawURL, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -93,6 +100,18 @@ func TestDownload(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDownload(t *testing.T) {
|
||||||
|
defer leakChecks(t)()
|
||||||
|
testDownload(t, testVideoRawURL, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHTTPChunkSize(t *testing.T) {
|
||||||
|
defer leakChecks(t)()
|
||||||
|
testDownload(t, testVideoRawURL, func(options *goutubedl.Options) {
|
||||||
|
options.HTTPChunkSize = 1000000
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestParseInfo(t *testing.T) {
|
func TestParseInfo(t *testing.T) {
|
||||||
for _, c := range []struct {
|
for _, c := range []struct {
|
||||||
url string
|
url string
|
||||||
@ -106,7 +125,7 @@ func TestParseInfo(t *testing.T) {
|
|||||||
defer leakChecks(t)()
|
defer leakChecks(t)()
|
||||||
|
|
||||||
ctx, cancelFn := context.WithCancel(context.Background())
|
ctx, cancelFn := context.WithCancel(context.Background())
|
||||||
ydlResult, err := New(ctx, c.url, Options{
|
ydlResult, err := goutubedl.New(ctx, c.url, goutubedl.Options{
|
||||||
DownloadThumbnail: true,
|
DownloadThumbnail: true,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -156,8 +175,8 @@ func TestParseInfo(t *testing.T) {
|
|||||||
func TestPlaylist(t *testing.T) {
|
func TestPlaylist(t *testing.T) {
|
||||||
defer leakChecks(t)()
|
defer leakChecks(t)()
|
||||||
|
|
||||||
ydlResult, ydlResultErr := New(context.Background(), playlistRawURL, Options{
|
ydlResult, ydlResultErr := goutubedl.New(context.Background(), playlistRawURL, goutubedl.Options{
|
||||||
Type: TypePlaylist,
|
Type: goutubedl.TypePlaylist,
|
||||||
DownloadThumbnail: false,
|
DownloadThumbnail: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -184,7 +203,7 @@ func TestPlaylist(t *testing.T) {
|
|||||||
func TestTestUnsupportedURL(t *testing.T) {
|
func TestTestUnsupportedURL(t *testing.T) {
|
||||||
defer leaktest.Check(t)()
|
defer leaktest.Check(t)()
|
||||||
|
|
||||||
_, ydlResultErr := New(context.Background(), "https://www.google.com", Options{})
|
_, ydlResultErr := goutubedl.New(context.Background(), "https://www.google.com", goutubedl.Options{})
|
||||||
if ydlResultErr == nil {
|
if ydlResultErr == nil {
|
||||||
t.Errorf("expected unsupported url")
|
t.Errorf("expected unsupported url")
|
||||||
}
|
}
|
||||||
@ -199,8 +218,8 @@ func TestPlaylistWithPrivateVideo(t *testing.T) {
|
|||||||
defer leaktest.Check(t)()
|
defer leaktest.Check(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 := goutubedl.New(context.Background(), playlistRawURL, goutubedl.Options{
|
||||||
Type: TypePlaylist,
|
Type: goutubedl.TypePlaylist,
|
||||||
DownloadThumbnail: false,
|
DownloadThumbnail: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -218,10 +237,10 @@ func TestPlaylistWithPrivateVideo(t *testing.T) {
|
|||||||
func TestSubtitles(t *testing.T) {
|
func TestSubtitles(t *testing.T) {
|
||||||
defer leakChecks(t)()
|
defer leakChecks(t)()
|
||||||
|
|
||||||
ydlResult, ydlResultErr := New(
|
ydlResult, ydlResultErr := goutubedl.New(
|
||||||
context.Background(),
|
context.Background(),
|
||||||
subtitlesTestVideoRawURL,
|
subtitlesTestVideoRawURL,
|
||||||
Options{
|
goutubedl.Options{
|
||||||
DownloadSubtitles: true,
|
DownloadSubtitles: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -250,11 +269,11 @@ func TestSubtitles(t *testing.T) {
|
|||||||
func TestErrorNotAPlaylist(t *testing.T) {
|
func TestErrorNotAPlaylist(t *testing.T) {
|
||||||
defer leakChecks(t)()
|
defer leakChecks(t)()
|
||||||
|
|
||||||
_, ydlResultErr := New(context.Background(), testVideoRawURL, Options{
|
_, ydlResultErr := goutubedl.New(context.Background(), testVideoRawURL, goutubedl.Options{
|
||||||
Type: TypePlaylist,
|
Type: goutubedl.TypePlaylist,
|
||||||
DownloadThumbnail: false,
|
DownloadThumbnail: false,
|
||||||
})
|
})
|
||||||
if ydlResultErr != ErrNotAPlaylist {
|
if ydlResultErr != goutubedl.ErrNotAPlaylist {
|
||||||
t.Errorf("expected is playlist error, got %s", ydlResultErr)
|
t.Errorf("expected is playlist error, got %s", ydlResultErr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -262,11 +281,11 @@ func TestErrorNotAPlaylist(t *testing.T) {
|
|||||||
func TestErrorNotASingleEntry(t *testing.T) {
|
func TestErrorNotASingleEntry(t *testing.T) {
|
||||||
defer leakChecks(t)()
|
defer leakChecks(t)()
|
||||||
|
|
||||||
_, ydlResultErr := New(context.Background(), playlistRawURL, Options{
|
_, ydlResultErr := goutubedl.New(context.Background(), playlistRawURL, goutubedl.Options{
|
||||||
Type: TypeSingle,
|
Type: goutubedl.TypeSingle,
|
||||||
DownloadThumbnail: false,
|
DownloadThumbnail: false,
|
||||||
})
|
})
|
||||||
if ydlResultErr != ErrNotASingleEntry {
|
if ydlResultErr != goutubedl.ErrNotASingleEntry {
|
||||||
t.Errorf("expected is single entry error, got %s", ydlResultErr)
|
t.Errorf("expected is single entry error, got %s", ydlResultErr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user