blogger what’s playing hack

That little playlist automation script I talked about earlier was neat, but sort of limited.

I was thinking that it would’ve been neat to also update that script when I listed to some music at work, but since FTP is out of the question in that case, I had to come up with something different.
A HTTP post would’ve worked, but since I didn’t feel like writing some ASP/PHP script somewhere to store the data into a database, I thought of using a second blog to store the playlist instead. I could then change the blogs template in such a way that it would render javascript code instead of html code, so I could include it in this blogs template, and have it print out my most recently played tunes.
Sweet!

So this is what I did.
To get the data in the blog I’ve set it up to use an email address to receive posts on, and automatically publish those. I’m using Blat to email a textfile containing the last mp3’s info, which is created by the AMIP “now playing” WinAmp plugin, to the blogs email address.

I changed the template so it generates javascript code to populate a class that stores the data, using the “number of posts” setting to control the number of items written into the script (e.g. 10), which is also the number of items that will be displayed in the result.
This worked fine, besides for the fact that blogger insert a number of custom HTML headers (like the blogger toolbar) into the file, which screwed up the javascript include. Fortunately, when you have blogger publish the page using FTP to a different server, this doesn’t happen, so my javascript include is now clean, and ready to be used.

W00t!!

It ain’t perfect though, but nothing in life is, isn’t it. I noticed that sometimes blogger seems to have some trouble posting the results automatically from the email interface, especially in the evening, why the US gets active I guess, but few emails seem to really go lost. I get an “XML-RPC Error or Publishing Problem” error message now and then, and I guess zapping through some mp3’s, and thus sending some emails in short sequence of each other, might be simply choking the blog a bit… oh well. I did extend the update delay in AMIP to 40 seconds to prevent zapping around from being noticed, giving me some time to skip that Britney Spears track before it ends up in the log…
At other times there seems to be a delay processing the posts, which means the now playing list can be behind on what’s really playing at the moment, but that’s no biggy either. I’ll just remove the time indication if it starts to annoy me too much.

Here’s some of the code in case you’re interested in the more technical stuff (as if it wasn’t technical enough already, whoehahahaha!!):

The playlist blogs template:

<Blogger>
playlist.add("", "<$BlogItemBody$>");
</Blogger>

Which results into something like this:

playlist.add("12/11/2004 07:33:31 PM", "bong ra - kinkfmmix");
playlist.add("12/11/2004 02:34:48 PM", "kid606 how we shock it");

This piece of script takes care of handling and writing the playlist. Feel free to rip it.


function Playlist(){
this.items = new Array();

this.add = Playlist_add;
this.last = Playlist_last;
this.getItem = Playlist_getItem;
}

function Playlist_add(date, item){
var re;
re = /<\/(.*)>/g; // remove html end tags
item = item.replace(re, “”);
re = /<(.*)>/g; // remove html start tags
item = item.replace(re, “”);
this.items[this.items.length]=Array(new Date(date), item);
}

function Playlist_last(){
return this.getItem(0);
//return this.items[0][1] + ” at ” + this.items[0][0];
}

function Playlist_getItem(index) {
var result = “”;
var now = new Date();
var itemdate;

if (this.items.length > index) {
/*
itemdate = this.items[index][0];
if (itemdate.getDay() == now.getDay()) {
result = itemdate.getHours() + “:” + itemdate.getMinutes();
} else {
result = itemdate.getDate() + “/” + (itemdate.getMonth()+1) + ” ” + itemdate.getHours() + “:” + itemdate.getMinutes();
}
result = “” + result +”
result += ” : ” + this.items[index][1];
*/
result += this.items[index][1];
}
return result;
}

function getPlaylistHTML() {
var i;
var s = “”;

for (ix=0; ix < playlist.items.length; ix++) {
s += playlist.getItem(ix) + ”
“;
}
return s;
}

function showPlaylist() {
document.write(getPlaylistHTML());
}

var playlist = new Playlist();

One of the cool things about this thing is that the blog has become a crude database, which can be exported to XML easily, and used for whatever purpose I see fit.
Now I just have to keep RIAA away from it… ;)

Leave a Reply