1
00:00:00,990 --> 00:00:01,350
All right.

2
00:00:01,350 --> 00:00:06,350
So the first thing we need to do that we learnt in order to work with APIs is to

3
00:00:06,510 --> 00:00:08,910
import the requests module.

4
00:00:09,390 --> 00:00:11,520
And once we've imported that,

5
00:00:11,640 --> 00:00:16,640
we need to get a response from the URL that we saw over here.

6
00:00:18,270 --> 00:00:22,440
As I mentioned, the end point is everything that's before the question mark.

7
00:00:22,890 --> 00:00:26,760
So let's put that in as the URL for our get request.

8
00:00:27,270 --> 00:00:30,180
And then in addition, we need to add some parameters.

9
00:00:30,690 --> 00:00:35,040
So let's make our parameters a Python dictionary,

10
00:00:35,760 --> 00:00:40,110
and the key must match the key that's shown here

11
00:00:40,500 --> 00:00:43,830
and the value has to match the value that's given here as well.

12
00:00:44,250 --> 00:00:46,770
So we've got two keys; amount and type.

13
00:00:47,220 --> 00:00:49,680
So these are both going to be strings,

14
00:00:49,710 --> 00:00:53,340
but the values can be numbers if it is appropriate.

15
00:00:53,850 --> 00:00:58,170
So amount sets the number of questions we want back from the API,

16
00:00:58,770 --> 00:01:03,270
and then the type sets the type of questions that we want. So in our case,

17
00:01:03,300 --> 00:01:08,220
we want the boolean data type that matches this exact entry.

18
00:01:08,520 --> 00:01:11,160
So that way we get back true-false questions.

19
00:01:11,940 --> 00:01:16,410
Now that we've done that we can use this parameters dictionary as the

20
00:01:16,410 --> 00:01:17,243
params,

21
00:01:17,700 --> 00:01:22,700
and then we want our response to raise an exception if there were any errors.

22
00:01:23,460 --> 00:01:27,750
And then we're going to get our response to give us the data using the JSON

23
00:01:27,750 --> 00:01:31,290
method. If this is our data,

24
00:01:31,320 --> 00:01:34,650
let's go ahead and print it and see what it looks like.

25
00:01:35,100 --> 00:01:40,100
We can run this particular file, data.py, by right-clicking and just saying

26
00:01:40,170 --> 00:01:44,940
run data. And once it's done, you can see we printed out the data

27
00:01:45,450 --> 00:01:50,450
and the part that we're interested in is the value of this key results,

28
00:01:51,300 --> 00:01:55,050
because it contains the list of questions.

29
00:01:55,590 --> 00:01:59,760
That's the only part we're interested in. So in order to tap into that,

30
00:02:00,060 --> 00:02:03,750
we have to add a set of square brackets and then pass in the key

31
00:02:04,260 --> 00:02:08,100
which is called results. So let's put that as the key.

32
00:02:08,400 --> 00:02:13,400
And now we're fetching all of the items that are the results from this data

33
00:02:14,430 --> 00:02:19,140
object that we get back. So now, if I run this again, you can see

34
00:02:19,140 --> 00:02:22,560
we now have a list of question objects

35
00:02:22,890 --> 00:02:25,080
which each contains a dictionary

36
00:02:25,350 --> 00:02:29,520
and it looks now pretty much identical to this structure that we had before.

37
00:02:30,300 --> 00:02:34,890
Now, instead of printing this, we can save it as the question_data.

38
00:02:36,450 --> 00:02:40,200
So now this question_data and has replaced this question_data,

39
00:02:40,620 --> 00:02:41,760
and as I mentioned,

40
00:02:41,790 --> 00:02:46,790
we can run our main.py and it should now work just as it did before.

41
00:02:49,200 --> 00:02:54,200
But this time it's going to use questions that came from our API.

42
00:02:54,840 --> 00:02:58,890
The cold war ended with Joseph Stalin's death, true or false.

43
00:02:59,170 --> 00:03:03,430
I think that's actually false. But notice how

44
00:03:03,430 --> 00:03:05,830
when we get the data back from the API,

45
00:03:06,160 --> 00:03:11,160
it's formatted in such a way that we've got certain symbols that have been

46
00:03:11,290 --> 00:03:14,290
encoded using this particular format

47
00:03:14,650 --> 00:03:19,650
because all of this is meant to represent a single apostrophe. In the next

48
00:03:19,900 --> 00:03:20,350
lesson,

49
00:03:20,350 --> 00:03:25,120
I'm going to show you how we can format the text that we get back to turn this

50
00:03:25,120 --> 00:03:28,030
encoding into the actual characters

51
00:03:28,180 --> 00:03:32,650
that it should be. For all of that and more, I'll see you on the next lesson.

