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