From fa15060098c8dba5d847db3c1dce932efa5a7cd6 Mon Sep 17 00:00:00 2001 From: Jim Shepich III Date: Tue, 30 Nov 2021 04:01:24 -0500 Subject: [PATCH] Added JSON extraction script for E&E bgm page --- scripts/json_scraper.js | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 scripts/json_scraper.js diff --git a/scripts/json_scraper.js b/scripts/json_scraper.js new file mode 100644 index 0000000..8e3363c --- /dev/null +++ b/scripts/json_scraper.js @@ -0,0 +1,63 @@ +function extract_data(){ + var data = document.getElementsByTagName("tr"); + var master = {}; + var header = null; + for(row of data){ + if(row.className == "row0"){ + continue; + //Skip table headers + } else if (row.className == "row1"){ + header = row.parentElement.parentElement.parentElement.parentElement.previousElementSibling.innerText; + //If we're at a new table, then we have to go up the DOM tree until we + //find that section's header. + master[header] = []; + //And create a new list for that header in the master object. + } + + var row_object = {}; + row_object["link"] = row.children[0].children[0].href; + //The link can be found in the anchor element in the first cell of the + //row. + var text = row.children[0].children[0].innerText; + //Get the text of the link. + row_object["song"] = text.substring(0,text.indexOf("(")-1); + //The name of the song is the start of the text until the space before + //the parenthesis. I've made sure to use hyphens in place of parentheses in + //song titles. + row_object["source"] = text.substring(text.indexOf("(")+1, text.indexOf(")")); + //The work that the song is from is indicated between the parentheses. + + row_object["vibes"] = row.children[1].innerText.split(", ") + //The vibes are in the second column, delimited by a comma-space. + + + master[header].push(row_object); + + } + + return master; +} + + + + +function download(filename, text) { + //Borrowed shamelessly from: + //https://ourcodeworld.com/articles/read/189/how-to-create-a-file-and-generate-a-download-with-javascript-in-the-browser-without-a-server + var element = document.createElement('a'); + element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); + element.setAttribute('download', filename); + + element.style.display = 'none'; + document.body.appendChild(element); + + element.click(); + + document.body.removeChild(element); +} + +var button = document.getElementById("download-JSON"); +button.addEventListener("click",function(){ + var json_data = JSON.stringify(extract_data(),null,2); + download("epics_bgm.json",json_data); +});