|
| 1 | +package builder |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/mxpv/podsync/pkg/feed" |
| 10 | + "github.com/mxpv/podsync/pkg/model" |
| 11 | + "github.com/nicklaw5/helix" |
| 12 | + "github.com/pkg/errors" |
| 13 | +) |
| 14 | + |
| 15 | +type TwitchBuilder struct { |
| 16 | + client *helix.Client |
| 17 | +} |
| 18 | + |
| 19 | +func (t *TwitchBuilder) Build(_ctx context.Context, cfg *feed.Config) (*model.Feed, error) { |
| 20 | + info, err := ParseURL(cfg.URL) |
| 21 | + if err != nil { |
| 22 | + return nil, errors.Wrap(err, "failed to parse URL") |
| 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.TypeUser { |
| 36 | + |
| 37 | + users, err := t.client.GetUsers(&helix.UsersParams{ |
| 38 | + Logins: []string{info.ItemID}, |
| 39 | + }) |
| 40 | + if err != nil { |
| 41 | + return nil, errors.Wrapf(err, "failed to get user: %s", info.ItemID) |
| 42 | + } |
| 43 | + user := users.Data.Users[0] |
| 44 | + |
| 45 | + feed.Title = user.DisplayName |
| 46 | + feed.Author = user.DisplayName |
| 47 | + feed.Description = user.Description |
| 48 | + feed.ItemURL = fmt.Sprintf("https://www.twitch.tv/%s", user.Login) |
| 49 | + feed.CoverArt = user.ProfileImageURL |
| 50 | + feed.PubDate = user.CreatedAt.Time |
| 51 | + |
| 52 | + videos, err := t.client.GetVideos(&helix.VideosParams{ |
| 53 | + UserID: user.ID, |
| 54 | + Period: "all", |
| 55 | + Type: "all", |
| 56 | + Sort: "time", |
| 57 | + First: 10, |
| 58 | + }) |
| 59 | + if err != nil { |
| 60 | + return nil, errors.Wrapf(err, "failed to get videos for user: %s", info.ItemID) |
| 61 | + } |
| 62 | + |
| 63 | + var added = 0 |
| 64 | + for _, video := range videos.Data.Videos { |
| 65 | + |
| 66 | + date, err := time.Parse(time.RFC3339, video.PublishedAt) |
| 67 | + if err != nil { |
| 68 | + return nil, errors.Wrapf(err, "cannot parse PublishedAt time: %s", video.PublishedAt) |
| 69 | + } |
| 70 | + |
| 71 | + replacer := strings.NewReplacer("%{width}", "300", "%{height}", "300") |
| 72 | + thumbnailUrl := replacer.Replace(video.ThumbnailURL) |
| 73 | + |
| 74 | + duration, err := time.ParseDuration(video.Duration) |
| 75 | + if err != nil { |
| 76 | + return nil, errors.Wrapf(err, "cannot parse duration: %s", video.Duration) |
| 77 | + } |
| 78 | + durationSeconds := int64(duration.Seconds()) |
| 79 | + |
| 80 | + feed.Episodes = append(feed.Episodes, &model.Episode{ |
| 81 | + ID: video.ID, |
| 82 | + Title: fmt.Sprintf("%s (%s)", video.Title, date), |
| 83 | + Description: video.Description, |
| 84 | + Thumbnail: thumbnailUrl, |
| 85 | + Duration: durationSeconds, |
| 86 | + Size: durationSeconds * 33013, // Very rough estimate |
| 87 | + VideoURL: video.URL, |
| 88 | + PubDate: date, |
| 89 | + Status: model.EpisodeNew, |
| 90 | + }) |
| 91 | + |
| 92 | + added++ |
| 93 | + if added >= feed.PageSize { |
| 94 | + return feed, nil |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + return feed, nil |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + return nil, errors.New("unsupported feed type") |
| 104 | +} |
| 105 | + |
| 106 | +func NewTwitchBuilder(clientIDSecret string) (*TwitchBuilder, error) { |
| 107 | + parts := strings.Split(clientIDSecret, ":") |
| 108 | + if len(parts) != 2 { |
| 109 | + return nil, errors.New("invalid twitch key, need to be \"CLIENT_ID:CLIENT_SECRET\"") |
| 110 | + } |
| 111 | + |
| 112 | + clientID := parts[0] |
| 113 | + clientSecret := parts[1] |
| 114 | + |
| 115 | + client, err := helix.NewClient(&helix.Options{ |
| 116 | + ClientID: clientID, |
| 117 | + ClientSecret: clientSecret, |
| 118 | + }) |
| 119 | + if err != nil { |
| 120 | + return nil, errors.Wrap(err, "failed to create twitch client") |
| 121 | + } |
| 122 | + |
| 123 | + token, err := client.RequestAppAccessToken([]string{}) |
| 124 | + if err != nil { |
| 125 | + return nil, errors.Wrap(err, "failed to request twitch app token") |
| 126 | + } |
| 127 | + |
| 128 | + // Set the access token on the client |
| 129 | + client.SetAppAccessToken(token.Data.AccessToken) |
| 130 | + |
| 131 | + return &TwitchBuilder{client: client}, nil |
| 132 | +} |
0 commit comments