A little while ago, I wrote about a small perlscript for playing internet radio stations. Since then, a few more features have crept in, such as resume support (‘radio start’ will play the last station you listened to).
Another idea I had been toying with was adding scrobbling support, so I could submit the tracks to last.fm. Their API isn’t extremely advanced but it did require some information that wasn’t readily available, such as the unix timestamp for when a track started. But thanks to an ugly hack (piping mplayer output to `perl -nle ‘BEGIN { open STDOUT, “>”, $lfmfile or die $!; } print time, “:”, $_;’`, someone please give me a better solution), that was solvable. Another issue I haven’t figured a not-completely-horrible solution to is calculating the length of a track, since last.fm doesn’t want tracks that are shorter than 30 secs (those are usually ads anyway).
But in conclusion radio.pl now supports submitting your tracks to last.fm, if you provide your credentials. Basically, just create a file ~/.radio.cfg with the following content:
lastfm_user = arkanoid
lastfm_passwd = md5:117a520adbd19eff51100215aa7a7fbf
and it should work (provided the radio stream actually contains the proper information in ICY, which at least SomaFM, SKY Radio and Digitally-Imported streams seem to do). You can also add your password in plaintext (without the md5: prefix), but that’s just silly.
The current support is quite ugly so expect breakages. If you want to know what’s going on with the scrobbler, just add –lfmfile /tmp/lfmlog when you start the script and tail that file. Please do that as well, if you submit a bugreport.
Download the radio script
A sample .radiostations file
Please note that compared to the old script which only spawned mplayer, you will now have three processes spawned (mplayer, a perl process for adding timestamps to mplayer output and a fork of the radio script for handling last.fm submission) if you enable last.fm submission (by adding your credentials). This version also leaves the radio.state file behind when you stop it, since it’s used for the resume feature.
Go ahead and break it!
Adding scrobbling support took less than a day, thanks to last.fm’s really simple and quite well-documented submission protocol and their quick response when I asked for a client ID. The only snag I hit, was with the length of a track, “l[0]” in the postdata. According to the documentation, this is optional for radio scrobbling, but in fact you need to pass it along, although you can just set it to zero. I guess my interpretation of ‘optional’ differs from their.