WEBVTT

00:04.160 --> 00:08.440
So now that we have the first piece of the puzzle, which is the playlist ID, we now need to get the

00:08.440 --> 00:10.040
list of unique video IDs.

00:10.240 --> 00:14.880
To do this, we will need to build another function which will be similar in structure, but there are

00:14.880 --> 00:21.280
some changes, the first change being the resource we will use before we were using the channels resource,

00:21.280 --> 00:24.680
which we see in the Russian doll analogy in the bottom left.

00:25.240 --> 00:29.560
But for the video IDs we will need to use the playlist item resource.

00:30.080 --> 00:35.560
Another point that we need to mention is the max results parameter you see in the API docs under Playlist

00:35.560 --> 00:36.600
Item Resource.

00:36.760 --> 00:42.320
This parameter represents the maximum number of videos the YouTube API shows per page.

00:43.040 --> 00:48.040
As you can see, the default value is five and the maximum is 50.

00:48.080 --> 00:53.240
You will see here in the API explorer that if we change this parameter from the default of 5 to 10,

00:53.240 --> 00:56.920
for example, then ten videos will be listed for each page.

00:57.280 --> 01:02.060
We will do a demonstration of this shortly, but before I will tell you why we are mentioning this parameter.

01:02.660 --> 01:04.700
We know that from the previous lectures.

01:04.740 --> 01:09.180
The total number of videos that Mr. Beast currently has is 840.

01:09.580 --> 01:13.940
So it won't be a matter of setting this parameter to eight photo.

01:14.180 --> 01:17.420
And we will do just one API call and get all the videos.

01:18.100 --> 01:24.740
What we can do is set this parameter to the maximum of 50 and loop to each page, effectively making

01:24.740 --> 01:30.580
an API call each time until we have looped through each video that Mr. Beast has on his channel.

01:30.900 --> 01:36.060
Now let's do a quick demonstration, starting off by building the URL using the API explorer.

01:36.260 --> 01:39.700
I have pre-filled the parameters, but we will go over them quickly.

01:40.020 --> 01:44.980
For parts we use again the content detail scheme as this will contain the video ID key.

01:45.500 --> 01:50.620
And for the playlist id we found this using the previous function, which is this parameter.

01:51.940 --> 01:53.180
Now for max results.

01:53.180 --> 01:56.060
If we leave it as a blank, we know that five will be returned.

01:56.460 --> 02:00.820
So let's first try setting into one which should return one video.

02:01.720 --> 02:05.440
Let's copy the URL and paste it into postman.

02:09.480 --> 02:13.600
This time we can use postman inside vs code, so let me pull that up.

02:13.920 --> 02:19.840
So the extension, you can actually get it from the extensions and you can write it in postman.

02:21.680 --> 02:24.840
Make sure you install the official postman extension.

02:26.120 --> 02:29.520
Going back here we do a new HTTP request.

02:32.200 --> 02:33.640
Make sure it's a Get request.

02:34.880 --> 02:40.800
We paste the URL here and we just need to change the API key here.

02:41.000 --> 02:44.840
So this will get it from the dot env.

02:47.720 --> 02:50.560
Let's paste this here and hit send.

02:50.880 --> 02:52.440
So this is the response.

02:52.560 --> 02:55.800
Let's just switch the view a bit so it's more clear.

02:56.280 --> 03:01.860
And here you can see that one video was returned with the video ID of the video.

03:02.540 --> 03:08.700
If we had to change this max results to ten, then ten videos will be listed up to a maximum of 50.

03:09.500 --> 03:13.380
Actually lets try to give this a number greater than 50.

03:13.420 --> 03:16.380
So lets say 60 and see what happens.

03:16.860 --> 03:17.980
So I can press send.

03:20.340 --> 03:23.180
So our response was still given.

03:23.940 --> 03:28.180
But maybe if we scroll down here we see that the results per page is 50.

03:28.500 --> 03:34.780
So even though we set max result to 60, the maximum of 50 is still being shown in this page.

03:34.980 --> 03:39.340
So now that we have seen this in action, lets apply these changes to the Python script.

03:40.540 --> 03:42.340
Lets go back to video stats.

03:42.820 --> 03:46.700
First we can set the global variable max results to 50.

03:47.580 --> 03:51.380
We can set it here max results equals 50.

03:51.820 --> 03:56.860
We can also copy and paste the URL from postman and give it the variable name base URL.

03:57.780 --> 03:58.820
Lets do that right now.

04:02.440 --> 04:06.080
This would be the first function and we will focus on the second function.

04:06.400 --> 04:07.880
But for now paste it here.

04:12.640 --> 04:17.400
Now let's substitute some of the variables so we don't have the values hard coded in the URL.

04:17.840 --> 04:19.760
Let's use f strings.

04:20.520 --> 04:24.040
We can keep the part as the quantum details.

04:24.520 --> 04:25.640
Mixed results.

04:25.840 --> 04:29.960
We can replace with the max results variable of 50.

04:31.720 --> 04:33.960
We can do that like that.

04:34.480 --> 04:37.040
The playlist ID we get from the previous function.

04:37.040 --> 04:42.840
So what we can do for now is to create a variable playlist id and set it equal to the function.

04:48.840 --> 04:54.200
The last change that we need to do is to replace the API key, as we did in the previous URL.

04:55.600 --> 04:57.760
So let's change this API key.

04:58.800 --> 05:01.280
And this is the base URL which we will be using.

05:01.620 --> 05:07.380
Going back to the API explorer, there is this page token parameter, which for the first page of results

05:07.380 --> 05:08.580
we don't need to fill in.

05:09.460 --> 05:12.700
If we have to press execute, we have to switch this off.

05:13.340 --> 05:17.220
Press execute and we get a response.

05:18.020 --> 05:23.420
You will see in this next page token parameter, which refers to the token request parameter that you

05:23.420 --> 05:24.100
see here.

05:25.100 --> 05:31.660
If we had to copy the next page token into page token, we would get the next 50 videos of Mr. Beast's

05:31.660 --> 05:32.180
channel.

05:32.860 --> 05:34.060
Let's do that right now.

05:34.060 --> 05:39.540
So we copy the next page token and paste it into the parameter.

05:39.780 --> 05:44.580
We press execute and now you see the response of the next page.

05:44.780 --> 05:50.020
Here you also see the previous page token which relates to the first videos that came up when we use

05:50.020 --> 05:50.660
postman.

05:51.500 --> 05:55.740
So here we are starting to see that to get the next batch of videos, we need to have the associated

05:55.740 --> 05:57.820
page token for that video batch.

05:58.060 --> 06:03.480
Before we move on to the next step, let me remind you that we are after the video IDs, which we know

06:03.480 --> 06:08.560
is under the content details, and we have seen before and we know it's this one.

06:08.720 --> 06:10.400
So we are interested in this video ID.

06:10.640 --> 06:12.960
So now the question is how will we code this up.

06:13.280 --> 06:17.640
I would say the only tricky part of coding this logic up is to keep in mind that for the first batch

06:17.640 --> 06:21.760
of 50 videos, we don't need to define a page token argument in the URL.

06:22.160 --> 06:27.560
However, in the next iterations we will need to and we will get the page token using the next page

06:27.560 --> 06:29.680
token key which we just went over.

06:30.280 --> 06:32.560
Knowing this, let's continue building the syntax.

06:32.560 --> 06:37.560
And we can do this by defining the second function which we can call get video IDs.

06:37.680 --> 06:41.600
Let's go back to the script and create a function.

06:43.520 --> 06:46.680
Get video IDs.

06:47.600 --> 06:52.600
Now as argument to this function, we will specify the playlist id variable which we get from the first

06:52.600 --> 06:53.200
function.

06:54.240 --> 06:59.000
So let's do some cleaning by first defining the playlist ID variable and moving the definition of this

06:59.000 --> 07:04.460
variable into the double underscore name Rename it with double underscore main part of the script.

07:07.740 --> 07:10.580
And this we can move it to here.

07:14.540 --> 07:15.660
Since we don't need it anymore.

07:17.420 --> 07:20.340
And thus we can also move inside the function.

07:25.220 --> 07:29.140
Now that this is done, let's also do some initialization of some variables.

07:29.780 --> 07:33.860
Let's define a list with the variable name video IDs.

07:36.220 --> 07:40.100
And this will contain all the video IDs of the channel.

07:40.220 --> 07:42.540
For now we will initialize it as an empty list.

07:42.740 --> 07:46.660
Also, we should define a page token variable and initialize it to null.

07:51.540 --> 07:54.060
This will change as we loop through each batch of videos.

07:54.220 --> 08:01.260
We will now encapsulate all of this code in a try except clause, same as we did in the previous function.

08:03.290 --> 08:08.810
Requests dot exceptions dot requests exception.

08:10.850 --> 08:13.650
Then we raise the error if there is one.

08:13.770 --> 08:17.690
Now we need to start a loop that will break only when a certain condition is met.

08:17.850 --> 08:22.610
In our case, this condition will be when there are no page tokens left to go through.

08:23.170 --> 08:26.810
At this point, we will have all the video IDs in our list.

08:27.730 --> 08:31.850
A while true condition will take care of this looping until the condition is satisfied.

08:32.530 --> 08:37.890
In our case, we will know that the loop is broken when in the API response, there won't be a next

08:37.890 --> 08:38.850
page token key.

08:39.650 --> 08:40.810
So inside the API.

08:41.530 --> 08:45.890
Let's write the while true clause.

08:49.210 --> 08:55.050
Now, while the condition is true, let's create a URL variable that is based on the base URL.

08:55.530 --> 08:59.650
So we can say URL is equal to the base URL.

08:59.810 --> 09:04.550
And now let's define a condition in which for the first iteration will be false with regards to the

09:04.550 --> 09:08.270
page token variable, since it is initially set to none.

09:08.470 --> 09:09.950
So now we can write.

09:10.230 --> 09:19.950
If each token, the URL will append the page token part to the main URL.

09:24.630 --> 09:31.590
What this part does is it says that if this is true meaning page token has a non-none value, then to

09:31.630 --> 09:36.750
the URL variable you need to append the page token string inside this f string.

09:36.790 --> 09:41.510
Again, I will repeat this statement is false in the first iteration as we have initialized page token

09:41.510 --> 09:42.030
to none.

09:42.350 --> 09:47.030
So the appending of the URL will not happen and the URL will be simply the base URL.

09:47.510 --> 09:53.510
As soon as the previous function, we get the response check for errors using the raise for status and

09:53.510 --> 09:56.990
if there are no errors, get the response data in JSON format.

09:57.670 --> 09:59.070
So let's write all of this.

09:59.750 --> 10:02.970
Actually we can get from the previous function.

10:03.410 --> 10:05.290
So let's copy all of this.

10:08.410 --> 10:10.490
And paste it here.

10:10.530 --> 10:12.690
As we have seen in the API explorer.

10:12.970 --> 10:17.770
We know that our variable of interest, which is the video ID is in the item scheme.

10:17.770 --> 10:22.890
And if we drill down the JSON file, we need to go through content details to find the get the video

10:22.930 --> 10:24.050
ID value.

10:24.090 --> 10:29.610
To manipulate this response in Python we use dictionaries, and here we can use the get method to get

10:29.610 --> 10:31.210
the value from the items key.

10:31.330 --> 10:36.450
Using a for loop, we can therefore loop through and extract the video IDs and their content details,

10:36.850 --> 10:40.690
and while doing so, appending to the initially empty video ID list.

10:40.850 --> 10:43.170
If we had to write this down, we would have this.

10:43.570 --> 10:53.530
So starting the for loop for item in data dot get, we are going to loop through the items key as a

10:53.530 --> 10:55.130
failsafe with the empty list.

10:55.130 --> 10:57.250
I will soon explain why we will use it.

10:58.170 --> 11:01.630
Now the video ID will be equal to item Tim.

11:02.470 --> 11:10.950
We're going to pass through contents, details, key and the video ID will be in the video ID final

11:10.950 --> 11:11.270
key.

11:13.590 --> 11:21.630
Finally, we will append to the initially empty video IDs list and we will append the video ID inside

11:21.630 --> 11:22.110
the loop.

11:22.630 --> 11:26.870
Now we have used this way of writing the get methods with the empty list.

11:27.070 --> 11:30.110
This one over here because there's a good reason for it.

11:30.590 --> 11:37.350
It prevents the program from crashing if the key items is not found in the data dictionary, and also

11:37.350 --> 11:41.310
it ensures that we always have a list to work with, even if it's empty.

11:41.830 --> 11:44.270
This makes the code more robust at this point.

11:44.270 --> 11:49.430
If we were to run the code, we would extract the first batch of 50 videos from the first page, but

11:49.430 --> 11:50.430
it would stop there.

11:51.030 --> 11:56.070
We now need to specify the next page token where we will get the next batch of videos.

11:56.110 --> 12:01.390
We do this by using the get command, this time specifying the next page token parameter.

12:02.450 --> 12:06.290
So let's continue the code from here to define the page token.

12:07.450 --> 12:11.930
And we'll set it equal to the page token variable.

12:17.130 --> 12:19.850
Finally we just need to break out of the infinite loop.

12:19.850 --> 12:23.010
If there is no next page token this will be in the last page.

12:23.410 --> 12:27.610
And we can do this by writing if not page token.

12:28.130 --> 12:30.810
Which means if it doesn't find it, then we break the loop.

12:31.450 --> 12:38.050
Now, the very last thing that we need to do is to return the video IDs final list.

12:38.530 --> 12:41.850
So we write return video IDs.

12:42.050 --> 12:47.490
Looking at our function, something that I am now just realizing is that in the base URL, we didn't

12:47.490 --> 12:51.730
apply the change of replacing the hardcoded playlist ID.

12:52.290 --> 12:55.210
As you can see, it is still hardcoded.

12:55.730 --> 12:57.450
So let's apply the change right now.

12:57.650 --> 13:05.670
This can easily be done by replacing this with the playlist ID curly brackets, and this playlist ID

13:05.670 --> 13:08.270
is referring the input argument of the function.

13:08.670 --> 13:13.870
Now, in order to execute the function, we need to write it in the double underscore name equals double

13:13.870 --> 13:15.950
underscore main part of the script.

13:17.950 --> 13:23.190
Here we have already defined the playlist id variable, which takes the output from the get playlist

13:23.190 --> 13:24.430
id function.

13:24.430 --> 13:32.630
So the only change that we need to do is to simply write the function name to just defined, referencing

13:32.670 --> 13:34.590
the playlist ID variable.

13:35.950 --> 13:41.830
For the purpose of this demonstration, I will also add a print to showcase the output in the terminal.

13:43.830 --> 13:49.910
So now what is left is to press run and we should get the full list of video IDs in list format.

13:51.870 --> 13:56.430
There appears to be an error and here we are seeing it is something in the URL.

13:58.270 --> 14:08.370
So let's look at the URL URL we be copied from the API explorer, so it should be okay and the change

14:08.370 --> 14:15.250
with it for the playlist id appears to be okay, so maybe there is somewhere here I guess.

14:15.290 --> 14:19.650
So what I forgot to include was the end, which is this one here.

14:19.690 --> 14:26.610
Notice that when we are defining a new variable, we use the At to append this.

14:26.610 --> 14:29.050
You can see here here and even for the key.

14:29.770 --> 14:31.970
So let's include the end.

14:32.450 --> 14:35.730
And hopefully with this change the code will now run.

14:35.930 --> 14:39.170
Let's click save and press run again.

14:43.330 --> 14:44.450
Seems to be executing.

14:44.450 --> 14:45.570
And there we have it.

14:46.330 --> 14:49.410
These are all the video IDs from the Mr. Beast YouTube channel.

14:49.730 --> 14:56.010
So now what I will do is I will remove the print, save and commit this change to GitHub.

14:56.010 --> 14:59.210
And I will see you in part three of the video stats script.
