So now that we have the first piece of the puzzle, which is the playlist ID, we now need to get the
list of unique video IDs.
To do this, we will need to build another function which will be similar in structure, but there are
some changes, the first change being the resource we will use before we were using the channels resource,
which we see in the Russian doll analogy in the bottom left.
But for the video IDs we will need to use the playlist item resource.
Another point that we need to mention is the max results parameter you see in the API docs under Playlist
Item Resource.
This parameter represents the maximum number of videos the YouTube API shows per page.
As you can see, the default value is five and the maximum is 50.
You will see here in the API explorer that if we change this parameter from the default of 5 to 10,
for example, then ten videos will be listed for each page.
We will do a demonstration of this shortly, but before I will tell you why we are mentioning this parameter.
We know that from the previous lectures.
The total number of videos that Mr. Beast currently has is 840.
So it won't be a matter of setting this parameter to eight photo.
And we will do just one API call and get all the videos.
What we can do is set this parameter to the maximum of 50 and loop to each page, effectively making
an API call each time until we have looped through each video that Mr. Beast has on his channel.
Now let's do a quick demonstration, starting off by building the URL using the API explorer.
I have pre-filled the parameters, but we will go over them quickly.
For parts we use again the content detail scheme as this will contain the video ID key.
And for the playlist id we found this using the previous function, which is this parameter.
Now for max results.
If we leave it as a blank, we know that five will be returned.
So let's first try setting into one which should return one video.
Let's copy the URL and paste it into postman.
This time we can use postman inside vs code, so let me pull that up.
So the extension, you can actually get it from the extensions and you can write it in postman.
Make sure you install the official postman extension.
Going back here we do a new HTTP request.
Make sure it's a Get request.
We paste the URL here and we just need to change the API key here.
So this will get it from the dot env.
Let's paste this here and hit send.
So this is the response.
Let's just switch the view a bit so it's more clear.
And here you can see that one video was returned with the video ID of the video.
If we had to change this max results to ten, then ten videos will be listed up to a maximum of 50.
Actually lets try to give this a number greater than 50.
So lets say 60 and see what happens.
So I can press send.
So our response was still given.
But maybe if we scroll down here we see that the results per page is 50.
So even though we set max result to 60, the maximum of 50 is still being shown in this page.
So now that we have seen this in action, lets apply these changes to the Python script.
Lets go back to video stats.
First we can set the global variable max results to 50.
We can set it here max results equals 50.
We can also copy and paste the URL from postman and give it the variable name base URL.
Lets do that right now.
This would be the first function and we will focus on the second function.
But for now paste it here.
Now let's substitute some of the variables so we don't have the values hard coded in the URL.
Let's use f strings.
We can keep the part as the quantum details.
Mixed results.
We can replace with the max results variable of 50.
We can do that like that.
The playlist ID we get from the previous function.
So what we can do for now is to create a variable playlist id and set it equal to the function.
The last change that we need to do is to replace the API key, as we did in the previous URL.
So let's change this API key.
And this is the base URL which we will be using.
Going back to the API explorer, there is this page token parameter, which for the first page of results
we don't need to fill in.
If we have to press execute, we have to switch this off.
Press execute and we get a response.
You will see in this next page token parameter, which refers to the token request parameter that you
see here.
If we had to copy the next page token into page token, we would get the next 50 videos of Mr. Beast's
channel.
Let's do that right now.
So we copy the next page token and paste it into the parameter.
We press execute and now you see the response of the next page.
Here you also see the previous page token which relates to the first videos that came up when we use
postman.
So here we are starting to see that to get the next batch of videos, we need to have the associated
page token for that video batch.
Before we move on to the next step, let me remind you that we are after the video IDs, which we know
is under the content details, and we have seen before and we know it's this one.
So we are interested in this video ID.
So now the question is how will we code this up.
I would say the only tricky part of coding this logic up is to keep in mind that for the first batch
of 50 videos, we don't need to define a page token argument in the URL.
However, in the next iterations we will need to and we will get the page token using the next page
token key which we just went over.
Knowing this, let's continue building the syntax.
And we can do this by defining the second function which we can call get video IDs.
Let's go back to the script and create a function.
Get video IDs.
Now as argument to this function, we will specify the playlist id variable which we get from the first
function.
So let's do some cleaning by first defining the playlist ID variable and moving the definition of this
variable into the double underscore name Rename it with double underscore main part of the script.
And this we can move it to here.
Since we don't need it anymore.
And thus we can also move inside the function.
Now that this is done, let's also do some initialization of some variables.
Let's define a list with the variable name video IDs.
And this will contain all the video IDs of the channel.
For now we will initialize it as an empty list.
Also, we should define a page token variable and initialize it to null.
This will change as we loop through each batch of videos.
We will now encapsulate all of this code in a try except clause, same as we did in the previous function.
Requests dot exceptions dot requests exception.
Then we raise the error if there is one.
Now we need to start a loop that will break only when a certain condition is met.
In our case, this condition will be when there are no page tokens left to go through.
At this point, we will have all the video IDs in our list.
A while true condition will take care of this looping until the condition is satisfied.
In our case, we will know that the loop is broken when in the API response, there won't be a next
page token key.
So inside the API.
Let's write the while true clause.
Now, while the condition is true, let's create a URL variable that is based on the base URL.
So we can say URL is equal to the base URL.
And now let's define a condition in which for the first iteration will be false with regards to the
page token variable, since it is initially set to none.
So now we can write.
If each token, the URL will append the page token part to the main URL.
What this part does is it says that if this is true meaning page token has a non-none value, then to
the URL variable you need to append the page token string inside this f string.
Again, I will repeat this statement is false in the first iteration as we have initialized page token
to none.
So the appending of the URL will not happen and the URL will be simply the base URL.
As soon as the previous function, we get the response check for errors using the raise for status and
if there are no errors, get the response data in JSON format.
So let's write all of this.
Actually we can get from the previous function.
So let's copy all of this.
And paste it here.
As we have seen in the API explorer.
We know that our variable of interest, which is the video ID is in the item scheme.
And if we drill down the JSON file, we need to go through content details to find the get the video
ID value.
To manipulate this response in Python we use dictionaries, and here we can use the get method to get
the value from the items key.
Using a for loop, we can therefore loop through and extract the video IDs and their content details,
and while doing so, appending to the initially empty video ID list.
If we had to write this down, we would have this.
So starting the for loop for item in data dot get, we are going to loop through the items key as a
failsafe with the empty list.
I will soon explain why we will use it.
Now the video ID will be equal to item Tim.
We're going to pass through contents, details, key and the video ID will be in the video ID final
key.
Finally, we will append to the initially empty video IDs list and we will append the video ID inside
the loop.
Now we have used this way of writing the get methods with the empty list.
This one over here because there's a good reason for it.
It prevents the program from crashing if the key items is not found in the data dictionary, and also
it ensures that we always have a list to work with, even if it's empty.
This makes the code more robust at this point.
If we were to run the code, we would extract the first batch of 50 videos from the first page, but
it would stop there.
We now need to specify the next page token where we will get the next batch of videos.
We do this by using the get command, this time specifying the next page token parameter.
So let's continue the code from here to define the page token.
And we'll set it equal to the page token variable.
Finally we just need to break out of the infinite loop.
If there is no next page token this will be in the last page.
And we can do this by writing if not page token.
Which means if it doesn't find it, then we break the loop.
Now, the very last thing that we need to do is to return the video IDs final list.
So we write return video IDs.
Looking at our function, something that I am now just realizing is that in the base URL, we didn't
apply the change of replacing the hardcoded playlist ID.
As you can see, it is still hardcoded.
So let's apply the change right now.
This can easily be done by replacing this with the playlist ID curly brackets, and this playlist ID
is referring the input argument of the function.
Now, in order to execute the function, we need to write it in the double underscore name equals double
underscore main part of the script.
Here we have already defined the playlist id variable, which takes the output from the get playlist
id function.
So the only change that we need to do is to simply write the function name to just defined, referencing
the playlist ID variable.
For the purpose of this demonstration, I will also add a print to showcase the output in the terminal.
So now what is left is to press run and we should get the full list of video IDs in list format.
There appears to be an error and here we are seeing it is something in the URL.
So let's look at the URL URL we be copied from the API explorer, so it should be okay and the change
with it for the playlist id appears to be okay, so maybe there is somewhere here I guess.
So what I forgot to include was the end, which is this one here.
Notice that when we are defining a new variable, we use the At to append this.
You can see here here and even for the key.
So let's include the end.
And hopefully with this change the code will now run.
Let's click save and press run again.
Seems to be executing.
And there we have it.
These are all the video IDs from the Mr. Beast YouTube channel.
So now what I will do is I will remove the print, save and commit this change to GitHub.
And I will see you in part three of the video stats script.