Skip to content

Instantly share code, notes, and snippets.

@duplaja
Last active April 28, 2022 22:15
Show Gist options
  • Save duplaja/9bab6ec9344b1534583a3383ac8688bd to your computer and use it in GitHub Desktop.
Save duplaja/9bab6ec9344b1534583a3383ac8688bd to your computer and use it in GitHub Desktop.
AllMusic Favorites Scraper JS
for (let i = 1; i < 8; i++) {
setTimeout(function timer() {
window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' })
if(i == 7) {
export_csv_favorites();
}
}, i * 4000);
}
function export_csv_favorites() {
const rows = [["Artist-Album","AllMusic Link"]]
var title
var artist
var combined
var list = document.getElementById("album-recommendations");
var items = list.getElementsByClassName("single-album-recommendation");
for (i = 0; i < items.length; i++) {
title = items[i].getElementsByTagName('a')[0].getAttribute("oldtitle");
href = items[i].getElementsByTagName('a')[0].href;
artist = items[i].getElementsByClassName("artist")[0].innerText;
combined = artist+' - '+title;
nonrated = items[i].getElementsByClassName('rating-user-00');
if (nonrated.length > 0) {
rows[rows.length] = [combined,href];
}
}
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 = "all-music-"+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