I created a way to export my Google Play playlists to a text file and also a way to export all of my songs in Google Play to a text file. Now I have both the song list (in an array format) and playlist data (in object and array format) in their respective text files. Here is a simplified version of the files:
[
"song 1 name",
"song 2 name",
"song 3 name"
]
and
{
"playlist 1": [
"song 1",
"song 3"
],
"playlist 2": [
"song 1"
]
}
When I run them in my script, it works correctly and creates the following output:
{
"song 1": [
"playlist 1",
"playlist 2"
],
"song 2": [],
"song 3": [
"playlist 1"
]
}
Here's my code:
var songInput = document.getElementById("songInput"),
playlistInput = document.getElementById("playlistInput"),
result = document.querySelector("textarea");
var songList,
playlistData;
function loadData(id, elem) {
if(elem.files
&& elem.files[0]) {
let myFile = elem.files[0];
let reader = new FileReader();
reader.addEventListener('load', function (e) {
if(id === "songInput")
songList = JSON.parse(e.target.result);
if(id === "playlistInput")
playlistData = JSON.parse(e.target.result);
checkBothAdded();
});
reader.readAsBinaryString(myFile);
}
}
function checkBothAdded() {
if(songList
&& playlistData) {
getSongPlaylistData();
}
}
var songData = {};
function getSongPlaylistData() {
for(let song of songList) {
let playlistsItsIn = [];
Object.keys(playlistData).forEach(function(key) {
if(playlistData[key].includes(song)) {
playlistsItsIn.push(key);
}
});
songData[song] = playlistsItsIn;
}
result.value = JSON.stringify(songData, null, '\t');
}
songInput.addEventListener("change", function() {
loadData("songInput", this);
});
playlistInput.addEventListener("change", function() {
loadData("playlistInput", this);
});
<label>Song text file (generated from <a href="https://webapps.stackexchange.com/q/108103/140514">this answer</a>): <input type="file" id="songInput"></label>
<br>
<label>Playlist text file (generated from <a href="https://webapps.stackexchange.com/a/106604/140514">this answer</a>): <input type="file" id="playlistInput"></label>
<br>
<textarea style="width: 500px; height: 200px;"></textarea>
It works on this small set of data I hand-wrote, but it freezes the browser page when I run it on my ~4,000 song list and substantial playlist file.
How can I do this same thing while helping it perform better so it doesn't freeze my browser tab?
I'm theorizing that it would work faster if I keep them as text files and try to parse each of the playlist names if the song is found, but I'm curious if I can speed up/reduce the memory of this object approach somehow.