Skip to content

Commit

Permalink
Refactor packages
Browse files Browse the repository at this point in the history
  • Loading branch information
mxpv committed Apr 18, 2020
1 parent 10c2de0 commit e764ab6
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 87 deletions.
5 changes: 2 additions & 3 deletions pkg/db/badger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/stretchr/testify/require"

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

Expand Down Expand Up @@ -188,8 +187,8 @@ func getFeed() *model.Feed {
return &model.Feed{
ID: "1",
ItemID: "2",
LinkType: link.TypeChannel,
Provider: link.ProviderVimeo,
LinkType: model.TypeChannel,
Provider: model.ProviderVimeo,
CreatedAt: time.Now().UTC(),
LastAccess: time.Now().UTC(),
ExpirationTime: time.Now().UTC().Add(1 * time.Hour),
Expand Down
7 changes: 3 additions & 4 deletions pkg/feed/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/pkg/errors"

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

Expand All @@ -20,15 +19,15 @@ func New(ctx context.Context, cfg *config.Feed, tokens config.Tokens) (Builder,
err error
)

info, err := link.Parse(cfg.URL)
info, err := ParseURL(cfg.URL)
if err != nil {
return nil, err
}

switch info.Provider {
case link.ProviderYoutube:
case model.ProviderYoutube:
provider, err = NewYouTubeBuilder(tokens.YouTube)
case link.ProviderVimeo:
case model.ProviderVimeo:
provider, err = NewVimeoBuilder(ctx, tokens.Vimeo)
default:
return nil, errors.Errorf("unsupported provider %q", info.Provider)
Expand Down
42 changes: 22 additions & 20 deletions pkg/link/url.go → pkg/feed/url.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package link
package feed

import (
"net/url"
"strings"

"github.com/pkg/errors"

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

func Parse(link string) (Info, error) {
func ParseURL(link string) (model.Info, error) {
parsed, err := parseURL(link)
if err != nil {
return Info{}, err
return model.Info{}, err
}

info := Info{}
info := model.Info{}

if strings.HasSuffix(parsed.Host, "youtube.com") {
kind, id, err := parseYoutubeURL(parsed)
if err != nil {
return Info{}, err
return model.Info{}, err
}

info.Provider = ProviderYoutube
info.Provider = model.ProviderYoutube
info.LinkType = kind
info.ItemID = id

Expand All @@ -31,17 +33,17 @@ func Parse(link string) (Info, error) {
if strings.HasSuffix(parsed.Host, "vimeo.com") {
kind, id, err := parseVimeoURL(parsed)
if err != nil {
return Info{}, err
return model.Info{}, err
}

info.Provider = ProviderVimeo
info.Provider = model.ProviderVimeo
info.LinkType = kind
info.ItemID = id

return info, nil
}

return Info{}, errors.New("unsupported URL host")
return model.Info{}, errors.New("unsupported URL host")
}

func parseURL(link string) (*url.URL, error) {
Expand All @@ -57,13 +59,13 @@ func parseURL(link string) (*url.URL, error) {
return parsed, nil
}

func parseYoutubeURL(parsed *url.URL) (Type, string, error) {
func parseYoutubeURL(parsed *url.URL) (model.Type, string, error) {
path := parsed.EscapedPath()

// https://www.youtube.com/playlist?list=PLCB9F975ECF01953C
// https://www.youtube.com/watch?v=rbCbho7aLYw&list=PLMpEfaKcGjpWEgNtdnsvLX6LzQL0UC0EM
if strings.HasPrefix(path, "/playlist") || strings.HasPrefix(path, "/watch") {
kind := TypePlaylist
kind := model.TypePlaylist

id := parsed.Query().Get("list")
if id != "" {
Expand All @@ -76,7 +78,7 @@ func parseYoutubeURL(parsed *url.URL) (Type, string, error) {
// - https://www.youtube.com/channel/UC5XPnUk8Vvv_pWslhwom6Og
// - https://www.youtube.com/channel/UCrlakW-ewUT8sOod6Wmzyow/videos
if strings.HasPrefix(path, "/channel") {
kind := TypeChannel
kind := model.TypeChannel
parts := strings.Split(parsed.EscapedPath(), "/")
if len(parts) <= 2 {
return "", "", errors.New("invalid youtube channel link")
Expand All @@ -92,7 +94,7 @@ func parseYoutubeURL(parsed *url.URL) (Type, string, error) {

// - https://www.youtube.com/user/fxigr1
if strings.HasPrefix(path, "/user") {
kind := TypeUser
kind := model.TypeUser

parts := strings.Split(parsed.EscapedPath(), "/")
if len(parts) <= 2 {
Expand All @@ -110,23 +112,23 @@ func parseYoutubeURL(parsed *url.URL) (Type, string, error) {
return "", "", errors.New("unsupported link format")
}

func parseVimeoURL(parsed *url.URL) (Type, string, error) {
func parseVimeoURL(parsed *url.URL) (model.Type, string, error) {
parts := strings.Split(parsed.EscapedPath(), "/")
if len(parts) <= 1 {
return "", "", errors.New("invalid vimeo link path")
}

var kind Type
var kind model.Type
switch parts[1] {
case "groups":
kind = TypeGroup
kind = model.TypeGroup
case "channels":
kind = TypeChannel
kind = model.TypeChannel
default:
kind = TypeUser
kind = model.TypeUser
}

if kind == TypeGroup || kind == TypeChannel {
if kind == model.TypeGroup || kind == model.TypeChannel {
if len(parts) <= 2 {
return "", "", errors.New("invalid channel link")
}
Expand All @@ -139,7 +141,7 @@ func parseVimeoURL(parsed *url.URL) (Type, string, error) {
return kind, id, nil
}

if kind == TypeUser {
if kind == model.TypeUser {
id := parts[1]
if id == "" {
return "", "", errors.New("invalid id")
Expand Down
28 changes: 15 additions & 13 deletions pkg/link/url_test.go → pkg/feed/url_test.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,47 @@
package link
package feed

import (
"net/url"
"testing"

"github.com/stretchr/testify/require"

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

func TestParseYoutubeURL_Playlist(t *testing.T) {
link, _ := url.ParseRequestURI("https://www.youtube.com/playlist?list=PLCB9F975ECF01953C")
kind, id, err := parseYoutubeURL(link)
require.NoError(t, err)
require.Equal(t, TypePlaylist, kind)
require.Equal(t, model.TypePlaylist, kind)
require.Equal(t, "PLCB9F975ECF01953C", id)

link, _ = url.ParseRequestURI("https://www.youtube.com/watch?v=rbCbho7aLYw&list=PLMpEfaKcGjpWEgNtdnsvLX6LzQL0UC0EM")
kind, id, err = parseYoutubeURL(link)
require.NoError(t, err)
require.Equal(t, TypePlaylist, kind)
require.Equal(t, model.TypePlaylist, kind)
require.Equal(t, "PLMpEfaKcGjpWEgNtdnsvLX6LzQL0UC0EM", id)
}

func TestParseYoutubeURL_Channel(t *testing.T) {
link, _ := url.ParseRequestURI("https://www.youtube.com/channel/UC5XPnUk8Vvv_pWslhwom6Og")
kind, id, err := parseYoutubeURL(link)
require.NoError(t, err)
require.Equal(t, TypeChannel, kind)
require.Equal(t, model.TypeChannel, kind)
require.Equal(t, "UC5XPnUk8Vvv_pWslhwom6Og", id)

link, _ = url.ParseRequestURI("https://www.youtube.com/channel/UCrlakW-ewUT8sOod6Wmzyow/videos")
kind, id, err = parseYoutubeURL(link)
require.NoError(t, err)
require.Equal(t, TypeChannel, kind)
require.Equal(t, model.TypeChannel, kind)
require.Equal(t, "UCrlakW-ewUT8sOod6Wmzyow", id)
}

func TestParseYoutubeURL_User(t *testing.T) {
link, _ := url.ParseRequestURI("https://youtube.com/user/fxigr1")
kind, id, err := parseYoutubeURL(link)
require.NoError(t, err)
require.Equal(t, TypeUser, kind)
require.Equal(t, model.TypeUser, kind)
require.Equal(t, "fxigr1", id)
}

Expand All @@ -57,47 +59,47 @@ func TestParseVimeoURL_Group(t *testing.T) {
link, _ := url.ParseRequestURI("https://vimeo.com/groups/109")
kind, id, err := parseVimeoURL(link)
require.NoError(t, err)
require.Equal(t, TypeGroup, kind)
require.Equal(t, model.TypeGroup, kind)
require.Equal(t, "109", id)

link, _ = url.ParseRequestURI("http://vimeo.com/groups/109")
kind, id, err = parseVimeoURL(link)
require.NoError(t, err)
require.Equal(t, TypeGroup, kind)
require.Equal(t, model.TypeGroup, kind)
require.Equal(t, "109", id)

link, _ = url.ParseRequestURI("http://www.vimeo.com/groups/109")
kind, id, err = parseVimeoURL(link)
require.NoError(t, err)
require.Equal(t, TypeGroup, kind)
require.Equal(t, model.TypeGroup, kind)
require.Equal(t, "109", id)

link, _ = url.ParseRequestURI("https://vimeo.com/groups/109/videos/")
kind, id, err = parseVimeoURL(link)
require.NoError(t, err)
require.Equal(t, TypeGroup, kind)
require.Equal(t, model.TypeGroup, kind)
require.Equal(t, "109", id)
}

func TestParseVimeoURL_Channel(t *testing.T) {
link, _ := url.ParseRequestURI("https://vimeo.com/channels/staffpicks")
kind, id, err := parseVimeoURL(link)
require.NoError(t, err)
require.Equal(t, TypeChannel, kind)
require.Equal(t, model.TypeChannel, kind)
require.Equal(t, "staffpicks", id)

link, _ = url.ParseRequestURI("http://vimeo.com/channels/staffpicks/146224925")
kind, id, err = parseVimeoURL(link)
require.NoError(t, err)
require.Equal(t, TypeChannel, kind)
require.Equal(t, model.TypeChannel, kind)
require.Equal(t, "staffpicks", id)
}

func TestParseVimeoURL_User(t *testing.T) {
link, _ := url.ParseRequestURI("https://vimeo.com/awhitelabelproduct")
kind, id, err := parseVimeoURL(link)
require.NoError(t, err)
require.Equal(t, TypeUser, kind)
require.Equal(t, model.TypeUser, kind)
require.Equal(t, "awhitelabelproduct", id)
}

Expand Down
9 changes: 4 additions & 5 deletions pkg/feed/vimeo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"golang.org/x/oauth2"

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

Expand Down Expand Up @@ -160,7 +159,7 @@ func (v *VimeoBuilder) queryVideos(getVideos getVideosFunc, feed *model.Feed) er
}

func (v *VimeoBuilder) Build(ctx context.Context, cfg *config.Feed) (*model.Feed, error) {
info, err := link.Parse(cfg.URL)
info, err := ParseURL(cfg.URL)
if err != nil {
return nil, err
}
Expand All @@ -175,7 +174,7 @@ func (v *VimeoBuilder) Build(ctx context.Context, cfg *config.Feed) (*model.Feed
UpdatedAt: time.Now().UTC(),
}

if info.LinkType == link.TypeChannel {
if info.LinkType == model.TypeChannel {
if err := v.queryChannel(feed); err != nil {
return nil, err
}
Expand All @@ -187,7 +186,7 @@ func (v *VimeoBuilder) Build(ctx context.Context, cfg *config.Feed) (*model.Feed
return feed, nil
}

if info.LinkType == link.TypeGroup {
if info.LinkType == model.TypeGroup {
if err := v.queryGroup(feed); err != nil {
return nil, err
}
Expand All @@ -199,7 +198,7 @@ func (v *VimeoBuilder) Build(ctx context.Context, cfg *config.Feed) (*model.Feed
return feed, nil
}

if info.LinkType == link.TypeUser {
if info.LinkType == model.TypeUser {
if err := v.queryUser(feed); err != nil {
return nil, err
}
Expand Down
File renamed without changes.
Loading

0 comments on commit e764ab6

Please sign in to comment.