Created
March 24, 2024 08:51
-
-
Save BANKA2017/cef4e33e52e794fc00f5510683311d49 to your computer and use it in GitHub Desktop.
Download lyrics from genius in browser console
This file contains 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
// download lyrics from genius in browser console | |
const artists = { | |
poppin_party: 2041573, | |
afterglow: 2600308, | |
pastel_palettes: 2967893, | |
roselia: 2599982, | |
hello_happy_world: 2968729, | |
morfonica: 2601112, | |
raise_a_suilen: 2531378, | |
glitter_green: 3094950, | |
chispa: 3411955, | |
mygo: 3290646, | |
avemujica: 3500082, | |
popy: 3460159, | |
rose: 3438577 | |
} | |
function download (filename, text) { | |
let 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); | |
} | |
const getDoc = async (link) => { | |
const doc = (new DOMParser()).parseFromString(await (await fetch(link)).text(), "text/html") | |
return [...new Set(doc.querySelectorAll('[data-lyrics-container]'))].map(x => { | |
if (x.querySelector('[data-exclude-from-selection]')) { | |
x.removeChild(doc.querySelector('[data-exclude-from-selection]')) | |
} | |
x.innerHTML = x.innerHTML.toString().replaceAll('<br>', "\n") | |
return x.innerText | |
}).join("\n") | |
} | |
let globalList = [] | |
for (const artist in artists) { | |
let page = 1 | |
let songsData = {} | |
let tmpList = [] | |
do { | |
songsData = await (await fetch(`https://genius.com/api/artists/${artists[artist]}/songs?page=${page}&sort=release_date`)).json() | |
//console.log(songsData) | |
tmpList.push(songsData.response.songs)//..map(x => x.url)) | |
page++ | |
} while (songsData?.response?.next_page) | |
globalList = globalList.concat(tmpList.flat()) | |
} | |
//download('config.json', JSON.stringify(config)) | |
download(`song.json`, JSON.stringify(globalList.flat(), null, 2)) | |
let tmpLyrics = [] | |
for (const tmpSong of globalList) { | |
tmpLyrics.push([tmpSong.path, await getDoc(tmpSong.url)]) | |
} | |
download(`lyrics.json`, JSON.stringify([...new Set(tmpLyrics.filter(x => x[1].length > 0).map(x => x[0]))].map(x => [x.slice(1), tmpLyrics.filter(y => y[0] === x)[0][1]]), null, 4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment