Version: 1.1
Type: Full Script
Category: Other
License: GNU General Public License
Description: This code pulls down tvguide.com's listing and parses it into a RSS feed.
<?php
/*
-------------------------------------
Author: Chris Hilbert (Hilbe)
Email: chris.hilbert@fileflash.com
Website: http://www.fileflash.com
File Name: tvguide.php
Version: 1.1
Built: 05/05/03
Updated: 05/06/03
Requires: PHP 4.x or greater
Licence:
This source is freely distributable. If you borrow, change, or use any of the code, please give me credit.
Contributers:
TrillHunter
-------------------------------------
Required Variables:
$I, $Zip
Usage:
tvguide.php?I=#####&Zip=#####
Optional Variables:
$Channels
Usage:
tvguide.php?I=#####&Zip=#####&Channels=#,#,# ...
Tip:
To get a I and Zip, disable cookies in your browser and visit http://www.tvguide.com/Listings/. Enter your zip code and select your area.
You should be able to copy/paste from the URL of the listings grid.
*/
//Test For Required Variables
if (!($I && $Zip))
{
echo "Error: You did not supply either an I or Zip value, please visit www.tvguide.com and disable cookies to get an I and Zip value. See your address bar for the values: http://www.tvguide.com/Listings/index.asp?I=<b>#####</b>&Zip=<b>#####</b>.";
return;
}
//Build URL to TV Guide Data
$tvURL = "http://www.tvguide.com/Listings/index.asp?I=" . $I . "&zip=" . $Zip;
$link = "http://www.tvguide.com/Listings/index.asp?I=" . $I . "&zip=" . $Zip;
//Open TV Guide URL and Get Contents
//Select which code to use based on your version of PHP4.
/*
//Option 1 (for PHP 4.x)
$handle = fopen ($tvURL,"rb");
if (!$handle)
{
echo "Could not open URL: " . $tvURL;
return;
}
for ($count = 0; $count < 1000 && !feof ($handle); $count++) {
$tvHTML .= fread ($handle,1024);
}
fclose ($handle);
*/
//Option 2 (for PHP 4.3.x or greater)
$tvHTML = file_get_contents($tvURL);
if (!strcmp($tvHTML,""))
{
echo "Nothing read from URL: " . $tvURL;
return;
}
//Test For Successful Data Pull and Match
if (!preg_match("#<!--BEGIN COMPONENT OUTPUT-->(.*)<!--END COMPONENT OUTPUT-->#s",$tvHTML,$tvArray))
{
echo "The supplied HTML was not formatted correctly for parsing.<BR><BR>";
echo $tvHTML;
return;
}
$tvHTML = ""; // We're done with this; save some memory
$tvArray[0] = ""; // We don't need this; save some memory
//Pull Out Data Portion
//Build Time Array
if (!preg_match("#^.*?<nobr>(\d\d?):(\d\d) ([AP]M)</nobr>#s",$tvArray[1],$tvTime))
{
echo "Couldn't parse time information.<BR><BR>";
echo $tvArray[1];
return;
}
//Build Channels Array
if (!preg_match_all("#<TR.*?>(\d+) ([^<]{1,10})</(.*?)</TR>#s",$tvArray[1],$tvChannels))
{
echo "Couldn't parse channels.<BR><BR>";
echo $tvArray[1];
return;
}
$tvArray[1] = ""; // We're done with this; save some memory
//Create Array Of User Defined Channels
if ($Channels)
{
$myChannels = explode(",",$Channels);
}
//Begin RSS Construction
//Send XML Header
header("Content-Type: text/xml; charset=utf-8");
echo "<?xml version='1.0' encoding='utf-8' ?>";
?>
<rss version="0.91">
<channel>
<title><? echo $Zip; ?> TV Guide</title>
<description>TV Guide</description>
<link>http://www.tvguide.com</link>
<copyright>Copyright <? echo date("Y"); ?> TV Guide Magazine Group, Inc.</copyright>
<?
//Begin Channel Loop
for ($counter = 0; $counter < count($tvChannels[0]); $counter++)
{
//Check For Optional Variable and See If Channel In Optional Variable
if (!$Channels || in_array($tvChannels[1][$counter],$myChannels))
{
$tvStation = $tvChannels[1][$counter] . " " . $tvChannels[2][$counter];
$tvStation = preg_replace("#([^A-Za-z0-9 ,'!\?\:\-\.\$\(\)])#e", "'&#' . ord('\\1') . ';'", $tvStation);
//Build Show Array
preg_match_all("#<TD.*? colspan=(\d+)\D.*?>([^<&]+)<#s",$tvChannels[3][$counter],$tvShows);
?>
<item>
<title><? echo $tvStation; ?></title>
<link><? echo $link; ?></link>
<description><?
//Add Show Times To Description
$hours = $tvTime[1];
$minutes = $tvTime[2];
$ampm = $tvTime[3];
$showName = preg_replace("#([^A-Za-z0-9 ,'!\?\:\-\.\$\(\)])#e", "'&#' . ord('\\1') . ';'", $tvShows[2][0]);
echo "$hours:$minutes $ampm - $showName";
for ($showCounter = 1; $showCounter < count($tvShows[0]); $showCounter++)
{
$minutes += 5 * $tvShows[1][$showCounter - 1];
while ($minutes >= 60)
{
$minutes -= 60;
$hours++;
}
if ($minutes < 10)
{
$minutes = "0" . $minutes;
}
if ($hours >= 12)
{
if ($ampm == "AM")
{
$ampm = "PM";
}
else
{
$ampm = "AM";
}
if ($hours > 12)
{
$hours -= 12;
}
}
$showName = preg_replace("#([^A-Za-z0-9 ,'!\?\:\-\.\$\(\)])#e", "'&#' . ord('\\1') . ';'", $tvShows[2][$showCounter]);
echo "\n$hours:$minutes $ampm - $showName";
}
?></description>
</item>
<?
}
}
//End Channel Loop
?>
</channel>
</rss>