Follow these instructions to update a website that displays characters from TV shows.
Click here to view the TV Characters Starter project. Copy the project to begin the activity.
There is significant code and functionality already built into this project.
When someone goes to the page, they are able to click the “Load Characters” button to see characters from Stranger Things appear.
The API that makes this possible is named TVMAZE. There are a variety of endpoints that return data about TV shows. The TV Characters site uses the /singlesearch/ endpoint to get data about Stranger Things, and the cast data is embedded. The URL is static, so it currently returns the same data each time. It looks something like this:
https://api.tvmaze.com/singlesearch/shows?q=Stranger Things&embed=cast
{
"id": 2993,
"url": "https://www.tvmaze.com/shows/2993/stranger-things",
"name": "Stranger Things",
"summary": "<p>A love letter to the '80s classics that captivated a generation, <b>Stranger Things</b> is set in 1983 Indiana, where a young boy vanishes into thin air. As friends, family and local police search for answers, they are drawn into an extraordinary mystery involving top-secret government experiments, terrifying supernatural forces and one very strange little girl.</p>",
"_embedded": {
"cast": [
{
"person": {
"name": "Winona Ryder",
"image": {
"medium": "https://static.tvmaze.com/uploads/images/medium_portrait/204/511427.jpg"
}
},
"character": {
"name": "Joyce Byers",
"image": {
"medium": "https://static.tvmaze.com/uploads/images/medium_portrait/406/1015330.jpg"
}
}
},
{
"person": {
"name": "Caleb McLaughlin",
"image": {
"medium": "https://static.tvmaze.com/uploads/images/medium_portrait/16/40907.jpg"
}
},
"character": {
"name": "Lucas Sinclair",
"image": {
"medium": "https://static.tvmaze.com/uploads/images/medium_portrait/410/1027426.jpg"
}
}
}
]
}
}
All the data that renders is in the _embedded.cast array property.
There are several functions in the script.js file that make the current site work how it does.
objectToHtml: takes a JSON data object and returns an HTML element with a nice image/namegetCharactersForShow: makes the request to the API - currently returns information about Stranger ThingsloadCharacters: makes the call to get the data, and then puts it on the pageRight now, the site is exclusively about Stranger Things. Update it so that it can display characters from any TV show!
First, there needs to be a way for the user to say which TV show they would like.
<h1>Characters</h1> element<input> elementtype attribute to be "text"id attribute to be "tv-show"placeholder attribute to be "Enter a TV Show"Run the project and verify that the new text box appears! The added HTML should look like this:
<input id="tv-show" placeholder="Enter a TV Show" />
Next, the JavaScript code needs to take what the user enters, and pass it along to get the data.
Currently, the getCharactersForShow function does not take any arguments. Add a parameter so that it will be possible to pass a show search query into the API.
getCharactersForShow function definitionshow between the parenthesesThe updated code should look like this:
async function getCharactersForShow(show) { /* ... */ }
Now, it will be possible to pass the value to the function call.
loadCharacters function definitioncharacters variable creationtvShowInputdocument.querySelector to grab the <input id="tv-show"> from the HTML, and store it in the tvShowInput variabletvShowtvShow to be the value of tvShowInputgetCharactersForShow by passing tvShow as an argumentNothing will actually change yet, but now the getCharactersForShow function has a show to use! The updated code should look something like this:
let tvShowInput = document.querySelector("#tv-show");
let tvShow = tvShowInput.value;
let characters = await getCharactersForShow(tvShow);
Here’s where the magic happens. The API call can be updated to search for whatever show the user entered in the text box!
getCharactersForShow functionurl variable creationStranger Thingsshow parameterRun the project, enter a real TV show, click the button, and verify that characters from that TV show appear! The updated URL code should look something like this:
let url = `https://api.tvmaze.com/singlesearch/shows?q=${show}&embed=cast`;
Now this is all well and good, but what happens when a fake show title is entered? Luckily, there is a try/catch in place, so a message like TypeError: Cannot read properties of null (reading '_embedded') may be alerted. That message is not very clear, though… the site can do better.
HTTP responses have status codes that indicate the success of the request. For example, a 404 error indicates that a resource has not been found. In the case of the TVMAZE API, it will return a 404 response if a TV search does not return any results.
The status code is returned with the response object from a fetch call, so it is possible to check it. It is also possible to use throw to display a new message to bubble up in the try/catch.
The goal will be to display a new message if the search fails, and the entered show is not found!
After the response is returned, it will be possible to handle any errors gracefully.
getCharactersForShow function definitionfetch call in the try blockif structureif condition, check if response.status is equal to 404if statement, use throw to send an error message of “Show not found ☹”Now, run the project, enter a fake show, and verify that a nicer error message is alerted! The added code should look something like this:
if (response.status === 404) {
throw `Show not found ☹`;
}
This activity showed another example of an API with a lot of possibility. The code only used a small part of the data returned, but so much more of it could be useful! Here are some ideas for this API:
Feel free to use the API for any desired purpose.