#100DaysOfCode in Python Transcripts
Chapter: Days 43-45: Consuming HTTP services
Lecture: Demo: Data version two: Better results
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's make two quick improvements before we wrap up this application. First, we're always searching for runner.
0:06
How exciting is it to always run this program and just get the same results? Let's actually make this a thing that the users can input.
0:14
So we'll begin by creating a variable. We could just go and type a variable name, assign the value and put it over here, and use PyCharm to refactor.
0:21
Say I want a variable called keyword. That's pretty cool. Make sure it still runs, it does. But let's actually go and say input.
0:28
We'll ask the user for input, and we'll give them a prompt. We'll say keyword of title search for movie, or something like this.
0:37
So now when we run it down here I can search for away and we get The Runaways. I can search for runner and we should get Bladerunner,
0:47
and we can search for fight, Fight Club, whatever. Whatever we want to search for, we can now do that.
0:53
This is already really awesome, but notice the API is, I'm going to call it crummy. So we're going to fix that with one more thing.
1:00
We're going to use this thing called collections.namedtuple So down here I'll say I'm going to define this type that represents a movie.
1:08
It's going to be a collection.namedtuple, and the way it works is you say the name that it thinks its own name is,
1:14
and then you say all the fields that go in there. Now this turns out to be quite a pain, so let's go back over here and see what we get.
1:22
IMDB score, we have a rating, an we have all these things. So this is the typical value here, it's going to look like this
1:30
So in order for this to work well, we have to have all these values listed in series over here.
1:36
So, I'm just going to type this in, then I'll speed it up. Phew, there I've typed them all in and I'll maybe do a few
1:46
line breaks here just so it fits on the screen. I just typed in the top level elements here. So what we can do, is we can actually instead of just
1:55
churning JSON results, we can actually go over them and return these types, and we'll see why that's a benefit in just a second.
2:02
So we'll say this, we'll say for our end results, you know, the hits, maybe H, I don't know.
2:10
We're going to need a list, going to say movies, that's a list. I'm going to put this in here, so we'll say movies.append
2:16
and we need to append one of these movies. So we can create a new one and we have to type all of these values in here.
2:23
We have to say IMDB code equals R.get IMDB code. Title equals R.get title, and so on. Or, there's a way to unpack a dictionary, which is what this
2:35
is, back into keyword arguments with the names being the keys just like that. And, the way you do that is you say **, the dictionary name.
2:45
So it's as if you said IMDB code equals whatever value it has for IMDB code. Title equals whatever value it has for title.
2:53
It's just a super short, simple way to do that. So now if we return movies, up here is not going to work the
2:59
same, but now we can just say R.title, and things like that. HasScore, take that little bit away. HasScore, let's say,r., what did we call it?
3:14
IMDB Score, that, now let's try that. Now we are going to search for runner, stick with that. Boom! Look how cool that is.
3:25
Now, we're still not where we quite really wanted to be. If I hit dot, we get not, so as in not helpful!
3:32
One final trick of what we're going to do, let's go over here and we can use what's called type hint in Python.
3:39
Python is a dynamic language, it has no syntactical typing, but you can give the editors hints, and I can say this
3:45
is actually a string, so you say colon type name and the return value is a list which is actually defined in a typings module, so got to import that.
3:56
So we want typings.List. Notice the, let's put at the top here. And it's going to be a list of movies like this, okay.
4:04
So with a little bit of a hint, I can come over here and now I type r., look at that, director, duration, IMDB code, IMDB score.
4:14
Let's just add one more. With code r.code, now that's the way we like to write. All the help we can get. Let's search one more.
4:27
Anything on computer, of course! Hackers with code TT whatever HasScore 6.2. Awesome, there you have it. This is how we can consume the movie API.
4:38
When you've broken apart into our, our sort of user interaction bit of code here, our API access code here.
4:47
We use requests to actually get the data and convert it to JSON and we used a namedtuple to help package it up into
4:56
something that makes more sense to our program, as well as adding a little type hints, so that the editor actually leverages that help we gave it.