Skip to content

Comments

Fix page_size limited at 50 on YouTube#320

Merged
mxpv merged 6 commits intomxpv:mainfrom
Th0masL:fix_pagesize_bug
Jun 14, 2022
Merged

Fix page_size limited at 50 on YouTube#320
mxpv merged 6 commits intomxpv:mainfrom
Th0masL:fix_pagesize_bug

Conversation

@Th0masL
Copy link
Contributor

@Th0masL Th0masL commented Jun 12, 2022

This PR fixes the page_size limited to 50 episodes on YouTube (see issue #306).

This was due to the fact that the function queryVideoDescriptions was sending an API call with more than 50 video IDs, and was therefore crashing.

What I'm doing is basically to break down the video IDs by chunks of 50, and loop as many times as requires to query their Description.

I've also added a DEBUG message that shows how many API calls are expected to be made based on this number of episodes.

DEBU[2022-06-13T01:39:11+03:00] Expected to make 3 API calls to get the descriptions for 105 episode(s).    <== NEW  
DEBU[2022-06-13T01:39:11+03:00] received 105 episode(s) for "XXXX YYYY" 
DEBU[2022-06-13T01:39:11+03:00] successfully saved updates to storage  

And since I was doing this PR, I've also added a small log line to say when an Episode download is skipped because it has already been downloaded earlier, which is something I was always wondering.

# Changes related to the log line when file exist :
INFO[2022-06-13T01:39:11+03:00] downloading episodes                          page_size=105
INFO[2022-06-13T01:39:11+03:00] skipping due to mismatch                      episode_id=ABCD1 filter=title
INFO[2022-06-13T01:39:11+03:00] skipping due to already downloaded            episode_id=ABCD3   <== NEW  

PS: Since I had to add some tabulations to a good portion of the function, the diff is super ugly, and it looks like I've changed way more stuff than what I actually did 😞

Copy link
Contributor

@Contextualist Contextualist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be a bit verbose, but overall organization and comments are clear, and here are some suggestions to simplify things.

@Th0masL
Copy link
Contributor Author

Th0masL commented Jun 13, 2022

Thanks for the suggestion @Contextualist ! I've applied them, let me know if you see other things that need to be changed 🙂

Copy link
Contributor

@Contextualist Contextualist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! One minor change, and also make sure that you follow the suggestions from the lint, in this case, camelCase and formatting the files.

@mxpv
Copy link
Owner

mxpv commented Jun 13, 2022

Error: File is not `gofmt`-ed with `-s` (gofmt)
[25](https://github.com/mxpv/podsync/runs/6866147950?check_suite_focus=true#step:4:27)
Error: don't use underscores in Go names; var `count_expected_api_calls` should be `countExpectedAPICalls` (golint)

There are a couple complains from CI's linter.

@Th0masL
Copy link
Contributor Author

Th0masL commented Jun 13, 2022

Thanks for your feedback guys ! The problems should now be resolved 😎

@Contextualist
Copy link
Contributor

I might be a bit pedantic 😉 :

  1. countExpectedAPICalls doesn't need to be computed; it's simply len(ids_list) after the chunking process.
  2. (Not sure why the lint does not catch that but) ids_list should be idsList.

@Th0masL
Copy link
Contributor Author

Th0masL commented Jun 13, 2022

Good call ! I've applied those changes too 😎

Copy link
Contributor

@Contextualist Contextualist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Waiting for opinions from @mxpv
Also attaching a better diff for reference:

 268            for _, s := range playlist {
 269                    ids = append(ids, s.ResourceId.VideoId)
 270            }
-272            req, err := yt.client.Videos.List("id,snippet,contentDetails").Id(strings.Join(ids, ",")).Context(ctx).Do(yt.key)
+   273         // Init a list that will contains the aggregated strings of videos IDs (capped at 50 IDs per API Calls)
+   274         idsList := make([]string, 0, 1)
+   276         // Chunk the list of IDs by slices limited to maxYoutubeResults
+   277         for i := 0; i < len(ids); i += maxYoutubeResults {
+   278                 end := i + maxYoutubeResults
+   279                 if end > len(ids) {
+   280                         end = len(ids)
+   281                 }
+   282                 // Save each slice as comma-delimited string
+   283                 idsList = append(idsList, strings.Join(ids[i:end], ","))
+   284         }
+   286         // Show how many API calls will be required
+   287         log.Debugf("Expected to make %d API calls to get the descriptions for %d episode(s).", len(idsList), len(ids))
+   289         // Loop in each slices of 50 (or less) IDs and query their description
+   290         for _, idsI := range idsList {
+   291                 req, err := yt.client.Videos.List("id,snippet,contentDetails").Id(idsI).Context(ctx).Do(yt.key)
    292                 if err != nil {
    293                         return errors.Wrap(err, "failed to query video descriptions")
    294                 }

@distbit0
Copy link

Can't wait for this to merged! Thanks so much for developing it @Th0masL and for reviewing it @Contextualist 👌

@mxpv
Copy link
Owner

mxpv commented Jun 14, 2022

Thanks!

@mxpv mxpv merged commit 7a69ddb into mxpv:main Jun 14, 2022
@Th0masL Th0masL deleted the fix_pagesize_bug branch June 14, 2022 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants