Skip to content

Instantly share code, notes, and snippets.

@alexhulbert
Last active May 5, 2022 21:21
Show Gist options
  • Select an option

  • Save alexhulbert/898c7a145d37d5155d4c63c19e3de451 to your computer and use it in GitHub Desktop.

Select an option

Save alexhulbert/898c7a145d37d5155d4c63c19e3de451 to your computer and use it in GitHub Desktop.
OrganizeYourMusic Import/Export
const w = window
const importBtn = document.createElement('label')
importBtn.style.display = 'inline-block'
importBtn.style.cursor = 'pointer'
importBtn.style['margin-right'] = '20px'
importBtn.setAttribute('class', 'btn btn-primary')
importBtn.setAttribute('id', 'importBtn-btn')
importBtn.innerHTML = 'Import'
const input = document.createElement('input')
input.setAttribute('type', 'file')
input.setAttribute('id', 'importBtn')
input.style.display = 'none'
importBtn.appendChild(input)
document.getElementById('info').append(importBtn)
const fn = () => {
const fr = new FileReader()
fr.onload = () => loadFile(JSON.parse(fr.result))
fr.readAsText(input.files[0])
}
input.addEventListener('change', fn)
const saveFile = () => {
const elem = document.createElement('a')
const data = {
theWorld: w.theWorld,
curArtists: w.curArtists,
curTracks: w.curTracks,
curAlbums: w.curAlbums
}
const fileBlob = new Blob([JSON.stringify(data)], { type: 'application/json' })
elem.href = URL.createObjectURL(fileBlob)
elem.download = 'spotify-data.json'
elem.click()
}
const loadFile = data => {
w.theWorld = data.theWorld
w.curArtists = data.curArtists
w.curAlbums = data.curAlbums
w.curTracks = data.curTracks
w.theWorld.forEach((bin, binIndex) => bin.nodes.forEach((node, nodeIndex) => {
node.sorter = x => x
node.tracks.forEach(track => {
track.feats.date_added = { format: () => track.feats.date_added }
})
}))
w.refreshTheWorld()
}
const exportBtn = document.createElement('button')
document.getElementById('info').append(exportBtn)
exportBtn.innerText = 'export'
exportBtn.setAttribute('class', 'btn btn-primary')
exportBtn.addEventListener('click', saveFile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment