Location: PHPKode > scripts > Winamp TV Guide > winamptvguide-0.1/winamptvguide.php
<?session_start();?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
  <title>Winamp TV Guide</title>
  <meta name="AUTHOR" content="hide@address.com">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="KEYWORDS" content="winamp tv lister xine mplayer stream streamlister linux firefox">
  
  <style>

body {
    font : 16px 'sans-serif';
    background : #a9caee;
    text-align : center;
  }


a {
    color : #3f4e70;
    cursor : pointer;
  }


a:hover {
    color : #000000;
    cursor : pointer;
  }


.tblheadCell {
    background : #000000;
    font : 14px 'sans-serif';
    color : #ffffff;
    cursor : pointer;
    text-align : left;
  }

.tblcell {
    font : 12px 'sans-serif';
    text-align : left;
  }
	
.tblRow1 {
  }
.tblRow0 {
		background : #E3F0FF;		
  }

.mytable {
    border : #0a0a0a solid 10px;
  }

</style>
</head>
<body  class="mybody">
<H3>Winamp TV Guide</H3>

<?
$tvlistUrl = "http://www.shoutcast.com/sbin/newtvlister.phtml?alltv=1";
$tvtunerUrl = "http://www.shoutcast.com/sbin/tunein-tvstation.pls?id=";
$tvlistCache = "cache.xml";
$tvstations =array();

$attr_map = array(
"NAME"    => "Name",
"BR"    => "Bitrate",
"RT"    => "Rating",
"CT"    => "What's on",
"LOAD"    => "Load",
"GENRE"    => "Genre",
"LC"    => "Users",
);

$map_array = array
(
	"STATION"    => "STATION"
);

// get parameters
if (isset($_GET))
{
	if ($_GET['cmd'] == 'refresh')
	{
		refreshCache();
	}
}
if (isset($_GET))
{
	if (isset($_GET['sort']))
	{
		$_SESSION['sort']=  $_GET['sort'];
	}
	if (isset($_GET['query']))
	{
		$_SESSION['query']=  trim($_GET['query']);
	}	
}
if (!isset($_SESSION['sort']))
{
	$_SESSION['sort'] = "NAME";	
}
if (!isset($_SESSION['query']))
{
	$_SESSION['query'] = "";
}
// get parameters


//

function in_array_nocase($search, &$array)
{
	foreach ($array as $item)
	{
		if ( !(strpos(strtolower($item), strtolower($search)) === FALSE) )
		{
			return TRUE;
		}		
	}
	return FALSE;
}

function refreshCache()
{
	global $tvtunerUrl,$tvlistUrl;
	$cachefile = 'cache.xml';	
	$content = file_get_contents($tvlistUrl) ;
	if (!$handle = fopen($cachefile, 'w+'))
	{
		die( "Cannot open cache.");	
	}	
	if (fwrite($handle, $content) === FALSE) {
		die("Cannot write to cache.");			
	}		
}


function startElement($parser, $name, $attrs)
{
	global $map_array,$tvstations;
	if (isset($map_array[$name]))
	{
		if (trim($_SESSION['query']) != "")
		{
			if (!in_array_nocase($_SESSION['query'],$attrs))		
				return;
		}
		$tvstations[] =  $attrs;
	}
}

function endElement($parser, $name)
{  
	global $map_array;
	if (isset($map_array[$name]))
	{
		//echo "</$map_array[$name]>";
	}
}

function characterData($parser, $data)
{
   //
}

$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");

if (!file_exists($tvlistCache))
{
	refreshCache();
}

if (!($fp = @fopen($tvlistCache, "r")))
{
	die("Error: Could not open cache.");
}

while ($data = fread($fp, 4096))
{
	if (!xml_parse($xml_parser, $data, feof($fp)))
	{
		die(sprintf("XML Error: %s at line %d",
		xml_error_string(xml_get_error_code($xml_parser)),
		xml_get_current_line_number($xml_parser)));
	}
}
xml_parser_free($xml_parser);



echo "Last updated: " . @date ("F d Y H:i:s.", filemtime($tvlistCache)) ; 
echo "<BUTTON class=\"btn\" onClick=\"document.location.href='?cmd=refresh'\">Refresh</BUTTON><BR>";
echo "";
echo "Sorted by : " . $attr_map[$_SESSION['sort']] ."<BR>" ;

echo "<FORM action=\"?\" method=\"GET\" style=\"display : inline;\">
<INPUT type=\"text\" name=\"query\" size=\"10\" maxlength=\"10\" value=\"". htmlentities($_SESSION['query'])."\">
<INPUT type=\"submit\" value=\"search\">
</FORM><BR>
" ;


if (count ($tvstations)==0)
{
	echo "No stations found.<BR>";
	exit();
}
else
{
	echo count ($tvstations) . " stations found.<BR>";	

}


echo "<TABLE cellspacing=\"0\"  cellpadding=\"5\" class=\"mytable\" align=\"center\" >";
echo "
	<TR class=\"tblhead\">
		<TD onClick=\"document.location.href='?sort=NAME'\" class=\"tblheadCell\">".
		$attr_map['NAME'] ."</TD>
		<TD onClick=\"document.location.href='?sort=BR'\"  class=\"tblheadCell\">".
		$attr_map['BR'] ."</TD>
		<TD onClick=\"document.location.href='?sort=RT'\"  class=\"tblheadCell\">".
		$attr_map['RT'] ."</TD>
		<TD onClick=\"document.location.href='?sort=LOAD'\"  class=\"tblheadCell\">".
		$attr_map['LOAD'] ."</TD>
		<TD onClick=\"document.location.href='?sort=GENRE'\"  class=\"tblheadCell\">".
		$attr_map['GENRE'] ."</TD>
		<TD onClick=\"document.location.href='?sort=LC'\"  class=\"tblheadCell\">".
		$attr_map['LC'] ."</TD>
	</TR>";
/*
		<!--<TD onClick=\"document.location.href='?sort=CT'\"  class=\"tblheadCell\">".
		$attr_map['CT'] ."</TD>-->
*/	

// sort
foreach ($tvstations as $key => $row)
{
   $s[$key]  = $row[$_SESSION['sort']];
}
array_multisort($s, SORT_ASC, $tvstations);
// sort	
$switch = false;
foreach ($tvstations as $a=>$b)
{	
	$name			= $b['NAME'];
	$id				= $b['ID'];
	$bitrate	= $b['BR'];
	$rating		= $b['RT'];
	$now			= $b['CT'];
	$load			= $b['LOAD'];	
	$genre		= $b['GENRE'];
	$users		= $b['LC'];	

if ($switch == false)
{
	$cellclass = "tblRow0";
	$switch = true;
}
else
{
	$cellclass = "tblRow1";	
	$switch = false;
}

echo "
	<TR class=\"".$cellclass."\" >
		<TD class=\"tblcell\">" 
		."<A class\"tvlink\" href=\"".$tvtunerUrl. $id ."\">".$name."</A>".
		"&nbsp;</TD>
		<TD class=\"tblcell\">".$bitrate."&nbsp;</TD>
		<TD class=\"tblcell\">".$rating."&nbsp;</TD>	
		<TD class=\"tblcell\">".round($load,1)."&nbsp;</TD>
		<TD class=\"tblcell\">".$genre."&nbsp;</TD>
		<TD class=\"tblcell\">".$users."&nbsp;</TD>
	</TR>";
if ($now != "")
	echo "<TR class=\"".$cellclass."\" ><TD colspan=\"6\" class=\"tblcell\">Now playing: ".substr($now, 0, 60)."</TD></TR>";

}
echo "</TABLE>";
//	<!--<TD class=\"tblCell\">".$now."&nbsp;</TD>-->
?>


</body>
</html>
Return current item: Winamp TV Guide