In my last blog I listed the web pages on my site that play music:
---------- COMMUNITY-MUSIC.HTML: 207
Then I admit that music.m3u isn't a web page, but it is used to play music. m3u
files are just a list of song URLs. A Windows Media Player can read in an m3u
file, and play every song in the file. ...but it's not loaded in by a web page,
so it won't update automatically when it's loaded; it never gets loaded by html. So we
need to write a program that reads in music.txt (my JSON file that contains a
list of my songs) and creates music.m3u
What type of program should I use? More specifically, what language should I use? A bunch of languages can parse JSON; e.g., Perl, C/C++, Java, etc. However, I've already written the JavaScript to do the job, so why not run the JavaScript from the command line too parse out music.txt too create (recreate) music.m3u? It can be done. Node.js is a package intended to allow JavaScript to be used as a server side language, but this just means that Node.js lets you run JavaScript from the command line.
I went to the Node.js website, http://nodejs.org and clicked on the INSTALL button. An msi file was downloaded to my PC, ran it. and Node.js was installed on my PC. Next I had to copy and paste (and modify, a little bit) my JavaScript into a new text file:
var fs = require('fs');
var file = 'music.txt';
var os = require('os');
fs.readFile(file, 'utf8', function (err, data)
{
if (err)
{
console.log('Error: ' + err);
return;
}
data = JSON.parse(data);
for (obj in data)
{
console.log(data[obj].song);
fs.appendFileSync('music.m3u',data[obj].song + os.EOL);
}
});
I named the file ReadJSON.js (kinda unimaginative; I'll rename it), so
you execute it by entering C:\inetpub\wwwroot>
node ReadJSON.js <CR>
I rename the music.m3u file before running ReadJSON.js
This isn't a stand alone blog. If you read this without reading my previous blog, Using JSON When An Include File Won't Do The Job, it won't make a lot of sense; go back and read that previous blog, first.