Page 1 of 1

Shoutcast Server Stream Hits [Today]

Posted: Tue Apr 06, 2010 11:47 am
by Conor Hyland
Hi Folks,

I'm a newbie to this forum and also a newbie to PHP and web programming - so I don't know much!
I have Apache and PHP installed on my PC.
I have a Shoutcast Server that I wrote some simple HTML / CSS / PHP code to send me back data on Listeners, Server Status, Songs Played etc.
The Shoutcast Servers scastxml_var.php file has a value of $streamhits which will increase by one every time a Listener connects to the stream and I can display quite simply as:

Stream Hits [Total]
$streamhits

I want to add another statistic to my reporting page that will display the Stream Hits [Today] as $streamhitstoday that will basically reset its counter to zero at midnight each night and then count up the Stream Hits that day i.e. both counters will increase the same amount each day depending on Listeners, but the Stream Hits [Today] will reset at midnight every night.

Stream Hits [Today]
$streamhitstoday

Any ideas how I can do this?
Will I need MYSQL installed on my Webserver PC to write the value of Stream Hits [Total] at midnight and then subtract this value from the current value of Stream Hits [Total] to give me Stream Hits [Today]?
Or is there a way to do this using PHP code only?

Any help would be much appreciated,
Regards,
Conor

Re: Shoutcast Server Stream Hits [Today]

Posted: Tue Apr 06, 2010 3:38 pm
by AbraCadaver
How and where is the current value of $streamhits saved?

Re: Shoutcast Server Stream Hits [Today]

Posted: Wed Apr 07, 2010 3:26 pm
by Conor Hyland
I am telling my index.php page on my webserver to include the following php files, which are also located in the htdocs folder:

<?php
include('config.php');
include('scastxml.php');
?>

config.php contains host, port and password info etc to connect to the Shoutcast DNAS Server.
scastxml.php is the following file:

<?php
///////////////////////////////////////////////////////////////////////////////////////////////
// Just a little bit about this script.
// Part 1 Written by: T. Stanley
// Part 2 add-ons Written by: ST. JOHN
// Connection stuff from Jay Krivanek's and Oddsock's SHOUTout script.
// First feel free to edit it to suit your needs.
// This script does all that T. Peppers does (however it is probably a bit slower
// but it works w/o the XML stuff just pure PHP) and also retrieves the bitrate
// (if stream is up) and whether there is a source (DSP connected with DNAS).
// It relies heavily on the assumption that 7.html, index.html and XML page will not change.
// Currently works with DNAS 1.8.0 (haven't tested older versions).
// If you upgrade the DNAS and it breaks it is due to the fact that Nullsoft has changed
// the data on 7.html, index.html and XML page and the ereg_replace and explode will need
// to be tweaked.
// Right now it is setup to print out every variable it gets.
// Song data (titles and playedats)is recursive so it can get up to 20 (if set in DNAS) as
// well a unlimited listener info (IP, etc.)
// It is suggested that any variables you don't need you comment out or delete the extraction
// of the data.
// Arrays (song, playedats, etc.) start with 0. (i.e. song[0] = currently playing song)
// Thanks to Ari for the explanation of 7.html on the SHOUTcast listserv (I was lurking)
// Run the srcipt as is (after changing host, port, password) for a list of variables printed
// out (listnener data and song history will not show up unless there is data for them).
// Contacts:
// tom@shoutclub.com
// dstjohn@mediacast1.com
// Notes:
// Very Important >> Uncomment(take away the forward slashes //)
// all the echo statements to get a full list of all the variables,
// if you dont know what im talking about, then stop right here and go learn php
// http://www.php.net
// http://www.thickbook.com << very resourcefull for beginners
///////////////////////////////////////////////////////////////////////////////////////////////
include ("config.php");
$listenlink = 'http://'.$host.':'.$port.'/listen.pls'; //make link to stream
$fp = fsockopen("$host", $port, &$errno, &$errstr, 30); //open connection
if(!$fp) {
$success=2; //set if no connection
}
if($success!=2){ //if connection
fputs($fp,"GET /7.html HTTP/1.0\r\nUser-Agent: XML Getter (Mozilla Compatible)\r\n\r\n"); //get 7.html
while(!feof($fp)) {
$page .= fgets($fp, 1000);
}
fclose($fp); //close connection
$page = ereg_replace(".*<body>", "", $page); //extract data
$page = ereg_replace("</body>.*", ",", $page); //extract data
$numbers = explode(",",$page); //extract data
$currentlisteners=$numbers[0]; //set variable
$connected=$numbers[1]; //set variable

if($connected==1) //if DSP is connected
$wordconnected="yes"; //set variable
else //if no DSP connection
$wordconnected="no"; //set variable
$peaklisteners=$numbers[2]; //set variable
$maxlisteners=$numbers[3]; //set variable
$reportedlisteners=$numbers[4]; //set variable

##Stuff printing out to screen may want to shut off.##
// echo('$listenlink = '.$listenlink.'<BR>');
// echo('$reportedlisteners = '.$reportedlisteners.'<BR>');
// echo('$maxlisteners = '.$maxlisteners.'<BR>');
// echo('$peaklisteners = '.$peaklisteners.'<BR>');
// echo('$connected = '.$connected.' <BR>');
// echo('$wordconnection = '.$wordconnected.'<BR>');
// echo('$currentlisteners = '.$currentlisteners.'<BR>');
##Okay stop commenting stuff out now.##
}

if($connected==1){ //only do if DSP is connected
$fp = fsockopen("$host", $port, &$errno, &$errstr, 30); //open connection
if(!$fp) { //if no connection
$success1=2; //dummy variable to see if successful connect
}
if($success1!=2){ //only do if connected
fputs($fp,"GET /index.html HTTP/1.0\r\nUser-Agent: XML Getter (Mozilla Compatible)\r\n\r\n"); //get index.html
while(!feof($fp)) {
$page .= fgets($fp, 1000);
}
$pageed = ereg_replace(".*Stream is up at ", "", $page); //extract data
$bitrate = ereg_replace(" kbps.*", "", $pageed); //extract data
fclose($fp); //close connection

##Print out bitrate.##
//echo('$bitrate = '.$bitrate.' <BR>');
##You know the drill stop here.##

}
}

$fp = fsockopen("$host", $port, &$errno, &$errstr, 30); //open connection yet again
if(!$fp) { //if connection
$success2=2;
}
if($success2!=2){ //if connected
fputs($fp,"GET /admin.cgi?pass=$password&mode=viewxml HTTP/1.0\r\nUser-Agent: XML Getter (Mozilla Compatible)\r\n\r\n"); //get XML page
while(!feof($fp)) {
$page .= fgets($fp, 1000);
}

$loop = array("AVERAGETIME", "SERVERGENRE", "SERVERURL", "SERVERTITLE", "SONGTITLE", "SONGURL", "IRC", "ICQ", "AIM", "WEBHITS", "STREAMHITS", "INDEX", "LISTEN", "PALM7",
"LOGIN", "LOGINFAIL", "PLAYED", "COOKIE", "ADMIN", "UPDINFO", "KICKSRC", "KICKDST", "UNBANDST", "BANDST", "VIEWBAN", "UNRIPDST", "VIEWRIP", "VIEWXML",
"VIEWLOG", "INVALID"); //define all the variables to get (delte any ones you don't want)
$y=0; //dummy variable for while loop
while($loop[$y]!=''){ //while there are things in loop
$pageed = ereg_replace(".*<$loop[$y]>", "", $page); // extract data
$phpname = strtolower($loop[$y]); //make names in loop lowercase for variable names
$$phpname = ereg_replace("</$loop[$y]>.*", "", $pageed); //finish extracting data
if($loop[$y]==SERVERGENRE || $loop[$y]==SERVERTITLE || $loop[$y]==SONGTITLE) //if for code clean-up (if you have problems with variables with URL encoding (i.e. %20 for space put them in this loop)
$$phpname = urldecode($$phpname); // replace URL code with regular text (i.e. %20 = space)

##More stuff that prints##
//echo ('$'.$phpname.' = '.$$phpname.' <BR>');
##Stop here. Your server will be upset if you comment out the next line of code ($y++;).##

$y++; //update dummy variable for while loop
}
$pageed = ereg_replace(".*<SONGHISTORY>", "", $page); //extract data
$pageed = ereg_replace("<SONGHISTORY>.*", "", $pageed); //extract data
$songatime = explode("<SONG>", $pageed); //break data down for each song
$r=1; //dummy variable
while($songatime[$r]!=""){ //while loop for each song
$t=$r-1; //correction for first value in array from explode is worthless
$playedat[$t] = ereg_replace(".*<PLAYEDAT>", "", $songatime[$r]); // extract data
$playedat[$t] = ereg_replace("</PLAYEDAT>.*", "", $playedat[$t]); //extract data
$song[$t] = ereg_replace(".*<TITLE>", "", $songatime[$r]); //extract data
$song[$t] = ereg_replace("</TITLE>.*", "", $song[$t]); //extract data
$song[$t] = urldecode($song[$t]); //cleans-up the URL code thing again

##Yet even more crap that gets printed out.##
//echo ('$song['.$t.'] = '.$song[$t].' <BR>$playedat['.$t.'] = '.$playedat[$t].' <BR>');
##Same as last time. Unhappy servers tend to hurt people.##

$r++; //update loop variable
}
$pageed = ereg_replace(".*<LISTENERS>", "", $page); //extract data
$pageed = ereg_replace("</LISTENERS>.*", "", $pageed); //extract data
$listeninfo = explode("<LISTENER>", $pageed); //break apart data
$r=1; //dummy loop variable
while($listeninfo[$r]!=""){ //while loop for extraction
$t=$r-1; //correction for first value in array from explode is worthless
$hostname[$t] = ereg_replace(".*<HOSTNAME>", "", $listeninfo[$r]); //extract data
$hostname[$t] = ereg_replace("</HOSTNAME>.*", "", $hostname[$t]); //extract data
$useragent[$t] = ereg_replace(".*<USERAGENT>", "", $listeninfo[$r]); //extract data
$useragent[$t] = ereg_replace("</USERAGENT>.*", "", $useragent[$t]); //extract data
$underruns[$t] = ereg_replace(".*<UNDERRUNS>", "", $listeninfo[$r]); //extract data
$underruns[$t] = ereg_replace("</UNDERRUNS>.*", "", $underruns[$t]); //extract data
$connecttime[$t] = ereg_replace(".*<CONNECTTIME>", "", $listeninfo[$r]); //extract data
$connecttime[$t] = ereg_replace("</CONNECTTIME>.*", "", $connecttime[$t]); //extract data

##Yet even more crap that gets printed out.##
// echo ('$hostname['.$t.'] = '.$hostname[$t].' <BR>$useragent['.$t.'] = '.$useragent[$t].' <BR>$underruns['.$t.'] = '.$underruns[$t].' <BR>$connecttime['.$t.'] = '.$connecttime[$t].'<BR>');
##STOP. Just think that was the last set of echos you have to comment out.##

$r++; //update loop variable
}
fclose($fp); //close connection
}
$password=changeme //Leave this. It writes over real value so if someone were to use
//include to get this page they cannot use echo $password to get your
//password. There is probably a better way to do this but I can't code.
?>

i also have a fourth file in my htdocs folder called scastxml_var.php that when I view through my browser it will echo the outputs, along with other variables as for example:

$streamhits = 29316
$peaklisteners = 20

Code for scastxml.php and scastxml_var.php look very similar if not identical.

index.php cut down and simplified a lot might look something like this:

<html>
<head>
<?php
include('config.php');
include('scastxml.php');
echo"
<style type=text/css>
body{background-color:$backgroundcolor}
table{table-layout:fixed;border-collapse:collapse}
col{width:50%}
th{text-align:center;font:$headingtextfont;color:$headingtextcolor}
td{text-align:center;font:$celltextfont;color:$celltextcolor}
</style>
<meta http-equiv=refresh content=$refreshrate1>
</head>
<body>
<table border=0>
<col>
<col>
<tr>
<th colspan=1>Stream Hits [Today]</th>
<th colspan=1>Peak Listeners [Today]</th>
</tr>
<tr>
<td colspan=1>$streamhitstoday</td>
<td colspan=1>$peaklistenerstoday</td>
</tr>
<tr>
<th colspan=2>&nbsp;</th>
</tr>
<tr>
<th colspan=1>Stream Hits [Total]</th>
<th colspan=1>Peak Listeners [Total]</th>
</tr>
<tr>
<td colspan=1>$streamhits</td>
<td colspan=1>$peaklisteners</td>
</tr>
</table>
"?>
</body>
</html>

I want to do something similar with $peaklistenerstoday as $streamhitstoday, but trouble is neither of them print out on the scastxml_var.php file when viewed through a browser so this is where I get stuck with my basic basic knowledge of php!

If you can help me then that would be great!
I hope I have explained everything as best I can!

Thanks in advance,
Conor