Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: Calling the weather API intro
0:00 Now that we have our location in structured data, not just plain text, we're gonna be able to call our weather API.
0:07 So let's say "data = call_weather_api" and we're gonna pass this location over. And as we've been doing,
0:16 let's go over here and write this function. Well, we start out by having our URL, remember we go back
0:24 to our example, it's gonna be "weather.talkpython.fm/api/weather" and then stuff up here. So let's go and work with it like this.
0:33 So it's gonna be this base URL up to here and then city, what goes in here is going to be location dot.. Check out the named tuple,
0:42 so cool! Now the state is tricky, so I'm gonna put the state to the side for a second here,
0:47 and the country is going to be "location dot country" like that, and then the units, I'm gonna just hard code it to Imperial.
0:57 You can hard code it to metric or if you want to pass metric or imperial and let the user decide how they like it,
1:04 go for it. But to me I'm just gonna leave it like this because you gotta pick one, and I'll leave it like that.
1:08 Alright, so now it gets a little bit interesting, because if they pass a state, we want to use it, but if they don't pass a state,
1:15 same state equals blank, might throw off the API and freak it out. So what we want to do is we'll just do a little test, if location
1:22 dot state, then we're going to say "url plus equals ampersand, make that an f-string as well, state equals state, like that. Location,
1:33 not state. There we go. Okay, So if you pass a state through our structured data, we're going to tack it on with an ampersand.
1:41 The order of the query string, these key value pieces, this whole thing is a query string,
1:45 the order, the key value pairs in the query string, doesn't matter. So if it goes at the end with units in the middle, doesn't matter at all. Alright,
1:53 so now let's just print out the URL, would call, and with PyCharm, here's a cool thing with f-strings:
2:00 I can type curly brace, and notice the curly brace is still stringy, it's still green, it's not actually interpreted as anything,
2:07 and there's no f at the front. But if I start to type of variable like URL, it's still not an f-string.
2:14 But if I hit enter, it puts the f at the front and completes it. So that's pretty cool. Let's just, and I'll just say for now it's supposed to
2:22 return something, so I'll say it returns "None". Functions always return None anyway, but whatever. Say "Portland, US",
2:30 like that. The country and the state need to be two digits, or two characters. So check it out,
2:36 we would call Portland, US with imperial units and let's click it, see what we get. Look at that.
2:42 Broken clouds, 65 degrees. A wonderful fall day here in Oregon. Now let's call this again, but this time let's get Portland,
2:51 Tennessee, actually, yeah sure, Portland, Tennessee. Like that and says, notice the state says TN at the end, and here we have Portland, US,
3:02 Tennessee and the weather is 60 degrees. Looks like it's also nice. Little more clear skies, but not the same forecast to be clear.
3:09 Cool, huh? This is great. We're actually structuring what we need to send over to the API perfectly. But now what? This "would call" needs to turn into
3:19 "I actually went to the Internet and got data and understood what came back", but we're off to a great start.