From e1bb02384a34501902d72f3f3482049b3d170beb Mon Sep 17 00:00:00 2001 From: Huseyin Zengin Date: Mon, 25 Nov 2019 21:30:45 +0100 Subject: [PATCH] Use youtube-dl output template with extension parameter #50 --- cmd/podsync/updater.go | 7 ++++++- pkg/ytdl/ytdl.go | 10 +++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/cmd/podsync/updater.go b/cmd/podsync/updater.go index 94a6fa85..0cd68385 100644 --- a/cmd/podsync/updater.go +++ b/cmd/podsync/updater.go @@ -78,6 +78,7 @@ func (u *Updater) Update(ctx context.Context, feedConfig *config.Feed) error { }) episodePath := filepath.Join(feedPath, u.episodeName(feedConfig, episode)) + episodeOutputTemplate := filepath.Join(feedPath, u.episodeNameOutputTemplate(episode)) _, err := os.Stat(episodePath) if err != nil && !os.IsNotExist(err) { return errors.Wrap(err, "failed to check whether episode exists") @@ -86,7 +87,7 @@ func (u *Updater) Update(ctx context.Context, feedConfig *config.Feed) error { if os.IsNotExist(err) { // There is no file on disk, download episode logger.Infof("! downloading episode %s", episode.VideoURL) - if output, err := u.downloader.Download(ctx, feedConfig, episode.VideoURL, episodePath); err == nil { + if output, err := u.downloader.Download(ctx, feedConfig, episode.VideoURL, episodeOutputTemplate); err == nil { downloaded++ } else { // YouTube might block host with HTTP Error 429: Too Many Requests @@ -257,6 +258,10 @@ func (u *Updater) fileSize(path string) (int64, error) { return info.Size(), nil } +func (u *Updater) episodeNameOutputTemplate(episode *model.Episode) string { + return fmt.Sprintf("%s.%s", episode.ID, "%(ext)s") +} + func (u *Updater) makeBuilder(ctx context.Context, cfg *config.Feed) (builder.Builder, error) { var ( provider builder.Builder diff --git a/pkg/ytdl/ytdl.go b/pkg/ytdl/ytdl.go index b79c53b0..b0f765fd 100644 --- a/pkg/ytdl/ytdl.go +++ b/pkg/ytdl/ytdl.go @@ -38,7 +38,7 @@ func New(ctx context.Context) (*YoutubeDl, error) { return ytdl, nil } -func (dl YoutubeDl) Download(ctx context.Context, feedConfig *config.Feed, url string, destPath string) (string, error) { +func (dl YoutubeDl) Download(ctx context.Context, feedConfig *config.Feed, url string, outputTemplate string) (string, error) { if feedConfig.Format == model.FormatAudio { // Audio if feedConfig.Quality == model.QualityHigh { @@ -50,7 +50,7 @@ func (dl YoutubeDl) Download(ctx context.Context, feedConfig *config.Feed, url s "--format", "bestaudio", "--output", - destPath, + outputTemplate, url, ) } else { //nolint @@ -62,7 +62,7 @@ func (dl YoutubeDl) Download(ctx context.Context, feedConfig *config.Feed, url s "--format", "worstaudio", "--output", - destPath, + outputTemplate, url, ) } @@ -76,7 +76,7 @@ func (dl YoutubeDl) Download(ctx context.Context, feedConfig *config.Feed, url s "--format", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best", "--output", - destPath, + outputTemplate, url, ) } else { //nolint @@ -85,7 +85,7 @@ func (dl YoutubeDl) Download(ctx context.Context, feedConfig *config.Feed, url s "--format", "worstvideo[ext=mp4]+worstaudio[ext=m4a]/worst[ext=mp4]/worst", "--output", - destPath, + outputTemplate, url, ) }