Daily TVRage w/ Cache (+MySQL)

By zenx on Jul 13, 2012

Grabs & formats a new version of tvrage's myrss.php file daily, from here it'd be easy to insert items to mysql
...
Mobbed @ 08:00 pm ( 2012-07-14 ) [ S01E07 ] You're Fired http://www.tvrage.com/Mobbed
My Cat From Hell @ 08:00 pm ( 2012-07-14 ) [ S03E03 ] Kitty Dearest http://www.tvrage.com/My_Cat_From_Hell
NYC 22 @ 08:00 pm ( 2012-07-14 ) [ S01E07 ] Block Party http://www.tvrage.com/The_2-2
...

<?php

$tmpfile = "myrss.tmp";
$forceupdate = "0";
$sdate = "today"; # yesterday, today, tomorrow
$country = strtoupper("US"); # US, UK, 

$add2sql = "yes"; # insert data to mysql - yes/no
$sqlhost = "localhost";
$sqluser = "";
$sqlpw = "";
$sqldb = "";
$sqltbl = "";

// check if we need to get a new file
if ($sdate == "yesterday") { $qdate = '?date=yesterday&country='.$country; }
elseif ($sdate == "today") { $qdate = '?country='.$country; }
elseif ($sdate == "tomorrow") { $qdate = '?date=tomorrow&country='.$country; }
else { die('error'); }

if ($forceupdate == "1" || !file_exists($tmpfile) || date("Ymd", filemtime($tmpfile)) != date("Ymd", time())) {
    $data = file_get_contents('http://www.tvrage.com/myrss.php'.$qdate);
    $fp = fopen($tmpfile, 'w');
    fwrite($fp, $data);
    fclose($fp);
}

// undo htmlspecialchars()
function unhtmlspecial($str) { return str_replace(array("&amp;","&#39;","&#039;","&quot;"), array("&","'","'","\""), $str); }

// fetch rss page & regex
if (!$rsstoday = file_get_contents($tmpfile)) { die(''); }

$rssd = date("Y-m-d", strtotime($sdate));

preg_match_all('/<title>(?!TVrage)(.*)<\/title>/', $rsstoday, $title); // regex titles
preg_match_all('/<link>http:\/\/www\.tvrage\.com\/([a-zA-Z0-9\/_-]+)<\/link>/', $rsstoday, $link); // regex links
preg_match_all('/<description>(?!TVrage)(.*)<\/description>/', $rsstoday, $descr); // regex descriptions

// create arrays
$sheeep = array(); $show = array(); $description = array(); $time = array();

// format descriptions
foreach ($descr['1'] as $desc) {
    $description[] = unhtmlspecial(preg_replace('/^([ ])/', '', preg_replace('/( \(.*)$/', '', $desc)), ENT_NOQUOTES );
}

foreach ($title['1'] as $all) {
    if (preg_match('/^[0-9:]+ (a|p)m$/', $all)) {
        $showtime = $all;
    } else {
        $time[] = $showtime;
        $all = preg_replace('/^([-])/', '', $all);
        $show[] = unhtmlspecial(preg_replace('/^([ ])/', '', preg_replace('/( \([a-zA-Z0-9_ -]+)\)$/', '', $all)), ENT_NOQUOTES );
        preg_match('/\(([0-9]+)x([0-9]+)\)$/', $all, $sheep);
        if (isset($sheep['1']) && isset($sheep['2'])) { 
            $sheeep[] = "S".$sheep['1']."E".$sheep['2'];
        } else {
            preg_match('/\(([a-zA-Z0-9-]+)\)$/', $all, $sheep);
            $sheeep[] = $sheep['1'];
        }
    }
}

echo "<pre>";
$con = mysql_connect($sqlhost,$sqluser,$sqlpw);
mysql_select_db($sqldb, $con);

/*
CREATE TABLE `tvrage` (
 `id` int(10) NOT NULL AUTO_INCREMENT,
 `showname` varchar(255) NOT NULL,
 `time` varchar(8) NOT NULL,
 `date` varchar(10) NOT NULL,
 `sep` varchar(10) NOT NULL,
 `descr` varchar(255) NOT NULL,
 `link` varchar(255) NOT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `showsep` (`showname`,`sep`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 COMMENT='tv info'
*/

for ($i = 0; $i <= count($show) - 1; $i++) {
    echo $show["$i"] . " @ <b>" . $time["$i"] . "</b> ( " . $rssd . " ) [ " . $sheeep["$i"] . " ] " . $description["$i"] . " http://www.tvrage.com/" . $link['1']["$i"] . "\n";
    if ($add2sql = "yes") {
        $sql = "INSERT INTO `".$sqldb."`.`".$sqltbl."` (`id`, `showname`, `time`, `date`, `sep`, `descr`, `link`, `country`) VALUES (NULL, '".str_replace("'", "\'", $show["$i"])."', '".$time["$i"]."', '".$rssd."', '".$sheeep["$i"]."', '".str_replace("'", "\'", $description["$i"])."', '".$link['1']["$i"]."', '".$country."') ON DUPLICATE KEY UPDATE id=id";
        $q = mysql_query($sql) or die(mysql_error());
    }
}

mysql_close($con);
echo "</pre>";

?>

Comments

Sign in to comment.
sean   -  Jul 20, 2012

Not quite sure what this is exactly used for but just a few friendly notes after a quick glance.

1) I'd recommend looking into PDO or MySQLi at least.
2) You don't need single quotes around integers. (ie: $sheep[1] not $sheep['1'])
3) You could have your script determine if the tvrage table exists (if not create it)
4) You can extract

, and without running multiple regex's
5) It would be great if there were an API for this instead
6) You're missing a check after line 23 to ensure $data has populated
7) It would be better to assign "count($show) - 1" to a variable instead of repeated calculation
 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.