3. Testing the TasteDive API¶
TasteDive is a tool that:
helps you discover new music, movies, TV shows, books, authors, games, podcasts, and people with shared interests. – TasteDive
In the following exercise, we will use the TasteDive API to search for works or artists similar to another of our choice. The documentation for the TasteDive API.
In this case, we will use the requests
library to make the API request. The base url
is "https://tastedive.com/api/similar"
. To this url, a parameter q
with the
value of the artist Ariana Grande will be passed. Finally, the url will look like this: "https://tastedive.com/api/similar?q=ariana+grande"
.
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 band Coldplay. 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 TasteDive for the movie Coco. Then the dictionary parameters
should have the
value "Coco"
assigned to the key "q"
. Additionally, this time we only want 5 results instead of 20.
For this, there is a parameter called "limit"
, which can be assigned to the number of results needed.
Another parameter that will be passed to the url will be "info"
and its value will be 1. This will make
the results come with extra text with information about the movie.
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). Because we set a limit, this number should match the limit.
Now, you will create the list similar_movies
. Inside data
you have a dictionary of dictionaries
and lists. What you will do is to search through the sets within which are the names of the movies
similar to Coco, and you will add the names of those movies to similar_movies
. There should be 5 in total.
Hint: the movie data is located within data["Similar"]["Results"]
, and the key to access it is "Name"
.
Lastly, you will search for the number of times the word "Pixar"
appears in the information texts of the
movies related to Coco. You will save that number in the variable pixar
. Hint: "wTeaser"
is the
key that stores the text. This key is located in the same dictionary as the movie names.