From 0895e7ff5545e2a37f6498f814a1a7deb45cabc6 Mon Sep 17 00:00:00 2001 From: kobayashi90 Date: Tue, 21 Jan 2025 06:18:24 +0000 Subject: [PATCH] Add last.fm/scrobble.js --- last.fm/scrobble.js | 69 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 last.fm/scrobble.js diff --git a/last.fm/scrobble.js b/last.fm/scrobble.js new file mode 100644 index 0000000..2d4774c --- /dev/null +++ b/last.fm/scrobble.js @@ -0,0 +1,69 @@ +const API_KEY = "YOUR_LASTFM_API_KEY"; // Replace with your Last.fm API Key +const USERNAME = "YOUR_LASTFM_USERNAME"; // Replace with your Last.fm username +const POLLING_INTERVAL = 5000; // Polling interval in milliseconds + +const recentScrobblesUrl = `https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=${USERNAME}&api_key=${API_KEY}&format=json`; + +// Function to fetch recent scrobbles +async function fetchRecentScrobbles() { + try { + const response = await fetch(recentScrobblesUrl); + if (!response.ok) { + throw new Error("Failed to fetch scrobbles"); + } + const data = await response.json(); + const tracks = data.recenttracks.track; + updatePanel(tracks); + } catch (error) { + console.error("Error fetching scrobbles:", error); + } +} + +// Function to update the `panel2` section +function updatePanel(tracks) { + const trackInfoContainer = document.getElementById("track-info-container"); + trackInfoContainer.innerHTML = ""; // Clear previous content + + if (tracks.length > 0) { + const recentTracks = tracks.slice(0, 12); // Get the latest 5 tracks + + // Album cover for the latest track + const albumArt = recentTracks[0]?.image?.[3]?.["#text"] || "default-album-art.jpg"; // Large size image + const albumArtHtml = ` +
+ Album Art +
+ `; + trackInfoContainer.innerHTML += albumArtHtml; + + // Recent tracks list with CD icon and heart if favorited + const playlistHtml = ` + + `; + trackInfoContainer.innerHTML += playlistHtml; + } else { + trackInfoContainer.innerHTML = "

No tracks found.

"; + } +} + +// Start polling for updates +function startPolling() { + fetchRecentScrobbles(); + setInterval(fetchRecentScrobbles, POLLING_INTERVAL); +} + +// Call the function on page load +document.addEventListener("DOMContentLoaded", () => { + startPolling(); +}); \ No newline at end of file