Small improvements

This commit is contained in:
Th0masL
2022-06-13 11:44:51 +03:00
parent ec5e4445cc
commit 937145a68f
+6 -11
View File
@@ -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")
}