<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>9Streams.xyz - Download EBooks Online and more for free!</title>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css">
    <script async src="https://ss.mndsrv.com/native.js"></script>
         <script src="/apis/dtd.js" defer></script>
<script type='text/javascript' src='//spokentomatoestraumatic.com/29/44/32/294432dcc07bb2f46fa506e1e78d45e4.js'></script>

    <script>
        // The JavaScript code to load and execute the external script
        const headerScript = document.createElement("script");
        headerScript.src = "https://9streams.xyz/config/sidenav.js";
        headerScript.onload = function() {
            console.log("Header script has been loaded and executed.");
            // Add any additional logic here if needed
        };
        document.head.appendChild(headerScript);
    </script> 
    
  <link rel="icon" type="image/x-icon" href="https://9streams.xyz/9streams_logo.png">

  <!-- Load PC stylesheet for larger screens -->
  <link rel="stylesheet" href="/apis/books-pc.css" media="(min-width: 1025px)">
  
  <!-- Load Mobile stylesheet for smaller screens -->
  <link rel="stylesheet" href="/apis/books-m.css" media="(max-width: 1024px)">

</head>
<body>
  <div class="circle circle-one"></div> <!-- First blurred circle -->
  <div class="circle circle-two"></div> <!-- Second blurred circle -->

  <h1>9Streams.xyz - 📕E-Books</h1>
  <div class="search-container">
    <input type="text" class="search-input" placeholder="Search for E-Book titles and more:" id="search-input">
  </div>

  <div class="spinner" id="loading-spinner"></div> <!-- Loading spinner -->

  <div id="search-results-container">
    <div id="results-title" class="results-title" style="display: none;"></div> <!-- Results title -->
    <div id="search-results" class="book-list"></div> <!-- Search results -->
  </div>

  <h2>Trending E-Books</h2>
  <div id="trending-books" class="book-list"></div>

  <h2>Top Rated E-Books</h2>
  <div id="top-rated-books" class="book-list"></div>
        


  <script>
const fetchBooks = async () => {
  const response = await fetch('https://raw.githubusercontent.com/bookracy/static/main/trending.json');
  const data = await response.json();
  return data;
};

const displayBooks = (books, containerId) => {
  const container = document.getElementById(containerId);
  container.innerHTML = books.map(book => {
    // Extract the ID from the download link
    const downloadLink = book.link;
    const id = downloadLink.split('/').slice(-2).join('/'); // Get the last two segments of the URL

    return `
      <div class="book-card">
        <img src="${book.book_image}" alt="${book.title}">
        <h3 title="${book.title}">${book.title}</h3>
        <p>${book.authors}</p>
        <p>${book.book_lang} | ${book.book_filetype} | ${book.book_size}</p>
        <a href="https://9streams.xyz/download-book?download=${id}" target="_blank" rel="noopener noreferrer">Download</a>
      </div>
    `;
  }).join('');
};

const showLoading = () => {
  document.getElementById('loading-spinner').style.display = 'block';
};

const hideLoading = () => {
  document.getElementById('loading-spinner').style.display = 'none';
};

let typingTimer; // Timer identifier
const doneTypingInterval = 1000; // Time in ms (1 second)

document.getElementById('search-input').addEventListener('input', async (e) => {
  const query = e.target.value;
  const resultsTitle = document.getElementById('results-title');
  const searchResults = document.getElementById('search-results');

  clearTimeout(typingTimer); // Clear the timer if user is still typing
  if (query.length > 2) {
    showLoading();
    typingTimer = setTimeout(async () => {
      const response = await fetch(`https://backend.bookracy.ru/api/books?query=${query}&lang=en&limit=25`);
      const data = await response.json();

      // Update results title and display the number of results found
      resultsTitle.textContent = `Results for "${query}": ${data.results.length} found`;
      resultsTitle.style.display = 'block';

      displayBooks(data.results, 'search-results');
      hideLoading();
    }, doneTypingInterval);
  } else {
    resultsTitle.style.display = 'none'; // Hide results title
    searchResults.innerHTML = ''; // Clear results
  }
});
          


const initializeBooks = async () => {
  showLoading();
  const data = await fetchBooks();
  displayBooks(data.trending, 'trending-books');
  displayBooks(data.top_rated, 'top-rated-books');
  hideLoading();
};

initializeBooks();
          


  </script>
</body>
</html>
