A new MusicBrainz user script was released

Export Brainz ratings (to a JSON flat file) is a very interesting userscript, written by @HebPlacesAGehen:

// ==UserScript==
// @name     Export Brainz ratings
// @version  1.1
// @include  /^https://musicbrainz.org/user/\w+/ratings/recording/
// @grant    GM.getValue
// @grant    GM.setValue
// ==/UserScript==

function download(filename, text) {
	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);
}
(async () => {
	let ratingDict = JSON.parse(await GM.getValue("ratings", "{}"));
	let $ratingContainers = document.querySelectorAll(".inline-rating"); for(let $ratingContainer of $ratingContainers){
		let $ratingEl = $ratingContainer.querySelector(".remove-rating");
		if(!$ratingEl){ continue }
		// Get rating
		let rating = Number.parseInt($ratingEl.text);
		// Get recordingId
		let $container = $ratingContainer.parentElement;
		let $anchor = $container.querySelector('a[href^="/recording"]');
		if(!$anchor){ continue }
		let recordingId = $anchor.href.split("/").pop(-1);
		if(recordingId){
			ratingDict[recordingId] = rating * 0.2;
			//console.log(`${recordingId} rated ${rating}`);
		}
	}
	await GM.setValue("ratings", JSON.stringify(ratingDict));
	// Get next page
	let $nextPage = document.querySelector("ul.pagination li:last-child a");
	if($nextPage){
		window.setTimeout(() => $nextPage.click(), 1000);
	} else {
		download("ratings.json", JSON.stringify(ratingDict, null, "\t"));
	}
})();

It does so much with a so small code!

I have just changed this JSON.stringify small parameter to get a more human readable download file:

--		download("ratings.json", JSON.stringify(ratingDict));
++		download("ratings.json", JSON.stringify(ratingDict, null, "\t"));
2 Likes