File: /home/internet/www/oyya.net/search_videos.php
<?php
$apiKey = 'AIzaSyA2pMAsxEI-6z-1okdCXbIL172yD-E5qAE';
$channelId = 'UCoti1aghXiiRLAmpsZifd1A';
function getVideoList($channelId, $apiKey, $pageToken = '') {
$apiUrl = 'https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=' . $channelId . '&maxResults=25&key=' . $apiKey . '&pageToken=' . $pageToken;
$videoListJson = file_get_contents($apiUrl);
$videoList = json_decode($videoListJson, true);
return $videoList;
}
function getVideoDetails($videoId, $apiKey) {
$apiUrl = 'https://www.googleapis.com/youtube/v3/videos?id=' . $videoId . '&part=snippet%2Cstatistics&key=' . $apiKey;
$videoDetailsJson = file_get_contents($apiUrl);
$videoDetails = json_decode($videoDetailsJson, true);
return $videoDetails;
}
function sortByViewsDesc($a, $b) {
return $b['views'] - $a['views'];
}
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$itemsPerPage = 10;
$videoList = getVideoList($channelId, $apiKey);
$videoDetailsList = [];
foreach ($videoList['items'] as $video) {
if ($video['id']['kind'] == 'youtube#video') {
$videoId = $video['id']['videoId'];
$videoDetails = getVideoDetails($videoId, $apiKey);
$title = $videoDetails['items'][0]['snippet']['title'];
$views = $videoDetails['items'][0]['statistics']['viewCount'];
$videoDetailsList[] = ['title' => $title, 'views' => (int) $views];
}
}
usort($videoDetailsList, 'sortByViewsDesc');
$totalItems = count($videoDetailsList);
$totalPages = ceil($totalItems / $itemsPerPage);
$offset = ($page - 1) * $itemsPerPage;
$videoDetailsList = array_slice($videoDetailsList, $offset, $itemsPerPage);
function getChannelInfo($channelId, $apiKey) {
$apiUrl = 'https://www.googleapis.com/youtube/v3/channels?part=snippet%2Cstatistics&id=' . $channelId . '&key=' . $apiKey;
$channelInfoJson = file_get_contents($apiUrl);
$channelInfo = json_decode($channelInfoJson, true);
return $channelInfo;
}
$channelInfo = getChannelInfo($channelId, $apiKey);
$channelTitle = $channelInfo['items'][0]['snippet']['title'];
$subscriberCount = $channelInfo['items'][0]['statistics']['subscriberCount'];
$totalViews = $channelInfo['items'][0]['statistics']['viewCount'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>YouTube Video List</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
header {
background-color: #333;
color: #fff;
padding: 20px;
text-align: center;
}
.search-container {
display: flex;
justify-content: center;
padding: 20px;
}
.responsive-table {
overflow-x: auto;
padding: 20px;
}
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
font-weight: bold;
}
.pagination {
display: flex;
justify-content: center;
margin: 20px 0;
}
.pagination a {
padding: 8px 12px;
margin: 0 4px;
border: 1px solid #ddd;
text-decoration: none;
color: #333;
}
.pagination a:hover {
background-color: #f2f2f2;
}
.pagination .active {
background-color: #333;
color: #fff;
pointer-events: none;
}
@media screen and (max-width: 600px) {
table {
display: block;
}
thead {
display: none;
}
tr {
display: block;
margin-bottom: 15px;
border: 1px solid black;
}
td {
display: flex;
justify-content: space-between;
align-items: center;
padding: 8px;
border: none;
}
td:before {
content: attr(data-label);
font-weight: bold;
flex-basis: 50%;
}
}
/* Loader styles */
.loader {
display: inline-block;
width: 80px;
height: 80px;
border: 12px solid #f3f3f3;
border-radius: 50%;
border-top: 12px solid #3498db;
animation: spin 2s linear infinite;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 9999;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
<script>
function hideLoader() {
document.getElementById("loader").style.display = "none";
}
function searchVideo() {
const input = document.getElementById("searchInput");
const filter = input.value.toUpperCase();
const table = document.querySelector("table");
const tr = table.getElementsByTagName("tr");
for (let i = 1; i < tr.length; i++) {
const td = tr[i].getElementsByTagName("td")[0];
if (td) {
const txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
function checkPassword() {
if (!sessionStorage.getItem("isLoggedIn")) {
const password = prompt("Please enter the password to view this page:");
const correctPassword = "password";
if (password === correctPassword) {
sessionStorage.setItem("isLoggedIn", "true");
document.getElementById("main-content").style.display = "block";
} else {
document.getElementById("incorrect-password").style.display = "block";
}
} else {
document.getElementById("main-content").style.display = "block";
}
}
</script>
</head>
<body onload="hideLoader(); checkPassword();">
<!-- Loader element -->
<div id="loader" class="loader"></div>
<!-- Incorrect password message -->
<div id="incorrect-password" style="display: none; text-align: center; padding: 20px;">
<h2>Incorrect password. Please refresh the page and try again.</h2>
</div>
<!-- Main content -->
<div id="main-content" style="display: none;">
<!-- Channel header -->
<header>
<h1><?php echo htmlspecialchars($channelTitle); ?></h1>
<p>Subscribers: <?php echo number_format($subscriberCount); ?></p>
<p>Total views: <?php echo number_format($totalViews); ?></p>
</header>
<!-- Search box -->
<div class="search-container">
<input type="text" id="searchInput" onkeyup="searchVideo()" placeholder="Search for video titles...">
</div>
<div class="responsive-table">
<table>
<thead>
<tr>
<th>Video Title</th>
<th>Views</th>
</tr>
</thead>
<tbody>
<?php
foreach ($videoDetailsList as $videoDetails) {
$title = $videoDetails['title'];
$views = $videoDetails['views'];
?>
<tr>
<td data-label="Video Title"><?php echo htmlspecialchars($title); ?></td>
<td data-label="Views"><?php echo number_format($views); ?> views</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<div class="pagination">
<?php
for ($i = 1; $i <= $totalPages; $i++) {
$activeClass = $i === $page ? 'active' : '';
echo '<a class="' . $activeClass . '" href="?page=' . $i . '">' . $i . '</a>';
}
?>
</div>
</div>
</body>
</html>