From 937145a68f5f83d0807910e216de07d42654186e Mon Sep 17 00:00:00 2001 From: Th0masL Date: Mon, 13 Jun 2022 11:44:51 +0300 Subject: [PATCH] Small improvements --- pkg/builder/youtube.go | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/pkg/builder/youtube.go b/pkg/builder/youtube.go index 585f12e..f2a858d 100644 --- a/pkg/builder/youtube.go +++ b/pkg/builder/youtube.go @@ -3,6 +3,7 @@ package builder import ( "context" "fmt" + "math" "net/http" "sort" "strconv" @@ -265,20 +266,14 @@ func (yt *YouTubeBuilder) getSize(duration int64, feed *model.Feed) int64 { // See https://developers.google.com/youtube/v3/docs/videos/list#part func (yt *YouTubeBuilder) queryVideoDescriptions(ctx context.Context, playlist map[string]*youtube.PlaylistItemSnippet, feed *model.Feed) error { // Make the list of video ids - // and count how many API calls will be required ids := make([]string, 0, len(playlist)) - count := 0 - count_expected_api_calls := 1 for _, s := range playlist { - count++ ids = append(ids, s.ResourceId.VideoId) - // Increment the counter of expected API calls - if count == maxYoutubeResults { - count_expected_api_calls++ - count = 0 - } } + // Count how many API calls will be required + count_expected_api_calls := int(math.Ceil(float64(len(playlist)) / maxYoutubeResults)) + log.Debugf("Expected to make %d API calls to get the descriptions for %d episode(s).", count_expected_api_calls, len(ids)) // Init a list that will contains the aggregated strings of videos IDs (capped at 50 IDs per API Calls) @@ -316,8 +311,8 @@ func (yt *YouTubeBuilder) queryVideoDescriptions(ctx context.Context, playlist m } // Loop in each list of 50 (or less) IDs and query the description - for list_number := 0; list_number < len(ids_list); list_number++ { - req, err := yt.client.Videos.List("id,snippet,contentDetails").Id(ids_list[list_number]).Context(ctx).Do(yt.key) + for _, idsI := range ids_list { + req, err := yt.client.Videos.List("id,snippet,contentDetails").Id(idsI).Context(ctx).Do(yt.key) if err != nil { return errors.Wrap(err, "failed to query video descriptions") }