1
00:00:01,020 --> 00:00:07,410
Looking back in this lecture, you had to use your solitude with my crew, Python, to get the current

2
00:00:07,440 --> 00:00:12,120
weather conditions on pretty much any location on the planet.

3
00:00:12,870 --> 00:00:18,360
First of all, let's have a look around the open weather map website and show you where to find information

4
00:00:18,360 --> 00:00:19,410
about the API.

5
00:00:19,650 --> 00:00:26,280
And, of course, had to get an API key that you need in order to be authenticated with the service

6
00:00:26,280 --> 00:00:27,480
and receive data from it.

7
00:00:28,560 --> 00:00:37,320
So this is the open weather map LICHEE website, and there's a search bar here that I've just used to

8
00:00:37,320 --> 00:00:40,070
search for the weather in Sydney, Australia.

9
00:00:40,470 --> 00:00:46,980
And you can see that this is the information that comes back, a subset of this information that contains

10
00:00:46,980 --> 00:00:53,490
current weather data will be provided when you make an appropriate get request authenticated, of course,

11
00:00:53,490 --> 00:00:55,380
via an API key.

12
00:00:56,610 --> 00:01:03,330
So having a look at the various APIs that service provides and just going to go one level back so that

13
00:01:03,330 --> 00:01:06,100
you can see the richness of this service.

14
00:01:06,120 --> 00:01:13,580
So this current weather data, which is the API that will be using this hourly forecast for four days,

15
00:01:13,580 --> 00:01:21,540
a single cool API, 16 day forecast, etc, etc. So there's a load here that you can use a lot of APIs

16
00:01:21,540 --> 00:01:28,460
that you can use, including historical data and other data, etc. And some of this is free and the

17
00:01:28,530 --> 00:01:32,040
APIs paid for the one that we are using.

18
00:01:32,040 --> 00:01:35,310
This example, the current weather API is totally free.

19
00:01:35,520 --> 00:01:44,250
There is a limit of how many requests you can make per day, but that limit is quite high and I have

20
00:01:44,250 --> 00:01:45,530
never reached that actually.

21
00:01:45,900 --> 00:01:53,520
So it's a perfect way to test out making such calls to a API service like this.

22
00:01:54,180 --> 00:02:00,870
Having a look at the current weather data documentation in here, you find information on how to make

23
00:02:00,870 --> 00:02:02,430
a API call.

24
00:02:02,730 --> 00:02:04,560
So here's the API call.

25
00:02:04,560 --> 00:02:10,470
Your real ID gives you a couple of options on how to request data for a particular city.

26
00:02:10,860 --> 00:02:17,160
So here's one example where you put in the city name after the Q key value pair.

27
00:02:18,030 --> 00:02:20,280
And here's an alternative.

28
00:02:20,280 --> 00:02:29,700
We've got the city name, the state code, or you can go for city name, state code and country code.

29
00:02:30,210 --> 00:02:33,990
You've got an example here as well where I'm using this notation right there.

30
00:02:34,860 --> 00:02:39,630
So I'm passing through New York, New York State and us as the country code.

31
00:02:39,870 --> 00:02:46,620
And that is helpful if you have a city name which has synonyms around the world.

32
00:02:46,620 --> 00:02:50,370
So that could be, for example, in Athens and Georgia and in Greece.

33
00:02:50,970 --> 00:02:54,050
So you can differentiate between those in that way.

34
00:02:54,480 --> 00:03:02,610
The method I actually prefer to use to deal with these ambiguities much easier is to use the city ID

35
00:03:02,610 --> 00:03:04,010
method right there.

36
00:03:04,030 --> 00:03:10,080
So instead of using queue for the query, you're using the ID key.

37
00:03:10,470 --> 00:03:13,550
And that's the example that I've got right here.

38
00:03:13,800 --> 00:03:16,560
So I'm looking for a city with a particular idea.

39
00:03:16,590 --> 00:03:20,020
Now, how to find this ID is quite easy.

40
00:03:20,040 --> 00:03:24,060
It go to the homepage of Open with a map.

41
00:03:24,420 --> 00:03:28,770
You make a search for whichever city you're interested in and that comes back.

42
00:03:29,070 --> 00:03:35,130
If the name that you're looking for is ambiguous, it will give you a choice and you pick the one that

43
00:03:35,370 --> 00:03:36,360
suits you best.

44
00:03:36,390 --> 00:03:39,420
For example, if I look for Sydney and Click Search.

45
00:03:41,180 --> 00:03:47,090
I've got a choice of various Sidneys around the world, so go for Sydney, Australia, and you can see

46
00:03:47,090 --> 00:03:52,900
that the idea of this particular location appears up here on the euro.

47
00:03:53,120 --> 00:03:56,720
So this is a number that you can grab and use this number in your query.

48
00:03:58,130 --> 00:04:00,620
OK, there's one more thing here that is important.

49
00:04:00,630 --> 00:04:03,530
There is the open weather API key.

50
00:04:04,010 --> 00:04:12,620
So to get the key to create a free account, then go into the Mendis page and under Epic's, you can

51
00:04:12,620 --> 00:04:15,610
generate or retrieve an existing key.

52
00:04:16,190 --> 00:04:23,480
And of course, this you are real right here in the header of my example script.

53
00:04:23,480 --> 00:04:28,020
So you can quickly go there and get your own key to use for this example.

54
00:04:29,030 --> 00:04:29,800
All right.

55
00:04:30,080 --> 00:04:34,790
So that's a very I'm going to go back to the documentation and actually scroll down.

56
00:04:37,330 --> 00:04:45,160
This Jason file, which is an example of the API response and this is what comes back when you make

57
00:04:45,160 --> 00:04:53,620
a get request for a particular location and you pass your API key, and that request is successful and

58
00:04:53,620 --> 00:04:59,310
authenticated, what comes back is a fairly large chasten file with a lot of information.

59
00:04:59,650 --> 00:05:06,100
So then what you need to do is to decode this Jason file and extract the information that you need out

60
00:05:06,100 --> 00:05:06,410
of it.

61
00:05:07,330 --> 00:05:09,370
So here you see temperatures.

62
00:05:09,370 --> 00:05:11,320
I believe these are in Fahrenheit.

63
00:05:12,190 --> 00:05:21,130
You can you can change the unit used in the degree response to whatever you want.

64
00:05:21,130 --> 00:05:29,680
And you just need to use the units parameter here, standard Petrich, Imperial and muchly using.

65
00:05:31,970 --> 00:05:39,070
Units for the metric system right here, so I'm getting degrees Celsius, for example, instead of Fahrenheit,

66
00:05:39,070 --> 00:05:41,080
but they say you can make the change yourself.

67
00:05:41,800 --> 00:05:44,670
So here is the example, Jason file.

68
00:05:45,130 --> 00:05:53,260
I'm going to show you in a minute how to pinpoint and extract specific bits of data out of this chasten

69
00:05:53,260 --> 00:05:54,010
response.

70
00:05:54,010 --> 00:05:57,620
But again, as you can see, there's loads here that you can pick from.

71
00:05:58,420 --> 00:06:02,610
This is what makes this service such a good example for this lecture.

72
00:06:04,350 --> 00:06:04,960
All right.

73
00:06:05,080 --> 00:06:13,000
Now let's have a look at the actual script, importing the usual modules, as I have with the other

74
00:06:13,000 --> 00:06:21,460
examples in this set of lectures and using the same construct to open up the credentials file, setting

75
00:06:21,460 --> 00:06:24,440
the headers, as I have before, said the drill.

76
00:06:24,460 --> 00:06:27,880
I'm going for the one with the idea parameter here.

77
00:06:29,390 --> 00:06:39,340
And passing the app, I'd like this so I can grab the open with a key from the credentials file, then

78
00:06:39,340 --> 00:06:44,920
the new Connect method is identical to what you've seen in the past.

79
00:06:45,340 --> 00:06:46,330
Same thing lines.

80
00:06:46,330 --> 00:06:49,110
Seventy and seventy two are identical.

81
00:06:49,120 --> 00:06:57,280
No change is still same thing with lines and four to seventy eight just printing out the IP address

82
00:06:57,820 --> 00:07:03,400
that my wi fi network has given me in line eighty one.

83
00:07:03,550 --> 00:07:10,750
I make the request and it's just a simple get request to this eurorail up here.

84
00:07:11,970 --> 00:07:17,780
And then print out the response and because we know that the responses are chasten document, different

85
00:07:17,790 --> 00:07:26,220
payload, I just use the load's method of the Chechen war to convert this response into an object that

86
00:07:26,220 --> 00:07:31,900
then I can use Jason module notation to extract the individual components of.

87
00:07:32,520 --> 00:07:34,500
So there's one little issue here with the weather.

88
00:07:35,040 --> 00:07:41,940
Now notice that the weather key contains an array, so it does not contain a JSON document, just like,

89
00:07:42,090 --> 00:07:47,240
say, the coordinates key contains some of the adjacent document in here.

90
00:07:47,580 --> 00:07:54,380
Instead, the way the key contains an array and inside the array you've got the adjacent document.

91
00:07:54,570 --> 00:08:04,680
And for this reason, I'm using this construct here to go inside the the key and then extract the individual

92
00:08:04,680 --> 00:08:13,650
components such as Maine and description using this for in the back, whether annotation and that allows

93
00:08:13,650 --> 00:08:17,670
me to extract the keys and the values that I want out of this array.

94
00:08:17,670 --> 00:08:18,120
So.

95
00:08:20,640 --> 00:08:22,070
But for the rest, it's all straightforward.

96
00:08:22,170 --> 00:08:31,260
For example, if I want to grab the wind speed, the wind speed is let's see, where is that wind speed

97
00:08:31,360 --> 00:08:31,850
think?

98
00:08:35,950 --> 00:08:44,710
There's wind speed, you can see this speed is inside the wind key, so I'm just using the regular chasten

99
00:08:44,710 --> 00:08:48,550
notation to extract those pieces of data.

100
00:08:48,760 --> 00:08:55,650
Same thing with the wind degrees or the direction of the wind and so on.

101
00:08:55,670 --> 00:09:03,910
Some pulling out some information that I'm just able to decode and extract out of the chasten response

102
00:09:04,030 --> 00:09:08,270
that I got from the weather map, that VOYCE website.

103
00:09:09,430 --> 00:09:12,840
So let's try this out.

104
00:09:12,850 --> 00:09:21,280
I have already uploaded the credentials file to my especially to it's right there and I'm going to click

105
00:09:21,280 --> 00:09:28,210
on the script button and handed the script executed on the device.

106
00:09:28,390 --> 00:09:28,800
All right.

107
00:09:29,260 --> 00:09:29,830
So.

108
00:09:32,190 --> 00:09:39,990
Let's have a look at what's happening in New York and take that idea, just paste it right here.

109
00:09:43,920 --> 00:09:50,760
And that gives me New York City information, so let's compare the Web version of the weather in New

110
00:09:50,760 --> 00:09:53,100
York with the AP version.

111
00:09:53,340 --> 00:09:57,650
So the weather, as reported by the AP, is actually similarities.

112
00:09:57,660 --> 00:10:00,950
For example, it feels like minus four point fifty six degrees.

113
00:10:00,960 --> 00:10:04,590
We get some decimals here on the Web version.

114
00:10:04,590 --> 00:10:06,390
We only get an integer.

115
00:10:06,390 --> 00:10:07,830
So minus four degrees.

116
00:10:08,340 --> 00:10:10,470
What I can say that is pretty much the same.

117
00:10:11,580 --> 00:10:13,320
The humidity is 55 percent.

118
00:10:15,040 --> 00:10:23,860
That that matches the pressure at sea level one thousand twenty four to Pascal, that matches will.

119
00:10:25,740 --> 00:10:30,380
The there's a discrepancy in the wind speed.

120
00:10:30,430 --> 00:10:32,970
You've got two point thirty four meters per second.

121
00:10:34,120 --> 00:10:40,300
Here we've got one point five meters per second, I guess this is the direction north, which pretty

122
00:10:40,300 --> 00:10:44,340
much matches the degrees here, three forty nine degrees approximately north.

123
00:10:45,040 --> 00:10:53,260
So the current information on the website looks like this is a pretty concise and we have a lot more

124
00:10:53,260 --> 00:10:57,610
information here in the AP version for the current weather.

125
00:11:00,580 --> 00:11:09,370
One thing that I want to expand on is the temperature here we see the responses in object.

126
00:11:10,620 --> 00:11:11,910
But let's see.

127
00:11:13,910 --> 00:11:16,160
If we can print.

128
00:11:17,840 --> 00:11:25,340
The chasten version of the response, I'm just going to print with a back, see if that comes out.

129
00:11:27,420 --> 00:11:29,790
All right, so let's do a run.

130
00:11:32,860 --> 00:11:42,850
All right, and now we see the full chasten response from the AP and we can see just kind of refresh

131
00:11:42,850 --> 00:11:47,180
this page and see if we can find anything else that is interesting.

132
00:11:47,890 --> 00:11:51,790
So now the wind is two point one meters per second.

133
00:11:52,720 --> 00:11:55,370
And let's see the wind in here.

134
00:11:55,660 --> 00:11:57,850
Two point three four.

135
00:11:59,390 --> 00:12:04,670
Meters per second and still north know everything else to match it the same way we have that visibility.

136
00:12:05,570 --> 00:12:07,580
That should be reported somewhere here.

137
00:12:07,720 --> 00:12:12,770
The only good visibility, 10 kilometers or 10000 meters, it's reported in meters.

138
00:12:13,940 --> 00:12:19,100
And finally, the two point that must be reported here is Will.

139
00:12:21,560 --> 00:12:24,200
You don't see that viewpoint here, so that is missing.

140
00:12:24,980 --> 00:12:25,940
OK, I'll leave it at that.

141
00:12:26,030 --> 00:12:33,750
That's how easy it is to extract current weather information from open weather map, already leaving

142
00:12:33,800 --> 00:12:35,820
your industry to the marketplace.
