Created
November 22, 2024 23:11
-
-
Save MazeW/abb9576235394c9d96e1b2fe44cda623 to your computer and use it in GitHub Desktop.
Simple function for getting last.fm user status using native fetch function.
This file contains hidden or 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
async function getSongInfo(user) { | |
const url = `https://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user=${user}&api_key=${API_KEY}&limit=1&format=json`; | |
const response = await fetch(url); | |
const json = await response.json(); | |
const track = json.recenttracks.track[0]; | |
return { user: json.recenttracks["@attr"].user, artist: track.artist["#text"], name: track.name, image: track.image[track.image.length - 1]["#text"], playing: ((typeof (track["@attr"]) != "undefined") ? true : false) }; | |
} | |
// example response: | |
// {"user":"I0l","artist":"STEREO DIVE FOUNDATION","name":"KATAWARA","image":"https://lastfm.freetls.fastly.net/i/u/300x300/0dc7eab8cf74c799c679eddf3183c5d1.jpg","playing":true} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment