brinkiepie
6/4/2016 - 11:26 AM

imports playlists from JSON exports into plug.dj (Dubtrack export compatible)

imports playlists from JSON exports into plug.dj (Dubtrack export compatible)

// enter your exported JSON here. With a large playlist, this can get pretty long.
// don't worry if your browser freezes for a moment when you paste it
// example:
// exportedJSON = {"time":1416,"status":"ok", ...}
exportedJSON = {}

// sanity check
if (!exportedJSON || !exportedJSON.data.length || !exportedJSON.data.length) {
    throw new Error("the `exportedJSON` you passed in is invalid. Please read the top comment on this script or ask for help")
}

if (exportedJSON.data.length > 200) {
    console.log("plug has a fixed playlist limit of 200 songs, but our exported playlist is larger")
    console.log("splitting up playlist into chunks of max. 200 songs.")
    console.log("("+Math.ceil(exportedJSON.data.length / 200)+" chunks in total)")
    console.log()

    // import first chunk
    importChunk(0)

    // this function defines how to import chunks
    function importChunk(i) {
        console.log("loading "+xth(i)+" chunk...")

        // because Dubtrack doesn't store the songs' name and artist separately, we're imitating
        // what happens when you grab a song from the search on plug.dj, and reset the artist
        // and title to what Youtube/Soundcloud tell us.
        // if you want to keep your custom song titles, ask for this code to be adjusted
        mediaLookup(exportedJSON.data.slice(i*200, (i+1)*200)).then(function(songs) {
            // all song data has been looked up, proceed with creating a playlist

            importPlaylist("### import "+exportedJSON.meta.name+" ("+(i+1)+")", songs)

            // if we're not done, import the next chunk
            if (i*200 <= exportedJSON.data.length)
                importChunk(i+1)
        })
    }
} else {
    // import playlist as a whole
    console.log("loading song data...")
    mediaLookup(exportedJSON.data).then(function(songs) {
        importPlaylist("### import " + exportedJSON.meta.name, songs)
    })
}

// this is the function used to convert and import our playlist
function importPlaylist(playlistName, songs) {
    console.log('importing songs into playlist "'+playlistName+'"...')

    createPlaylist(playlistName, songs.filter(function(s){return !!s.format}).map(function(s, i){
        // the following gets executed for every single song in our playlist:

        // first, we try to get the artist name from the upload title (just like plug.dj would)
        // we split the title on " - ", I think plug splits on "-", so results may differ a little bit
        // using " - " yields better results, however
        var i = s.title.indexOf(" - ");
        if (i != -1) {
            s.author = s.title.substr(0, i);
            s.title = s.title.substr(i+3);
        } else {
            // if the upload title doesn't have " - ", we use the uploader's name
            // if we couldn't get the uploader's name (shouldn't happen), we use "?"
            s.author = s.uploader.name || "?"
        }

        // return the song data in the necessary format
        return {
            cid: ""+s.cid,
            format: s.format,
            duration: ~~s.duration,
            image: s.format == 1
                // if our song is from Youtube, use the normal video thumbnail
                ? "https://i.ytimg.com/vi/"+s.cid+"/default.jpg"
                // if it's from Soundcloud, either use the custom artwork,
                // or plug's default Soundcloud thumbnail
                : s.image || plugUrls && plugUrls.scThumbnail || "https://i.imgur.com/41EAJBO.png",
            author: s.author,
            title: s.title,
            id: 0
        }
    }));

}