|
| 1 | +package builder |
| 2 | + |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/pkg/errors" |
| 8 | + soundcloudapi "github.com/zackradisic/soundcloud-api" |
| 9 | + "golang.org/x/net/context" |
| 10 | + |
| 11 | + "github.com/mxpv/podsync/pkg/config" |
| 12 | + "github.com/mxpv/podsync/pkg/model" |
| 13 | +) |
| 14 | + |
| 15 | +type SoundCloudBuilder struct { |
| 16 | + client *soundcloudapi.API |
| 17 | +} |
| 18 | + |
| 19 | +func (s *SoundCloudBuilder) Build(ctx context.Context, cfg *config.Feed) (*model.Feed, error) { |
| 20 | + info, err := ParseURL(cfg.URL) |
| 21 | + if err != nil { |
| 22 | + return nil, err |
| 23 | + } |
| 24 | + |
| 25 | + feed := &model.Feed{ |
| 26 | + ItemID: info.ItemID, |
| 27 | + Provider: info.Provider, |
| 28 | + LinkType: info.LinkType, |
| 29 | + Format: cfg.Format, |
| 30 | + Quality: cfg.Quality, |
| 31 | + PageSize: cfg.PageSize, |
| 32 | + UpdatedAt: time.Now().UTC(), |
| 33 | + } |
| 34 | + |
| 35 | + if info.LinkType == model.TypePlaylist { |
| 36 | + if soundcloudapi.IsPlaylistURL(cfg.URL) { |
| 37 | + scplaylist, err := s.client.GetPlaylistInfo(cfg.URL) |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + |
| 42 | + feed.Title = scplaylist.Title |
| 43 | + feed.Description = scplaylist.Description |
| 44 | + feed.ItemURL = cfg.URL |
| 45 | + |
| 46 | + date, err := time.Parse(time.RFC3339, scplaylist.CreatedAt) |
| 47 | + if err == nil { |
| 48 | + feed.PubDate = date |
| 49 | + } |
| 50 | + feed.Author = scplaylist.User.Username |
| 51 | + feed.CoverArt = scplaylist.ArtworkURL |
| 52 | + |
| 53 | + var added = 0 |
| 54 | + for _, track := range scplaylist.Tracks { |
| 55 | + pubDate, _ := time.Parse(time.RFC3339, track.CreatedAt) |
| 56 | + var ( |
| 57 | + videoID = strconv.FormatInt(track.ID, 10) |
| 58 | + duration = track.DurationMS / 1000 |
| 59 | + mediaURL = track.PermalinkURL |
| 60 | + trackSize = track.DurationMS * 15 // very rough estimate |
| 61 | + ) |
| 62 | + |
| 63 | + feed.Episodes = append(feed.Episodes, &model.Episode{ |
| 64 | + ID: videoID, |
| 65 | + Title: track.Title, |
| 66 | + Description: track.Description, |
| 67 | + Duration: duration, |
| 68 | + Size: trackSize, |
| 69 | + VideoURL: mediaURL, |
| 70 | + PubDate: pubDate, |
| 71 | + Thumbnail: track.ArtworkURL, |
| 72 | + Status: model.EpisodeNew, |
| 73 | + }) |
| 74 | + |
| 75 | + added++ |
| 76 | + |
| 77 | + if added >= feed.PageSize { |
| 78 | + return feed, nil |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return feed, nil |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return nil, errors.New(("unsupported soundcloud feed type")) |
| 87 | +} |
| 88 | + |
| 89 | +func NewSoundcloudBuilder() (*SoundCloudBuilder, error) { |
| 90 | + sc, err := soundcloudapi.New(soundcloudapi.APIOptions{}) |
| 91 | + if err != nil { |
| 92 | + return nil, errors.Wrap(err, "failed to create soundcloud client") |
| 93 | + } |
| 94 | + |
| 95 | + return &SoundCloudBuilder{client: sc}, nil |
| 96 | +} |
0 commit comments