Pruebas2006

Metadata

Mar 4th, 2024
1,119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Change to the domain or IP and port of your Icecast statistics page
  2. const METADATA = "http://your_icecast_static_page.com:8000/status.xsl";
  3.  
  4. window.onload = function() {
  5.    fetchAndUpdateMetadata(); // Call the function when the window is fully loaded
  6.    setInterval(function() {
  7.         fetchAndUpdateMetadata(); // Call the function every 5 seconds using setInterval
  8.     }, 5e3);
  9. };
  10.  
  11.  
  12. // 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.
  13. function fetchAndUpdateMetadata() {
  14.     fetch(METADATA)
  15.         .then(response => {
  16.             if (!response.ok) {
  17.                 throw new Error(`Error in the request: ${response.status} ${response.statusText}`);
  18.             }
  19.             return response.text();
  20.         })
  21.         .then(html => {
  22.             const parser = new DOMParser();
  23.             const doc = parser.parseFromString(html, "text/html");
  24.  
  25.             const rows = doc.querySelectorAll(".mountcont table.yellowkeys tbody tr");
  26.             let currentlyPlaying = "";
  27.  
  28.             rows.forEach(row => {
  29.                 const label = row.querySelector("td:first-child").textContent.trim();
  30.                 const value = row.querySelector("td:last-child").textContent.trim();
  31.  
  32.                 if (label === "Currently playing:") {
  33.                     currentlyPlaying = value;
  34.  
  35.                     // Extract the artist name and track
  36.                     const separatorIndex = currentlyPlaying.indexOf(" - ");
  37.                     if (separatorIndex !== -1) {
  38.                         const artistName = currentlyPlaying.substring(0, separatorIndex).trim();
  39.                         const songTitle = currentlyPlaying.substring(separatorIndex + 3).trim();
  40.                         console.log("Artist:", artistName);
  41.                         console.log("Track:", songTitle);
  42.  
  43.                         // You can send the artist name to another function if needed
  44.                         // updateArtist(artistName);
  45.                     } else {
  46.                         console.error("Incorrect format for 'Currently playing:'");
  47.                     }
  48.                 }
  49.             });
  50.  
  51.             if (!currentlyPlaying) {
  52.                 console.error("Unable to find the 'Currently playing:' element in the HTML.");
  53.             }
  54.         })
  55.         .catch(error => {
  56.             console.error("Error fetching metadata:", error.message);
  57.         });
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment