3. Testing the TVMaze APIΒΆ
TVMaze is a tool that:
Allows you to search for TV shows and get information about them. Provides a RESTful API to access the data.
In the following example, we will use the TVMaze API to search for TV shows. The base url is TVMaze API.
In this case, we will use the requests
library to make the API request. The base url
is "https://api.tvmaze.com/search/shows"
. To this url, a parameter q
with the
value of the show big bang theory will be passed. Finally, the url will look like this: "https://api.tvmaze.com/search/shows?q=big+bang+theory"
.
Note that after the base url, a ?
is written to indicate that the parameters follow.
In the previous example, you could see that the API returns text, which if passed through json.loads
transforms into a Python dictionary. However, it is not entirely readable. This can be solved with
json.dumps
.
Now, we will request information about the show golden girls. This time we will print the data in a
readable format. We will use urllib
to make the request.
The following exercise comes with automatic grading.
Now we will ask TVMaze for the show suits. Then the dictionary parameters
should have the value
"suits"
assigned to the key "q"
.
First, you will request from the API what was described above, and save this in the variable request
.
In another variable, request_url
, save the url of the request. Then, assign the data to the variable data
.
Next, assign the variable results
the number of results that the request returned
(as was done in the previous example).
Now, you will calculate the average rating of all the shows that were returned.
You will save this in the variable average_rating
. Hint: the ratings are located within
data["show"]["rating"]["average"]
. You will need to use a for loop to calculate the average.
Lastly you will search for the number of times the word "Drama"
appears in the genres related to suits.
You will save that number in the variable drama_count
. Hint: the genres are located within
data["show"]["genres"]
. You will need to use a for loop to calculate the number of dramas.