Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 10: Movie Search App
Lecture: Exploring the search API

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now we know where our data is coming from let's write some code and get started on this app.
0:05 We're going to do it in two steps, so let's take this url here this movie service.talkpython.fm/api/search/ the name there
0:14 and let's go over here and we're just going to write some sort of play around code in the beginning
0:20 and then we'll structure this into a proper program or proper app in a minute. So we're going to go and make a request against this
0:26 and let's make it really clear that there is a search term happening here so we'll say format search, search equals this, I'll search for a capital
0:35 and we're going to need to make a request against this service, well, we've already seen one of the best ways to do that
0:42 is to use the request package, so import request; now I don't have request installed in the virtual environment here
0:50 so we'll go ahead and install that, you might be using it your system wide Python and have it left over from other examples
0:59 or maybe using the same virtual environment but here I'm an going to install it separately; we're also going to be using named tuples,
1:05 so I'm going to import collections while we're at it, so let's start by going and doing a request against this,
1:11 so we'll say the response is request.get url, and then let's just print out the status code and go ahead and run this, see what we get.
1:20 Status code 200, that's good, so we could actually print out the text that we got back
1:27 that looks like the javascripty json text that we're looking for, we could use the json module and parse that,
1:34 but it turns out that if we have let's say I have a variable here it turns out that request is very commonly used to talk to these javascript apis
1:44 and it can automatically turn this into the Python dictionaries that we're going to work with, so now I could print out this
1:51 let's say the type of movie data and maybe data, we'll see what we get back
1:57 so if we run this, we get a dictionary, not the strings, but an actual dictionary which we can use to start working with this data.
2:05 So notice we have our keyword capital in our hits which is an array of these movies, so we're already on a good path here,
2:14 we probably should do some kind of check to make sure everything is ok and we could say something like if response.status code is not 200,
2:24 do something, but request has a nice little thing here we can say response.raise for status so this is going to cause an error an exception
2:34 if something went wrong basically if it's not a successful status code, all right
2:39 and we'll talk about how to handle these types of situations in a little bit but for now, let's just keep playing around with this.
2:46 So we've got our movie data, and we actually care about our movies and that's going to be going to the movie data and we want to get out the hits,
2:55 so let's print out our movies here and see what we get. Now this time it shouldn't come back as a dictionary,
3:03 notice the square bracket, this should come back as an array, a list; so here's a list, and then these are all of the movies,
3:09 now the next thing we want to do is convert them just from these flat dictionaries into something that's more useful for application,
3:19 we've already seen that named tuples are really powerful and let us work with data better,
3:24 we could go and create classes like we did in the wizard app, but I think probably named tuples are good enough
3:30 for what we're trying to do here, so let's go and set that up.
3:33 Let's call this a movie result and we're going to set that to be a collection.named tuple,
3:40 and we need the name right here to be exactly the same so movie results, and then the next thing that goes in here is actually
3:48 a list of all of the fields, and you can see we have rating we have duration, we have title, we have imdb code
3:54 and it needs to match up, well, it should match up exactly to this, to make it as easy as possible, we could of course transform it
4:01 and if we don't want imdb code we just want to say imdb or just code or something like that, we could change it but we have to account for that later;
4:09 so our plan is to just use the same field names or attribute names here, so I've already copied those over, so you don't have to watch me type them in,
4:17 so we have this movie result and let's go instead of printing these out, let's actually go and loop over all of this data that we got back
4:27 and build up a list of these movie results, so let's rename this to movie list or something like that
4:33 and then we'll define our movies, this is the thing we really are after we really want to work with, we're going to add a bunch of these movie results
4:40 converted from the data we got from the web service. So we'll say for md in a movie list, all right and then we want to come down here
4:51 and we're going to create one of the movie results and I'm going to do this in three different ways,
4:57 I'm going to start out in the most sort of verbose least Pythonic way and we're going to improve it in the next video in a couple of different ways.
5:05 So we're going to say m is a movie result, then we need to set a bunch of stuff, we need to set the imdb code is equal to md.get,
5:15 imdb code we're going to come over here and we're going to set the title to be the title and I'm just going to knock the rest of these out.
5:28 There we go, that was a lot of typing so we've got all of these here and some of these are numbers
5:33 so we might want to be a little careful we could say things like the score if you don't have it in here in this dictionary get
5:41 instead of giving us none you could give us zero, same maybe for the year or the rating, things like that
5:47 but this is not entirely necessary I believe all of them have a year and rating. Okay, so now we're just going to say movies.append,
5:55 and give it this m, so now we could do things like this we could just print out something about the movies,
6:05 so we can print them out and something like this we could say, we could just put the year and the title
6:10 we could also save print found some number of movies for search something like this
6:15 the length of the movies and the search string that we sent in, okay, so let's run this one more time and we should go find a couple movies for capital
6:26 and then print them out here. Look at that, searching for capital, we get Supercapitalist, Capital C and Capitalism A Love Story,
6:32 let's go and search for a runner, oh look at that, we found Blade Runner, Kite Runner, Logan's Run,
6:39 let's make it feel a little more real, I had let the user input something here, so let it come down here, alright,
6:46 so now we could do blade for Blade Runner or something like that, we got Sling Blade, Dragon Blade, Blade Runner,
6:53 we come down here and look for a runner, get these again beautiful all right so it looks like this is working,
7:00 we'll see that we can actually improve upon this in a couple of ways, make this much more concise and Pythonic.


Talk Python's Mastodon Michael Kennedy's Mastodon