From d6e8a221de7fd1e135d9d378c7adfd1fd7a05f99 Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Sun, 12 Apr 2020 14:32:32 -0700 Subject: [PATCH] Looup for youtube-dl binary #95 --- pkg/ytdl/ytdl.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/pkg/ytdl/ytdl.go b/pkg/ytdl/ytdl.go index d40039b..4d01b13 100644 --- a/pkg/ytdl/ytdl.go +++ b/pkg/ytdl/ytdl.go @@ -25,10 +25,21 @@ var ( ErrTooManyRequests = errors.New(http.StatusText(http.StatusTooManyRequests)) ) -type YoutubeDl struct{} +type YoutubeDl struct { + path string +} func New(ctx context.Context) (*YoutubeDl, error) { - ytdl := &YoutubeDl{} + path, err := exec.LookPath("youtube-dl") + if err != nil { + return nil, errors.Wrap(err, "youtube-dl binary not found") + } + + log.Debugf("found youtube-dl binary at %q", path) + + ytdl := &YoutubeDl{ + path: path, + } // Make sure youtube-dl exists version, err := ytdl.exec(ctx, "--version") @@ -85,12 +96,11 @@ func (dl YoutubeDl) Download(ctx context.Context, feedConfig *config.Feed, episo return f, nil } -func (YoutubeDl) exec(ctx context.Context, args ...string) (string, error) { +func (dl YoutubeDl) exec(ctx context.Context, args ...string) (string, error) { ctx, cancel := context.WithTimeout(ctx, DownloadTimeout) defer cancel() - cmd := exec.CommandContext(ctx, "youtube-dl", args...) - + cmd := exec.CommandContext(ctx, dl.path, args...) output, err := cmd.CombinedOutput() if err != nil { return string(output), errors.Wrap(err, "failed to execute youtube-dl")