Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 10: Movie Search App
Lecture: Adding search to the app
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Now, it's fun to play around and sort of explore the api, but let's build something that's a little bit more formal, a little bit more of a proper app,
0:09
and instead of having you watch me type it all out, I kind of put it together and we'll just go over the skeleton bit of it,
0:13
and then we'll actually make it work here next. So, we have this main method, and it's going to do two things
0:19
it's going to print out the header and you can imagine that just prints out movie search app, nothing major there,
0:25
and then it runs this search event loop here, and we're using our dunder main convention;
0:29
so down here it's just going to say go through this while loop, and long as you don't press x, it's going to let you keep searching,
0:37
so press x to exit, but if you don't press that, it's going to go do a search, right, we'll go down here, and we're going to run this little search,
0:44
so we'll go to this thing that I've created called a movie service, and we will call the find movies function,
0:49
and then this is exactly the same thing we did before, just printing out the year and title, and if you do hit x
0:54
it gives you this little message like see you. Let's look at the movie search, really I just moved the stuff that was in a play around function
1:01
over here and put it into a function we can call, so we've got our movie, result, name tuple, exactly like before,
1:07
we've got our url, now we're passing in the search text, and we're calling requests, checking for the errors
1:15
to make sure everything's ok to carry on, converting to json, getting the movie list, doing our cool list comprehension here,
1:19
with our dictionary unpacking thing there, and then we're returning them. I guess one more thing that might be fun,
1:27
let's run this real quick just to see that it basically works the same, except for now it goes in a loop, so we could search for let's say 'cats',
1:33
we get a bunch of cats, we could search for 'runner' we get runner, and so on but notice, the years are just kind of random,
1:40
whatever order it comes back from the service, suppose that we would like instead to see the newest movies first,
1:48
so let's go ahead and upgrade this, we'll same movies.sort and here we're going to sort, we're going to say key = lambda
1:55
given a movie say m, how are we going to sort it, we could say sort by m.year, and that would show oldest to newest
2:04
let's try that, 'runner', see Logan's Run for 76, then Blade Runner, now it's sorted, that's great, but not what we were hoping.
2:13
We can sort for numerical values, which this is a number luckily, we can sort from numerical values and reverse this by reversing the key,
2:20
so put a negative there, now if I search 'runner' then we get 2014, 2013, and so on, I installed little programs, every time we restart it,
2:31
PyCharm is creating another one, let's get rid of these, and we can fix that real quick.
2:36
We'll go up here, it's just single instance only, now it's going to restart it, so if I run it again, it will say I'm going to restart this, it's fine.
2:43
Great, so it looks like this is working, we have our skeleton of our app going, and now we've moved this finding movies capability over here
2:53
to this movie service and our program is just using it here. So it looks like everything's great, but it turns out
3:01
things on the internet can break, so let's go explore that next.