Skip to content

Instantly share code, notes, and snippets.

@cosmicle0
Last active March 14, 2025 04:44
Show Gist options
  • Save cosmicle0/7f5ad70d880c56b87fdc3229877d4281 to your computer and use it in GitHub Desktop.
Save cosmicle0/7f5ad70d880c56b87fdc3229877d4281 to your computer and use it in GitHub Desktop.
watch carti's i am music drop on spotify real-time
let accessToken;
const getAccessToken = async () => {
try {
const response = await fetch('https://accounts.spotify.com/api/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
grant_type: 'client_credentials',
client_id: 'your-client-id',
client_secret: 'your-client-secret'
})
});
const data = await response.json();
if (data.access_token) {
accessToken = data.access_token;
return accessToken;
} else {
console.error("no access_token in response", data);
return null;
}
} catch (err) {
console.error(err);
return null;
}
};
const watchCarti = async () => {
if (!accessToken) {
console.log("Fetching new token...");
accessToken = await getAccessToken();
}
fetch('https://api.spotify.com/v1/artists/699OTQXzgjhIYAHMy9RyPD/albums?include_groups=album&market=US', {
headers: {
"Authorization": `Bearer ${accessToken}`
}
})
.then(res=>res.json())
.then(data=>iAmMusic(data.items.map(o => o.name)))
.catch(err=>console.error(err))
}
const iAmMusic = (albums) => {
const time = new Date();
if (albums.length == 3) {
console.log(`[${time.toLocaleTimeString()}] no album yet\nCurrent albums: ${albums.join(', ')}\n`);
} else {
console.log(`-`.repeat(24));
console.log(`[${time.toLocaleTimeString()}] album is here! ${albums.join(', ')}`);
console.log(`-`.repeat(24));
}
}
watchCarti();
setInterval(watchCarti, 15 * 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment