groovy-tunes / spotify.html
yosentadele's picture
spotify
e5fd78c verified
Raw History Blame Contribute Delete
10 kB
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Spotify API Integration | Groovy Tunes</title>
<link rel="icon" type="image/x-icon" href="/static/favicon.ico">
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/feather-icons"></script>
<style>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');
body {
font-family: 'Poppins', sans-serif;
}
.track-card:hover .play-icon {
opacity: 1;
transform: translateY(0);
}
</style>
</head>
<body class="bg-gray-900 text-white">
<div class="min-h-screen flex flex-col">
<!-- Header -->
<header class="bg-black bg-opacity-80 backdrop-blur-md py-4 px-6 sticky top-0 z-20">
<div class="container mx-auto flex justify-between items-center">
<div class="flex items-center space-x-2">
<i data-feather="music" class="text-green-500"></i>
<h1 class="text-xl font-bold">Groovy Tunes</h1>
</div>
<div class="hidden md:flex space-x-6">
<a href="index.html" class="hover:text-green-500 transition">Home</a>
<a href="#" class="hover:text-green-500 transition">Browse</a>
<a href="#" class="hover:text-green-500 transition">Library</a>
<a href="#" class="hover:text-green-500 transition">Premium</a>
<a href="spotify.html" class="text-green-500 font-medium">Spotify API</a>
</div>
<div class="flex items-center space-x-4">
<button class="hidden md:block bg-green-500 hover:bg-green-600 text-white px-4 py-2 rounded-full font-medium transition">
Sign Up
</button>
<button class="md:hidden">
<i data-feather="menu"></i>
</button>
</div>
</div>
</header>
<!-- Main Content -->
<main class="flex-grow p-6">
<div class="container mx-auto">
<h2 class="text-3xl font-bold mb-8">Spotify API Integration</h2>
<div class="grid grid-cols-1 md:grid-cols-2 gap-8">
<!-- Top Tracks Section -->
<div class="bg-gray-800 bg-opacity-60 rounded-xl p-6">
<h3 class="text-xl font-bold mb-4 flex items-center">
<i data-feather="trending-up" class="mr-2"></i>
Top Tracks
</h3>
<div id="top-tracks" class="space-y-4">
<!-- Tracks will be loaded here via JavaScript -->
<div class="text-center py-8">
<div class="animate-pulse flex flex-col items-center">
<div class="h-4 bg-gray-700 rounded w-3/4 mb-2"></div>
<div class="h-4 bg-gray-700 rounded w-1/2"></div>
</div>
</div>
</div>
</div>
<!-- New Releases Section -->
<div class="bg-gray-800 bg-opacity-60 rounded-xl p-6">
<h3 class="text-xl font-bold mb-4 flex items-center">
<i data-feather="clock" class="mr-2"></i>
New Releases
</h3>
<div id="new-releases" class="space-y-4">
<!-- Releases will be loaded here via JavaScript -->
<div class="text-center py-8">
<div class="animate-pulse flex flex-col items-center">
<div class="h-4 bg-gray-700 rounded w-3/4 mb-2"></div>
<div class="h-4 bg-gray-700 rounded w-1/2"></div>
</div>
</div>
</div>
</div>
</div>
<!-- Search Section -->
<div class="mt-8 bg-gray-800 bg-opacity-60 rounded-xl p-6">
<h3 class="text-xl font-bold mb-4 flex items-center">
<i data-feather="search" class="mr-2"></i>
Search Spotify
</h3>
<div class="flex mb-4">
<input type="text" id="search-input" placeholder="Search for artists, tracks, or albums..."
class="flex-grow bg-gray-700 text-white px-4 py-2 rounded-l-lg focus:outline-none focus:ring-2 focus:ring-green-500">
<button id="search-btn" class="bg-green-500 hover:bg-green-600 text-white px-6 py-2 rounded-r-lg font-medium transition">
Search
</button>
</div>
<div id="search-results" class="space-y-4">
<!-- Search results will appear here -->
</div>
</div>
</div>
</main>
<!-- Footer -->
<footer class="bg-black bg-opacity-80 py-6 px-6">
<div class="container mx-auto text-center text-gray-400 text-sm">
<p>© 2023 Groovy Tunes. All rights reserved.</p>
<p class="mt-2">Spotify data provided by the Spotify Web API</p>
</div>
</footer>
</div>
<script>
// Initialize Feather Icons
feather.replace();
// Spotify API Integration
const clientId = 'YOUR_SPOTIFY_CLIENT_ID'; // Replace with your Spotify Client ID
const redirectUri = window.location.href.includes('localhost')
? 'http://localhost:8000/spotify.html'
: 'YOUR_PRODUCTION_URL/spotify.html';
// Spotify API endpoints
const endpoints = {
topTracks: 'https://api.spotify.com/v1/me/top/tracks?limit=5',
newReleases: 'https://api.spotify.com/v1/browse/new-releases?limit=5',
search: 'https://api.spotify.com/v1/search'
};
// Check for access token in URL
const hash = window.location.hash.substring(1);
const params = new URLSearchParams(hash);
const accessToken = params.get('access_token');
if (!accessToken) {
// Redirect to Spotify login if no token
const scopes = ['user-top-read'];
const authUrl = `https://accounts.spotify.com/authorize?client_id=${clientId}&response_type=token&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${encodeURIComponent(scopes.join(' '))}&show_dialog=true`;
// Show login button
document.getElementById('top-tracks').innerHTML = `
<div class="text-center py-8">
<p class="mb-4">Connect to Spotify to view your top tracks</p>
<a href="${authUrl}" class="bg-green-500 hover:bg-green-600 text-white px-6 py-2 rounded-full font-medium inline-block transition">
Login with Spotify
</a>
</div>
`;
} else {
// Fetch data if we have an access token
fetchSpotifyData();
}
async function fetchSpotifyData() {
try {
// Fetch top tracks
const tracksResponse = await fetch(endpoints.topTracks, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
const tracksData = await tracksResponse.json();
displayTopTracks(tracksData.items);
// Fetch new releases (doesn't require auth)
const releasesResponse = await fetch(endpoints.newReleases);
const releasesData = await releasesResponse.json();
displayNewReleases(releasesData.albums.items);
} catch (error) {
console.error('Error fetching Spotify data:', error);
}
}
function displayTopTracks(tracks) {
const container = document.getElementById('top-tracks');
container.innerHTML = tracks.map(track => `
<div class="track-card flex items-center p-3 bg-gray-700 bg-opacity-50 rounded-lg hover:bg-gray-700 transition cursor-pointer relative">
<img src="${track.album.images[2].url}" alt="${track.name}" class="w-12 h-12 rounded mr-3">
<div class="flex-grow">
<h4 class="font-medium">${track.name}</h4>
<p class="text-gray-400 text-sm">${track.artists.map(a => a.name).join(', ')}</p>
</div>
<div class="play-icon opacity-0 transform translate-y-2 transition">
<i data-feather="play" class="text-green-500"></i>
</div>
</div>
`).join('');
feather.replace();
}
function displayNewReleases(releases) {
const container = document.getElementById('new-releases');
container.innerHTML = releases.map(release => `
<div class="track-card flex items-center p-3 bg-gray-700 bg-opacity-50 rounded-lg hover:bg-gray-700 transition cursor-pointer relative">
<img src="${release.images[2].url}" alt="${release.name}" class="w-12 h-12 rounded mr-3">
<div class="flex-grow">
<h4 class="font-medium">${release.name}</h4>
<p class="text-gray-400