I had a problem: I wanted my radio station to automagically play my newer music and update the stuff that it plays every day, yet keep enough older stuff that there is still some variety, especially if no new music gets discovered for a while. I’d need to find some way to keep the most recent 200 or so tracks playing on my station.
Let’s start by looking at what we have and what we have to do:
Files on a Linux fileserver playing on a Windows server (via samba) in Winamp using EdCast to stream to sc_serv (Shoutcast) on a different Linux server in the DMZ. Currently, I have the X: drive on the Windows server mapped to the Linux Fileserver. Winamp re-scans the fileserver every 3 days for new music and adds it to the library, but I still have to manually go in and add new tracks to the playlist.
Some initial ideas and problems:
-Use XML-RPC / SOAP from another music cataloging program that I currently use
+I just really feel like there was an easier way to do what I wanted without going through all the trouble.
-Telling Winamp to [re]load the playlist: the Winamp API Uses C++…yuck…I don’t know or want to learn C++ just for this project. Plus, DRY applies, someone HAS to have thought of this before.
+Solution: httpQ, a simple http listener plugin for Winamp that uses a simple and easy to implement control scheme: simply call the right URL and Winamp does what you want. Awesome. ( http://httpq.sourceforge.net )
-Finding files that have been modified recently, and if none have been modified in the time specified, keep looking until you find enough files
So let’s look at what we’re going to do with this script:
-Set up some variables for internal use and for how many tracks we want in our playlist
-Loop through something to find our tracks and write them out to a file
-Check that file to see if if has as many lines as we want (I know a better way to do this would be to just store it in memory and index it until you have enough, but on a modest p4 this executes in about 1 second in a directory with ~300GB files…YMMV but it wasn’t slow enough to justify re-writing it for me)
-if it doesn’t, re-generate it and let some files that are a bit older get included as well
-Tell Winamp to drop its current playlist
-Tell Winamp to load our new playlist
-Tell Winamp to play, just to make sure it’s playing…you don’t want to have dead air, even on the internet.
And finally, the script:
#!/bin/bash
DAY=3
#set the default to search for files 3 or less days oldCNT=0
#initalize count…don’t really need to but it makes me feel betterDESIRED=200
#how many files do you want (at least)?until [ $CNT -ge $DESIRED ]; do
#until the playlist has as many or more files than we specified, keep doing this over and over…probably should set some limit so it doesn’t go nuts if it has an errorfind /foo/bar -mtime -”$DAY” -iname “*.mp3″ -print | sed ‘s^/foo/bar/^X:\\files\\^’ | tr ‘/’ ‘\’ > /foo/bar/current.m3u
#find files named *.mp3 in /foo/bar with a modified time of minus X days and echo the results…to sed which replaces /foo/bar with X:\Windows\Path…fix for converting slashes to
#backslashes…sed also works but tr runs fasterCNT=$(wc -l /foo/bar/current.m3u | cut -d\ -f1)
#count the lines of the playlist. Note that I’m passing a space as the delimeter to cut, by escaping with a backslash then double space until the next flag,
#which grabs column 1 (col2 is the filename, yick)let DAY+=1
#shorthand for day = day+1…won’t be used unless we loop again.
donewget -O – –quiet “http://windows:4800/delete?p=passwd”
wget -O – –quiet “http://windows:4800/playfile?p=passwd&file=X:\current.m3u”
wget -O – –quiet “http://windows:4800/play?p=passwd”
#the RPC calls to the Windows server. Note that we’re telling wget to output to -…i.e. drop the output.
#Also, be sure to double quote that URL.
exit 0;
#exit with no error code