YouTube Video Stats Checker

Error fetching video statistics. Please try again.

'; chartContainer.innerHTML = ''; }); } else { resultContainer.innerHTML = '

No video ID found. Please check the URL and try again.

'; chartContainer.innerHTML = ''; } } async function fetchVideoStatistics(videoId) { const apiKey = 'AIzaSyAjtFd3waoyhiIHZixRX2HAeFXWpiRPxCY'; const apiUrl = `https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics&id=${videoId}&key=${apiKey}`; const response = await fetch(apiUrl); const data = await response.json(); if (data.items && data.items.length > 0) { return data.items[0]; } else { throw new Error('Video statistics not available.'); } } function isValidUrl(url) { // Simple URL validation const urlPattern = /^(https?:\/\/)?(www\.)?(youtube\.com\/watch\?v=|youtu\.be\/)/; return urlPattern.test(url); } function getVideoId(url) { // Extract video ID from YouTube URL const videoParams = new URLSearchParams(new URL(url).search); return videoParams.get('v') || url.split('/').pop(); } function generateTable(statistics) { const { snippet, statistics: videoStats } = statistics; const tableHtml = `

Video Statistics:

AttributeValue
Channel ID${snippet.channelId}
Channel Title${snippet.channelTitle}
Video Title${snippet.title}
Video Views${videoStats.viewCount}
Video Likes${videoStats.likeCount}
Video Comments${videoStats.commentCount}
Published at${snippet.publishedAt}
Description${snippet.description}
ThumbnailsThumbnail
Tags${snippet.tags ? snippet.tags.join(', ') : 'N/A'}
Category${snippet.categoryId}
Default Language${snippet.defaultLanguage}
`; return tableHtml; } function displayChart(statistics, chartContainer) { const { statistics: videoStats } = statistics; const ctx = document.createElement('canvas'); chartContainer.innerHTML = ''; chartContainer.appendChild(ctx); new Chart(ctx, { type: 'bar', data: { labels: ['Views', 'Likes', 'Comments'], datasets: [{ label: 'Count', data: [videoStats.viewCount, videoStats.likeCount, videoStats.commentCount], backgroundColor: [ 'rgba(75, 192, 192, 0.2)', 'rgba(255, 99, 132, 0.2)', 'rgba(255, 205, 86, 0.2)', ], borderColor: [ 'rgba(75, 192, 192, 1)', 'rgba(255, 99, 132, 1)', 'rgba(255, 205, 86, 1)', ], borderWidth: 1, }], }, options: { scales: { y: { beginAtZero: true, }, }, }, }); }