Looking back in this lecture, you had to use your solitude with my crew, Python, to get the current
weather conditions on pretty much any location on the planet.
First of all, let's have a look around the open weather map website and show you where to find information
about the API.
And, of course, had to get an API key that you need in order to be authenticated with the service
and receive data from it.
So this is the open weather map LICHEE website, and there's a search bar here that I've just used to
search for the weather in Sydney, Australia.
And you can see that this is the information that comes back, a subset of this information that contains
current weather data will be provided when you make an appropriate get request authenticated, of course,
via an API key.
So having a look at the various APIs that service provides and just going to go one level back so that
you can see the richness of this service.
So this current weather data, which is the API that will be using this hourly forecast for four days,
a single cool API, 16 day forecast, etc, etc. So there's a load here that you can use a lot of APIs
that you can use, including historical data and other data, etc. And some of this is free and the
APIs paid for the one that we are using.
This example, the current weather API is totally free.
There is a limit of how many requests you can make per day, but that limit is quite high and I have
never reached that actually.
So it's a perfect way to test out making such calls to a API service like this.
Having a look at the current weather data documentation in here, you find information on how to make
a API call.
So here's the API call.
Your real ID gives you a couple of options on how to request data for a particular city.
So here's one example where you put in the city name after the Q key value pair.
And here's an alternative.
We've got the city name, the state code, or you can go for city name, state code and country code.
You've got an example here as well where I'm using this notation right there.
So I'm passing through New York, New York State and us as the country code.
And that is helpful if you have a city name which has synonyms around the world.
So that could be, for example, in Athens and Georgia and in Greece.
So you can differentiate between those in that way.
The method I actually prefer to use to deal with these ambiguities much easier is to use the city ID
method right there.
So instead of using queue for the query, you're using the ID key.
And that's the example that I've got right here.
So I'm looking for a city with a particular idea.
Now, how to find this ID is quite easy.
It go to the homepage of Open with a map.
You make a search for whichever city you're interested in and that comes back.
If the name that you're looking for is ambiguous, it will give you a choice and you pick the one that
suits you best.
For example, if I look for Sydney and Click Search.
I've got a choice of various Sidneys around the world, so go for Sydney, Australia, and you can see
that the idea of this particular location appears up here on the euro.
So this is a number that you can grab and use this number in your query.
OK, there's one more thing here that is important.
There is the open weather API key.
So to get the key to create a free account, then go into the Mendis page and under Epic's, you can
generate or retrieve an existing key.
And of course, this you are real right here in the header of my example script.
So you can quickly go there and get your own key to use for this example.
All right.
So that's a very I'm going to go back to the documentation and actually scroll down.
This Jason file, which is an example of the API response and this is what comes back when you make
a get request for a particular location and you pass your API key, and that request is successful and
authenticated, what comes back is a fairly large chasten file with a lot of information.
So then what you need to do is to decode this Jason file and extract the information that you need out
of it.
So here you see temperatures.
I believe these are in Fahrenheit.
You can you can change the unit used in the degree response to whatever you want.
And you just need to use the units parameter here, standard Petrich, Imperial and muchly using.
Units for the metric system right here, so I'm getting degrees Celsius, for example, instead of Fahrenheit,
but they say you can make the change yourself.
So here is the example, Jason file.
I'm going to show you in a minute how to pinpoint and extract specific bits of data out of this chasten
response.
But again, as you can see, there's loads here that you can pick from.
This is what makes this service such a good example for this lecture.
All right.
Now let's have a look at the actual script, importing the usual modules, as I have with the other
examples in this set of lectures and using the same construct to open up the credentials file, setting
the headers, as I have before, said the drill.
I'm going for the one with the idea parameter here.
And passing the app, I'd like this so I can grab the open with a key from the credentials file, then
the new Connect method is identical to what you've seen in the past.
Same thing lines.
Seventy and seventy two are identical.
No change is still same thing with lines and four to seventy eight just printing out the IP address
that my wi fi network has given me in line eighty one.
I make the request and it's just a simple get request to this eurorail up here.
And then print out the response and because we know that the responses are chasten document, different
payload, I just use the load's method of the Chechen war to convert this response into an object that
then I can use Jason module notation to extract the individual components of.
So there's one little issue here with the weather.
Now notice that the weather key contains an array, so it does not contain a JSON document, just like,
say, the coordinates key contains some of the adjacent document in here.
Instead, the way the key contains an array and inside the array you've got the adjacent document.
And for this reason, I'm using this construct here to go inside the the key and then extract the individual
components such as Maine and description using this for in the back, whether annotation and that allows
me to extract the keys and the values that I want out of this array.
So.
But for the rest, it's all straightforward.
For example, if I want to grab the wind speed, the wind speed is let's see, where is that wind speed
think?
There's wind speed, you can see this speed is inside the wind key, so I'm just using the regular chasten
notation to extract those pieces of data.
Same thing with the wind degrees or the direction of the wind and so on.
Some pulling out some information that I'm just able to decode and extract out of the chasten response
that I got from the weather map, that VOYCE website.
So let's try this out.
I have already uploaded the credentials file to my especially to it's right there and I'm going to click
on the script button and handed the script executed on the device.
All right.
So.
Let's have a look at what's happening in New York and take that idea, just paste it right here.
And that gives me New York City information, so let's compare the Web version of the weather in New
York with the AP version.
So the weather, as reported by the AP, is actually similarities.
For example, it feels like minus four point fifty six degrees.
We get some decimals here on the Web version.
We only get an integer.
So minus four degrees.
What I can say that is pretty much the same.
The humidity is 55 percent.
That that matches the pressure at sea level one thousand twenty four to Pascal, that matches will.
The there's a discrepancy in the wind speed.
You've got two point thirty four meters per second.
Here we've got one point five meters per second, I guess this is the direction north, which pretty
much matches the degrees here, three forty nine degrees approximately north.
So the current information on the website looks like this is a pretty concise and we have a lot more
information here in the AP version for the current weather.
One thing that I want to expand on is the temperature here we see the responses in object.
But let's see.
If we can print.
The chasten version of the response, I'm just going to print with a back, see if that comes out.
All right, so let's do a run.
All right, and now we see the full chasten response from the AP and we can see just kind of refresh
this page and see if we can find anything else that is interesting.
So now the wind is two point one meters per second.
And let's see the wind in here.
Two point three four.
Meters per second and still north know everything else to match it the same way we have that visibility.
That should be reported somewhere here.
The only good visibility, 10 kilometers or 10000 meters, it's reported in meters.
And finally, the two point that must be reported here is Will.
You don't see that viewpoint here, so that is missing.
OK, I'll leave it at that.
That's how easy it is to extract current weather information from open weather map, already leaving
your industry to the marketplace.