Skip to content

Instantly share code, notes, and snippets.

@thedivtagguy
Last active August 8, 2025 07:46
Show Gist options
  • Save thedivtagguy/3e39779653978b1d87bf62b480417719 to your computer and use it in GitHub Desktop.
Save thedivtagguy/3e39779653978b1d87bf62b480417719 to your computer and use it in GitHub Desktop.
---
const username = import.meta.env.LASTFM_USERNAME;
const apiKey = import.meta.env.LASTFM_API_KEY;
const response = await fetch(
`http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=${username}&api_key=${apiKey}&format=json&limit=1`
);
const data = await response.json();
const track = data?.recenttracks?.track?.[0];
const isPlaying = track?.["@attr"]?.nowplaying === "true";
---
{track && (
<div class="music-box">
{isPlaying && <div class="pulse"></div>}
<img src={track.image?.[1]?.["#text"]} alt="" />
<div>
<div class="track">{track.name}</div>
<div class="artist">{track.artist?.["#text"]}</div>
</div>
</div>
)}
<style>
.music-box {
display: flex;
gap: 12px;
padding: 12px;
background: #f5f5f5;
border-radius: 6px;
max-width: 280px;
align-items: center;
position: relative;
}
.pulse {
position: absolute;
top: 8px;
right: 8px;
width: 8px;
height: 8px;
background: #22c55e;
border-radius: 50%;
animation: pulse 1.5s infinite;
}
img {
width: 48px;
height: 48px;
border-radius: 4px;
flex-shrink: 0;
}
.track {
font-weight: 600;
font-size: 14px;
line-height: 1.2;
margin-bottom: 2px;
}
.artist {
font-size: 12px;
color: #666;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.3; }
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment