Created
December 11, 2012 12:16
-
-
Save makotom/4258138 to your computer and use it in GitHub Desktop.
Add song metadata to a cue sheet using XML from Musicbrainz
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
(function(){ | |
var document = require("jsdom").jsdom(require("fs").readFileSync(process.argv[2])), | |
cue = require("fs").readFileSync(process.argv[3]).toString().split("\n"), | |
discTitle = document.getElementsByTagName("release")[0].getElementsByTagName("title")[0].textContent, artists = "", titles = [], | |
medium = document.getElementsByTagName("medium"), | |
tracks, credits, discNo = process.argv.length > 4 ? parseInt(process.argv[4], 10) - 1 : 0, i = 0, j = 0; | |
if(medium.length > 1){ | |
discTitle += " (Disc " + (discNo + 1).toString() + ")"; | |
} | |
credits = document.getElementsByTagName("artist-credit")[0].getElementsByTagName("name-credit"); | |
artists = ""; | |
for(i = 0; i < credits.length; i += 1){ | |
artists += credits[i].getElementsByTagName("artist")[0].getElementsByTagName("name")[0].textContent; | |
if(credits[i].getAttribute("joinphrase") !== undefined){ | |
artists += credits[i].getAttribute("joinphrase"); | |
}else if(j < credits.length - 1){ | |
artists += ", "; | |
} | |
} | |
tracks = medium[discNo].getElementsByTagName("track"); | |
for(i = 0; i < tracks.length; i += 1){ | |
titles.push(tracks[i].getElementsByTagName("title")[0].textContent); | |
} | |
for(i = 0, j = 0; i < cue.length && j < titles.length; i += 1){ | |
if(/^\s*FILE/i.test(cue[i])){ | |
cue.splice(i, 0, "PERFORMER \"" + artists.replace(/"([^"]*)"/g, "«$1»") + "\"", "TITLE \"" + discTitle.replace(/"([^"]*)"/g, "«$1»") + "\""); | |
i += 2; | |
}else if(/^\s*TRACK/i.test(cue[i])){ | |
cue.splice(i + 1, 0, " PERFORMER \"" + artists.replace(/"([^"]*)"/g, "«$1»") + "\"", " TITLE \"" + titles[j].replace(/"([^"]*)"/g, "«$1»") + "\""); | |
j += 1; | |
} | |
} | |
console.log(cue.join("\n")); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment