Skip to content

Commit

Permalink
Refactor feed package
Browse files Browse the repository at this point in the history
  • Loading branch information
mxpv committed Apr 21, 2020
1 parent d1bd2c2 commit 24c9e79
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 92 deletions.
13 changes: 12 additions & 1 deletion cmd/podsync/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/mxpv/podsync/pkg/builder"
"github.com/mxpv/podsync/pkg/config"
"github.com/mxpv/podsync/pkg/db"
"github.com/mxpv/podsync/pkg/feed"
Expand Down Expand Up @@ -90,8 +91,18 @@ func (u *Updater) Update(ctx context.Context, feedConfig *config.Feed) error {

// updateFeed pulls API for new episodes and saves them to database
func (u *Updater) updateFeed(ctx context.Context, feedConfig *config.Feed) error {
info, err := builder.ParseURL(feedConfig.URL)
if err != nil {
return errors.Wrapf(err, "failed to parse URL: %s", feedConfig.URL)
}

keyProvider, ok := u.keys[info.Provider]
if !ok {
return errors.Errorf("key provider %q not loaded", info.Provider)
}

// Create an updater for this feed type
provider, err := feed.New(ctx, feedConfig, u.keys)
provider, err := builder.New(ctx, info.Provider, keyProvider.Get())
if err != nil {
return err
}
Expand Down
25 changes: 25 additions & 0 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package builder

import (
"context"

"github.com/pkg/errors"

"github.com/mxpv/podsync/pkg/config"
"github.com/mxpv/podsync/pkg/model"
)

type Builder interface {
Build(ctx context.Context, cfg *config.Feed) (*model.Feed, error)
}

func New(ctx context.Context, provider model.Provider, key string) (Builder, error) {
switch provider {
case model.ProviderYoutube:
return NewYouTubeBuilder(key)
case model.ProviderVimeo:
return NewVimeoBuilder(ctx, key)
default:
return nil, errors.Errorf("unsupported provider %q", provider)
}
}
2 changes: 1 addition & 1 deletion pkg/feed/url.go → pkg/builder/url.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package feed
package builder

import (
"net/url"
Expand Down
2 changes: 1 addition & 1 deletion pkg/feed/url_test.go → pkg/builder/url_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package feed
package builder

import (
"net/url"
Expand Down
2 changes: 1 addition & 1 deletion pkg/feed/vimeo.go → pkg/builder/vimeo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package feed
package builder

import (
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion pkg/feed/vimeo_test.go → pkg/builder/vimeo_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package feed
package builder

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion pkg/feed/youtube.go → pkg/builder/youtube.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package feed
package builder

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion pkg/feed/youtube_test.go → pkg/builder/youtube_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package feed
package builder

import (
"context"
Expand Down
43 changes: 0 additions & 43 deletions pkg/feed/common.go

This file was deleted.

17 changes: 17 additions & 0 deletions pkg/feed/deps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:generate mockgen -source=deps.go -destination=deps_mock_test.go -package=feed

package feed

import (
"context"

"github.com/mxpv/podsync/pkg/model"
)

type feedProvider interface {
GetFeed(ctx context.Context, feedID string) (*model.Feed, error)
}

type urlProvider interface {
URL(ctx context.Context, ns string, fileName string) (string, error)
}
41 changes: 1 addition & 40 deletions pkg/feed/common_mock_test.go → pkg/feed/deps_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions pkg/feed/opml_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:generate mockgen -source=common.go -destination=common_mock_test.go -package=feed

package feed

import (
Expand Down

0 comments on commit 24c9e79

Please sign in to comment.