Something went wrong. Try again.
Blog Built with AT Protocol skiddle.id
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220<!-- Client-side Last.fm widget that updates in real-time --><div id="lastfm-widget" class="lastfm-widget border border-muted rounded-lg p-4 bg-[var(--color-black)]"> <!-- Loading state --> <div id="lastfm-loading" class="flex items-center gap-2 text-sm text-base03"> <i class="fa-solid fa-music"></i> <span>Loading Last.fm data...</span> </div> <!-- Error state --> <div id="lastfm-error" class="hidden flex items-center gap-2 text-sm text-base03"> <i class="fa-solid fa-exclamation-triangle"></i> <span>Unable to load Last.fm data</span> </div> <!-- Content --> <div id="lastfm-content" class="hidden flex items-center gap-4"> <!-- Album Art --> <div class="flex-shrink-0"> <img id="lastfm-image" class="w-16 h-16 rounded-lg border border-muted object-cover hidden" alt="Album art" /> <div id="lastfm-no-image" class="w-16 h-16 rounded-lg border border-muted bg-muted flex items-center justify-center hidden"> <i class="fa-solid fa-music text-base03"></i> </div> </div> <!-- Track Info --> <div class="flex-1 min-w-0"> <div class="flex items-center gap-2 text-sm text-base03 mb-1"> <span id="lastfm-status">Last Played</span> <span>on</span> <a href="https://www.last.fm/user/arcestia" target="_blank" rel="noopener noreferrer" class="text-[var(--color-primary)] hover:text-base0E transition-colors" aria-label="Last.fm Profile" > <i class="fa-brands fa-lastfm"></i> </a> </div> <div class="space-y-1"> <a id="lastfm-track-link" href="#" target="_blank" rel="noopener noreferrer" class="block font-medium text-[var(--color-primary)] hover:text-base0E transition-colors truncate" > <span id="lastfm-track-name">Track Name</span> </a> <a id="lastfm-artist-link" href="#" target="_blank" rel="noopener noreferrer" class="block text-sm text-base03 hover:text-[var(--color-primary)] transition-colors truncate" > <span id="lastfm-artist-name">Artist Name</span> </a> </div> </div> <!-- Status Indicator --> <div id="lastfm-playing-indicator" class="flex-shrink-0 hidden"> <div class="w-2 h-2 bg-green-500 rounded-full animate-pulse" title="Currently playing"></div> </div> </div></div>
<script> // Last.fm API configuration const MAIN = "arcestia"; interface Track { name: string; artist: string; imageUrl: string; isCurrent: boolean; } async function fetchLastFmData(): Promise<Track | null> { try { // Fetch from server-side API to avoid exposing API keys to the client const response = await fetch(`/api/lastfm.json?user=${encodeURIComponent(MAIN)}`, { headers: { 'Accept': 'application/json' }, }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = (await response.json()) as { track?: any } | null; const t = data?.track; if (t) { return { name: t.name, artist: t.artist, imageUrl: t.imageUrl, isCurrent: Boolean(t.isCurrent), }; } return null; } catch (error) { console.error("Last.fm fetch error:", error); throw error; } } function updateUI(track: Track | null, error: boolean = false) { const loading = document.getElementById('lastfm-loading'); const errorEl = document.getElementById('lastfm-error'); const content = document.getElementById('lastfm-content'); // Hide loading if (loading) loading.classList.add('hidden'); if (error || !track) { if (errorEl) { errorEl.classList.remove('hidden'); errorEl.classList.add('flex'); } if (content) content.classList.add('hidden'); return; } // Hide error, show content if (errorEl) errorEl.classList.add('hidden'); if (content) { content.classList.remove('hidden'); content.classList.add('flex'); } // Update track info const trackName = document.getElementById('lastfm-track-name'); const artistName = document.getElementById('lastfm-artist-name'); const trackLink = document.getElementById('lastfm-track-link') as HTMLAnchorElement; const artistLink = document.getElementById('lastfm-artist-link') as HTMLAnchorElement; const status = document.getElementById('lastfm-status'); const playingIndicator = document.getElementById('lastfm-playing-indicator'); const image = document.getElementById('lastfm-image') as HTMLImageElement; const noImage = document.getElementById('lastfm-no-image'); if (trackName) trackName.textContent = track.name; if (artistName) artistName.textContent = track.artist; if (status) status.textContent = track.isCurrent ? "Now Playing" : "Last Played"; if (trackLink) { trackLink.href = `https://www.last.fm/music/${encodeURIComponent(track.artist)}/_/${encodeURIComponent(track.name)}`; trackLink.title = track.name; } if (artistLink) { artistLink.href = `https://www.last.fm/music/${encodeURIComponent(track.artist)}`; artistLink.title = track.artist; } // Handle playing indicator if (playingIndicator) { if (track.isCurrent) { playingIndicator.classList.remove('hidden'); } else { playingIndicator.classList.add('hidden'); } } // Handle image const hasValidImage = track.imageUrl && track.imageUrl !== "https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png"; if (hasValidImage && image && noImage) { image.src = track.imageUrl; image.alt = `${track.name} by ${track.artist}`; image.classList.remove('hidden'); noImage.classList.add('hidden'); } else if (image && noImage) { image.classList.add('hidden'); noImage.classList.remove('hidden'); } } async function loadLastFm() { try { const track = await fetchLastFmData(); updateUI(track); } catch (error) { updateUI(null, true); } } // Load initially loadLastFm(); // Refresh every 30 seconds setInterval(loadLastFm, 30000);</script>
<style> .lastfm-widget { max-width: 100%; } @keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.5; } } .animate-pulse { animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite; }</style>