Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTPChunkSize option (--http-chunk-size) #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions goutubedl.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ type Options struct {
DownloadSubtitles bool
DebugLog Printer
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)
}

Expand Down Expand Up @@ -454,6 +455,9 @@ func (result Result) Download(ctx context.Context, filter string) (*DownloadResu
if !result.Info.Direct {
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
var w io.WriteCloser
Expand All @@ -472,14 +476,16 @@ func (result Result) Download(ctx context.Context, filter string) (*DownloadResu
return nil, err
}

var waitErr error

go func() {
cmd.Wait()
waitErr = cmd.Wait()
w.Close()
os.RemoveAll(tempPath)
close(dr.waitCh)
}()

return dr, nil
return dr, waitErr
}

func (dr *DownloadResult) Read(p []byte) (n int, err error) {
Expand Down
63 changes: 41 additions & 22 deletions goutubedl_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goutubedl
package goutubedl_test

// TODO: currently the tests only run on linux as they use osleaktest which only
// has linux support
Expand All @@ -14,6 +14,7 @@ import (
"testing"

"github.com/fortytw2/leaktest"
"github.com/wader/goutubedl"
"github.com/wader/osleaktest"
)

Expand All @@ -33,10 +34,10 @@ func leakChecks(t *testing.T) func() {

func TestBinaryNotPath(t *testing.T) {
defer leakChecks(t)()
defer func(orig string) { Path = orig }(Path)
Path = "/non-existing"
defer func(orig string) { goutubedl.Path = orig }(goutubedl.Path)
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") {
t.Fatalf("err should be nil 'no such file or directory': %v", versionErr)
}
Expand All @@ -46,7 +47,7 @@ func TestVersion(t *testing.T) {
defer leakChecks(t)()

versionRe := regexp.MustCompile(`^\d{4}\.\d{2}.\d{2}.*$`)
version, versionErr := Version(context.Background())
version, versionErr := goutubedl.Version(context.Background())

if versionErr != nil {
t.Fatalf("err: %s", versionErr)
Expand All @@ -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)()

stderrBuf := &bytes.Buffer{}
r, err := New(context.Background(), testVideoRawURL, Options{

options := goutubedl.Options{
StderrFn: func(cmd *exec.Cmd) io.Writer {
return stderrBuf
},
})
}
if optionsFn != nil {
optionsFn(&options)
}

r, err := goutubedl.New(context.Background(), rawURL, options)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -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) {
for _, c := range []struct {
url string
Expand All @@ -106,7 +125,7 @@ func TestParseInfo(t *testing.T) {
defer leakChecks(t)()

ctx, cancelFn := context.WithCancel(context.Background())
ydlResult, err := New(ctx, c.url, Options{
ydlResult, err := goutubedl.New(ctx, c.url, goutubedl.Options{
DownloadThumbnail: true,
})
if err != nil {
Expand Down Expand Up @@ -156,8 +175,8 @@ func TestParseInfo(t *testing.T) {
func TestPlaylist(t *testing.T) {
defer leakChecks(t)()

ydlResult, ydlResultErr := New(context.Background(), playlistRawURL, Options{
Type: TypePlaylist,
ydlResult, ydlResultErr := goutubedl.New(context.Background(), playlistRawURL, goutubedl.Options{
Type: goutubedl.TypePlaylist,
DownloadThumbnail: false,
})

Expand All @@ -184,7 +203,7 @@ func TestPlaylist(t *testing.T) {
func TestTestUnsupportedURL(t *testing.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 {
t.Errorf("expected unsupported url")
}
Expand All @@ -199,8 +218,8 @@ func TestPlaylistWithPrivateVideo(t *testing.T) {
defer leaktest.Check(t)()

playlistRawURL := "https://www.youtube.com/playlist?list=PLX0g748fkegS54oiDN4AXKl7BR7mLIydP"
ydlResult, ydlResultErr := New(context.Background(), playlistRawURL, Options{
Type: TypePlaylist,
ydlResult, ydlResultErr := goutubedl.New(context.Background(), playlistRawURL, goutubedl.Options{
Type: goutubedl.TypePlaylist,
DownloadThumbnail: false,
})

Expand All @@ -218,10 +237,10 @@ func TestPlaylistWithPrivateVideo(t *testing.T) {
func TestSubtitles(t *testing.T) {
defer leakChecks(t)()

ydlResult, ydlResultErr := New(
ydlResult, ydlResultErr := goutubedl.New(
context.Background(),
subtitlesTestVideoRawURL,
Options{
goutubedl.Options{
DownloadSubtitles: true,
})

Expand Down Expand Up @@ -250,23 +269,23 @@ func TestSubtitles(t *testing.T) {
func TestErrorNotAPlaylist(t *testing.T) {
defer leakChecks(t)()

_, ydlResultErr := New(context.Background(), testVideoRawURL, Options{
Type: TypePlaylist,
_, ydlResultErr := goutubedl.New(context.Background(), testVideoRawURL, goutubedl.Options{
Type: goutubedl.TypePlaylist,
DownloadThumbnail: false,
})
if ydlResultErr != ErrNotAPlaylist {
if ydlResultErr != goutubedl.ErrNotAPlaylist {
t.Errorf("expected is playlist error, got %s", ydlResultErr)
}
}

func TestErrorNotASingleEntry(t *testing.T) {
defer leakChecks(t)()

_, ydlResultErr := New(context.Background(), playlistRawURL, Options{
Type: TypeSingle,
_, ydlResultErr := goutubedl.New(context.Background(), playlistRawURL, goutubedl.Options{
Type: goutubedl.TypeSingle,
DownloadThumbnail: false,
})
if ydlResultErr != ErrNotASingleEntry {
if ydlResultErr != goutubedl.ErrNotASingleEntry {
t.Errorf("expected is single entry error, got %s", ydlResultErr)
}
}