Created
September 5, 2021 03:04
-
-
Save duplaja/215425fee6a5bf3cc2ddbb339cf1b49b to your computer and use it in GitHub Desktop.
Spotispin CSV Downloader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getSpotispin(); | |
function getSpotispin() { | |
const rows = [["Artists-Album","Link"]] | |
var tableRows = document.querySelectorAll("tr.single_rec"); | |
for (i = 0; i < tableRows.length; i++) { | |
combined = tableRows[i].querySelectorAll("td.album_name")[0].innerText.trim(); | |
link = tableRows[i].querySelectorAll("a.spotify_link")[0].href | |
rows[rows.length] = [combined,link]; | |
} | |
var today = new Date(); | |
var dd = String(today.getDate()).padStart(2, '0'); | |
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0! | |
var yyyy = today.getFullYear(); | |
today = mm + '-' + dd + '-' + yyyy; | |
filename = "spotispin-recs-"+today+".csv" | |
exportToCsv(filename,rows) | |
} | |
function exportToCsv(filename, rows) { | |
var processRow = function (row) { | |
var finalVal = ''; | |
for (var j = 0; j < row.length; j++) { | |
var innerValue = row[j] === null ? '' : row[j].toString(); | |
if (row[j] instanceof Date) { | |
innerValue = row[j].toLocaleString(); | |
}; | |
var result = innerValue.replace(/"/g, '""'); | |
if (result.search(/("|,|\n)/g) >= 0) | |
result = '"' + result + '"'; | |
if (j > 0) | |
finalVal += ','; | |
finalVal += result; | |
} | |
return finalVal + '\n'; | |
}; | |
var csvFile = ''; | |
for (var i = 0; i < rows.length; i++) { | |
csvFile += processRow(rows[i]); | |
} | |
var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;' }); | |
if (navigator.msSaveBlob) { // IE 10+ | |
navigator.msSaveBlob(blob, filename); | |
} else { | |
var link = document.createElement("a"); | |
if (link.download !== undefined) { // feature detection | |
// Browsers that support HTML5 download attribute | |
var url = URL.createObjectURL(blob); | |
link.setAttribute("href", url); | |
link.setAttribute("download", filename); | |
link.style.visibility = 'hidden'; | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment