WEBVTT

0
00:00.360 --> 00:03.840
Now, whenever we start using a new API,

1
00:03.840 --> 00:07.950
the most important thing is to read the documentation.

2
00:07.950 --> 00:10.860
I know it's really tempting to skip over the instructions

3
00:10.860 --> 00:12.750
and just get right into it.

4
00:12.750 --> 00:15.420
I'm also the sort of person who buys a lawnmower

5
00:15.420 --> 00:18.090
and throws away the instruction manual,

6
00:18.090 --> 00:20.850
and then two days later something breaks

7
00:20.850 --> 00:23.760
and I just want to hurt myself

8
00:23.760 --> 00:24.960
because it would be so much easier

9
00:24.960 --> 00:27.810
if I had actually read the instruction manual.

10
00:27.810 --> 00:30.090
But in this case, it is really important.

11
00:30.090 --> 00:33.420
So let's head over to openweathermap.org together

12
00:33.420 --> 00:36.270
and let's go to the API section

13
00:36.270 --> 00:39.480
to see the API documentation.

14
00:39.480 --> 00:40.500
As you can see,

15
00:40.500 --> 00:44.400
this API provider actually has several products.

16
00:44.400 --> 00:47.370
So you could look at the current weather data,

17
00:47.370 --> 00:52.370
the hourly forecast, the One Call API, the daily forecast,

18
00:53.220 --> 00:55.530
and a whole bunch of other things.

19
00:55.530 --> 00:58.470
So each of these have their own endpoints.

20
00:58.470 --> 01:00.390
And if we take a look at the API doc,

21
01:00.390 --> 01:02.160
you can see this first one,

22
01:02.160 --> 01:07.160
the API is api.openweathermap.org/data/2.5/weather.

23
01:09.840 --> 01:11.430
That's the endpoint.

24
01:11.430 --> 01:14.070
Whereas the hourly forecast

25
01:14.070 --> 01:19.070
is pro.openweathermap.org/data/2.5/ forecast/hourly.

26
01:22.020 --> 01:24.510
So depending on what it is that we need,

27
01:24.510 --> 01:28.830
we can tap into each of these different APIs.

28
01:28.830 --> 01:30.540
Let's start off with an easy one.

29
01:30.540 --> 01:32.730
Let's try and get the current weather data

30
01:32.730 --> 01:34.770
for the city that we're in.

31
01:34.770 --> 01:37.320
You can see that this part is the endpoint,

32
01:37.320 --> 01:40.320
so the URL that we're going to tap into.

33
01:40.320 --> 01:42.900
And then we have some parameters.

34
01:42.900 --> 01:47.660
The parameter where we can specify the city name is q.

35
01:48.540 --> 01:50.700
So that's the name of the parameter

36
01:50.700 --> 01:53.490
and then we have to provide a value.

37
01:53.490 --> 01:55.680
Now, in addition, there's another parameter,

38
01:55.680 --> 02:00.060
which we have to include with all of the API calls

39
02:00.060 --> 02:04.140
to this particular service, and that is an appid.

40
02:04.140 --> 02:06.630
We have to get hold of an api key

41
02:06.630 --> 02:09.360
in order to use this service.

42
02:09.360 --> 02:11.490
And they show you an example here.

43
02:11.490 --> 02:13.530
If we click on this URL,

44
02:13.530 --> 02:17.610
it takes us to samples.openweathermap.org.

45
02:17.610 --> 02:19.770
And this is just some sample data

46
02:19.770 --> 02:23.130
where they provided a sample appid.

47
02:23.130 --> 02:26.160
Now, we can get our own appid for free

48
02:26.160 --> 02:29.580
by simply signing up to this weather service.

49
02:29.580 --> 02:32.280
And once you register and sign in,

50
02:32.280 --> 02:34.350
this is the page that you see.

51
02:34.350 --> 02:38.010
So we can now go into this section called API keys

52
02:38.010 --> 02:41.430
and grab our API key here.

53
02:41.430 --> 02:43.860
So you can create as many keys as you want,

54
02:43.860 --> 02:45.600
and you can provide a name

55
02:45.600 --> 02:49.680
that's associated with the final usage for that key.

56
02:49.680 --> 02:51.360
So I've named it Python.

57
02:51.360 --> 02:54.390
And now I'm going to copy this API key

58
02:54.390 --> 02:57.390
and I'm going to place it into my project.

59
02:57.390 --> 03:00.270
So I've started a new project called rain_alert,

60
03:00.270 --> 03:05.160
and inside the main.py, I'm going to create an API key,

61
03:05.160 --> 03:09.390
and I'm going to save that key that I copied

62
03:09.390 --> 03:12.900
into a set of double quotes as a string.

63
03:12.900 --> 03:15.360
Now, once I've got my API key,

64
03:15.360 --> 03:18.153
I can actually test out this API call.

65
03:20.340 --> 03:24.150
So I can replace this part with my city name,

66
03:24.150 --> 03:28.800
which is London, and then I can add my country afterwards

67
03:28.800 --> 03:31.290
just to make sure that it goes to London, U.K.,

68
03:31.290 --> 03:34.860
and not London, Ontario, or somewhere else.

69
03:34.860 --> 03:37.500
And then finally, I'm going to paste in the API key

70
03:37.500 --> 03:39.900
that I copied over just now.

71
03:39.900 --> 03:42.180
And now, when I hit Enter,

72
03:42.180 --> 03:44.610
and as long as my API key is valid,

73
03:44.610 --> 03:49.530
I should get a result in the format of my weather data.

74
03:49.530 --> 03:53.190
Now, I want you to make sure that your API key works as well

75
03:53.190 --> 03:55.290
by using the same method,

76
03:55.290 --> 03:59.010
but remember that you have to get hold of your own API key

77
03:59.010 --> 04:03.600
by signing up to OpenWeatherMap if you want this to work.

78
04:03.600 --> 04:07.830
Make sure that you replace this part with your own API key.

79
04:07.830 --> 04:10.050
Otherwise, it's not going to work.

80
04:10.050 --> 04:13.424
Now that we've seen that this particular API call

81
04:13.424 --> 04:15.420
actually works,

82
04:15.420 --> 04:19.860
let's see what happens if we provide the wrong API key.

83
04:19.860 --> 04:24.120
So if I just make up an API key and hit Enter,

84
04:24.120 --> 04:26.820
you can see we get a code back,

85
04:26.820 --> 04:30.390
which is an HTTP code "cod": 401.

86
04:30.390 --> 04:31.890
And we can also get a message,

87
04:31.890 --> 04:35.010
telling us that that's an "Invalid API key..."

88
04:35.010 --> 04:38.190
and we can go to this URL for more info.

89
04:38.190 --> 04:40.320
Now, remember, in previous lessons,

90
04:40.320 --> 04:43.290
I mentioned that HTTP status codes

91
04:43.290 --> 04:45.120
are the codes that we get back

92
04:45.120 --> 04:47.760
when we try to talk to another server.

93
04:47.760 --> 04:51.390
So in this case, we got the 401 code back,

94
04:51.390 --> 04:53.700
which means, Unauthorized.

95
04:53.700 --> 04:55.320
And when you get this code back

96
04:55.320 --> 04:56.730
and you're working with an API,

97
04:56.730 --> 05:00.180
it usually means that your API key

98
05:00.180 --> 05:03.390
or your way of authorizing with the API key

99
05:03.390 --> 05:05.730
is somehow not working.

100
05:05.730 --> 05:08.253
So that is the thing that you should examine.

101
05:09.570 --> 05:13.200
We can see that for this particular API provider,

102
05:13.200 --> 05:16.650
we have to authenticate ourselves with their server

103
05:16.650 --> 05:20.883
by providing the API key as a parameter.

104
05:22.170 --> 05:24.450
So now I want to give you a challenge.

105
05:24.450 --> 05:27.840
I want you to go to OpenWeatherMaps API docs

106
05:27.840 --> 05:30.840
for their five day weather forecast.

107
05:30.840 --> 05:32.190
The 5 day weather forecast

108
05:32.190 --> 05:35.520
will give you the weather forecast every three hours

109
05:35.520 --> 05:38.160
based on the location that you provide.

110
05:38.160 --> 05:41.040
What OpenWeatherMap means by a three-hour forecast

111
05:41.040 --> 05:44.970
is that they will give you eight weather forecasts per day.

112
05:44.970 --> 05:47.220
The earliest forecast would be at 3:00 AM,

113
05:47.220 --> 05:51.690
the next at 6:00 AM, and the one after that is for 9:00 AM,

114
05:51.690 --> 05:55.470
then one at midday, and so on until midnight.

115
05:55.470 --> 05:59.430
My challenge to you is to make an API request from PyCharm

116
05:59.430 --> 06:01.740
using the 5 day weather forecast,

117
06:01.740 --> 06:03.450
and locate the weather forecast

118
06:03.450 --> 06:06.000
in the JSON response that you get back.

119
06:06.000 --> 06:08.010
Now I could tell you exactly what to do,

120
06:08.010 --> 06:10.650
but it's really good to get some practice

121
06:10.650 --> 06:12.870
reading API documentation

122
06:12.870 --> 06:14.430
because after all, in the future,

123
06:14.430 --> 06:16.770
you're going be using loads of different APIs,

124
06:16.770 --> 06:18.090
and they all have different ways

125
06:18.090 --> 06:20.253
of working with their services.

126
06:21.240 --> 06:22.560
Have a look at this page.

127
06:22.560 --> 06:24.900
I'll link to it in the course resources,

128
06:24.900 --> 06:28.320
and I want you to implement this API call

129
06:28.320 --> 06:30.870
by providing a latitude and latitude

130
06:30.870 --> 06:33.210
of where you currently are,

131
06:33.210 --> 06:37.020
and make sure that you add your personal appid

132
06:37.020 --> 06:39.660
by getting hold of your API key.

133
06:39.660 --> 06:40.860
Now, remember, you can get

134
06:40.860 --> 06:42.870
your current latitude and longitude

135
06:42.870 --> 06:47.040
just by going to latlong.net, and then typing a place name,

136
06:47.040 --> 06:48.720
and then clicking Find.

137
06:48.720 --> 06:51.090
Don't worry if it tells you a Bot is detected,

138
06:51.090 --> 06:53.940
just go back to the website after a little while

139
06:53.940 --> 06:55.320
and try again.

140
06:55.320 --> 06:56.153
At some point,

141
06:56.153 --> 06:58.980
it should show you your latitude and longitude,

142
06:58.980 --> 07:01.740
which you can then plug in as a parameter

143
07:01.740 --> 07:04.200
to this particular API call.

144
07:04.200 --> 07:06.960
So if you manage to work through all the lessons yesterday,

145
07:06.960 --> 07:09.240
then this should be a piece of cake for you.

146
07:09.240 --> 07:10.990
Pause the video and give that a go.

147
07:17.430 --> 07:19.560
All right, so if that was hard at all,

148
07:19.560 --> 07:22.560
make sure that you've not skipped yesterday's lessons

149
07:22.560 --> 07:27.150
because I go through in detail how to hit up an API endpoint,

150
07:27.150 --> 07:28.770
how to provide parameters,

151
07:28.770 --> 07:32.160
and how to essentially complete this challenge.

152
07:32.160 --> 07:33.840
But hopefully you managed all right,

153
07:33.840 --> 07:36.930
and you're just here to check the solution.

154
07:36.930 --> 07:38.550
As always, we're going to start off

155
07:38.550 --> 07:41.643
by importing our requests package.

156
07:42.750 --> 07:46.590
Remember that if you are creating this file from scratch

157
07:46.590 --> 07:49.890
and you haven't downloaded it from a zip file,

158
07:49.890 --> 07:52.950
then you'll have to install this module manually.

159
07:52.950 --> 07:54.990
So if you click on it in PyCharm,

160
07:54.990 --> 07:56.460
you get a little red light bulb.

161
07:56.460 --> 07:59.348
And then when you click on the down arrow,

162
07:59.348 --> 08:02.580
you can install that package, simple.

163
08:02.580 --> 08:05.280
Now, once you've got requests installed,

164
08:05.280 --> 08:09.930
we can use the GET method to pass in our endpoint.

165
08:09.930 --> 08:12.520
Now the OpenWeatherMap endpoint

166
08:14.100 --> 08:16.980
is the one that we see in the API docs.

167
08:16.980 --> 08:18.840
So it's everything up to

168
08:18.840 --> 08:20.850
but not including the question mark.

169
08:20.850 --> 08:22.650
So all of this.

170
08:22.650 --> 08:26.550
And we can paste that in and save it as a string.

171
08:26.550 --> 08:29.340
That is what we want to make our GET request to.

172
08:29.340 --> 08:30.573
So OWM_Endpoint.

173
08:31.800 --> 08:34.860
And in addition, we're going to provide some params.

174
08:34.860 --> 08:38.670
So this is going to determine what kind of data we get back.

175
08:38.670 --> 08:40.770
And in this case, it's going to be the weather

176
08:40.770 --> 08:44.370
for a particular location that we're interested in.

177
08:44.370 --> 08:46.793
Let's create our weather_params,

178
08:48.510 --> 08:50.820
and this is going to be a dictionary.

179
08:50.820 --> 08:53.700
And we have to follow the keys

180
08:53.700 --> 08:56.670
that are provided in the API docs.

181
08:56.670 --> 09:00.720
So the parameters that we can provide for this API are lat,

182
09:00.720 --> 09:04.110
which is the latitude, lon, which is longitude.

183
09:04.110 --> 09:05.640
And a lot of people make typos here

184
09:05.640 --> 09:08.610
because they add a G. it's actually just L-O-N.

185
09:08.610 --> 09:11.730
That's the name of the parameter key.

186
09:11.730 --> 09:15.300
And finally, the appid, which is your API key

187
09:15.300 --> 09:16.533
that you got earlier on.

188
09:17.940 --> 09:21.840
In our case, we want to provide a lat, the latitude,

189
09:21.840 --> 09:26.040
we want to provide a lon, L-O-N, which is the longitude,

190
09:26.040 --> 09:29.550
and we also want to provide our appid.

191
09:29.550 --> 09:31.110
Now our appid is simple.

192
09:31.110 --> 09:34.470
It's simply the API key that we got earlier on.

193
09:34.470 --> 09:38.100
And again, just as a reminder, this key is not going to work.

194
09:38.100 --> 09:41.790
You'll have to register and get your own for it to work.

195
09:41.790 --> 09:43.920
Now the latitude we're going to figure out

196
09:43.920 --> 09:45.900
by using latlong.net.

197
09:45.900 --> 09:47.100
So I'm trying to get the weather

198
09:47.100 --> 09:50.430
for where I currently reside, which is London.

199
09:50.430 --> 09:55.230
So my latitude is this value and my longitude is this value.

200
09:55.230 --> 09:57.120
Make sure that you copy everything,

201
09:57.120 --> 09:59.823
including any negative signs like this.

202
10:01.380 --> 10:04.290
So these are going to go in as floating point numbers.

203
10:04.290 --> 10:07.530
And now that we've completed our weather_params,

204
10:07.530 --> 10:10.440
we're going to pass it in to this method.

205
10:10.440 --> 10:14.460
And now, we can save the output that comes from this

206
10:14.460 --> 10:16.620
as the response.

207
10:16.620 --> 10:20.680
And we can print the response.status_code

208
10:22.515 --> 10:24.330
to see if it was successful.

209
10:24.330 --> 10:27.240
So now let's go ahead and run our main.py.

210
10:27.240 --> 10:30.480
And you can see the response code we get back is 200,

211
10:30.480 --> 10:33.150
which means everything is A-OK.

212
10:33.150 --> 10:36.330
Now, if you get a response code that's a 401,

213
10:36.330 --> 10:39.600
that means your API key is probably not valid,

214
10:39.600 --> 10:42.930
or you've misspelled the name of that key.

215
10:42.930 --> 10:45.720
It should be appid, all in lowercase

216
10:45.720 --> 10:48.303
as they've shown us in their documentation.

217
10:49.740 --> 10:51.690
If you did get 200 though,

218
10:51.690 --> 10:53.700
that means everything was successful,

219
10:53.700 --> 10:57.000
and we can now tap into the actual data.

220
10:57.000 --> 10:59.722
So let's tap into the response.json(),

221
10:59.722 --> 11:01.893
and let's print that out.

222
11:03.690 --> 11:04.770
So there you have it.

223
11:04.770 --> 11:06.960
We've got all of the data here

224
11:06.960 --> 11:09.600
that came back from that API call.

225
11:09.600 --> 11:11.070
And if you scroll,

226
11:11.070 --> 11:14.253
there's actually quite a lot of data that's in there.

227
11:17.100 --> 11:19.110
Now, in order to view this,

228
11:19.110 --> 11:21.570
it's not very easy to look at it in the console.

229
11:21.570 --> 11:24.360
So I'm going to triple-click on this

230
11:24.360 --> 11:26.730
and copy all of that string.

231
11:26.730 --> 11:30.960
And I'm going to view it in a online JSON viewer.

232
11:30.960 --> 11:32.670
So I'll link to this as well,

233
11:32.670 --> 11:34.980
but you want to paste that JSON you got back

234
11:34.980 --> 11:37.470
into this and you can see, "Wow, what a mess?"

235
11:37.470 --> 11:39.060
So much data.

236
11:39.060 --> 11:40.980
But if you click on Viewer,

237
11:40.980 --> 11:44.100
then you can see it has nicely formatted

238
11:44.100 --> 11:45.660
all of that data for us,

239
11:45.660 --> 11:47.880
so we can easily see the weather forecast

240
11:47.880 --> 11:49.710
for the next five days.

241
11:49.710 --> 11:52.020
The forecasts are every three hours,

242
11:52.020 --> 11:54.660
starting from the time you made your request.

243
11:54.660 --> 11:58.020
Since there are eight individual forecasts per day

244
11:58.020 --> 11:59.550
for the whole five days,

245
11:59.550 --> 12:02.493
you should get back a total of 40 forecasts.

246
12:03.390 --> 12:04.860
At the bottom of the JSON,

247
12:04.860 --> 12:07.350
you'll see a location name that's closest

248
12:07.350 --> 12:10.590
to the latitude and longitude that you provided.

249
12:10.590 --> 12:11.730
For my coordinates,

250
12:11.730 --> 12:15.420
that's Abbey Wood in London, United Kingdom.

251
12:15.420 --> 12:18.540
Going back to the top, I can look at the actual weather.

252
12:18.540 --> 12:20.580
If I expand the first data point here,

253
12:20.580 --> 12:23.370
you can see that it gives me a timestamp.

254
12:23.370 --> 12:24.840
This timestamp format

255
12:24.840 --> 12:27.390
is the number of seconds that have passed

256
12:27.390 --> 12:30.600
since January 1, 1970,

257
12:30.600 --> 12:32.940
which is not very human readable.

258
12:32.940 --> 12:37.940
So OpenWeatherMap has given us a dt_txt field as well.

259
12:38.760 --> 12:42.990
If you expand main, you'll see the temperature in Kelvin's,

260
12:42.990 --> 12:47.310
the pressure, the humidity, but also the actual weather.

261
12:47.310 --> 12:49.320
As you can see from the square brackets,

262
12:49.320 --> 12:52.230
the weather data comes in a list.

263
12:52.230 --> 12:56.490
Looking inside, we see this list has a single entry.

264
12:56.490 --> 12:58.800
Here is where we have our description

265
12:58.800 --> 13:00.720
of the weather forecast.

266
13:00.720 --> 13:05.340
It's telling us that the forecast is overcast clouds,

267
13:05.340 --> 13:09.060
which is pretty normal, if you've ever been to London.

268
13:09.060 --> 13:10.360
At least it's not raining.

269
13:11.460 --> 13:15.720
We've now managed to use this OpenWeather app API,

270
13:15.720 --> 13:18.720
authenticate ourselves with an API key,

271
13:18.720 --> 13:22.680
and get the required data back from this service.

272
13:22.680 --> 13:23.940
So in the next lesson,

273
13:23.940 --> 13:26.310
we're going dig into the data that we got back,

274
13:26.310 --> 13:27.600
and we're going to use it

275
13:27.600 --> 13:30.150
to figure out whether if it's going to rain today.

276
13:30.150 --> 13:31.860
So for all of that and more,

277
13:31.860 --> 13:33.460
I'll see you on the next lesson.