Skip to content

Commit

Permalink
Looup for youtube-dl binary #95
Browse files Browse the repository at this point in the history
  • Loading branch information
mxpv committed Apr 12, 2020
1 parent 9090dcb commit d6e8a22
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions pkg/ytdl/ytdl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit d6e8a22

Please sign in to comment.