17_Movie App
๐ป ์ฃผ์ : API๋ฅผ ํ์ฉํ ์ํ ์๊ฐ ํ์ด์ง.
- TMDB์ API๋ฅผ ์ฌ์ฉํด ์ํ ์ ๋ชฉ, ๋ฑ๊ธ, ์ด๋ฏธ์ง๊ฐ ์๋ ์ํ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ๋ฐ๋๋ค.
โ https://www.themoviedb.org/settings/api- UI(HTML, CSS)๋ฅผ ๋ง๋ค๊ณ , API ํค๋ฅผ IMDB ์๋น์ค์ ๋ฑ๋กํ๋ค.
- JavaScript๋ฅผ ์ถ๊ฐํด Fetch ์์ฒญ์ ๋ง๋ค์ด ๋น์ ๊ฐ์ฅ ์ธ๊ธฐ ์์๋ ์ํ๋ฅผ ๋ณด์ฌ์ค ๋ฐ์ดํฐ๋ฅผ ์ป๋๋ค.
- 8 ์ด์ : ์ด๋ก | 5 ~ 8 : ์ฃผํฉ | 5 ์ดํ : ๋นจ๊ฐ
- ๊ฒ์๊ธฐ๋ฅ์ ํตํด ์ ๋ชฉ ๊ฒ์ ๊ฐ๋ฅ
const API_URL = 'https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=ae312a16848ed5ab44faf6aeb5640fa5&page=1';
const IMG_PATH = 'https://image.tmdb.org/t/p/w500';
const SEARCH_API = 'https://api.themoviedb.org/3/search/movie?api_key=ae312a16848ed5ab44faf6aeb5640fa5&query="';
const main = document.getElementById('main');
const form = document.getElementById('form');
const search = document.getElementById('search');
// Get initial movies
getMovies(API_URL);
async function getMovies(url) {
const res = await fetch(url);
const data = await res.json();
showMovies(data.results);
}
function showMovies(movies) {
main.innerHTML = ''
movies.forEach((movie) => {
const { title, poster_path, vote_average, overview } = movie;
const movieEl = document.createElement('div');
movieEl.classList.add('movie');
movieEl.innerHTML = `
<img src="${IMG_PATH + poster_path}" alt="${title}">
<div class="movie-info">
<h3>${title}</h3>
<span class="${getClassByRate(vote_average)}">${vote_average}</span>
</div>
<div class="overview">
<h3>Overview</h3>
${overview}
</div>
`
main.appendChild(movieEl);
})
}
function getClassByRate(vote) {
if(vote >= 8) {
return 'green';
} else if(vote >= 5) {
return 'orange';
} else {
return 'red';
}
}
form.addEventListener('submit', (e) => {
e.preventDefault()
const searchTerm = search.value
if(searchTerm && searchTerm !== '') {
getMovies(SEARCH_API + searchTerm)
search.value = '';
} else {
window.location.reload();
}
})