Last active
May 3, 2022 21:16
-
-
Save duplaja/b3a165e94d8db8a91cf76088a72f36b2 to your computer and use it in GitHub Desktop.
RYM Recs 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
//export_csv_favorites('page_recommendations_list_main'); | |
//export_csv_favorites('page_recommendations_list_non_affinity'); | |
function export_csv_favorites(passed_parent_id) { | |
const rows = [["Artist - Album","Year","Genres","RYM Link"]] | |
var title | |
var artist | |
var combined | |
var list = document.getElementById(passed_parent_id); | |
var items = list.getElementsByClassName("component_discography_item_info_inner"); | |
for (i = 0; i < items.length; i++) { | |
item_details = items[i].getElementsByClassName('component_discography_item_details')[0]; | |
item_year = item_details.firstElementChild.innerText.replaceAll('\n',''); | |
title_item = items[i].getElementsByClassName('component_discography_item_link')[0]; | |
href = title_item.href.replaceAll('\n',''); | |
title = title_item.innerText.replaceAll('\n',''); | |
genre_list = items[i].getElementsByClassName('comma_separated'); | |
genre = ''; | |
for(k = 0; k < genre_list.length; k++) { | |
if (k >=1) { | |
genre = genre + ', '; | |
} | |
genre = genre + genre_list[k].innerText.replaceAll('\n',''); | |
} | |
artist_string = ''; | |
artist_list = items[i].getElementsByClassName("artist"); | |
for (j = 0; j < artist_list.length; j++) { | |
artist_string = artist_string + artist_list[j].innerText.replaceAll('\n','') | |
} | |
artist = artist_string.replaceAll(' /',', ') | |
combined = artist+' - '+title; | |
rows[rows.length] = [combined,item_year,genre,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; | |
if(passed_parent_id == 'page_recommendations_list_main') { | |
filename = "rym-recs-"+today+".csv" | |
} | |
else { | |
filename = "rym-challenges-"+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