Building a Frontend for Shoutcast Using Codeigniter
I wanted a front end for lunchbox radio and wanted to be able to quickly show what is currently playing, the recently played tracks, and how many people were listening. I didn’t want to mess around too much, I just wanted to get something online. So, within about an hour, I installed Codeigniter and created a model and library for scraping the shoutcast stats from the shoutcast admin interface. I found a class for shoutcast from PHPClasses that I was able to modify for my needs. It didn’t require anything more than adjusting the class name. Codeigniter has some restrictions in regards to file names, but I haven’t run into any real issues with that yet.
Codeigniter makes it very easy to quickly create working, functional web sites. It would have taken me several days to build this while providing the same level of customization and expandability that this framework provides.
My model provides an easy way to access the data that is scraped and managed from the Shoutcast library. In my controller I simply call $this->model_shoutcast->getStats() and I am done.
I have a couple views, one for the home page, and another for the shoutcast stats. I have one controller, one custom model, and one library. It will be trivial to extend the site in the future.
Here is the model:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Model_shoutcast extends Model
{
function Model_shoutcast()
{
parent::Model();
$this->load->library('shoutcast', $shoutcast_array_config); // My model actually has the config array hardcoded in at the moment...
}
function getStats()
{
if ($this->shoutcast->openstats())
{
if ($this->shoutcast->GetStreamStatus())
{
$tpl['currently_playing'] = $this->shoutcast->GetCurrentSongTitle();
$tpl['history'] = $this->shoutcast->GetSongHistory();
$tpl['total_listeners'] = $this->shoutcast->GetCurrentListenersCount();
$tpl['max_listeners'] = $this->shoutcast->GetMaxListenersCount();
// load our template and populate with the shoutcast stats
return $this->load->view('shoutcast/stats', $tpl, true);
}
else
{
// looks like there isn't a stream available, send a response back to our controller...
return 'Server is up, but no stream available..';
}
}
else
{
return $this->shoutcast->geterror();;
}
}
}
?>
From my controller:
<?php
$this->load->model('Model_shoutcast', 'mc');
$tpl['shoutcast_stats'] = $this->mc->getStats();
$this->load->view($template, $tpl);
?>
The Library I used can be found at PHP Classes.
Eventually I will replace the library with one that takes advantage of some of the native Codeigniter features. For now though, it is working great as is.
This is a very basic demonstration of Codeigniter and using models, it doesn’t really provide us with much more than a quick way to get the Shoutcast stats. It might be nice to be able to pull only the current song playing, or just show the history, instead of lumping it all into one template.









Comments