Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Data from APIs!
You have completed Data from APIs!
Preview
Use your API key to make calls to the weather API and gather data.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Welcome back.
0:00
In this video,
0:01
we're going to use our new API key to get
information from the open weather map API.
0:02
Let's look at the weather API docs to see
how we need to write out our request.
0:09
Now you can see under the pricing and
under free,
0:14
there are a couple APIs
that we have access to.
0:18
Current weather takes us
to current weather, and
0:23
all of these actually take us to
this one call API that they have.
0:28
So we're gonna stick with
just current weather.
0:35
At the top here, you can see a few
different ways to make an API call.
0:38
You can see that you can put in
like a city name and then your key.
0:42
You can do city and state and then your
key, you can do city, state, country,
0:46
and key.
0:51
And then it tells you what
parameters are required.
0:52
So your query which is if you're
putting your city, your state,
0:54
your country, and then you have to pass
in your API ID, which is your API key.
0:59
And then there's some
optional values here.
1:04
Let's try making a call
using Rome as the place and
1:07
I'm gonna create some variables
to hold all these things.
1:11
So we have city equals Rome, and
1:16
then country equals and
1:21
then we do this just to be consistent.
1:25
Okay, and so
we actually pull up the docs real quick.
1:32
And country code,
it says country code, and so
1:38
what it means by that is that two-letter
code that each country is given.
1:41
So it has a link right
here in the document so
1:47
that you can look up what Italy would be.
1:50
But I've done that for you, Italy is IT.
1:52
And then we can do our request and
1:56
I'm just gonna add a little
space here to break it up.
1:58
So our response equals,
oops and I almost forgot.
2:01
I need to import requests.
2:07
Space this down one more.
2:15
And this is going to be requests.get and
we need to pass in,
2:18
and I'm just gonna copy this
straight from the documentation.
2:23
We make sure this is bigger.
2:29
Okay, so I'm gonna copy this whole thing,
and actually this is also copy,
2:31
so you can just click on that button
as well, makes it a little bit faster.
2:37
So I'm gonna put it in here, and because
we need to replace these with values and
2:42
we have variables, I'm gonna make this
an F string so that we can do that.
2:48
And you can see that change the colors
of these, so we have our city.
2:53
We don't have a state,
that's totally fine, and
2:58
we have our country code,
but we listed it as country.
3:02
And you can see we have api_key,
3:07
so we'll need to underscore,
3:11
this should be api_key,
perfect, and hit Save.
3:16
And so we've imported our API
key from our keys.py file.
3:23
We've imported requests,
we've set our city and our country, and
3:28
we are ready to send a request
using that information.
3:32
And right now I'm gonna leave this
on one line for pythonic reasons.
3:36
You would actually want to break this
up on to at least one other line
3:40
because it's so long.
3:45
But I'm gonna leave it right now just so
we can see the whole thing.
3:47
So we're accessing the weather API,
we're sending a query of the city and
3:50
country that we wanna get the weather for,
and
3:55
we're sending them our API key so
that they know who is making the request.
3:58
All right, and then at the end,
didn't scroll us back over, there we go.
4:03
I'm gonna print out our res.json.
4:08
Make sure I save it.
4:14
Come down here and do python3 app.py and
let's see what we get.
4:16
Okay, and it looks like we've got
an error and it's saying invalid URL.
4:22
And what's missing and
this is what I find a little bit annoying
4:27
about this API is they started
right off with this API right here,
4:33
but you actually need to
add the HTTP slash slash.
4:39
Save and let's try that again.
4:44
There we go, perfect.
4:47
So let me open this up a little bit.
4:49
And now you can see from our API call,
we've got some coordinates,
4:52
we got weather.
4:57
It looks like it's clear there right now.
4:58
The temperature is 296.
5:01
Obviously, that's not Celsius or
Fahrenheit.
5:03
So we're gonna have to look at that to
make sure we get the right information,
5:06
and it shows like visibility,
humidity, wind speed,
5:11
clouds, all sorts of information.
5:15
And then at the end,
you can see name Rome.
5:17
So we did get the information back for
Rome, wonderful.
5:20
So we've got the data we were looking for.
5:23
Now I want you to try and
make a different request on your own.
5:28
Try gathering data for Shanghai, China.
5:33
Then I want you to try and
add the parameter.
5:36
I'll bring back up our docs.
5:40
You can see we can add.
5:42
This is called a parameter.
5:45
This is another parameter.
5:46
And you can see there's some optional
ones here and there's one for units.
5:49
There's standard metric and
imperial available, and
5:54
if you don't put anything in,
it's gonna send us standards.
5:57
So that's why we are getting this like,
6:01
where was our temperature,
296.61 temperature.
6:04
So I want you to try and
add to your request when you're getting
6:10
information on Shanghai China
to put in the units as metric.
6:15
I want you to give that and
try on your own.
6:21
So go ahead and
pause me and give it a try.
6:22
Okay, you ready?
6:26
Bring this down.
6:31
I'm just gonna replace
it straight in here.
6:33
China is CN, so that's gonna automatically
update this because they're variables.
6:37
And then I'm going to add
the information about our units,
6:44
and so I'm just gonna copy this, Ctrl + C.
6:49
I'm gonna put it right here because this
is how you can see it's the pattern for
6:54
adding on a new parameter.
7:00
So this would need to be called units,
oops,
7:02
units, and then we're going to do metric.
7:07
And if I pull our docs back up, you can
see it actually needs to be lowercase, so
7:11
I need to go change that.
7:16
But it tells you exactly what you need to
pass in as far as you know, uppercase,
7:17
lowercase, metric.
7:22
So I'm gonna hit Save and
do the last final check.
7:26
Pull this up,
I'm gonna run a clear just so
7:33
our console is a little bit clearer,
and let's run the file.
7:36
The name is now Shanghai, so
now we're looking at weather for
7:41
a totally different city in the world.
7:44
And if we look at the temperature,
it is now 26 degrees which is definitely
7:47
more realistic and probably a lot closer
to what you imagined temperature to
7:52
look like compared to the 296 degrees that
we had earlier with the standard units.
7:57
So now we've got information that's
a little bit more useful to us.
8:03
Did you do all right getting
the information for Shanghai China?
8:09
Take some time to practice
playing around with the API.
8:13
Try getting the weather for,
say your hometown, or
8:16
where you'd like to take a dream vacation.
8:20
Try adding different parameters
that are available to customize
8:24
the data you receive and
just practice playing around.
8:28
See you in the next video.
8:33
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up