Merge pull request #156 from nonoo/master
Add the NoInfoDownload option
This commit is contained in:
71
goutubedl.go
71
goutubedl.go
@ -213,6 +213,10 @@ type Options struct {
|
|||||||
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)
|
||||||
MergeOutputFormat string // --merge-output-format
|
MergeOutputFormat string // --merge-output-format
|
||||||
SortingFormat string // --format-sort
|
SortingFormat string // --format-sort
|
||||||
|
|
||||||
|
// Set to true if you don't want to use the result.Info structure after the goutubedl.New() call,
|
||||||
|
// so the given URL will be downloaded in a single pass in the DownloadResult.Download() call.
|
||||||
|
noInfoDownload bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Version of youtube-dl.
|
// Version of youtube-dl.
|
||||||
@ -227,12 +231,30 @@ 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 {
|
||||||
options.DebugLog = nopPrinter{}
|
options.DebugLog = nopPrinter{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if options.noInfoDownload {
|
||||||
|
return Result{
|
||||||
|
RawURL: rawURL,
|
||||||
|
Options: options,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
info, rawJSON, err := infoFromURL(ctx, rawURL, options)
|
info, rawJSON, err := infoFromURL(ctx, rawURL, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result{}, err
|
return Result{}, err
|
||||||
@ -243,6 +265,7 @@ func New(ctx context.Context, rawURL string, options Options) (result Result, er
|
|||||||
|
|
||||||
return Result{
|
return Result{
|
||||||
Info: info,
|
Info: info,
|
||||||
|
RawURL: rawURL,
|
||||||
RawJSON: rawJSONCopy,
|
RawJSON: rawJSONCopy,
|
||||||
Options: options,
|
Options: options,
|
||||||
}, nil
|
}, nil
|
||||||
@ -421,6 +444,7 @@ func infoFromURL(ctx context.Context, rawURL string, options Options) (info Info
|
|||||||
// Result metadata for a URL
|
// Result metadata for a URL
|
||||||
type Result struct {
|
type Result struct {
|
||||||
Info Info
|
Info Info
|
||||||
|
RawURL string
|
||||||
RawJSON []byte // saved raw JSON. Used later when downloading
|
RawJSON []byte // saved raw JSON. Used later when downloading
|
||||||
Options Options // options passed to New
|
Options Options // options passed to New
|
||||||
}
|
}
|
||||||
@ -452,18 +476,24 @@ type DownloadOptions struct {
|
|||||||
func (result Result) DownloadWithOptions(ctx context.Context, options DownloadOptions) (*DownloadResult, error) {
|
func (result Result) DownloadWithOptions(ctx context.Context, options DownloadOptions) (*DownloadResult, error) {
|
||||||
debugLog := result.Options.DebugLog
|
debugLog := result.Options.DebugLog
|
||||||
|
|
||||||
if (result.Info.Type == "playlist" || result.Info.Type == "multi_video") && options.PlaylistIndex == 0 {
|
if !result.Options.noInfoDownload {
|
||||||
return nil, fmt.Errorf("can't download a playlist when the playlist index options is not set")
|
if (result.Info.Type == "playlist" || result.Info.Type == "multi_video") && options.PlaylistIndex == 0 {
|
||||||
|
return nil, fmt.Errorf("can't download a playlist when the playlist index options is not set")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tempPath, tempErr := ioutil.TempDir("", "ydls")
|
tempPath, tempErr := ioutil.TempDir("", "ydls")
|
||||||
if tempErr != nil {
|
if tempErr != nil {
|
||||||
return nil, tempErr
|
return nil, tempErr
|
||||||
}
|
}
|
||||||
jsonTempPath := path.Join(tempPath, "info.json")
|
|
||||||
if err := ioutil.WriteFile(jsonTempPath, result.RawJSON, 0600); err != nil {
|
var jsonTempPath string
|
||||||
os.RemoveAll(tempPath)
|
if !result.Options.noInfoDownload {
|
||||||
return nil, err
|
jsonTempPath = path.Join(tempPath, "info.json")
|
||||||
|
if err := ioutil.WriteFile(jsonTempPath, result.RawJSON, 0600); err != nil {
|
||||||
|
os.RemoveAll(tempPath)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dr := &DownloadResult{
|
dr := &DownloadResult{
|
||||||
@ -478,9 +508,36 @@ func (result Result) DownloadWithOptions(ctx context.Context, options DownloadOp
|
|||||||
"--ignore-errors",
|
"--ignore-errors",
|
||||||
"--newline",
|
"--newline",
|
||||||
"--restrict-filenames",
|
"--restrict-filenames",
|
||||||
"--load-info", jsonTempPath,
|
|
||||||
"-o", "-",
|
"-o", "-",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if result.Options.noInfoDownload {
|
||||||
|
// provide URL via stdin for security, youtube-dl has some run command args
|
||||||
|
cmd.Args = append(cmd.Args, "--batch-file", "-")
|
||||||
|
cmd.Stdin = bytes.NewBufferString(result.RawURL + "\n")
|
||||||
|
|
||||||
|
if result.Options.Type == TypePlaylist {
|
||||||
|
cmd.Args = append(cmd.Args, "--yes-playlist")
|
||||||
|
|
||||||
|
if result.Options.PlaylistStart > 0 {
|
||||||
|
cmd.Args = append(cmd.Args,
|
||||||
|
"--playlist-start", strconv.Itoa(int(result.Options.PlaylistStart)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if result.Options.PlaylistEnd > 0 {
|
||||||
|
cmd.Args = append(cmd.Args,
|
||||||
|
"--playlist-end", strconv.Itoa(int(result.Options.PlaylistEnd)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cmd.Args = append(cmd.Args,
|
||||||
|
"--no-playlist",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cmd.Args = append(cmd.Args, "--load-info", jsonTempPath)
|
||||||
|
}
|
||||||
|
|
||||||
// don't need to specify if direct as there is only one
|
// don't need to specify if direct as there is only one
|
||||||
// also seems to be issues when using filter with generic extractor
|
// also seems to be issues when using filter with generic extractor
|
||||||
if !result.Info.Direct && options.Filter != "" {
|
if !result.Info.Direct && options.Filter != "" {
|
||||||
|
@ -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