1
00:00:00,450 --> 00:00:07,170
Hi, welcome to this section, we're going to work with rest APIs through all these videos.

2
00:00:07,710 --> 00:00:12,450
Specifically, we're going to access various APIs from Python.

3
00:00:12,930 --> 00:00:18,120
And then later on, we're going to write our own API also with Python.

4
00:00:19,240 --> 00:00:25,210
So Python can both interact with APIs, but also create APIs.

5
00:00:25,990 --> 00:00:27,260
Now what is an API?

6
00:00:27,280 --> 00:00:33,310
This is what you learn in this video, and we're also going to look at an example of working with an

7
00:00:33,310 --> 00:00:34,990
API from Python.

8
00:00:35,980 --> 00:00:42,940
So specifically, we're going to work with the rest APIs, which is the most common type of API that

9
00:00:42,940 --> 00:00:43,990
you will encounter.

10
00:00:44,680 --> 00:00:47,080
So what is a race API?

11
00:00:47,530 --> 00:00:49,060
The boring part first.

12
00:00:49,540 --> 00:00:57,160
Well, API stands for representational state transfer application programming interface, but don't

13
00:00:57,160 --> 00:01:02,380
get intimidated by the name because I'll explain you what API is.

14
00:01:02,500 --> 00:01:03,550
We're using an example.

15
00:01:03,970 --> 00:01:05,080
Everything will be clear.

16
00:01:05,590 --> 00:01:11,890
No, the phone port to make you really understand what's a rest API is.

17
00:01:12,220 --> 00:01:15,370
First, let's look at the normal web page, such as this one in here.

18
00:01:15,370 --> 00:01:17,830
It's a space news website.

19
00:01:18,340 --> 00:01:24,760
So there's a server somewhere that hosts a web app, and this web app serves this website.

20
00:01:25,060 --> 00:01:27,730
These web pages to users like us.

21
00:01:28,420 --> 00:01:34,690
So when we visit this web page, we get some results laid out in a friendly, easy to read format,

22
00:01:34,870 --> 00:01:38,110
you know, nice font sizes and paragraphs.

23
00:01:38,650 --> 00:01:44,560
Now this is easy to read by us humans, but not by computers.

24
00:01:45,100 --> 00:01:52,960
So the language that has built such a readable format for humans is age HTML.

25
00:01:53,500 --> 00:01:57,220
So the back of this web page stands HTML language.

26
00:01:57,880 --> 00:01:59,940
Other browser renders that H.M..

27
00:02:00,400 --> 00:02:07,600
No, the problem is, if you want to read this web page, it's OK to read the dots on the content information

28
00:02:07,990 --> 00:02:15,310
with a computer program such as a Python script in order to get this data and do some other things with

29
00:02:15,310 --> 00:02:17,950
those news or other data.

30
00:02:18,400 --> 00:02:21,700
Then in that case, you could use a web scraping.

31
00:02:22,090 --> 00:02:24,670
Both web scraping is often tough.

32
00:02:25,570 --> 00:02:29,980
That's a lot of work involved because you have to pass all these information.

33
00:02:30,790 --> 00:02:33,820
And here is where rest APIs join the game.

34
00:02:34,450 --> 00:02:43,060
Rest APIs are web applications that don't serve web pages in a stable form, but they serve them in

35
00:02:43,060 --> 00:02:48,310
other formats that are meant to be interpreted by applications easily.

36
00:02:48,940 --> 00:02:55,810
So think of an API as a way for two applications to communicate.

37
00:02:55,990 --> 00:03:03,550
So on one side, you have one application, for example, an application that has news, and on the

38
00:03:03,550 --> 00:03:05,500
other hand, you have the application.

39
00:03:05,500 --> 00:03:06,430
That's you, right?

40
00:03:06,820 --> 00:03:13,510
With Python, in our case, that's Python application will access the data from.

41
00:03:14,580 --> 00:03:16,110
The news application.

42
00:03:17,810 --> 00:03:21,800
So let's look now at an API example.

43
00:03:22,580 --> 00:03:28,910
The application here, which is on the server, we don't see the application itself, but what we see

44
00:03:28,910 --> 00:03:32,270
is the dots of this application is giving to us.

45
00:03:32,780 --> 00:03:36,800
So this data now is friendly for computers.

46
00:03:37,160 --> 00:03:45,050
It's well-structured, it's in a Python dictionary like format, and it's called Jasons versus a JSON

47
00:03:45,050 --> 00:03:51,980
format, and Jason is the usual formats that is served by APIs.

48
00:03:52,250 --> 00:03:57,410
So for humans, we had HTML and for computers we have Jason.

49
00:03:58,310 --> 00:04:04,760
So this particular site's called news API dot org lets us access.

50
00:04:04,760 --> 00:04:06,350
These dots are in this format.

51
00:04:06,470 --> 00:04:13,340
Now we're accessing these news from a browser, but we can do that from.

52
00:04:14,670 --> 00:04:22,260
Python as well, so let's jump into Python, so I'm going to open a reptile to have an empty python

53
00:04:22,260 --> 00:04:22,680
file.

54
00:04:26,200 --> 00:04:32,440
And so through our process, we access this Web page using our browser in Python, we need to access

55
00:04:32,440 --> 00:04:35,770
it's using requests, this library.

56
00:04:37,190 --> 00:04:41,770
And then you create a request using requests that get.

57
00:04:45,040 --> 00:04:49,150
And then here you enter, that's euro, that's you have entered in your browser.

58
00:04:49,450 --> 00:04:53,320
You can get this euro from the election resources.

59
00:04:53,350 --> 00:04:55,390
There's a there's a euro atocha there.

60
00:04:55,390 --> 00:05:01,780
You can click that it will open in your browser, then copy it and put it inside this method.

61
00:05:06,650 --> 00:05:07,760
And then the contents.

62
00:05:08,900 --> 00:05:10,730
Will be our Jason.

63
00:05:12,880 --> 00:05:21,250
And then if you print the content, you should get the same content that you got in here.

64
00:05:22,790 --> 00:05:26,840
Well, this is in the form of a dictionary, it has keys and values.

65
00:05:27,110 --> 00:05:34,880
So, for example, a status quo here has a value of OK, which is some information about the requests

66
00:05:35,150 --> 00:05:36,020
that we made.

67
00:05:36,530 --> 00:05:41,150
And then we have total results, so many news articles we have.

68
00:05:41,540 --> 00:05:44,680
And then we have this articles key.

69
00:05:45,110 --> 00:05:48,140
So now we can get to that article's key.

70
00:05:49,600 --> 00:06:00,190
By using this syntax, since content is in the form of a dictionary, so print type.

71
00:06:01,230 --> 00:06:01,920
Content.

72
00:06:10,540 --> 00:06:12,790
Let me comment that out.

73
00:06:14,170 --> 00:06:23,380
So it's a dictionary content, therefore we can use such syntax to get the value of the articles only.

74
00:06:27,700 --> 00:06:37,030
So the article's key is connected to a list as valley and that lists as dictionaries also.

75
00:06:37,510 --> 00:06:44,080
So it's nice that dots on boats computers can understand it's if you follow the correct syntax in your

76
00:06:44,080 --> 00:06:45,100
programming language.

77
00:06:46,240 --> 00:06:52,930
So if we follow this, we would say we want the item with index one, which is this dictionary.

78
00:06:53,440 --> 00:06:57,190
So let's continue this sort of index zero.

79
00:06:57,490 --> 00:06:58,780
It's the first item.

80
00:07:00,210 --> 00:07:03,960
This warning here that we is perhaps the title.

81
00:07:08,490 --> 00:07:09,210
And Ron.

82
00:07:11,960 --> 00:07:15,470
And so we got the title of the first news article.

83
00:07:18,010 --> 00:07:23,680
So that's how our program is communicating with with this Web app, with this API.

84
00:07:23,920 --> 00:07:33,520
So the API app that is horses in this server is serving data to our application, to our python application

85
00:07:33,520 --> 00:07:33,820
here.

86
00:07:34,000 --> 00:07:37,290
So now we get those data and then we can do whatever we want with them.

87
00:07:38,950 --> 00:07:40,660
In this case, we got the title.

88
00:07:40,810 --> 00:07:46,060
You can also get the description so you can change that to this encryption.

89
00:07:47,050 --> 00:07:47,440
Run.

90
00:07:50,920 --> 00:07:58,990
And so you have some more information there, and you can explore more keys here to access those.

91
00:08:00,070 --> 00:08:09,220
So the way our Python application is communicating with this API is through URLs through URL parameters

92
00:08:09,220 --> 00:08:10,240
to be exact.

93
00:08:10,450 --> 00:08:18,280
Because if you look closely to the URL in here, you have some parameters with their values, you URL

94
00:08:18,280 --> 00:08:19,000
parameters.

95
00:08:19,180 --> 00:08:26,860
For example, this is a sort of parameters as Q and title, it means what kind of news do you want?

96
00:08:26,860 --> 00:08:32,500
In this case, we have stock markets as the keywords for the news we want to have.

97
00:08:32,740 --> 00:08:35,539
So you can change that to something else, for example.

98
00:08:35,559 --> 00:08:37,419
But you know it.

99
00:08:39,620 --> 00:08:48,830
Percentage 20, which stands for a space and then you say states, so United's percentage 20 states

100
00:08:49,040 --> 00:08:49,420
run.

101
00:08:52,090 --> 00:08:57,900
And so you get the first news description for that query.

102
00:08:58,120 --> 00:09:00,520
And then you have other providers such as from.

103
00:09:00,940 --> 00:09:02,560
So this is the date.

104
00:09:03,130 --> 00:09:10,060
Twenty twenty two to twenty seven, you can change that to 26 and you get that off for another period.

105
00:09:11,690 --> 00:09:19,490
If there are news for that period and then you have soared by popularity language in English and each

106
00:09:19,490 --> 00:09:26,390
parameter, as you see, it's divided by this and symbol and then you have the API key here.

107
00:09:27,430 --> 00:09:33,410
The API key is something you need to get from news API.

108
00:09:33,430 --> 00:09:35,630
That's all from that website.

109
00:09:36,170 --> 00:09:42,200
Then you place that API key in here to make it possible to get data from this API.

110
00:09:43,470 --> 00:09:48,180
So that was an example of working with APIs via Python.

111
00:09:48,720 --> 00:09:55,950
Now if you're interested about news, this particular news API, how to get news from this API, I'm

112
00:09:55,950 --> 00:10:00,030
going to elaborate on this script further in the next video.

113
00:10:00,300 --> 00:10:03,480
So to improve it a little bit to make it more dynamic.

114
00:10:04,200 --> 00:10:06,930
Thank you for following this, and I'll talk to you in the next video.

