mirror of
https://github.com/mxpv/podsync.git
synced 2024-05-11 05:55:04 +00:00
Fix YouTube pagination #42
This commit is contained in:
@ -90,8 +90,14 @@ func (yt *YouTubeBuilder) listPlaylists(ctx context.Context, id, channelID strin
|
||||
|
||||
// Cost: 3 units (call: 1, snippet: 2)
|
||||
// See https://developers.google.com/youtube/v3/docs/playlistItems/list#part
|
||||
func (yt *YouTubeBuilder) listPlaylistItems(ctx context.Context, itemID string, pageToken string) ([]*youtube.PlaylistItem, string, error) {
|
||||
req := yt.client.PlaylistItems.List("id,snippet").MaxResults(maxYoutubeResults).PlaylistId(itemID)
|
||||
func (yt *YouTubeBuilder) listPlaylistItems(ctx context.Context, feed *model.Feed, pageToken string) ([]*youtube.PlaylistItem, string, error) {
|
||||
count := maxYoutubeResults
|
||||
if count > feed.PageSize {
|
||||
// If we need less than 50
|
||||
count = feed.PageSize
|
||||
}
|
||||
|
||||
req := yt.client.PlaylistItems.List("id,snippet").MaxResults(int64(count)).PlaylistId(feed.ItemID)
|
||||
if pageToken != "" {
|
||||
req = req.PageToken(pageToken)
|
||||
}
|
||||
@ -334,7 +340,7 @@ func (yt *YouTubeBuilder) queryItems(ctx context.Context, feed *model.Feed) erro
|
||||
)
|
||||
|
||||
for {
|
||||
items, pageToken, err := yt.listPlaylistItems(ctx, feed.ItemID, token)
|
||||
items, pageToken, err := yt.listPlaylistItems(ctx, feed, token)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -380,6 +386,10 @@ func (yt *YouTubeBuilder) Build(ctx context.Context, cfg *config.Feed) (*model.F
|
||||
UpdatedAt: time.Now().UTC(),
|
||||
}
|
||||
|
||||
if feed.PageSize == 0 {
|
||||
feed.PageSize = maxYoutubeResults
|
||||
}
|
||||
|
||||
// Query general information about feed (title, description, lang, etc)
|
||||
if err := yt.queryFeed(ctx, feed, &info); err != nil {
|
||||
return nil, err
|
||||
@ -389,6 +399,12 @@ func (yt *YouTubeBuilder) Build(ctx context.Context, cfg *config.Feed) (*model.F
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// YT API client gets 50 episodes per query.
|
||||
// Round up to page size.
|
||||
if len(feed.Episodes) > feed.PageSize {
|
||||
feed.Episodes = feed.Episodes[:feed.PageSize]
|
||||
}
|
||||
|
||||
sort.Slice(feed.Episodes, func(i, j int) bool {
|
||||
item1, _ := strconv.Atoi(feed.Episodes[i].Order)
|
||||
item2, _ := strconv.Atoi(feed.Episodes[j].Order)
|
||||
|
@ -25,7 +25,7 @@ data_dir = "test/data/"
|
||||
[feeds]
|
||||
[feeds.XYZ]
|
||||
url = "https://youtube.com/watch?v=ygIUF678y40"
|
||||
page_size = 50
|
||||
page_size = 48
|
||||
update_period = "5h"
|
||||
format = "audio"
|
||||
quality = "low"
|
||||
@ -53,7 +53,7 @@ data_dir = "test/data/"
|
||||
feed, ok := config.Feeds["XYZ"]
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, "https://youtube.com/watch?v=ygIUF678y40", feed.URL)
|
||||
assert.EqualValues(t, 50, feed.PageSize)
|
||||
assert.EqualValues(t, 48, feed.PageSize)
|
||||
assert.EqualValues(t, Duration{5 * time.Hour}, feed.UpdatePeriod)
|
||||
assert.EqualValues(t, "audio", feed.Format)
|
||||
assert.EqualValues(t, "low", feed.Quality)
|
||||
|
Reference in New Issue
Block a user