Spaces:
Sleeping
Sleeping
File size: 21,922 Bytes
1d2a6c9 986a31d 1d2a6c9 1c0e161 1d2a6c9 1c0e161 1d2a6c9 1d233da 1d2a6c9 1c0e161 1d2a6c9 1c0e161 1d2a6c9 1d233da 1d2a6c9 1c0e161 1d2a6c9 1c0e161 1d2a6c9 986a31d 1d2a6c9 986a31d 1d2a6c9 986a31d 1d2a6c9 986a31d 1d2a6c9 1c0e161 1d2a6c9 986a31d 1d2a6c9 1c0e161 1d2a6c9 1c0e161 986a31d 1d2a6c9 1d233da 1d2a6c9 986a31d 1d2a6c9 986a31d 1d2a6c9 986a31d 1d2a6c9 986a31d 1d2a6c9 986a31d 1d2a6c9 986a31d 1d2a6c9 986a31d 1d2a6c9 986a31d 1d2a6c9 986a31d 1d2a6c9 1c0e161 1d2a6c9 cf482fb 1d2a6c9 986a31d 1d2a6c9 986a31d cf482fb 1d2a6c9 986a31d cf482fb 1c0e161 1d2a6c9 986a31d 1c0e161 1d2a6c9 cf482fb 1d2a6c9 cf482fb 1d2a6c9 cf482fb 1d2a6c9 cf482fb 1d2a6c9 986a31d cf482fb 1c0e161 1d2a6c9 986a31d 1d2a6c9 1c0e161 1d2a6c9 1c0e161 1d2a6c9 1d233da 1d2a6c9 986a31d 1d2a6c9 1d233da 1d2a6c9 986a31d 1d2a6c9 1d233da 1d2a6c9 1d233da 986a31d 1d2a6c9 1d233da 1d2a6c9 1d233da 1d2a6c9 1d233da 1d2a6c9 1d233da 1d2a6c9 1d233da 1d2a6c9 1d233da 1d2a6c9 1c0e161 1d2a6c9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 | class SpotifyPlayer {
constructor() {
this.token = null;
this.player = null;
this.deviceId = null;
this.currentTrack = null;
this.isPlaying = false;
this.currentPosition = 0;
this.duration = 0;
this.volume = 0.5;
this.queue = [];
this.currentQueueIndex = -1;
this.isRadioMode = false;
this.playbackContext = null;
this.init();
}
init() {
this.checkToken();
this.setupEventListeners();
}
checkToken() {
// Check localStorage first
const storedToken = localStorage.getItem('spotify_access_token');
if (storedToken) {
this.token = storedToken;
this.showMainApp();
return;
}
// Check URL hash
const hash = window.location.hash.substring(1);
const params = new URLSearchParams(hash);
const token = params.get('access_token');
if (token) {
this.token = token;
localStorage.setItem('spotify_access_token', token);
window.location.hash = '';
this.showMainApp();
} else {
this.showLoginScreen();
}
}
showLoginScreen() {
document.getElementById('login-screen').classList.remove('hidden');
document.getElementById('main-app').classList.add('hidden');
}
async showMainApp() {
document.getElementById('login-screen').classList.add('hidden');
document.getElementById('main-app').classList.remove('hidden');
await this.initializeSpotifyPlayer();
await this.loadUserProfile();
await this.loadLikedSongs();
}
async initializeSpotifyPlayer() {
return new Promise((resolve) => {
window.onSpotifyWebPlaybackSDKReady = () => {
this.player = new Spotify.Player({
name: 'Spotify Web Player',
getOAuthToken: cb => { cb(this.token); },
volume: this.volume
});
// Error handling
this.player.addListener('initialization_error', ({ message }) => {
console.error('Failed to initialize:', message);
});
this.player.addListener('authentication_error', ({ message }) => {
console.error('Failed to authenticate:', message);
this.logout();
});
this.player.addListener('account_error', ({ message }) => {
console.error('Failed to validate Spotify account:', message);
});
this.player.addListener('playback_error', ({ message }) => {
console.error('Failed to perform playback:', message);
});
// Playback status updates
this.player.addListener('player_state_changed', (state) => {
if (!state) return;
this.currentTrack = state.track_window.current_track;
this.isPlaying = !state.paused;
this.currentPosition = state.position;
this.duration = state.duration;
// Store context for next/previous functionality
if (state.context && state.context.uri) {
this.playbackContext = state.context;
}
this.updateUI();
// Check if track ended
if (state.paused && state.position === 0 && this.previousPosition > 0) {
this.handleTrackEnd();
}
this.previousPosition = state.position;
});
// Ready
this.player.addListener('ready', ({ device_id }) => {
console.log('Ready with Device ID', device_id);
this.deviceId = device_id;
resolve();
});
// Connect to the player!
this.player.connect();
};
});
}
async loadUserProfile() {
try {
const response = await fetch('https://api.spotify.com/v1/me', {
headers: { 'Authorization': `Bearer ${this.token}` }
});
const user = await response.json();
document.getElementById('user-name').textContent = user.display_name || 'User';
if (user.images && user.images.length > 0) {
document.getElementById('user-avatar').src = user.images[0].url;
}
} catch (error) {
console.error('Error loading user profile:', error);
}
}
async loadLikedSongs() {
try {
const response = await fetch('https://api.spotify.com/v1/me/tracks?limit=50', {
headers: { 'Authorization': `Bearer ${this.token}` }
});
const data = await response.json();
const container = document.getElementById('liked-songs');
container.innerHTML = '';
if (data.items && data.items.length > 0) {
// Store liked songs as a playlist
this.likedSongs = data.items.map(item => item.track);
data.items.forEach((item, index) => {
const trackElement = this.createTrackElement(item.track, true, index);
container.appendChild(trackElement);
});
} else {
container.innerHTML = '<div class="loading">No liked songs found</div>';
}
} catch (error) {
console.error('Error loading liked songs:', error);
document.getElementById('liked-songs').innerHTML = '<div class="loading">Error loading liked songs</div>';
}
}
async search(query) {
if (!query.trim()) return;
this.showLoading();
try {
const response = await fetch(`https://api.spotify.com/v1/search?q=${encodeURIComponent(query)}&type=track&limit=20`, {
headers: { 'Authorization': `Bearer ${this.token}` }
});
const data = await response.json();
const container = document.getElementById('search-results');
container.innerHTML = '';
if (data.tracks && data.tracks.items.length > 0) {
data.tracks.items.forEach(track => {
const trackElement = this.createTrackElement(track);
container.appendChild(trackElement);
});
} else {
container.innerHTML = '<div class="loading">No results found</div>';
}
} catch (error) {
console.error('Error searching:', error);
document.getElementById('search-results').innerHTML = '<div class="loading">Error searching</div>';
}
this.hideLoading();
}
createTrackElement(track, isLiked = false, index = -1) {
const div = document.createElement('div');
div.className = 'track-item';
if (this.currentTrack && this.currentTrack.id === track.id) {
div.classList.add('playing');
}
div.innerHTML = `
<img src="${track.album.images[2]?.url || track.album.images[0]?.url}" alt="${track.name}" class="track-image">
<div class="track-info">
<div class="track-name">${track.name}</div>
<div class="track-artist">${track.artists.map(a => a.name).join(', ')}</div>
</div>
<button class="like-btn ${isLiked ? 'liked' : ''}" data-track-id="${track.id}">
${isLiked ? '♥' : '♡'}
</button>
`;
div.addEventListener('click', (e) => {
if (!e.target.classList.contains('like-btn')) {
if (isLiked && index >= 0) {
// Playing from liked songs - set up queue
this.playFromLikedSongs(index);
} else {
// Playing from search - enable radio mode
this.playTrack(track, true);
}
}
});
div.querySelector('.like-btn').addEventListener('click', (e) => {
e.stopPropagation();
this.toggleLike(track.id, e.target);
});
return div;
}
async playFromLikedSongs(index) {
if (!this.likedSongs || index >= this.likedSongs.length) return;
try {
// Play all liked songs starting from the selected one
const uris = this.likedSongs.slice(index).map(track => track.uri);
await fetch(`https://api.spotify.com/v1/me/player/play?device_id=${this.deviceId}`, {
method: 'PUT',
body: JSON.stringify({
uris: uris
}),
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.token}`
}
});
this.isRadioMode = false;
this.queue = this.likedSongs.slice(index);
this.currentQueueIndex = 0;
} catch (error) {
console.error('Error playing from liked songs:', error);
}
}
async playTrack(track, startRadioMode = true) {
try {
if (startRadioMode) {
// Get recommendations first
await this.loadSimilarTracks(track);
// Play the queue if we have recommendations
if (this.queue.length > 0) {
const uris = this.queue.map(t => t.uri);
await fetch(`https://api.spotify.com/v1/me/player/play?device_id=${this.deviceId}`, {
method: 'PUT',
body: JSON.stringify({
uris: uris
}),
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.token}`
}
});
this.isRadioMode = true;
this.currentQueueIndex = 0;
} else {
// Fallback to single track if recommendations fail
await fetch(`https://api.spotify.com/v1/me/player/play?device_id=${this.deviceId}`, {
method: 'PUT',
body: JSON.stringify({
uris: [track.uri]
}),
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.token}`
}
});
this.isRadioMode = false;
}
} else {
// Just play single track
await fetch(`https://api.spotify.com/v1/me/player/play?device_id=${this.deviceId}`, {
method: 'PUT',
body: JSON.stringify({
uris: [track.uri]
}),
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.token}`
}
});
}
this.currentTrack = track;
this.updateUI();
} catch (error) {
console.error('Error playing track:', error);
}
}
async loadSimilarTracks(seedTrack) {
try {
const response = await fetch('/recommendations', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
access_token: this.token,
seed_tracks: seedTrack.id,
seed_artists: seedTrack.artists[0].id
})
});
if (response.ok) {
const data = await response.json();
if (data.tracks) {
this.queue = [seedTrack, ...data.tracks];
this.currentQueueIndex = 0;
return;
}
}
// Fallback - just use the seed track
this.queue = [seedTrack];
this.currentQueueIndex = 0;
} catch (error) {
console.error('Error loading similar tracks:', error);
// Fallback - just use the seed track
this.queue = [seedTrack];
this.currentQueueIndex = 0;
}
}
async handleTrackEnd() {
// This is handled by the SDK's queue now
}
async togglePlayPause() {
try {
if (this.isPlaying) {
await this.player.pause();
} else {
await this.player.resume();
}
} catch (error) {
console.error('Error toggling play/pause:', error);
}
}
async nextTrack() {
try {
await this.player.nextTrack();
} catch (error) {
console.error('Error skipping to next track:', error);
}
}
async previousTrack() {
try {
await this.player.previousTrack();
} catch (error) {
console.error('Error skipping to previous track:', error);
}
}
async setVolume(volume) {
this.volume = volume / 100;
try {
await this.player.setVolume(this.volume);
} catch (error) {
console.error('Error setting volume:', error);
}
}
async toggleLike(trackId, button) {
const isLiked = button.classList.contains('liked');
try {
const method = isLiked ? 'DELETE' : 'PUT';
await fetch(`https://api.spotify.com/v1/me/tracks?ids=${trackId}`, {
method: method,
headers: { 'Authorization': `Bearer ${this.token}` }
});
button.classList.toggle('liked');
button.textContent = button.classList.contains('liked') ? '♥' : '♡';
// Update current track like button if it's the same track
if (this.currentTrack && this.currentTrack.id === trackId) {
const currentLikeBtn = document.getElementById('like-current-btn');
currentLikeBtn.classList.toggle('liked');
currentLikeBtn.textContent = currentLikeBtn.classList.contains('liked') ? '♥' : '♡';
}
// Reload liked songs if we're on that tab
const likedTab = document.getElementById('liked-tab');
if (likedTab.classList.contains('active')) {
await this.loadLikedSongs();
}
} catch (error) {
console.error('Error toggling like:', error);
}
}
updateUI() {
// Update current track info
if (this.currentTrack) {
document.getElementById('current-track-info').classList.remove('hidden');
document.getElementById('current-track-image').src = this.currentTrack.album.images[2]?.url || this.currentTrack.album.images[0]?.url;
document.getElementById('current-track-name').textContent = this.currentTrack.name;
document.getElementById('current-track-artist').textContent = this.currentTrack.artists.map(a => a.name).join(', ');
// Check if current track is liked
this.checkIfTrackIsLiked(this.currentTrack.id);
}
// Update play button
document.getElementById('play-btn').textContent = this.isPlaying ? '⏸️' : '▶️';
// Update progress
const progressPercent = this.duration ? (this.currentPosition / this.duration) * 100 : 0;
document.getElementById('progress').style.width = progressPercent + '%';
// Update time display
document.getElementById('current-time').textContent = this.formatTime(this.currentPosition);
document.getElementById('total-time').textContent = this.formatTime(this.duration);
// Update playing indicators
document.querySelectorAll('.track-item').forEach(item => {
item.classList.remove('playing');
});
if (this.currentTrack) {
document.querySelectorAll('.track-item').forEach(item => {
const trackName = item.querySelector('.track-name').textContent;
if (trackName === this.currentTrack.name) {
item.classList.add('playing');
}
});
}
}
async checkIfTrackIsLiked(trackId) {
try {
const response = await fetch(`https://api.spotify.com/v1/me/tracks/contains?ids=${trackId}`, {
headers: { 'Authorization': `Bearer ${this.token}` }
});
const [isLiked] = await response.json();
const currentLikeBtn = document.getElementById('like-current-btn');
if (isLiked) {
currentLikeBtn.classList.add('liked');
currentLikeBtn.textContent = '♥';
} else {
currentLikeBtn.classList.remove('liked');
currentLikeBtn.textContent = '♡';
}
} catch (error) {
console.error('Error checking if track is liked:', error);
}
}
formatTime(ms) {
const minutes = Math.floor(ms / 60000);
const seconds = Math.floor((ms % 60000) / 1000);
return `${minutes}:${seconds.toString().padStart(2, '0')}`;
}
showLoading() {
document.getElementById('loading-overlay').classList.remove('hidden');
}
hideLoading() {
document.getElementById('loading-overlay').classList.add('hidden');
}
logout() {
localStorage.removeItem('spotify_access_token');
this.token = null;
if (this.player) {
this.player.disconnect();
}
this.showLoginScreen();
}
setupEventListeners() {
// Login
document.getElementById('login-btn').addEventListener('click', () => {
window.location.href = '/login';
});
// Logout
document.getElementById('logout-btn').addEventListener('click', () => {
this.logout();
});
// Tab switching
document.querySelectorAll('.nav-tab').forEach(tab => {
tab.addEventListener('click', () => {
const tabName = tab.dataset.tab;
// Update active tab
document.querySelectorAll('.nav-tab').forEach(t => t.classList.remove('active'));
tab.classList.add('active');
// Show corresponding content
document.querySelectorAll('.tab-content').forEach(content => {
content.classList.remove('active');
});
document.getElementById(`${tabName}-tab`).classList.add('active');
});
});
// Search
const searchInput = document.getElementById('search-input');
const searchBtn = document.getElementById('search-btn');
const performSearch = () => {
this.search(searchInput.value);
};
searchBtn.addEventListener('click', performSearch);
searchInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
performSearch();
}
});
// Player controls
document.getElementById('play-btn').addEventListener('click', () => {
this.togglePlayPause();
});
document.getElementById('next-btn').addEventListener('click', () => {
this.nextTrack();
});
document.getElementById('prev-btn').addEventListener('click', () => {
this.previousTrack();
});
// Volume control
document.getElementById('volume-slider').addEventListener('input', (e) => {
this.setVolume(e.target.value);
});
// Progress bar click
document.querySelector('.progress-bar').addEventListener('click', async (e) => {
const rect = e.target.getBoundingClientRect();
const percent = (e.clientX - rect.left) / rect.width;
const position = percent * this.duration;
try {
await this.player.seek(position);
} catch (error) {
console.error('Error seeking:', error);
}
});
// Current track like button
document.getElementById('like-current-btn').addEventListener('click', () => {
if (this.currentTrack) {
this.toggleLike(this.currentTrack.id, document.getElementById('like-current-btn'));
}
});
// Update progress periodically
setInterval(() => {
if (this.isPlaying) {
this.player.getCurrentState().then(state => {
if (state) {
this.currentPosition = state.position;
this.updateUI();
}
});
}
}, 1000);
}
}
// Initialize the app
const spotifyPlayer = new SpotifyPlayer(); |