Now you have seen the videos from day 43 and have some experience with JSON APIs. Over the next three days, we will build an application that lets you search Talk Python To Me using a JSON API.
Today is mostly watching the corresponding videos from the course. Be sure to watch the videos first. Then:
- Create a new empty Python project with a virtual environment
- Reminder: Virtual environments are created using the commend
python3 -m venv .env
(use python rather than python3 for the command on Windows). - Activate the environment:
- macOS / Linux:
. .env/bin/activate
- Windows:
.env/scripts/activate
- macOS / Linux:
- Install
requests
withpip
- Create a
program.py
Python file and supportingapi.py
file - Import requests inside the
api.py
, import api inprogram.py
, and runprogram.py
to make sure it's wall hanging together. - Install Postman for exploring the API.
Today, you will work with the search backend of Talk Python To Me:
Open that link and poke around a bit.
Now to properly explore the API, open Postman (you did install it the day before right?) and explore some of the search end-points. Study the structure of the resulting JSON.
Your goal today will be to flesh out program.py
and api.py
to:
- Get a search word from the user
- Call the search service via requests
- Verify the success of this
- Return basic dictionaries to
program.py
and list the resulting titles
You'll be done when you see something like:
$ python3 program.py
******* SEARCH TALK PYTHON *******
What keywords to search for? <ENTER WORDS>
There are 7 matching episodes:
1. Past, Present, and Future of IronPython
2. Deep Dive into Modules and Packages
3. Python at Netflix
4. ...
Your app is basically working. Today we'll polish it up a bit with some code cleanup and user interaction.
Start with code cleanup. We have been passing dictionaries around. These are not so much fun. Let's use namedtuples
. You create one like this:
import collections
Movie = collections.namedtuple('Movie',
'imdb_code, title, director, keywords,'
'duration, genres, rating, year, imdb_score')
Define a corresponding namedtuple
for search results and refactor your code to use this type instead of passing raw dictionaries.
Now for the interaction cleanup. Once you display the results, ask the user if they want to view any of them (use an index, ask for a number of the listed ones or use the episode ID returned from the service (e.g. 142)).
When they pick one, use the URL from the service response and open the users default web browser to that page. Sounds complicated, in Python it's just:
import webbrowser
webbrowser.open(full_url, new=2)
Once you have that interaction working, you're done with these three days of the challenge!
Be sure to share your last couple of days work on Twitter or Facebook. Use the hashtag #100DaysOfCode.
Here are some examples to inspire you. Consider including @talkpython and @pybites in your tweets.
See a mistake in these instructions? Please submit a new issue or fix it and submit a PR.