Skip to content

Instantly share code, notes, and snippets.

@O-Zone
Created August 2, 2020 15:03
Show Gist options
  • Save O-Zone/9126e8a123fe84098ab6bab63515e89b to your computer and use it in GitHub Desktop.
Save O-Zone/9126e8a123fe84098ab6bab63515e89b to your computer and use it in GitHub Desktop.
Script to harvest all song/title/album from the music.youtube.com/library/songs/ and export them to a .csv file
var waitForContentSeconds = 10;
var bottomTrials = 0;
var pageHeight = 0;
function grabYoutubeList() {
var AllNodes = [...document.getElementsByTagName('ytmusic-shelf-renderer')[0].getElementsByTagName('ytmusic-responsive-list-item-renderer')];
AllNodes.shift(); // remove first "blend" element
var harvestedTunes = AllNodes.map(t => {
var items = [...t.children[3].getElementsByTagName('yt-formatted-string')].map(n => n.getAttribute('title').replace(/"/g,'”'));
return `"${items[0]}", "${items[1]}", "${items[2]}"<br>\n`;
});
var resultWindow = window.open('', '');
resultWindow.document.open();
resultWindow.document.write('Title, Artist, Album<br>\n' + harvestedTunes.join(''));
resultWindow.document.close();
}
function scrollToBottom() {
if (pageHeight !== document.body.scrollHeight) {
bottomTrials = 0;
window.scrollTo(0,document.body.scrollHeight);
pageHeight = document.body.scrollHeight;
setTimeout(scrollToBottom, 1000);
} else {
if (bottomTrials < waitForContentSeconds) {
bottomTrials++;
setTimeout(scrollToBottom, 1000);
} else {
// We are at the bottom
grabYoutubeList();
}
}
}
scrollToBottom();
@O-Zone
Copy link
Author

O-Zone commented Aug 2, 2020

Log in on your youtube account, go to "songs", and run this from the console (ctrl+shift+i).
Copy the content in the new window (you'll need to allow popups), and save it as a .csv file.

@O-Zone
Copy link
Author

O-Zone commented Aug 2, 2020

//Here is a script to get all your favorite artists from youtube too (the https://music.youtube.com/library/artists page). Does exactly the same thing, only with artists

var waitForContentSeconds = 10;
var bottomTrials = 0;
var pageHeight = 0;
function grabYoutubeList() {
	var kunstnere = [...document.getElementsByClassName('yt-simple-endpoint')].map(t => t.getAttribute('aria-label')).filter(t => t != null && t != 'Start');
	var resultWindow = window.open('', '');
	resultWindow.document.open();
	resultWindow.document.write('Artist<br>\n' + kunstnere.join('<br>\n'));
	resultWindow.document.close();
}
function scrollToBottom() {
	if (pageHeight !== document.body.scrollHeight) {
		bottomTrials = 0;
		window.scrollTo(0,document.body.scrollHeight);
		pageHeight = document.body.scrollHeight;
		setTimeout(scrollToBottom, 1000);
	} else {
		if (bottomTrials < waitForContentSeconds) {
			bottomTrials++;
			setTimeout(scrollToBottom, 1000);
		} else {
			// We are at the bottom
			grabYoutubeList();
		}
	}
}
scrollToBottom();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment