#!/usr/bin/php " !"#$%&'()" * "&amp;&&amp;" => "&&&" * ""'" => ""'" * "!"#$%&'()" => "!"#$%&'()" */ 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: /*

Streams (29/75 slots used)
USA 80KBit/s (18/50 listeners) Swe 56KBit/s (27 listeners) Swe 32KBit/s (2 listeners)

Songs

Latest News
*/ //////////////////////////////////////////////////////////////////////// // Here is another sample portion of ocmain.php page: /*

Streams (33/75 slots used)
USA 80KBit/s (36/50 listeners) Swe 56KBit/s (32 listeners) Swe 32KBit/s (1 listener)

Songs
Total playtime: 3d 17h 19m 28s  
Total votes: 257,203
Bandwidth used: 0%
Playing: Tanaric - Jingle 26, Not a Remixer
Vote: N/A
 
 
Queue (739/1399 songs left)
[00:08] Rayza - Shinobi 3, Whirlwind (Gale Force Mix) (4:33) 
[03:39] McVaffe - Secret of Mana, Fear of the Flava (3:31) 
[04:25] mp - Radical Dreamers, Far Drama (2:14) 
[06:39] Orkybash - Final Fantasy 10, Far from Home (3:31) 
[10:10] JAXX - Ninja Gaiden 2, Trance (3:27) 
 
Recent songs:
GrayLightning - Donkey Kong Country, Machina Anesthesia (4:53) 
Vampire Hunter Dan - Breath of Fire, Flames of Valor (6:29) 
d0d0 - Sanxion, Overclocked (4:21) 
Ziwtra - Final Fantasy 8, Rain in Chicago (4:04) 
Darangen - Chrono Trigger, Atonement (3:51) 


Latest News
*/ ?>