Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Change to the domain or IP and port of your Icecast statistics page
- const METADATA = "http://your_icecast_static_page.com:8000/status.xsl";
- window.onload = function() {
- fetchAndUpdateMetadata(); // Call the function when the window is fully loaded
- setInterval(function() {
- fetchAndUpdateMetadata(); // Call the function every 5 seconds using setInterval
- }, 5e3);
- };
- // This function fetches data from the Icecast server (v. 2.4.4), parses the HTML response, and extracts the currently playing artist name and track title.
- function fetchAndUpdateMetadata() {
- fetch(METADATA)
- .then(response => {
- if (!response.ok) {
- throw new Error(`Error in the request: ${response.status} ${response.statusText}`);
- }
- return response.text();
- })
- .then(html => {
- const parser = new DOMParser();
- const doc = parser.parseFromString(html, "text/html");
- const rows = doc.querySelectorAll(".mountcont table.yellowkeys tbody tr");
- let currentlyPlaying = "";
- rows.forEach(row => {
- const label = row.querySelector("td:first-child").textContent.trim();
- const value = row.querySelector("td:last-child").textContent.trim();
- if (label === "Currently playing:") {
- currentlyPlaying = value;
- // Extract the artist name and track
- const separatorIndex = currentlyPlaying.indexOf(" - ");
- if (separatorIndex !== -1) {
- const artistName = currentlyPlaying.substring(0, separatorIndex).trim();
- const songTitle = currentlyPlaying.substring(separatorIndex + 3).trim();
- console.log("Artist:", artistName);
- console.log("Track:", songTitle);
- // You can send the artist name to another function if needed
- // updateArtist(artistName);
- } else {
- console.error("Incorrect format for 'Currently playing:'");
- }
- }
- });
- if (!currentlyPlaying) {
- console.error("Unable to find the 'Currently playing:' element in the HTML.");
- }
- })
- .catch(error => {
- console.error("Error fetching metadata:", error.message);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment