#!/usr/bin/php
" !"#$%&'()"
* "&&&" => "&&&"
* ""'" => ""'"
* "!"#$%&'()" => "!"#$%&'()"
*/
function html_decode_entities($str) {
$dst="";
$len=strlen($str);
for( $i=0 ; $i<$len ; $i++ )
{
if( $str[$i]=="&" )
{
$entity_len=strpos($str,";",$i);
if( $entity_len )
{
$entity_len+=-$i+1;
$entity=substr($str,$i,$entity_len);
if( $entity[1]=="#" )
{
$tmp=substr($entity,2,-1);
if( strlen($tmp) )
{
if( $tmp[0]=="x" || $tmp[1]=="X" )
$value=intval(substr($tmp,1),16);
else
$value=intval($tmp,10);
$dst.=chr($value);
}
else
$dst.=$entity;
}
else
{
$dst.=html_entity_decode($entity,ENT_QUOTES);
}
$i+=$entity_len-1;
}
else
$dst.=$str[$i];
}
else
$dst.=$str[$i];
}
return $dst;
}
/**
* The variable which will hold the output
*/
$info="";
$p=file($page);
if(!$p)
exit;
////////////////////////////////////////////////////////////////////////
// Delete everything until the line listing the Streams
// If it was PHP5, I could use this:
// foreach( $p as &$line ) {
foreach( $p as $i => $line ) {
// if( strpos($line,"Streams") !== FALSE && strpos($line,"slots used)") !== FALSE )
if( preg_match('/Streams.*slots? used\)/',$line) )
break;
unset($p[$i]);
}
$p=array_values($p);
////////////////////////////////////////////////////////////////////////
// Extract basic info on available streams
$line=trim(strip_tags($p[0])) . NEWLINE;
unset($p[0]);
$info.=$line;
// Extract info about each stream
foreach( $p as $i => $line ) {
if( strpos($line, ']*>([^<]+)<\/a> *\((.+)\)/',$p[$i+1],$parts);
$url=$parts[1];
$bitrate=$parts[2];
$listeners=$parts[3];
$url=fix_relative_url($url);
$info.=$name.": ".$bitrate." (".$listeners.") <".$url.">".NEWLINE;
}
// "Peercast" is commented in current ocmain.php output (see sample page
// at end of this script). I won't write code to parse it now or soon.
unset($p[$i]);
if( strpos($line, "") !== FALSE )
break;
}
$p=array_values($p);
$info.=NEWLINE;
////////////////////////////////////////////////////////////////////////
// Extract more info: total playtime and current song
$image="";
foreach( $p as $i => $line ) {
if( preg_match('/(Total playtime:[ 0-9dhms]+)/',$line,$parts) ) {
$info.=trim($parts[1]).NEWLINE;
}
elseif( preg_match('/(Bandwidth used:[ 0-9%]+)/',$line,$parts) ) {
$info.=trim($parts[1]).NEWLINE;
}
elseif( preg_match('/
]+alt=\'Game Image\'[^>]*>/',$line,$parts) ) {
$image=fix_relative_url($parts[1]);
}
elseif( preg_match('/Playing: *]*>([^<]+)<\/a>/',$line,$parts) ) {
$url=$parts[1];
$name=html_decode_entities($parts[2]);
$url=fix_relative_url($url);
$info.=NEWLINE."Now playing: ".$name.NEWLINE."<".$url.">".NEWLINE;
if( !empty($image) )
$info.="Image: <".$image.">".NEWLINE;
}
elseif( preg_match('/Queue.*songs? left/',$line) )
break;
unset($p[$i]);
}
unset($image);
$p=array_values($p);
$info.=NEWLINE;
////////////////////////////////////////////////////////////////////////
// Extract current queue and recent played songs
//
// Note: I suppose the entire queue and the list of recent played songs
// are on same line. When this program reaches this point, everything
// else will be ignored.
$line=$p[0];
unset($p);
// Splitting the big line at each table cell/row element
$p=preg_split('/<\/?t[rdh][^>]*>/',$line);
// Cleaning the result array
foreach( $p as $i => $line ) {
$line=trim($line);
if( $line==="" || $line===" " )
unset($p[$i]);
else
$p[$i]=$line;
}
$p=array_values($p);
// Aahh!! Much better now! :)
//var_dump($p);
foreach( $p as $i => $line ) {
// Warning! This WILL fail when the playlist is empty.
// (it will simply not display the queue)
if( preg_match('/Queue.*]*> *([0-9]+\/[0-9]+) songs? left/',$line,$parts) ) {
$url=$parts[1];
$songs=$parts[2];
$url=fix_relative_url($url);
$info.="Queue: ".$songs." songs <".$url.">".NEWLINE;
}
elseif( strpos($line, "Recent song") !==FALSE ) {
$info.=NEWLINE."Recent songs:".NEWLINE;
}
elseif( preg_match('/]*>([^<]+)<\/a>/',$line,$parts) ) {
$url=$parts[1];
$name=html_decode_entities($parts[2]);
$time="";
if( preg_match('/^ *\[([0-9:]+)\] *".NEWLINE;
}
}
////////////////////////////////////////////////////////////////////////
// Prints the output
print $info;
//var_dump($p);
////////////////////////////////////////////////////////////////////////
// Here is a sample portion of ocmain.php page:
/*
| Total playtime: 3d 17h 19m 28s |
|
| Total votes: 257,172 | |
| Bandwidth used: 0% | |
| Playing: Steve Pordon - Final Fantasy 9, Qu's Marsh (Swamp Rat) (4:14) | |
| Vote: Please login to vote | |
| Queue (784/1399 songs left) | |
| [02:25] Slow Poison - Space Harrier, BIT Live Arcade (3:08) | |
| [05:33] Chikusho Sound Team - Castlevania, Wickedest Child (2:23) | |
| [07:56] Zeratul - Radical Dreamers, Reve d'ordinateur (3:05) | |
| [11:01] BogusRed - Zelda 3, Into the Golden Sunset (3:50) | |
| [14:51] Orkybash - Final Fantasy 8, Breezy Beats (3:12) | |
| Recent songs: | |
| Bladiator, TO - Chrono Trigger, Tears of Contention (4:28) | |
| Marc Star - Chrono Cross, CrossFire (2:24) | |
| Disco Dan - Metal Gear, First Impressions (5:41) | |
| zircon - Zelda: Link's Awakening, Clash at the Mountains (3:07) | |
| Disco Dan - Mega Man 3, Blue Lightning (8:41) | |
Songs
Latest News | |