phpicecastdata-scrubbing

php scrubbing a website for icecast listeners


can anyone help extract the current listener count from the link below using php I have attached phph code below as well but it need to be modified

http://209.105.250.69:8382/

and the source is below

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Icecast Streaming Media Server</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0">
<h2>Icecast2 Status</h2>
<br><div class="roundcont">
<div class="roundtop"><img src="/corner_topleft.jpg" class="corner" style="display: none"></div>
<table border="0" width="100%" id="table1" cellspacing="0" cellpadding="4"><tr><td bgcolor="#656565">
<a class="nav" href="admin/">Administration</a><a class="nav" href="status.xsl">Server Status</a><a class="nav" href="server_version.xsl">Version</a>
</td></tr></table>
<div class="roundbottom"><img src="/corner_bottomleft.jpg" class="corner" style="display: none"></div>
</div>
<br><br><div class="roundcont">
<div class="roundtop"><img src="/corner_topleft.jpg" class="corner" style="display: none"></div>
<div class="newscontent">
<div class="streamheader"><table cellspacing="0" cellpadding="0">
<colgroup align="left"></colgroup>
<colgroup align="right" width="300"></colgroup>
<tr>
<td><h3>Mount Point /listen.mp3</h3></td>
<td align="right">
<a href="/listen.mp3.m3u">M3U</a><a href="/listen.mp3.xspf">XSPF</a>
</td>
</tr>
</table></div>
<table border="0" cellpadding="4">
<tr>
<td>Stream Title:</td>
<td class="streamdata">Quran Kareem Radio</td>
</tr>
<tr>
<td>Stream Description:</td>
<td class="streamdata">Quran Kareem Radio</td>
</tr>
<tr>
<td>Content Type:</td>
<td class="streamdata">audio/mpeg</td>
</tr>
<tr>
<td>Mount started:</td>
<td class="streamdata">Thu, 11 Apr 2013 19:19:59 -0400</td>
</tr>
<tr>
<td>Bitrate:</td>
<td class="streamdata">60</td>
</tr>
<tr>
<td>Current Listeners:</td>
<td class="streamdata">5</td>
</tr>
<tr>
<td>Peak Listeners:</td>
<td class="streamdata">25</td>
</tr>
<tr>
<td>Stream Genre:</td>
<td class="streamdata">Islam</td>
</tr>
<tr>
<td>Stream URL:</td>
<td class="streamdata"><a target="_blank" href="http://qkradio.com.au">http://qkradio.com.au</a></td>
</tr>
<tr>
<td>Current Song:</td>
<td class="streamdata"></td>
</tr>
</table>
</div>
<div class="roundbottom"><img src="/corner_bottomleft.jpg" class="corner" style="display: none"></div>
</div>
<br><br>&nbsp;


<div class="poster">Support icecast development at <a class="nav" target="_blank" href="http://www.icecast.org">www.icecast.org</a>
</div>
</body>
</html>

I have used the following so far but it needs to be modified

<?php

$fp = fsockopen("listen.qkradio.com.au", 8382, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {

for($i=0; $i<30; $i++) {
if(feof($fp)) break;
$fp_data=fread($fp,31337);
usleep(500000);
}
$fp_data=ereg_replace("^.*<body>","",$fp_data);
$fp_data=ereg_replace("</body>.*","",$fp_data);
list($current,$status,$peak,$max,$unique,$bitrate,$song) = explode(",", $fp_data);
if ($status == "0") {
echo "<center>Off Air</center>";
} else {
echo "<TABLE>";

//start edit below - comment with "//" unnecessary lines
echo "<TR><TD>Current Listeners: </TD><TD>".$current."</TD></TR>";
//echo "<TR><TD>Server Status: </TD><TD>".$status."</TD></TR>";

//stop editing from here

echo "</TABLE>";

//start edit below - comment with "//" next line to stop scrolling
echo "<marquee>".$song."</marquee>";
//stop editing here
}}
?>

Solution

  • First, I would use

    $html = file_get_contents('http://209.105.250.69:8382/');
    

    to retrieve the remote file. Then I would use:

    $doc = new DOMDocument();
    $doc->loadHtml($html);
    

    to build a DOM document of it. The you can use xpath to retrieve the information from it:

    $selector = new DOMXPath($doc);
    $result = $selector->query('....');
    

    Then, for example, you can use the following code to retrieve stream stats:

    $stats = array(
      'Stream Title' => '', 
      'Stream Description' => '', 
      'Bitrate' => ''
      // ...
    );
    
    foreach($stats as $key => $val) {
        $result = $selector->query("//td[text()='$key:']");
    
        foreach($result as $node) {
            $stats[$key] = $node->nextSibling->nextSibling->nodeValue;
        }   
    }
    
    var_dump($stats);
    
    // output the stream description
    echo $stats['Stream Description'];