1
00:00:00,360 --> 00:00:03,840
Instructor: Now, whenever we start using a new API,

2
00:00:03,840 --> 00:00:07,950
the most important thing is to read the documentation.

3
00:00:07,950 --> 00:00:10,860
I know it's really tempting to skip over the instructions

4
00:00:10,860 --> 00:00:12,750
and just get right into it.

5
00:00:12,750 --> 00:00:15,420
I'm also the sort of person who buys a lawnmower

6
00:00:15,420 --> 00:00:18,090
and throws away the instruction manual,

7
00:00:18,090 --> 00:00:20,850
and then two days later something breaks

8
00:00:20,850 --> 00:00:23,760
and I just want to hurt myself

9
00:00:23,760 --> 00:00:24,960
because it would be so much easier

10
00:00:24,960 --> 00:00:27,810
if I had actually read the instruction manual.

11
00:00:27,810 --> 00:00:30,090
But in this case, it is really important.

12
00:00:30,090 --> 00:00:33,420
So let's head over to openweathermap.org together

13
00:00:33,420 --> 00:00:36,270
and let's go to the API section

14
00:00:36,270 --> 00:00:39,480
to see the API documentation.

15
00:00:39,480 --> 00:00:40,500
As you can see,

16
00:00:40,500 --> 00:00:44,400
this API provider actually has several products.

17
00:00:44,400 --> 00:00:47,370
So you could look at the current weather data,

18
00:00:47,370 --> 00:00:52,370
the hourly forecast, the One Call API, the daily forecast,

19
00:00:53,220 --> 00:00:55,530
and a whole bunch of other things.

20
00:00:55,530 --> 00:00:58,470
So each of these have their own endpoints.

21
00:00:58,470 --> 00:01:00,390
And if we take a look at the API doc,

22
00:01:00,390 --> 00:01:02,160
you can see this first one,

23
00:01:02,160 --> 00:01:07,160
the API is api.openweathermap.org/data/2.5/weather.

24
00:01:09,840 --> 00:01:11,430
That's the endpoint.

25
00:01:11,430 --> 00:01:14,070
Whereas the hourly forecast

26
00:01:14,070 --> 00:01:19,070
is pro.openweathermap.org/data/2.5/ forecast/hourly.

27
00:01:22,020 --> 00:01:24,510
So depending on what it is that we need,

28
00:01:24,510 --> 00:01:28,830
we can tap into each of these different APIs.

29
00:01:28,830 --> 00:01:30,540
Let's start off with an easy one.

30
00:01:30,540 --> 00:01:32,730
Let's try and get the current weather data

31
00:01:32,730 --> 00:01:34,770
for the city that we're in.

32
00:01:34,770 --> 00:01:37,320
You can see that this part is the endpoint,

33
00:01:37,320 --> 00:01:40,320
so the URL that we're going to tap into.

34
00:01:40,320 --> 00:01:42,900
And then we have some parameters.

35
00:01:42,900 --> 00:01:47,660
The parameter where we can specify the city name is q.

36
00:01:48,540 --> 00:01:50,700
So that's the name of the parameter

37
00:01:50,700 --> 00:01:53,490
and then we have to provide a value.

38
00:01:53,490 --> 00:01:55,680
Now, in addition, there's another parameter,

39
00:01:55,680 --> 00:02:00,060
which we have to include with all of the API calls

40
00:02:00,060 --> 00:02:04,140
to this particular service, and that is an appid.

41
00:02:04,140 --> 00:02:06,630
We have to get hold of an API key

42
00:02:06,630 --> 00:02:09,360
in order to use this service.

43
00:02:09,360 --> 00:02:11,490
And they show you an example here.

44
00:02:11,490 --> 00:02:13,530
If we click on this URL,

45
00:02:13,530 --> 00:02:17,610
it takes us to samples.openweathermap.org.

46
00:02:17,610 --> 00:02:19,770
And this is just some sample data

47
00:02:19,770 --> 00:02:23,130
where they provided a sample appid.

48
00:02:23,130 --> 00:02:26,160
Now, we can get our own appid for free

49
00:02:26,160 --> 00:02:29,580
by simply signing up to this weather service.

50
00:02:29,580 --> 00:02:32,280
And once you register and sign in,

51
00:02:32,280 --> 00:02:34,350
this is the page that you see.

52
00:02:34,350 --> 00:02:38,010
So we can now go into this section called API keys

53
00:02:38,010 --> 00:02:41,430
and grab our API key here.

54
00:02:41,430 --> 00:02:43,860
So you can create as many keys as you want,

55
00:02:43,860 --> 00:02:45,600
and you can provide a name

56
00:02:45,600 --> 00:02:49,680
that's associated with the final usage for that key.

57
00:02:49,680 --> 00:02:51,360
So I've named it Python.

58
00:02:51,360 --> 00:02:54,390
And now I'm going to copy this API key

59
00:02:54,390 --> 00:02:57,390
and I'm gonna place it into my project.

60
00:02:57,390 --> 00:03:00,270
So I've started a new project called rain_alert,

61
00:03:00,270 --> 00:03:05,160
and inside the main.py, I'm going to create a API key,

62
00:03:05,160 --> 00:03:09,390
and I'm going to save that key that I copied

63
00:03:09,390 --> 00:03:12,900
into a set of double quotes as a string.

64
00:03:12,900 --> 00:03:15,360
Now, once I've got my API key,

65
00:03:15,360 --> 00:03:18,153
I can actually test out this API call.

66
00:03:20,340 --> 00:03:24,150
So I can replace this part with my city name,

67
00:03:24,150 --> 00:03:28,800
which is London, and then I can add my country afterwards

68
00:03:28,800 --> 00:03:31,290
just to make sure that it goes to London, U.K.,

69
00:03:31,290 --> 00:03:34,860
and not London, Ontario, or somewhere else.

70
00:03:34,860 --> 00:03:37,500
And then finally, I'm gonna paste in the API key

71
00:03:37,500 --> 00:03:39,900
that I copied over just now.

72
00:03:39,900 --> 00:03:42,180
And now, when I hit Enter,

73
00:03:42,180 --> 00:03:44,610
and as long as my API key is valid,

74
00:03:44,610 --> 00:03:49,530
I should get a result in the format of my weather data.

75
00:03:49,530 --> 00:03:53,190
Now, I want you to make sure that your API key works as well

76
00:03:53,190 --> 00:03:55,290
by using the same method,

77
00:03:55,290 --> 00:03:59,010
But remember that you have to get hold of your own API key

78
00:03:59,010 --> 00:04:03,600
by signing up to OpenWeatherMap if you want this to work.

79
00:04:03,600 --> 00:04:07,830
Make sure that you replace this part with your own API key.

80
00:04:07,830 --> 00:04:10,050
Otherwise, it's not gonna work.

81
00:04:10,050 --> 00:04:13,424
Now that we've seen that this particular API call

82
00:04:13,424 --> 00:04:15,420
actually works,

83
00:04:15,420 --> 00:04:19,860
let's see what happens if we provide the wrong API key.

84
00:04:19,860 --> 00:04:24,120
So if I just make up an API key and hit Enter,

85
00:04:24,120 --> 00:04:26,820
you can see we get a code back,

86
00:04:26,820 --> 00:04:30,390
which is an HTTP code called 401.

87
00:04:30,390 --> 00:04:31,890
And we can also get a message,

88
00:04:31,890 --> 00:04:35,010
telling us that that's an invalid API key

89
00:04:35,010 --> 00:04:38,190
and we can go to this URL for more info.

90
00:04:38,190 --> 00:04:40,320
Now, remember, in previous lessons,

91
00:04:40,320 --> 00:04:43,290
I mentioned that HTTP status codes

92
00:04:43,290 --> 00:04:45,120
are the codes that we get back

93
00:04:45,120 --> 00:04:47,760
when we try to talk to another server.

94
00:04:47,760 --> 00:04:51,390
So in this case, we got the 401 code back,

95
00:04:51,390 --> 00:04:53,700
which means unauthorized.

96
00:04:53,700 --> 00:04:55,320
And when you get this code back

97
00:04:55,320 --> 00:04:56,730
and you're working with an API,

98
00:04:56,730 --> 00:05:00,180
it usually means that your API key

99
00:05:00,180 --> 00:05:03,390
or your way of authorizing with the API key

100
00:05:03,390 --> 00:05:05,730
is somehow not working.

101
00:05:05,730 --> 00:05:08,253
So that is the thing that you should examine.

102
00:05:09,570 --> 00:05:13,200
We can see that for this particular API provider,

103
00:05:13,200 --> 00:05:16,650
we have to authenticate ourselves with their server

104
00:05:16,650 --> 00:05:20,883
by providing the API key as a parameter.

105
00:05:22,170 --> 00:05:24,450
So now I want to give you a challenge.

106
00:05:24,450 --> 00:05:27,840
I want you to go to OpenWeatherMaps API docs

107
00:05:27,840 --> 00:05:30,840
for their five day weather forecast.

108
00:05:30,840 --> 00:05:32,190
The five day weather forecast

109
00:05:32,190 --> 00:05:35,520
will give you the weather forecast every three hours

110
00:05:35,520 --> 00:05:38,160
based on the location that you provide.

111
00:05:38,160 --> 00:05:41,040
What OpenWeatherMap means by a three-hour forecast

112
00:05:41,040 --> 00:05:44,970
is that they will give you eight weather forecasts per day.

113
00:05:44,970 --> 00:05:47,220
The earliest forecast would be at 3:00 AM,

114
00:05:47,220 --> 00:05:51,690
the next at 6:00 AM, and the one after that is for 9:00 AM,

115
00:05:51,690 --> 00:05:55,470
then one at midday, and so on until midnight.

116
00:05:55,470 --> 00:05:59,430
My challenge to you is to make an API request from PyCharm

117
00:05:59,430 --> 00:06:01,740
using the five day weather forecast

118
00:06:01,740 --> 00:06:03,450
and locate the weather forecast

119
00:06:03,450 --> 00:06:06,000
in the JSON response that you get back.

120
00:06:06,000 --> 00:06:08,010
Now I could tell you exactly what to do,

121
00:06:08,010 --> 00:06:10,650
but it's really good to get some practice

122
00:06:10,650 --> 00:06:12,870
reading API documentation

123
00:06:12,870 --> 00:06:14,430
because after all, in the future,

124
00:06:14,430 --> 00:06:16,770
you're gonna be using loads of different APIs,

125
00:06:16,770 --> 00:06:18,090
and they all have different ways

126
00:06:18,090 --> 00:06:20,253
of working with their services.

127
00:06:21,240 --> 00:06:22,560
Have a look at this page.

128
00:06:22,560 --> 00:06:24,900
I'll link to it in the course resources,

129
00:06:24,900 --> 00:06:28,320
and I want you to implement this API call

130
00:06:28,320 --> 00:06:30,870
by providing a latitude and latitude

131
00:06:30,870 --> 00:06:33,210
of where you currently are,

132
00:06:33,210 --> 00:06:37,020
and make sure that you add your personal appid

133
00:06:37,020 --> 00:06:39,660
by getting hold of your API key.

134
00:06:39,660 --> 00:06:40,860
Now, remember, you can get

135
00:06:40,860 --> 00:06:42,870
your current latitude and longitude

136
00:06:42,870 --> 00:06:47,040
just by going to latlong.net, and then typing a place name,

137
00:06:47,040 --> 00:06:48,720
and then clicking Find.

138
00:06:48,720 --> 00:06:51,090
Don't worry if it tells you a bot is detected,

139
00:06:51,090 --> 00:06:53,940
just go back to the website after a little while

140
00:06:53,940 --> 00:06:55,320
and try again.

141
00:06:55,320 --> 00:06:56,153
At some point,

142
00:06:56,153 --> 00:06:58,980
it should show you your latitude and longitude,

143
00:06:58,980 --> 00:07:01,740
which you can then plug in as a parameter

144
00:07:01,740 --> 00:07:04,200
to this particular API call.

145
00:07:04,200 --> 00:07:06,960
So if you manage to work through all the lessons yesterday,

146
00:07:06,960 --> 00:07:09,240
then this should be a piece of cake for you.

147
00:07:09,240 --> 00:07:10,990
Pause the video and give that a go.

148
00:07:17,430 --> 00:07:19,560
All right, so if that was hard at all,

149
00:07:19,560 --> 00:07:22,560
make sure that you've not skipped yesterday's lessons

150
00:07:22,560 --> 00:07:27,150
because I go through in detail how to hit up a API endpoint,

151
00:07:27,150 --> 00:07:28,770
how to provide parameters,

152
00:07:28,770 --> 00:07:32,160
and how to essentially complete this challenge.

153
00:07:32,160 --> 00:07:33,840
But hopefully you managed all right,

154
00:07:33,840 --> 00:07:36,930
and you're just here to check the solution.

155
00:07:36,930 --> 00:07:38,550
As always, we're gonna start off

156
00:07:38,550 --> 00:07:41,643
by importing our requests package.

157
00:07:42,750 --> 00:07:46,590
Remember that if you are creating this file from scratch

158
00:07:46,590 --> 00:07:49,890
and you haven't downloaded it from a zip file,

159
00:07:49,890 --> 00:07:52,950
then you'll have to install this module manually.

160
00:07:52,950 --> 00:07:54,990
So if you click on it in PyCharm,

161
00:07:54,990 --> 00:07:56,460
you get a little red light bulb.

162
00:07:56,460 --> 00:07:59,348
And then when you click on the down arrow,

163
00:07:59,348 --> 00:08:02,580
you can install that package, simple.

164
00:08:02,580 --> 00:08:05,280
Now, once you've got requests installed,

165
00:08:05,280 --> 00:08:09,930
we can use the GET method to pass in our endpoint.

166
00:08:09,930 --> 00:08:12,520
Now the OpenWeatherMap endpoint

167
00:08:14,100 --> 00:08:16,980
is the one that we see in the API docs.

168
00:08:16,980 --> 00:08:18,840
So it's everything up to

169
00:08:18,840 --> 00:08:20,850
but not including the question mark.

170
00:08:20,850 --> 00:08:22,650
So all of this.

171
00:08:22,650 --> 00:08:26,550
And we can paste that in and save it as a string.

172
00:08:26,550 --> 00:08:29,340
That is what we wanna make our GET request to.

173
00:08:29,340 --> 00:08:30,573
So OWM_Endpoint.

174
00:08:31,800 --> 00:08:34,860
And in addition, we're going to provide some params.

175
00:08:34,860 --> 00:08:38,669
So this is going to determine what kind of data we get back.

176
00:08:38,669 --> 00:08:40,770
And in this case, it's going to be the weather

177
00:08:40,770 --> 00:08:44,370
for a particular location that we're interested in.

178
00:08:44,370 --> 00:08:46,793
Let's create our weather_params,

179
00:08:48,510 --> 00:08:50,820
and this is going to be a dictionary.

180
00:08:50,820 --> 00:08:53,700
And we have to follow the keys

181
00:08:53,700 --> 00:08:56,670
that are provided in the API docs.

182
00:08:56,670 --> 00:09:00,720
So the parameters that we can provide for this API are lats,

183
00:09:00,720 --> 00:09:04,110
which is the latitude, lon, which is longitude.

184
00:09:04,110 --> 00:09:05,640
And a lot of people make typos here

185
00:09:05,640 --> 00:09:08,610
because they add a G. it's actually just L-O-N.

186
00:09:08,610 --> 00:09:11,730
That's the name of the parameter key.

187
00:09:11,730 --> 00:09:15,300
And finally, the appid, which is your API key

188
00:09:15,300 --> 00:09:16,533
that you got earlier on.

189
00:09:17,940 --> 00:09:21,840
In our case, we want to provide a lat, the latitude,

190
00:09:21,840 --> 00:09:26,040
we wanna provide a lon, L-O-N, which is the longitude,

191
00:09:26,040 --> 00:09:29,550
and we also want to provide our appid.

192
00:09:29,550 --> 00:09:31,110
Now our appid is simple.

193
00:09:31,110 --> 00:09:34,470
It's simply the API key that we got earlier on.

194
00:09:34,470 --> 00:09:38,100
And again, just as a reminder, this key is not gonna work.

195
00:09:38,100 --> 00:09:41,790
You'll have to register and get your own for it to work.

196
00:09:41,790 --> 00:09:43,920
Now the latitude we're gonna figure out

197
00:09:43,920 --> 00:09:45,900
by using latlong.net.

198
00:09:45,900 --> 00:09:47,100
So I'm trying to get the weather

199
00:09:47,100 --> 00:09:50,430
for where I currently reside, which is London.

200
00:09:50,430 --> 00:09:55,230
So my latitude is this value and my longitude is this value.

201
00:09:55,230 --> 00:09:57,120
Make sure that you copy everything,

202
00:09:57,120 --> 00:09:59,823
including any negative signs like this.

203
00:10:01,380 --> 00:10:04,290
So these are gonna go in as floating point numbers.

204
00:10:04,290 --> 00:10:07,530
And now that we've completed our weather_params,

205
00:10:07,530 --> 00:10:10,440
we're gonna pass it in to this method.

206
00:10:10,440 --> 00:10:14,460
And now, we can save the output that comes from this

207
00:10:14,460 --> 00:10:16,620
as the response.

208
00:10:16,620 --> 00:10:20,680
And we can print the response.status_code

209
00:10:22,515 --> 00:10:24,330
to see if it was successful.

210
00:10:24,330 --> 00:10:27,240
So now let's go ahead and run our main.py.

211
00:10:27,240 --> 00:10:30,480
And you can see the response code we get back is 200,

212
00:10:30,480 --> 00:10:33,150
which means everything is A-OK.

213
00:10:33,150 --> 00:10:36,330
Now, if you get a response code that's a 401,

214
00:10:36,330 --> 00:10:39,600
that means your API key is probably not valid,

215
00:10:39,600 --> 00:10:42,930
or you've misspelled the name of that key.

216
00:10:42,930 --> 00:10:45,720
It should be appid, all in lowercase,

217
00:10:45,720 --> 00:10:48,303
as they've shown us in their documentation.

218
00:10:49,740 --> 00:10:51,690
If you did get 200 though,

219
00:10:51,690 --> 00:10:53,700
that means everything was successful,

220
00:10:53,700 --> 00:10:57,000
and we can now tap into the actual data.

221
00:10:57,000 --> 00:10:59,722
So let's tap into the response.json

222
00:10:59,722 --> 00:11:01,893
and let's print that out.

223
00:11:03,690 --> 00:11:04,770
So there you have it.

224
00:11:04,770 --> 00:11:06,960
We've got all of the data here

225
00:11:06,960 --> 00:11:09,600
that came back from that API call.

226
00:11:09,600 --> 00:11:11,070
And if you scroll,

227
00:11:11,070 --> 00:11:14,253
there's actually quite a lot of data that's in there.

228
00:11:17,100 --> 00:11:19,110
Now, in order to view this,

229
00:11:19,110 --> 00:11:21,570
it's not very easy to look at it in the console.

230
00:11:21,570 --> 00:11:24,360
So I'm going to triple-click on this

231
00:11:24,360 --> 00:11:26,730
and copy all of that string.

232
00:11:26,730 --> 00:11:30,960
And I'm going to view it in a online JSON viewer.

233
00:11:30,960 --> 00:11:32,670
So I'll link to this as well,

234
00:11:32,670 --> 00:11:34,980
but you wanna paste that JSON you got back

235
00:11:34,980 --> 00:11:37,470
into this and you can see, wow, what a mess.

236
00:11:37,470 --> 00:11:39,060
So much data.

237
00:11:39,060 --> 00:11:40,980
But if you click on Viewer,

238
00:11:40,980 --> 00:11:44,100
then you can see it has nicely formatted

239
00:11:44,100 --> 00:11:45,660
all of that data for us.

240
00:11:45,660 --> 00:11:47,880
So we can easily see the weather forecast

241
00:11:47,880 --> 00:11:49,710
for the next five days.

242
00:11:49,710 --> 00:11:52,020
The forecasts are every three hours,

243
00:11:52,020 --> 00:11:54,660
starting from the time you made your request.

244
00:11:54,660 --> 00:11:58,020
Since there are eight individual forecasts per day

245
00:11:58,020 --> 00:11:59,550
for the whole five days,

246
00:11:59,550 --> 00:12:02,493
you should get back a total of 40 forecasts.

247
00:12:03,390 --> 00:12:04,860
At the bottom of the JSON,

248
00:12:04,860 --> 00:12:07,350
you'll see a location name that's closest

249
00:12:07,350 --> 00:12:10,590
to the latitude and longitude that you provided.

250
00:12:10,590 --> 00:12:11,730
For my coordinates,

251
00:12:11,730 --> 00:12:15,420
that's Abbey Wood in London, United Kingdom.

252
00:12:15,420 --> 00:12:18,540
Going back to the top, I can look at the actual weather.

253
00:12:18,540 --> 00:12:20,580
If I expand the first data point here,

254
00:12:20,580 --> 00:12:23,370
you can see that it gives me a timestamp.

255
00:12:23,370 --> 00:12:24,840
This timestamp format

256
00:12:24,840 --> 00:12:27,390
is the number of seconds that have passed

257
00:12:27,390 --> 00:12:30,600
since January 1, 1970,

258
00:12:30,600 --> 00:12:32,940
which is not very human readable.

259
00:12:32,940 --> 00:12:37,940
So OpenWeatherMap has given us a dt_txt field as well.

260
00:12:38,760 --> 00:12:42,990
If you expand main, you'll see the temperature in Kelvins,

261
00:12:42,990 --> 00:12:47,310
the pressure, the humidity, but also the actual weather.

262
00:12:47,310 --> 00:12:49,320
As you can see from the square brackets,

263
00:12:49,320 --> 00:12:52,230
the weather data comes in a list.

264
00:12:52,230 --> 00:12:56,490
Looking inside, we see this list has a single entry.

265
00:12:56,490 --> 00:12:58,800
Here is where we have our description

266
00:12:58,800 --> 00:13:00,720
of the weather forecast.

267
00:13:00,720 --> 00:13:05,340
It's telling us that the forecast is overcast clouds,

268
00:13:05,340 --> 00:13:09,060
which is pretty normal, if you've ever been to London.

269
00:13:09,060 --> 00:13:10,360
At least it's not raining.

270
00:13:11,460 --> 00:13:15,720
We've now managed to use this OpenWeather app API,

271
00:13:15,720 --> 00:13:18,720
authenticate ourselves with an API key,

272
00:13:18,720 --> 00:13:22,680
and get the required data back from this service.

273
00:13:22,680 --> 00:13:23,940
So in the next lesson,

274
00:13:23,940 --> 00:13:26,310
we're gonna dig into the data that we got back,

275
00:13:26,310 --> 00:13:27,600
and we're going to use it

276
00:13:27,600 --> 00:13:30,150
to figure out whether if it's gonna rain today.

277
00:13:30,150 --> 00:13:31,860
So for all of that and more,

278
00:13:31,860 --> 00:13:33,460
I'll see you on the next lesson.

