Decouple config package

This commit is contained in:
Maksym Pavlenko
2022-01-02 14:57:10 +02:00
parent 0edf2836bb
commit 54834550d5
21 changed files with 156 additions and 161 deletions
+70
View File
@@ -0,0 +1,70 @@
package feed
import (
"time"
"github.com/mxpv/podsync/pkg/model"
)
// Config is a configuration for a feed loaded from TOML
type Config struct {
ID string `toml:"-"`
// URL is a full URL of the field
URL string `toml:"url"`
// PageSize is the number of pages to query from YouTube API.
// NOTE: larger page sizes/often requests might drain your API token.
PageSize int `toml:"page_size"`
// UpdatePeriod is how often to check for updates.
// Format is "300ms", "1.5h" or "2h45m".
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
// NOTE: too often update check might drain your API token.
UpdatePeriod time.Duration `toml:"update_period"`
// Cron expression format is how often to check update
// NOTE: too often update check might drain your API token.
CronSchedule string `toml:"cron_schedule"`
// Quality to use for this feed
Quality model.Quality `toml:"quality"`
// Maximum height of video
MaxHeight int `toml:"max_height"`
// Format to use for this feed
Format model.Format `toml:"format"`
// Only download episodes that match this regexp (defaults to matching anything)
Filters Filters `toml:"filters"`
// Clean is a cleanup policy to use for this feed
Clean Cleanup `toml:"clean"`
// Custom is a list of feed customizations
Custom Custom `toml:"custom"`
// List of additional youtube-dl arguments passed at download time
YouTubeDLArgs []string `toml:"youtube_dl_args"`
// Included in OPML file
OPML bool `toml:"opml"`
// Playlist sort
PlaylistSort model.Sorting `toml:"playlist_sort"`
}
type Filters struct {
Title string `toml:"title"`
NotTitle string `toml:"not_title"`
Description string `toml:"description"`
NotDescription string `toml:"not_description"`
// More filters to be added here
}
type Custom struct {
CoverArt string `toml:"cover_art"`
CoverArtQuality model.Quality `toml:"cover_art_quality"`
Category string `toml:"category"`
Subcategories []string `toml:"subcategories"`
Explicit bool `toml:"explicit"`
Language string `toml:"lang"`
Author string `toml:"author"`
Title string `toml:"title"`
Description string `toml:"description"`
OwnerName string `toml:"ownerName"`
OwnerEmail string `toml:"ownerEmail"`
}
type Cleanup struct {
// KeepLast defines how many episodes to keep
KeepLast int `toml:"keep_last"`
}
+2 -3
View File
@@ -9,16 +9,15 @@ import (
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/mxpv/podsync/pkg/config"
"github.com/mxpv/podsync/pkg/model"
)
func BuildOPML(ctx context.Context, config *config.Config, db feedProvider, hostname string) (string, error) {
func BuildOPML(ctx context.Context, feeds map[string]*Config, db feedProvider, hostname string) (string, error) {
doc := opml.OPML{Version: "1.0"}
doc.Head = opml.Head{Title: "Podsync feeds"}
doc.Body = opml.Body{}
for _, feed := range config.Feeds {
for _, feed := range feeds {
f, err := db.GetFeed(ctx, feed.ID)
if err == model.ErrNotFound {
// As we update OPML on per-feed basis, some feeds may not yet be populated in database.
+2 -8
View File
@@ -7,7 +7,6 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/mxpv/podsync/pkg/config"
"github.com/mxpv/podsync/pkg/model"
)
@@ -28,13 +27,8 @@ func TestBuildOPML(t *testing.T) {
dbMock := NewMockfeedProvider(ctrl)
dbMock.EXPECT().GetFeed(gomock.Any(), "1").Return(&model.Feed{Title: "1", Description: "desc"}, nil)
cfg := config.Config{
Feeds: map[string]*config.Feed{
"any": {ID: "1", OPML: true},
},
}
out, err := BuildOPML(context.Background(), &cfg, dbMock, "https://url/")
feeds := map[string]*Config{"any": {ID: "1", OPML: true}}
out, err := BuildOPML(context.Background(), feeds, dbMock, "https://url/")
assert.NoError(t, err)
assert.Equal(t, expected, out)
}
+2 -3
View File
@@ -11,7 +11,6 @@ import (
itunes "github.com/eduncan911/podcast"
"github.com/pkg/errors"
"github.com/mxpv/podsync/pkg/config"
"github.com/mxpv/podsync/pkg/model"
)
@@ -31,7 +30,7 @@ func (p timeSlice) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func Build(_ctx context.Context, feed *model.Feed, cfg *config.Feed, hostname string) (*itunes.Podcast, error) {
func Build(_ctx context.Context, feed *model.Feed, cfg *Config, hostname string) (*itunes.Podcast, error) {
const (
podsyncGenerator = "Podsync generator (support us at https://github.com/mxpv/podsync)"
defaultCategory = "TV & Film"
@@ -152,7 +151,7 @@ func Build(_ctx context.Context, feed *model.Feed, cfg *config.Feed, hostname st
return &p, nil
}
func EpisodeName(feedConfig *config.Feed, episode *model.Episode) string {
func EpisodeName(feedConfig *Config, episode *model.Episode) string {
ext := "mp4"
if feedConfig.Format == model.FormatAudio {
ext = "mp3"
+2 -3
View File
@@ -5,7 +5,6 @@ import (
"testing"
itunes "github.com/eduncan911/podcast"
"github.com/mxpv/podsync/pkg/config"
"github.com/mxpv/podsync/pkg/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -23,9 +22,9 @@ func TestBuildXML(t *testing.T) {
},
}
cfg := config.Feed{
cfg := Config{
ID: "test",
Custom: config.Custom{Description: "description", Category: "Technology", Subcategories: []string{"Gadgets", "Podcasting"}},
Custom: Custom{Description: "description", Category: "Technology", Subcategories: []string{"Gadgets", "Podcasting"}},
}
out, err := Build(context.Background(), &feed, &cfg, "http://localhost/")