-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
8 changed files
with
308 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package ytdl | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/mxpv/podsync/pkg/config" | ||
"github.com/mxpv/podsync/pkg/model" | ||
) | ||
|
||
type Options interface { | ||
GetConfig() []string | ||
} | ||
|
||
type OptionsDl struct{} | ||
|
||
func (o OptionsDl) New(feedConfig *config.Feed, episode *model.Episode, feedPath string) []string { | ||
|
||
var ( | ||
arguments []string | ||
options Options | ||
) | ||
|
||
if feedConfig.Format == model.FormatVideo { | ||
options = NewOptionsVideo(feedConfig) | ||
} else { | ||
options = NewOptionsAudio(feedConfig) | ||
} | ||
|
||
arguments = options.GetConfig() | ||
arguments = append(arguments, "--output", o.makeOutputTemplate(feedPath, episode), episode.VideoURL) | ||
|
||
return arguments | ||
} | ||
|
||
func (o OptionsDl) makeOutputTemplate(feedPath string, episode *model.Episode) string { | ||
filename := fmt.Sprintf("%s.%s", episode.ID, "%(ext)s") | ||
return filepath.Join(feedPath, filename) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package ytdl | ||
|
||
import ( | ||
"github.com/mxpv/podsync/pkg/config" | ||
"github.com/mxpv/podsync/pkg/model" | ||
) | ||
|
||
type OptionsAudio struct { | ||
quality model.Quality | ||
} | ||
|
||
func NewOptionsAudio(feedConfig *config.Feed) *OptionsAudio { | ||
options := &OptionsAudio{} | ||
options.quality = feedConfig.Quality | ||
|
||
return options | ||
} | ||
|
||
func (options OptionsAudio) GetConfig() []string { | ||
var arguments []string | ||
|
||
arguments = append(arguments, "--extract-audio", "--audio-format", "mp3") | ||
|
||
switch options.quality { | ||
case model.QualityLow: | ||
// really? somebody use it? | ||
arguments = append(arguments, "--format", "worstaudio") | ||
default: | ||
arguments = append(arguments, "--format", "bestaudio") | ||
} | ||
|
||
return arguments | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package ytdl | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/mxpv/podsync/pkg/config" | ||
"github.com/mxpv/podsync/pkg/model" | ||
) | ||
|
||
func TestNewOptionsAudio(t *testing.T) { | ||
type args struct { | ||
feedConfig *config.Feed | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *OptionsAudio | ||
}{ | ||
{ | ||
"Get OptionsAudio in low quality", | ||
args{ | ||
feedConfig: &config.Feed{Quality: model.QualityLow}, | ||
}, | ||
&OptionsAudio{quality: model.QualityLow}, | ||
}, | ||
{ | ||
"Get OptionsAudio in high quality", | ||
args{ | ||
feedConfig: &config.Feed{Quality: model.QualityHigh}, | ||
}, | ||
&OptionsAudio{quality: model.QualityHigh}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := NewOptionsAudio(tt.args.feedConfig); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("NewOptionsAudio() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestOptionsAudio_GetConfig(t *testing.T) { | ||
type fields struct { | ||
quality model.Quality | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want []string | ||
}{ | ||
{"OptionsAudio in unknown quality", fields{quality: model.Quality("unknown")}, []string{"--extract-audio", "--audio-format", "mp3", "--format", "bestaudio"}}, | ||
{"OptionsAudio in low quality", fields{quality: model.Quality("low")}, []string{"--extract-audio", "--audio-format", "mp3", "--format", "worstaudio"}}, | ||
{"OptionsAudio in high quality", fields{quality: model.Quality("high")}, []string{"--extract-audio", "--audio-format", "mp3", "--format", "bestaudio"}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
options := OptionsAudio{ | ||
quality: tt.fields.quality, | ||
} | ||
if got := options.GetConfig(); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("GetConfig() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package ytdl | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/mxpv/podsync/pkg/config" | ||
"github.com/mxpv/podsync/pkg/model" | ||
) | ||
|
||
type OptionsVideo struct { | ||
quality model.Quality | ||
maxHeight int | ||
} | ||
|
||
func NewOptionsVideo(feedConfig *config.Feed) *OptionsVideo { | ||
options := &OptionsVideo{} | ||
|
||
options.quality = feedConfig.Quality | ||
options.maxHeight = feedConfig.MaxHeight | ||
|
||
return options | ||
} | ||
|
||
func (options OptionsVideo) GetConfig() []string { | ||
var ( | ||
arguments []string | ||
format string | ||
) | ||
|
||
switch options.quality { | ||
// I think after enabling MaxHeight param QualityLow option don't need. | ||
// If somebody want download video in low quality then can set MaxHeight to 360p | ||
// ¯\_(ツ)_/¯ | ||
case model.QualityLow: | ||
format = "worstvideo[ext=mp4]+worstaudio[ext=m4a]/worst[ext=mp4]/worst" | ||
default: | ||
format = "bestvideo%s[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best" | ||
|
||
if options.maxHeight > 0 { | ||
format = fmt.Sprintf(format, fmt.Sprintf("[height<=%d]", options.maxHeight)) | ||
} else { | ||
// unset replace pattern | ||
format = fmt.Sprintf(format, "") | ||
} | ||
} | ||
|
||
arguments = append(arguments, "--format", format) | ||
|
||
return arguments | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package ytdl | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/mxpv/podsync/pkg/config" | ||
"github.com/mxpv/podsync/pkg/model" | ||
) | ||
|
||
func TestNewOptionsVideo(t *testing.T) { | ||
type args struct { | ||
feedConfig *config.Feed | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *OptionsVideo | ||
}{ | ||
{ | ||
"Get OptionsVideo in low quality", | ||
args{ | ||
feedConfig: &config.Feed{Quality: model.QualityLow}, | ||
}, | ||
&OptionsVideo{quality: model.QualityLow}, | ||
}, | ||
{ | ||
"Get OptionsVideo in low quality with maxheight", | ||
args{ | ||
feedConfig: &config.Feed{Quality: model.QualityLow, MaxHeight: 720}, | ||
}, | ||
&OptionsVideo{quality: model.QualityLow, maxHeight: 720}, | ||
}, | ||
{ | ||
"Get OptionsVideo in high quality", | ||
args{ | ||
feedConfig: &config.Feed{Quality: model.QualityHigh}, | ||
}, | ||
&OptionsVideo{quality: model.QualityHigh}, | ||
}, | ||
{ | ||
"Get OptionsVideo in high quality with maxheight", | ||
args{ | ||
feedConfig: &config.Feed{Quality: model.QualityHigh, MaxHeight: 720}, | ||
}, | ||
&OptionsVideo{quality: model.QualityHigh, maxHeight: 720}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := NewOptionsVideo(tt.args.feedConfig); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("NewOptionsVideo() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestOptionsVideo_GetConfig(t *testing.T) { | ||
type fields struct { | ||
quality model.Quality | ||
maxHeight int | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want []string | ||
}{ | ||
{ | ||
"OptionsVideo in unknown quality", | ||
fields{quality: model.Quality("unknown")}, | ||
[]string{"--format", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}, | ||
}, | ||
{ | ||
"OptionsVideo in unknown quality with maxheight", | ||
fields{quality: model.Quality("unknown"), maxHeight: 720}, | ||
[]string{"--format", "bestvideo[height<=720][ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}, | ||
}, | ||
{ | ||
"OptionsVideo in low quality", | ||
fields{quality: model.QualityLow}, | ||
[]string{"--format", "worstvideo[ext=mp4]+worstaudio[ext=m4a]/worst[ext=mp4]/worst"}, | ||
}, | ||
{ | ||
"OptionsVideo in low quality with maxheight", | ||
fields{quality: model.QualityLow, maxHeight: 720}, | ||
[]string{"--format", "worstvideo[ext=mp4]+worstaudio[ext=m4a]/worst[ext=mp4]/worst"}, | ||
}, | ||
{ | ||
"OptionsVideo in high quality", | ||
fields{quality: model.QualityHigh}, | ||
[]string{"--format", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}, | ||
}, | ||
{ | ||
"OptionsVideo in high quality with maxheight", | ||
fields{quality: model.QualityHigh, maxHeight: 720}, | ||
[]string{"--format", "bestvideo[height<=720][ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
options := OptionsVideo{ | ||
quality: tt.fields.quality, | ||
maxHeight: tt.fields.maxHeight, | ||
} | ||
if got := options.GetConfig(); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("GetConfig() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.