YT SEARCH NEW 1

YouTube API Search

Login

Sign Up

Wallet Points: 0

Contest Section

Contest Points: 0

Winner Details

async function searchYouTube() { const searchInput = document.getElementById('search-input').value; const url = `https://www.googleapis.com/youtube/v3/search?q=${searchInput}&part=snippet&type=video&maxResults=10&key=${apiKey}`; try { const response = await fetch(url); const data = await response.json(); displayResults(data); nextPageToken = data.nextPageToken; totalResults = data.pageInfo.totalResults; } catch (error) { console.error('Error:', error); } } async function displayResults(data) { const resultsContainer = document.getElementById('results-container'); resultsContainer.innerHTML = ''; for (const item of data.items) { const videoItem = document.createElement('div'); videoItem.classList.add('video-item'); const title = item.snippet.title; const publishedDate = new Date(item.snippet.publishedAt); const currentDate = new Date(); const stats = await getVideoStats(item.id.videoId); videoItem.innerHTML = `

${title}

Character Count: ${title.length}

Published Date: ${publishedDate.toLocaleString()}

Total Days: ${Math.floor((currentDate - publishedDate) / (1000 * 60 * 60 * 24))}

Comments Count: ${stats.commentCount}

Likes Count: ${stats.likeCount}

Comments to Like Ratio: ${calculateRatio(stats.commentCount, stats.likeCount)}

`; resultsContainer.appendChild(videoItem); } } async function addToContest(button, title, characterCount, totalDays, commentToLikeRatio) { const contestContainer = document.getElementById('contest-container'); if (!button.classList.contains('added')) { const total = totalDays * 0.01 + (characterCount * commentToLikeRatio); contestVideos.push({ title, total, addedBy: selectedUser }); const contestItem = document.createElement('div'); contestItem.classList.add('video-item', 'added'); contestItem.innerHTML = `

${title}

Total Character Count: ${characterCount}

Total Days: ${totalDays}

Comment To Like Ratio: ${commentToLikeRatio}

Total: ${total}

Added By: ${selectedUser}

`; contestContainer.appendChild(contestItem); button.classList.add('added'); button.innerText = 'Added to Contest'; } else { alert('Already added to contest.'); } } async function getVideoStats(videoId) { const statsUrl = `https://www.googleapis.com/youtube/v3/videos?part=statistics&id=${videoId}&key=${apiKey}`; try { const response = await fetch(statsUrl); const data = await response.json(); return data.items[0].statistics; } catch (error) { console.error('Error:', error); return {}; } } function calculateRatio(commentCount, likeCount) { const comments = parseInt(commentCount); const likes = parseInt(likeCount); return comments / likes; } async function loadMore() { if (nextPageToken) { const searchInput = document.getElementById('search-input').value; const url = `https://www.googleapis.com/youtube/v3/search?q=${searchInput}&part=snippet&type=video&maxResults=10&pageToken=${nextPageToken}&key=${apiKey}`; try { const response = await fetch(url); const data = await response.json(); displayResults(data); nextPageToken = data.nextPageToken; } catch (error) { console.error('Error:', error); } } else { alert('No more results to load.'); } } function updateGlobalTime() { const currentTime = new Date(); const minutes = currentTime.getMinutes(); const seconds = currentTime.getSeconds(); document.getElementById('global-time').innerText = `Global Time: ${minutes}:${seconds}`; if (seconds === 0 && minutes % 5 === 0) { announceWinner(); } } function announceWinner() { if (contestVideos.length > 0) { const winner = contestVideos.reduce((prev, current) => { return prev.total > current.total ? prev : current; }); const winnerDetails = document.getElementById('winner-details'); winnerDetails.innerHTML = `

Winner:

${winner.title}

Total: ${winner.total}

Added By: ${winner.addedBy}

`; const contestContainer = document.getElementById('contest-container'); contestContainer.innerHTML = ''; contestVideos.length = 0; } } // New function to clear contest points function clearContestPoints() { const contestPointsRef = database.ref('contest/points'); contestPointsRef.set(0); } // New function to update contest points function updateContestPoints() { const contestPointsRef = database.ref('contest/points'); contestPointsRef.once('value') .then((snapshot) => { let currentPoints = snapshot.val() || 0; // Deduct 10 points for each added video contestVideos.forEach((video) => { currentPoints -= 10; }); // Update the remaining points in the database contestPointsRef.set(currentPoints); }) .catch((error) => { console.error('Update Contest Points Error:', error); }); } setInterval(updateGlobalTime, 1000);

Comments

Popular posts from this blog