Shoutcast
Shoutcast and Bash - A Simple Howto
I needed a way parse the shoutcast info page and display via a dynamic image whether the station is online, and if it is whats playing and how many are tuned in.
Firstly the bash script:
#!/bin/bash
rm -f /tmp/test
rm -f /tmp/up
/usr/bin/lynx -dump -connect_timeout=5 http://localhost:8000 > /tmp/test 2>&1
/bin/cat /tmp/test | grep "up and public" > /tmp/up
if [ -s /tmp/up ]
then
echo “< ? \$up='1';" > /home/pross/public_html/data.php
SONG=`/bin/cat /tmp/test | grep “Current Song” | sed ’s/^[ \t]*//’`
echo “\$song=\”$SONG\”;” >> /home/pross/public_html/data.php
LIST=`/bin/cat /tmp/test | grep “Stream Status” | awk ‘ BEGIN { FS = “(” } { print $2 }’ | head -c 2`
echo “\$list=\”$LIST\”; ?>” >> /home/pross/public_html/data.php
else
echo “< ? \$up='0'; ?>” > /home/pross/public_html/data.php
fi
The above script is run with a cronjob and puts some variables into a file called ‘data.php’ which we can use with the php image script.
Now the php image code:
include ('data.php');
if ($up == "1" )
{
$text='VC-Radio is ONLINE '.$song. ' ';
} else { $text='VC-Radio is OFFLINE'; }
ob_start();
if(!isset($s)) $s=11;
$size = imagettfbbox($s,0,"times.ttf",$text);
$dx = abs($size[2]-$size[0]);
$dy = abs($size[5]-$size[3]);
$xpad=9;
$ypad=9;
$im = imagecreate($dx+$xpad,$dy+$ypad);
$blue = ImageColorAllocate($im, 0×2c,0×6D,0xAF);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
ImageRectangle($im,0,0,$dx+$xpad-1,$dy+$ypad-1,$black);
ImageRectangle($im,0,0,$dx+$xpad,$dy+$ypad,$white);
ImageTTFText($im, $s, 0, (int)($xpad/2)+1, $dy+(int)($ypad/2), $black, “times.ttf”, $text);
ImageTTFText($im, $s, 0, (int)($xpad/2), $dy+(int)($ypad/2)-1, $white, “times.ttf”, $text);
ImageGif($im);
header(”Expires: Mon, 26 Jul 1997 05:00:00 GMT”);
header(”Last-Modified: ” . gmdate(”D, d M Y H:i:s”) . ” GMT”);
header(”Cache-Control: no-store, no-cache, must-revalidate”);
header(”Cache-Control: post-check=0, pre-check=0″, false);
header(”Pragma: no-cache”);
header(”Content-type: image/jpeg”);
header(”Content-Length: ” . ob_get_length());
ob_end_flush();
ImageDestroy($im);
?>
The above code should produce a nice image like this:
I hope someone finds this useful.