728x90
๋ฐ์ํ
ํ๋ก๊ทธ๋๋ฐ ์ธ๊ณ๋ฅผ ํ๊ตฌํฉ์๋ค.
์ ํ๋ธ API๋ฅผ ํ์ฉํด์ ์์ ๋ฐ์ดํฐ๋ฅผ ํ์ด์ง์ ์ถ๋ ฅํ ์ ์์ต๋๋ค.
๊ตฌํ ํ๋ฉด
์ ํ๋ธ API๋ฅผ ์ด์ฉํด ์ ๊ธฐ์ฐจ ๊ด๋ จ ์์์ ์ถ๋ ฅํ์์ต๋๋ค. ์นดํ ๊ณ ๋ฆฌ๋ฅผ ์ ๊ธฐ์ฐจ์ ์นํ๊ฒฝ์ผ๋ก ๋๋์ด ํญ๋ชฉ์ ๋ฐ๋ผ ๋ค๋ฅธ ์์์ ๋ณผ ์ ์๊ฒ ํ์์ต๋๋ค.
์์ฑ ์ฝ๋
HomeController.java
@GetMapping("/media/reels")
public String reels(Model model) {
User user = (User) session.getAttribute("user_info");
if (user != null) {
int userCoins = user.getCoin();
model.addAttribute("userCoin", userCoins);
}
return "/media/reels";
}
reels.html
<!DOCTYPE html>
<html>
<head th:replace="common/head">
</head>
<style>
@font-face {
font-family: 'TheJamsil5Bold';
src: url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_2302_01@1.0/TheJamsil5Bold.woff2') format('woff2');
font-weight: 500;
font-style: normal;
}
body {
font-family: 'TheJamsil5Bold';
height: 100vh;
/* ํ๋ฉด ๋์ด์ 100%๋ก ์ค์ */
background-color: #000;
background-size: cover;
/* ์ด๋ฏธ์ง๋ฅผ ํ๋ฉด์ ๋ง๊ฒ ์กฐ์ */
background-repeat: no-repeat;
/* ์ด๋ฏธ์ง ๋ฐ๋ณต์ ์ค์ง */
}
#videos {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center; /* ์ถ๊ฐ๋ ์คํ์ผ */
height: 100vh; /* ์ถ๊ฐ๋ ์คํ์ผ */
}
/* ์์ ์ปจํ
์ด๋ ์คํ์ผ */
.video-container {
width: 30rem;
margin: 2rem;
box-sizing: border-box;
background-color: #f9f9f9;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1);
transition: all 0.3s ease;
}
.video-container iframe {
width: 100%;
height: 17rem;
border: none;
border-radius: 5px;
}
.video-container:hover {
transform: translateY(-5px);
box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.3);
}
.video-title {
margin: 1rem;
font-size: 1rem;
color: #333;
text-align: center;
}
/* ๋ชจ๋ฌ ์คํ์ผ */
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.7);
}
.modal-content {
position: relative;
background-color: rgba(0, 0, 0, 0.7);
margin: 10% auto;
padding: 20px;
width: 80%;
max-width: 800px;
}
.close {
position: absolute;
right: 10px;
top: 0;
color: white;
font-size: 28px;
font-weight: bold;
cursor: pointer;
}
</style>
<body>
<div th:replace="common/header"></div>
<nav th:replace="common/nav"></nav>
<div style="text-align: center;">
<label for="electricCarCheckbox" style="color: white;">
<input type="checkbox" id="electricCarCheckbox" onchange="updateVideos" checked>
์ ๊ธฐ์ฐจ
</label>
<label for="ecoCheckbox" style="color: white;">
<input type="checkbox" id="ecoCheckbox" onchange="updateVideos">
์นํ๊ฒฝ
</label>
</div>
<div id="videos"></div>
<!-- ๋ชจ๋ฌ HTML -->
<div id="videoModal" class="modal">
<div class="modal-content">
<span class="close" id="closeModal">×</span>
<iframe id="videoPlayer" width="730" height="400" src="" frameborder="0" allowfullscreen></iframe>
</div>
</div>
<script src="https://apis.google.com/api์ฃผ์"></script>
<script>
var defaultKeyword = '์ ๊ธฐ์ฐจ';
// Add an event listener to the "์นํ๊ฒฝ" checkbox
document.getElementById('ecoCheckbox').addEventListener('change', function () {
// If "์นํ๊ฒฝ" is checked, uncheck "์ ๊ธฐ์ฐจ"
if (this.checked) {
document.getElementById('electricCarCheckbox').checked = false;
}
updateVideos();
});
// Add an event listener to the "์ ๊ธฐ์ฐจ" checkbox
document.getElementById('electricCarCheckbox').addEventListener('change', function () {
// If "์ ๊ธฐ์ฐจ" is checked, uncheck "์นํ๊ฒฝ"
if (this.checked) {
document.getElementById('ecoCheckbox').checked = false;
}
updateVideos();
});
var apiKey = '์ฑํค';
// YouTube Data API ๋ก๋
gapi.load('client', function () {
gapi.client.init({
apiKey: apiKey,
discoveryDocs: ["https://www.googleapis.com/๊ตฌ๊ธapi"]
}).then(function () {
// YouTube API ์ด๊ธฐํ ์ฑ๊ณต
searchVideos(defaultKeyword);
});
});
// ์ฒดํฌ๋ฐ์ค ์ํ๋ฅผ ํ์ธํ๊ณ ๊ฒ์์ด ์
๋ฐ์ดํธ
function updateVideos() {
var electricCarCheckbox = document.getElementById('electricCarCheckbox');
var ecoCheckbox = document.getElementById('ecoCheckbox');
var keyword = '';
if (electricCarCheckbox.checked && ecoCheckbox.checked) {
// ๋ ์ฒดํฌ๋ฐ์ค ๋ชจ๋ ์ ํ๋ ๊ฒฝ์ฐ, ๋ชจ๋ ๋์์์ ๊ฒ์ํฉ๋๋ค.
keyword = '์ ๊ธฐ์ฐจ ์ถฉ์ ์';
} else if (electricCarCheckbox.checked) {
keyword = '์ ๊ธฐ์ฐจ';
} else if (ecoCheckbox.checked) {
keyword = '์นํ๊ฒฝ';
} else {
// ๋ชจ๋ ์ฒดํฌ๋ฐ์ค๊ฐ ์ ํ ํด์ ๋ ๊ฒฝ์ฐ ๊ธฐ๋ณธ ํค์๋๋ก ๊ฒ์
keyword = defaultKeyword;
}
searchVideos(keyword);
}
// ํน์ ํค์๋๋ก ๋์์ ๊ฒ์
function searchVideos(keyword) {
gapi.client.youtube.search.list({
q: keyword,
part: 'snippet',
maxResults: 4
}).then(function (response) {
var videos = response.result.items;
displayVideos(videos);
});
}
function decodeHTMLEntities(text) {
var textArea = document.createElement("textarea");
textArea.innerHTML = text;
return textArea.value;
}
// ๋์์์ HTML์ ํ์
function displayVideos(videos) {
var videosDiv = document.getElementById('videos');
// Remove all existing videos
while (videosDiv.firstChild) {
videosDiv.removeChild(videosDiv.firstChild);
}
videos.forEach(function (video) {
var videoId = video.id.videoId;
var title = decodeHTMLEntities(video.snippet.title);
// ์์ ์ปจํ
์ด๋ ์์ฑ
var videoContainer = document.createElement('div');
videoContainer.className = 'video-container';
// iframe ์์ฑ
var videoFrame = document.createElement('iframe');
videoFrame.src = 'https://www.youtube.com/embed/' + videoId;
// ์ ๋ชฉ ์์ฑ
var videoTitle = document.createElement('h2');
videoTitle.textContent = title;
videoTitle.className = 'video-title';
// ์์, ์ ๋ชฉ์ ์ปจํ
์ด๋์ ์ถ๊ฐ
videoContainer.appendChild(videoFrame);
videoContainer.appendChild(videoTitle);
// ์ปจํ
์ด๋๋ฅผ videosDiv์ ์ถ๊ฐ
videosDiv.appendChild(videoContainer);
// ์์ ํด๋ฆญ ์ ๋ชจ๋ฌ ์ด๊ธฐ
videoContainer.addEventListener('click', function () {
openModal(videoId);
});
});
}
// ๋ชจ๋ฌ ์ด๊ธฐ
function openModal(videoId) {
var modal = document.getElementById('videoModal');
var videoPlayer = document.getElementById('videoPlayer');
videoPlayer.src = 'https://www.youtube.com/embed/' + videoId;
modal.style.display = 'block';
}
// ๋ชจ๋ฌ ๋ซ๊ธฐ
function closeModal() {
var modal = document.getElementById('videoModal');
var videoPlayer = document.getElementById('videoPlayer');
videoPlayer.src = '';
modal.style.display = 'none';
}
// ๋ชจ๋ฌ ๋ซ๊ธฐ ๋ฒํผ ํด๋ฆญ ์ ๋ซ๊ธฐ
document.getElementById('closeModal').addEventListener('click', closeModal);
// ๋ชจ๋ฌ ์์ญ ์ธ๋ฅผ ํด๋ฆญํ์ฌ๋ ๋ซ๊ธฐ
window.onclick = function (event) {
var modal = document.getElementById('videoModal');
if (event.target === modal) {
closeModal();
}
};
</script>
</body>
</html>
์ ๋ฐฉ์์ ํ์ฉํ์ฌ ์ ํ๋ธ ์์์ ํ์ด์ง์ ์ถ๋ ฅํ ์ ์์ต๋๋ค.
728x90
๋ฐ์ํ