-
-
Save dustinrouillard/8140fd47c5900d4421637b098b6d92c0 to your computer and use it in GitHub Desktop.
Lanyard Spotify - Calculating current time and song length
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "isomorphic-fetch"; | |
const lanyard = await fetch( | |
"https://api.lanyard.rest/v1/users/156114103033790464" | |
).then((r) => r.json()); | |
const spotify = lanyard.data.spotify; | |
const currentTime = new Date().getTime(); | |
const songLength = spotify.timestamps.end - spotify.timestamps.start; | |
const timeElapsed = currentTime - spotify.timestamps.start; | |
function msToMinSeconds(millis) { | |
var minutes = Math.floor(millis / 60000); | |
var seconds = Number(((millis % 60000) / 1000).toFixed(0)); | |
return seconds == 60 | |
? minutes + 1 + ":00" | |
: minutes + ":" + (seconds < 10 ? "0" : "") + seconds; | |
} | |
console.log("ms song length:", songLength); | |
console.log("ms time elapsed:", timeElapsed); | |
console.log("formatted song length:", msToMinSeconds(songLength)); | |
console.log("formatted time elapsed:", msToMinSeconds(timeElapsed)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment