TMKOC-Quotes / index.html
DevBM's picture
Update index.html
2a57a98 verified
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TMKOC Quotes API Demo</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.header {
text-align: center;
margin-bottom: 40px;
animation: fadeInDown 1s ease-out;
}
.header h1 {
color: white;
font-size: 3rem;
margin-bottom: 10px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
.header p {
color: rgba(255,255,255,0.9);
font-size: 1.2rem;
margin-bottom: 20px;
}
.api-info {
background: rgba(255,255,255,0.1);
backdrop-filter: blur(10px);
border-radius: 15px;
padding: 15px 25px;
border: 1px solid rgba(255,255,255,0.2);
display: inline-block;
}
.api-info code {
color: #FFD700;
font-weight: bold;
}
.demo-section {
background: rgba(255,255,255,0.95);
border-radius: 20px;
padding: 30px;
margin-bottom: 30px;
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
animation: fadeInUp 1s ease-out;
}
.section-title {
font-size: 1.8rem;
color: #333;
margin-bottom: 20px;
display: flex;
align-items: center;
gap: 10px;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 15px;
margin-bottom: 25px;
}
.btn {
padding: 12px 24px;
border: none;
border-radius: 25px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
display: flex;
align-items: center;
gap: 8px;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}
.btn-primary {
background: linear-gradient(45deg, #FF6B6B, #FF8E53);
color: white;
}
.btn-secondary {
background: linear-gradient(45deg, #4ECDC4, #44A08D);
color: white;
}
.btn-accent {
background: linear-gradient(45deg, #A8E6CF, #88D8A3);
color: #333;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 8px 25px rgba(0,0,0,0.2);
}
.input-group {
display: flex;
gap: 10px;
margin-bottom: 15px;
flex-wrap: wrap;
}
.form-input {
flex: 1;
min-width: 200px;
padding: 12px 16px;
border: 2px solid #e0e0e0;
border-radius: 25px;
font-size: 1rem;
transition: all 0.3s ease;
}
.form-input:focus {
outline: none;
border-color: #667eea;
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}
.quote-display {
background: linear-gradient(135deg, #667eea, #764ba2);
color: white;
padding: 30px;
border-radius: 15px;
margin: 20px 0;
text-align: center;
min-height: 150px;
display: flex;
flex-direction: column;
justify-content: center;
position: relative;
overflow: hidden;
}
.quote-display::before {
content: '"';
font-size: 8rem;
position: absolute;
top: -20px;
left: 20px;
opacity: 0.1;
font-family: serif;
}
.quote-text {
font-size: 1.4rem;
line-height: 1.6;
margin-bottom: 15px;
font-style: italic;
}
.quote-author {
font-size: 1.1rem;
font-weight: 600;
opacity: 0.9;
}
.quote-tags {
margin-top: 10px;
}
.tag {
display: inline-block;
background: rgba(255,255,255,0.2);
padding: 4px 12px;
border-radius: 15px;
font-size: 0.8rem;
margin: 2px;
}
.results-container {
max-height: 400px;
overflow-y: auto;
border: 1px solid #e0e0e0;
border-radius: 10px;
padding: 15px;
}
.quote-item {
background: #f8f9fa;
padding: 15px;
border-radius: 8px;
margin-bottom: 10px;
border-left: 4px solid #667eea;
}
.loading {
text-align: center;
padding: 40px;
color: #666;
}
.spinner {
display: inline-block;
width: 30px;
height: 30px;
border: 3px solid #f3f3f3;
border-top: 3px solid #667eea;
border-radius: 50%;
animation: spin 1s linear infinite;
margin-bottom: 10px;
}
.error {
background: #ffebee;
color: #c62828;
padding: 15px;
border-radius: 8px;
border-left: 4px solid #c62828;
}
.endpoints {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
margin-top: 20px;
}
.endpoint-card {
background: #f8f9fa;
padding: 20px;
border-radius: 12px;
border: 1px solid #e0e0e0;
}
.endpoint-method {
background: #28a745;
color: white;
padding: 4px 8px;
border-radius: 4px;
font-size: 0.8rem;
font-weight: bold;
margin-bottom: 8px;
display: inline-block;
}
.endpoint-url {
font-family: 'Courier New', monospace;
background: #e9ecef;
padding: 8px 12px;
border-radius: 6px;
font-size: 0.9rem;
word-break: break-all;
}
@keyframes fadeInDown {
from {
opacity: 0;
transform: translateY(-30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
@media (max-width: 768px) {
.header h1 {
font-size: 2rem;
}
.controls {
flex-direction: column;
}
.input-group {
flex-direction: column;
}
.form-input {
min-width: unset;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1><i class="fas fa-quote-left"></i> TMKOC Quotes API</h1>
<p>Explore quotes from the beloved Indian sitcom Taarak Mehta Ka Ooltah Chashmah</p>
<div class="api-info">
<i class="fas fa-link"></i> Base URL: <code>https://tmkoc-quotes-api.onrender.com</code>
</div>
</div>
<!-- Random Quote Section -->
<div class="demo-section">
<h2 class="section-title">
<i class="fas fa-random"></i>
Random Quotes
</h2>
<div class="controls">
<button class="btn btn-primary" onclick="getRandomQuote()">
<i class="fas fa-dice"></i> Get Random Quote
</button>
<button class="btn btn-secondary" onclick="getMultipleQuotes()">
<i class="fas fa-layer-group"></i> Get 3 Random Quotes
</button>
</div>
<div id="randomQuoteDisplay" class="quote-display" style="display: none;">
<div class="quote-text" id="randomQuoteText"></div>
<div class="quote-author" id="randomQuoteAuthor"></div>
<div class="quote-tags" id="randomQuoteTags"></div>
</div>
<div id="multipleQuotesDisplay" class="results-container" style="display: none;"></div>
</div>
<!-- Character Filter Section -->
<div class="demo-section">
<h2 class="section-title">
<i class="fas fa-user"></i>
Filter by Character
</h2>
<div class="controls">
<button class="btn btn-accent" onclick="getQuotesByCharacter('Jethalal')">Jethalal</button>
<button class="btn btn-accent" onclick="getQuotesByCharacter('Daya')">Daya</button>
<button class="btn btn-accent" onclick="getQuotesByCharacter('Tapu')">Tapu</button>
<button class="btn btn-accent" onclick="getQuotesByCharacter('Popatlal')">Popatlal</button>
</div>
<div class="input-group">
<input type="text" class="form-input" id="characterInput" placeholder="Enter character name...">
<button class="btn btn-primary" onclick="getQuotesByCharacterInput()">
<i class="fas fa-search"></i> Search
</button>
</div>
<div id="characterQuotesDisplay" class="results-container" style="display: none;"></div>
</div>
<!-- Search Section -->
<div class="demo-section">
<h2 class="section-title">
<i class="fas fa-search"></i>
Search Quotes
</h2>
<div class="input-group">
<input type="text" class="form-input" id="searchAuthor" placeholder="Author name (optional)">
<input type="text" class="form-input" id="searchText" placeholder="Search text in quotes">
<button class="btn btn-primary" onclick="searchQuotes()">
<i class="fas fa-search"></i> Search
</button>
</div>
<div id="searchResults" class="results-container" style="display: none;"></div>
</div>
<!-- API Endpoints Reference -->
<div class="demo-section">
<h2 class="section-title">
<i class="fas fa-code"></i>
API Endpoints Reference
</h2>
<div class="endpoints">
<div class="endpoint-card">
<div class="endpoint-method">GET</div>
<div class="endpoint-url">/v1/quotes</div>
<p>Get a random quote</p>
</div>
<div class="endpoint-card">
<div class="endpoint-method">GET</div>
<div class="endpoint-url">/v1/quotes/{number}</div>
<p>Get multiple random quotes</p>
</div>
<div class="endpoint-card">
<div class="endpoint-method">GET</div>
<div class="endpoint-url">/v1/quotes/character/{author}</div>
<p>Get quotes by character</p>
</div>
<div class="endpoint-card">
<div class="endpoint-method">GET</div>
<div class="endpoint-url">/v1/quotes/tag/{tag}</div>
<p>Get quotes by tag/category</p>
</div>
<div class="endpoint-card">
<div class="endpoint-method">GET</div>
<div class="endpoint-url">/v1/quotes/search?author=X&text=Y</div>
<p>Search quotes by author and/or text</p>
</div>
</div>
</div>
</div>
<script>
const API_BASE = 'https://tmkoc-quotes-api.onrender.com/v1';
// Sample quotes as fallback
const sampleQuotes = [
{
"quote": "Tapu ke papa, sambhal ke!",
"author": "Daya Gada",
"tags": ["classic", "funny"]
},
{
"quote": "Jethalal, ye kya tamasha bana rakha hai!",
"author": "Champaklal Gada",
"tags": ["classic"]
},
{
"quote": "Hey Maa, Mataji!",
"author": "Daya",
"tags": ["surprised", "classic"]
},
{
"quote": "Fire! Fire! Fire!",
"author": "Jethalal",
"tags": ["funny", "panic"]
},
{
"quote": "Pappu pass ho gaya!",
"author": "Tapu Sena",
"tags": ["celebration", "school"]
},
{
"quote": "Bas kar pagle, rulayega kya?",
"author": "Jethalal",
"tags": ["emotional", "funny"]
},
{
"quote": "Arre yaar, kya baat hai!",
"author": "Popatlal",
"tags": ["excitement"]
},
{
"quote": "Shanti, shanti, shanti!",
"author": "Taarak Mehta",
"tags": ["peace", "wisdom"]
}
];
let useAPIFallback = false;
function showLoading(containerId) {
const container = document.getElementById(containerId);
container.style.display = 'block';
container.innerHTML = `
<div class="loading">
<div class="spinner"></div>
<p>Loading quotes...</p>
</div>
`;
}
function showError(containerId, message, showFallbackOption = false) {
const container = document.getElementById(containerId);
container.style.display = 'block';
container.innerHTML = `
<div class="error">
<i class="fas fa-exclamation-triangle"></i>
Error: ${message}
${showFallbackOption ? `
<br><br>
<button class="btn btn-secondary" onclick="enableFallbackMode()" style="margin-top: 10px;">
<i class="fas fa-shield-alt"></i> Use Demo Mode
</button>
<p style="margin-top: 8px; font-size: 0.9rem;">Demo mode uses sample quotes when API is unavailable</p>
` : ''}
</div>
`;
}
function enableFallbackMode() {
useAPIFallback = true;
showApiStatus();
getRandomQuote();
}
function showApiStatus() {
const statusDiv = document.createElement('div');
statusDiv.id = 'api-status';
statusDiv.innerHTML = `
<div style="background: #fff3cd; color: #856404; padding: 10px; border-radius: 8px; margin-bottom: 20px; border: 1px solid #ffeaa7;">
<i class="fas fa-info-circle"></i>
<strong>Demo Mode:</strong> Using sample quotes since the API is unavailable.
<button onclick="testAPI()" style="background: none; border: none; color: #667eea; text-decoration: underline; cursor: pointer;">Try API again</button>
</div>
`;
const container = document.querySelector('.container');
const firstSection = container.querySelector('.demo-section');
if (!document.getElementById('api-status')) {
container.insertBefore(statusDiv, firstSection);
}
}
async function testAPI() {
useAPIFallback = false;
const statusEl = document.getElementById('api-status');
if (statusEl) statusEl.remove();
await getRandomQuote();
}
function displaySingleQuote(quote) {
const container = document.getElementById('randomQuoteDisplay');
// Build the entire quote display using innerHTML
const tagsHTML = quote.tags && quote.tags.length > 0
? `<div class="quote-tags">${quote.tags.map(tag => `<span class="tag">#${tag}</span>`).join('')}</div>`
: '';
container.innerHTML = `
<div class="quote-text">${quote.quote}</div>
<div class="quote-author">— ${quote.author}</div>
${tagsHTML}
`;
container.style.display = 'flex';
}
function displayMultipleQuotes(quotes, containerId) {
const container = document.getElementById(containerId);
container.style.display = 'block';
container.innerHTML = quotes.map(quote => `
<div class="quote-item">
<div style="font-size: 1.1rem; margin-bottom: 8px; font-style: italic;">
"${quote.quote}"
</div>
<div style="font-weight: 600; color: #667eea;">
${quote.author}
</div>
${quote.tags ? `
<div style="margin-top: 8px;">
${quote.tags.map(tag => `<span class="tag" style="background: #e3f2fd; color: #1976d2;">#${tag}</span>`).join('')}
</div>
` : ''}
</div>
`).join('');
}
function getRandomSampleQuotes(count = 1) {
const shuffled = [...sampleQuotes].sort(() => 0.5 - Math.random());
return shuffled.slice(0, count);
}
function filterSampleQuotesByCharacter(character) {
return sampleQuotes.filter(quote =>
quote.author.toLowerCase().includes(character.toLowerCase())
);
}
function searchSampleQuotes(author, text) {
return sampleQuotes.filter(quote => {
const matchesAuthor = !author || quote.author.toLowerCase().includes(author.toLowerCase());
const matchesText = !text || quote.quote.toLowerCase().includes(text.toLowerCase());
return matchesAuthor && matchesText;
});
}
async function makeAPICall(endpoint) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 15000); // 15 second timeout
try {
console.log(`Making API call to: ${API_BASE}${endpoint}`);
const response = await fetch(`${API_BASE}${endpoint}`, {
signal: controller.signal,
method: 'GET',
mode: 'cors',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
});
clearTimeout(timeoutId);
console.log(`API Response status: ${response.status}`);
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status}: ${response.statusText}${errorText ? ` - ${errorText}` : ''}`);
}
const data = await response.json();
console.log('API Response data:', data);
return data;
} catch (error) {
clearTimeout(timeoutId);
console.error('API Call failed:', error);
if (error.name === 'AbortError') {
throw new Error('Request timed out - API server may be sleeping on free hosting');
}
if (error.message.includes('Failed to fetch') || error.message.includes('NetworkError')) {
throw new Error('Network error - API server may be down or have CORS issues');
}
throw error;
}
}
async function getRandomQuote() {
try {
document.getElementById('multipleQuotesDisplay').style.display = 'none';
showLoading('randomQuoteDisplay');
let quote;
if (useAPIFallback) {
quote = getRandomSampleQuotes(1)[0];
} else {
try {
quote = await makeAPICall('/quotes');
} catch (error) {
console.error('API Error:', error);
// Remove next line (redundant)
showError('randomQuoteDisplay', `${error.message}. The API might be sleeping on free hosting.`, true);
return;
}
}
// REMOVE THIS LINE: It's unnecessary and causes flickering
// document.getElementById('randomQuoteDisplay').style.display = 'none';
displaySingleQuote(quote);
} catch (error) {
// REMOVE THIS LINE: showError already handles display
// document.getElementById('randomQuoteDisplay').style.display = 'none';
showError('randomQuoteDisplay', error.message, !useAPIFallback);
}
}
async function getMultipleQuotes() {
try {
document.getElementById('randomQuoteDisplay').style.display = 'none';
showLoading('multipleQuotesDisplay');
let quotes;
if (useAPIFallback) {
quotes = getRandomSampleQuotes(3);
} else {
try {
quotes = await makeAPICall('/quotes/3');
} catch (error) {
console.error('API Error:', error);
showError('multipleQuotesDisplay', `${error.message}. The API might be sleeping on free hosting.`, true);
return;
}
}
displayMultipleQuotes(quotes, 'multipleQuotesDisplay');
} catch (error) {
showError('multipleQuotesDisplay', error.message, !useAPIFallback);
}
}
async function getQuotesByCharacter(character) {
try {
showLoading('characterQuotesDisplay');
let quotes;
if (useAPIFallback) {
quotes = filterSampleQuotesByCharacter(character);
if (quotes.length === 0) {
showError('characterQuotesDisplay', `No quotes found for character "${character}" in demo data`);
return;
}
} else {
try {
quotes = await makeAPICall(`/quotes/character/${encodeURIComponent(character)}`);
} catch (error) {
console.error('API Error:', error);
showError('characterQuotesDisplay', `${error.message}. The API might be sleeping on free hosting.`, true);
return;
}
}
displayMultipleQuotes(quotes, 'characterQuotesDisplay');
} catch (error) {
showError('characterQuotesDisplay', error.message, !useAPIFallback);
}
}
async function getQuotesByCharacterInput() {
const character = document.getElementById('characterInput').value.trim();
if (!character) {
alert('Please enter a character name');
return;
}
await getQuotesByCharacter(character);
}
async function searchQuotes() {
const author = document.getElementById('searchAuthor').value.trim();
const text = document.getElementById('searchText').value.trim();
if (!author && !text) {
alert('Please enter either author name or search text');
return;
}
try {
showLoading('searchResults');
let quotes;
if (useAPIFallback) {
quotes = searchSampleQuotes(author, text);
if (quotes.length === 0) {
showError('searchResults', 'No quotes found matching your search in demo data');
return;
}
} else {
try {
const params = new URLSearchParams();
if (author) params.append('author', author);
if (text) params.append('text', text);
quotes = await makeAPICall(`/quotes/search?${params.toString()}`);
} catch (error) {
console.error('API Error:', error);
showError('searchResults', `${error.message}. The API might be sleeping on free hosting.`, true);
return;
}
}
displayMultipleQuotes(quotes, 'searchResults');
} catch (error) {
showError('searchResults', error.message, !useAPIFallback);
}
}
// Add enter key support for inputs
document.getElementById('characterInput').addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
getQuotesByCharacterInput();
}
});
document.getElementById('searchAuthor').addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
searchQuotes();
}
});
document.getElementById('searchText').addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
searchQuotes();
}
});
// Load a random quote on page load
window.addEventListener('load', getRandomQuote);
</script>
</body>
</html>