-
-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da93686
commit c1130f3
Showing
7 changed files
with
183 additions
and
3 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
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,96 @@ | ||
package builder | ||
|
||
import ( | ||
"strconv" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
soundcloudapi "github.com/zackradisic/soundcloud-api" | ||
"golang.org/x/net/context" | ||
|
||
"github.com/mxpv/podsync/pkg/config" | ||
"github.com/mxpv/podsync/pkg/model" | ||
) | ||
|
||
type SoundCloudBuilder struct { | ||
client *soundcloudapi.API | ||
} | ||
|
||
func (s *SoundCloudBuilder) Build(ctx context.Context, cfg *config.Feed) (*model.Feed, error) { | ||
info, err := ParseURL(cfg.URL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
feed := &model.Feed{ | ||
ItemID: info.ItemID, | ||
Provider: info.Provider, | ||
LinkType: info.LinkType, | ||
Format: cfg.Format, | ||
Quality: cfg.Quality, | ||
PageSize: cfg.PageSize, | ||
UpdatedAt: time.Now().UTC(), | ||
} | ||
|
||
if info.LinkType == model.TypePlaylist { | ||
if soundcloudapi.IsPlaylistURL(cfg.URL) { | ||
scplaylist, err := s.client.GetPlaylistInfo(cfg.URL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
feed.Title = scplaylist.Title | ||
feed.Description = scplaylist.Description | ||
feed.ItemURL = cfg.URL | ||
|
||
date, err := time.Parse(time.RFC3339, scplaylist.CreatedAt) | ||
if err == nil { | ||
feed.PubDate = date | ||
} | ||
feed.Author = scplaylist.User.Username | ||
feed.CoverArt = scplaylist.ArtworkURL | ||
|
||
var added = 0 | ||
for _, track := range scplaylist.Tracks { | ||
pubDate, _ := time.Parse(time.RFC3339, track.CreatedAt) | ||
var ( | ||
videoID = strconv.FormatInt(track.ID, 10) | ||
duration = track.DurationMS / 1000 | ||
mediaURL = track.PermalinkURL | ||
trackSize = track.DurationMS * 15 // very rough estimate | ||
) | ||
|
||
feed.Episodes = append(feed.Episodes, &model.Episode{ | ||
ID: videoID, | ||
Title: track.Title, | ||
Description: track.Description, | ||
Duration: duration, | ||
Size: trackSize, | ||
VideoURL: mediaURL, | ||
PubDate: pubDate, | ||
Thumbnail: track.ArtworkURL, | ||
Status: model.EpisodeNew, | ||
}) | ||
|
||
added++ | ||
|
||
if added >= feed.PageSize { | ||
return feed, nil | ||
} | ||
} | ||
|
||
return feed, nil | ||
} | ||
} | ||
|
||
return nil, errors.New(("unsupported soundcloud feed type")) | ||
} | ||
|
||
func NewSoundcloudBuilder() (*SoundCloudBuilder, error) { | ||
sc, err := soundcloudapi.New(soundcloudapi.APIOptions{}) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to create soundcloud client") | ||
} | ||
|
||
return &SoundCloudBuilder{client: sc}, nil | ||
} |
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,42 @@ | ||
package builder | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/mxpv/podsync/pkg/config" | ||
) | ||
|
||
func TestSC_BUILDFEED(t *testing.T) { | ||
builder, err := NewSoundcloudBuilder() | ||
require.NoError(t, err) | ||
|
||
urls := []string{ | ||
"https://soundcloud.com/moby/sets/remixes", | ||
"https://soundcloud.com/npr/sets/soundscapes", | ||
} | ||
|
||
for _, addr := range urls { | ||
t.Run(addr, func(t *testing.T) { | ||
feed, err := builder.Build(testCtx, &config.Feed{URL: addr}) | ||
require.NoError(t, err) | ||
|
||
assert.NotEmpty(t, feed.Title) | ||
assert.NotEmpty(t, feed.Description) | ||
assert.NotEmpty(t, feed.Author) | ||
assert.NotEmpty(t, feed.ItemURL) | ||
|
||
assert.NotZero(t, len(feed.Episodes)) | ||
|
||
for _, item := range feed.Episodes { | ||
assert.NotEmpty(t, item.Title) | ||
assert.NotEmpty(t, item.VideoURL) | ||
assert.NotZero(t, item.Duration) | ||
assert.NotEmpty(t, item.Title) | ||
assert.NotEmpty(t, item.Thumbnail) | ||
} | ||
}) | ||
} | ||
} |
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