Untitled

 avatar
unknown
plain_text
2 years ago
3.1 kB
7
Indexable
const corsProxy = "https://cors-anywhere.herokuapp.com/";
const endpoint = "https://api.deezer.com";
const playlistIds = ["2021626162", "4676814864", "8074584322", "11153531204"];
const playlistRapBtn = document.getElementById("playlistRap");
const playlistRockBtn = document.getElementById("playlistRock");
const playlistRnbBtn = document.getElementById("playlistRnb");
const playlistPopularBtn = document.getElementById("playlistPopular");

playlistRapBtn.addEventListener("click", function () {
    getPlaylist(4676814864);
});

playlistRockBtn.addEventListener("click", function () {
    getPlaylist(8074584322);
});

playlistRnbBtn.addEventListener("click", function () {
    getPlaylist(2021626162).then((r) => {
        console.log(r);
        const numberOfTracks = 10;
         const randomTracks = getRandomTracks(numberOfTracks, r.tracks)
        console.log("randomTracks : ", randomTracks) 
        localStorage.setItem('randomTracks', JSON.stringify(randomTracks));
        const receivedTracks = localStorage.getItem('randomTracks')
        console.log("receivedTracks ----> ", JSON.parse(receivedTracks))
    });

});

playlistPopularBtn.addEventListener("click", function () {
    getPlaylist(11153531204);
});

document.getElementById("playlistRap").addEventListener(
    "click",
    function () {
        console.log("click on rap button");
        window.location.href = "quizz_rap.html";
    },
    false,
);

document.getElementById("playlistRock").addEventListener("click", function () {
    window.location.href = "quizz_rock.html";
});

/* document.getElementById("playlistRnb").addEventListener("click", function () {
    window.location.href = "quizz_rnb.html";
}); */
 
document.getElementById("playlistPopular").addEventListener("click", function () {
    window.location.href = "quizz_popular.html";
});

async function getPlaylists() {
    try {
        const promises = playlistIds.map(async (id) => {
            const response = await fetch(`${endpoint}/playlist/${id}`);
            const data = await response.json();
            return data;
        });

        const playlists = await Promise.all(promises);
        return playlists;
    } catch (error) {
        console.error(error);
    }
}

async function getPlaylist(id) {
    try {
        const response = await fetch(`${corsProxy}${endpoint}/playlist/${id}`);
        const data = await response.json();
        return data;
    } catch (error) {
        console.error(error);
    }
}

function getRandomInt(max) {
    return Math.floor(Math.random() * max);
}


getRandomTracks = (numberOfTracks, tracks) => {
    const chosenTracks = [];
    let i = 0;
    while (i < numberOfTracks) {
        const randIndex = getRandomInt(tracks.data.length);
        const chosenTrack = tracks.data[randIndex];        
        if (chosenTracks.length === 0) {
          chosenTracks.push(chosenTrack);
          i++;
        }
        else if (chosenTracks.length && !chosenTracks.map((c) => c.id).includes(chosenTrack.id)) {
            chosenTracks.push(chosenTrack);
            i++;
        }
    }
    return chosenTracks;
};
Editor is loading...