Created
August 3, 2022 00:55
-
-
Save duplaja/98920b8fffb4f420479adab553caa838 to your computer and use it in GitHub Desktop.
New All Music Favorites Scraper
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
start_countdown(); | |
function start_countdown() { | |
for (let i = 1; i <= 2; i++) { | |
setTimeout(function timer() { | |
window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' }) | |
if (i == 2) { | |
export_csv_favorites(); | |
} | |
}, i * 4000); | |
} | |
} | |
function export_csv_favorites() { | |
var rows = ''; | |
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; | |
artist.replace(/\//g, ","); | |
combined = artist + ' - ' + title; | |
nonrated = items[i].getElementsByClassName('rating-user-00'); | |
if (nonrated.length > 0) { | |
if (rows == '') { | |
rows = rows + combined + ' ' + href; | |
} else { | |
rows = rows + '\r\n' + 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 + ".txt" | |
generateDownload(filename, rows) | |
} | |
function generateDownload(filename, text, type = 'text/plain;charset=utf-8') { | |
// Create an invisible A element | |
const a = document.createElement('a'); | |
a.style.display = 'none'; | |
document.body.appendChild(a); | |
var BOM = new Uint8Array([0xEF, 0xBB, 0xBF]); | |
// Set the HREF to a Blob representation of the data to be downloaded | |
a.href = window.URL.createObjectURL( | |
new Blob([BOM, text], { type }) | |
); | |
// Use download attribute to set set desired file name | |
a.setAttribute('download', filename); | |
// Trigger the download by simulating click | |
a.click(); | |
// Cleanup | |
window.URL.revokeObjectURL(a.href); | |
document.body.removeChild(a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment