Skip to content

Instantly share code, notes, and snippets.

@robotanarchy
Last active August 29, 2015 14:06
Show Gist options
  • Save robotanarchy/32f8a19c3d1c277cd033 to your computer and use it in GitHub Desktop.
Save robotanarchy/32f8a19c3d1c277cd033 to your computer and use it in GitHub Desktop.
xspfcat
#!/usr/bin/node
// License: Public Domain
if(process.argv.length != 3)
{
console.log("Lists XSPF files as fb2k-style text playlists.");
console.log("Syntax: xspfcat.js playlist.xspf");
process.exit(1);
}
function two_digits(num){return num>9?num:"0"+num;}
function cut(big, start, end)
{
if(start)
{
var pos = big.indexOf(start);
if(pos == -1) return "";
big = big.substr(pos + start.length);
}
if(!end) return big;
big = big.substr(0,big.indexOf(end));
return big;
}
var fs = require("fs");
var file = process.argv[2];
var xml = fs.readFileSync(file)+"";
var tags = ["title", "creator", "album", "trackNum", "location"];
var symbols = // source: https://github.com/buglabs/node-xml2json
{
'<': '&lt;',
'>': '&gt;',
'(': '&#40;',
')': '&#41;',
'#': '&#35;',
'&': '&amp;',
'"': '&quot;',
"'": '&apos;'
};
while(xml.indexOf("<track>") > -1)
{
// parse the track into obj
var obj = {};
xml = cut(xml, "<track>");
var track = cut(xml, null, "</track>");
for(var i=0;i<tags.length;i++)
{
var tag = tags[i];
var value = cut(track, "<"+tag+">", "</"+tag+">");
for(var symbol in symbols)
while(value.indexOf(symbols[symbol]) > -1)
value = value.replace(symbols[symbol], symbol);
obj[tag] = value;
}
// output fb2k style
var line = obj.creator;
if(obj.title)
{
if(obj.album)
{
line+=" ["+obj.album;
if(obj.trackNum) line+=" #"+two_digits(obj.trackNum);
line+="] - ";
}
else line+=" - ";
line+=obj.title;
}
else line = obj.location;
console.log(line);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment