62 lines
2.2 KiB
JavaScript
62 lines
2.2 KiB
JavaScript
const TARGET_URL = "http://localhost:8080/endpoint";
|
|
let lastSentSignature = "";
|
|
|
|
function extractAndSendSong() {
|
|
const trackInfo = document.querySelector('[data-testid="now-playing-widget"]');
|
|
|
|
if (trackInfo) {
|
|
const links = trackInfo.querySelectorAll('a');
|
|
const songTitle = links[0] ? links[0].innerText : null;
|
|
const artistName = links[1] ? links[1].innerText : "Unknown Artist";
|
|
|
|
// Získání obrázku alba
|
|
const imgElement = trackInfo.querySelector('img');
|
|
const albumCoverUrl = imgElement ? imgElement.src : null;
|
|
|
|
// Získání času
|
|
const currentTimeEl = document.querySelector('[data-testid="playback-position"]');
|
|
const durationEl = document.querySelector('[data-testid="playback-duration"]');
|
|
|
|
const currentTime = currentTimeEl ? currentTimeEl.innerText : "0:00";
|
|
const duration = durationEl ? durationEl.innerText : "0:00";
|
|
|
|
const progressFull = `${currentTime}/${duration}`;
|
|
|
|
// Spolehlivější detekce reklamy:
|
|
// Reklamy na Spotify trvají vždy přesně 30 vteřin (0:30) nebo nemají běžné odkazy na skladbu
|
|
const isAd = (duration === "0:30") || (links.length < 2) || (songTitle === null);
|
|
|
|
const finalTitle = isAd ? "Reklama" : songTitle;
|
|
const finalArtist = isAd ? "Spotify" : artistName;
|
|
|
|
if (finalTitle || isAd) {
|
|
const currentSignature = `${finalArtist}-${finalTitle}-${currentTime}-${isAd}`;
|
|
|
|
if (currentSignature !== lastSentSignature) {
|
|
lastSentSignature = currentSignature;
|
|
|
|
console.log("Odesílám data:", finalArtist, "-", finalTitle, `[${progressFull}]`, "Reklama:", isAd);
|
|
|
|
fetch(TARGET_URL, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify({
|
|
artist: finalArtist,
|
|
title: finalTitle,
|
|
coverImg: albumCoverUrl,
|
|
progressTime: progressFull,
|
|
ad: isAd ? "true" : "false",
|
|
timestamp: new Date().toISOString()
|
|
})
|
|
}).catch(err => {
|
|
console.error("Chyba při odesílání:", err);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Kontrola každou vteřinu
|
|
setInterval(extractAndSendSong, 1000); |