<?php
/*******************************************************************************************************************
Filename: trailer_bot.php
Version: 1.0
Author: Cole Joplin, TopTenReviews.com
Summary: This file is used to look at Quciktime for availale trailers
*******************************************************************************************************************/
// omitted require code
$movie_id = 0;
$movie_name = '';
$contents = ''; // set
$handle = 0;
$url = ''; // set
$trailer_trunc = false; // position to strip off all the junk at the top of the page
$soundtrack_pos = false; // pos to strip off junk at the bottom
$trailer_contents = ''; // this gets us a smaller string chunk to work with
$trailer_id = 0;
$sqlResult = 0; // SQL run result
$qryMovieStat = ''; // SQL string
$qry = ''; // SQL string
$sqlStatResult = 0; // SQL run result
mysql_select_db($database_t10, $t10);
$query_rstMovieName = "SELECT id, name FROM movies WHERE status_id=20 LIMIT 1";
$rstMovieName = mysql_query($query_rstMovieName, $t10) or die(mysql_error());
$row_rstMovieName = mysql_fetch_assoc($rstMovieName);
$totalRows_rstMovieName = mysql_num_rows($rstMovieName);
if ($totalRows_rstMovieName > 0) {
$movie_id = $row_rstMovieName['id']; // use this for the child records
$movie_name = $row_rstMovieName['name'];
$movie_name = substr($movie_name, 0, (strlen($movie_name) - 7));
// Go to http://www.apple.com/trailers/ for new movie trailers this week
$handle = fopen('http://www.apple.com/trailers/', "r");
if (!$handle) {
print "_root.trailer_display=Could not connect to Quicktime for $movie_name.";
} else {
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
}
if ($contents <> '') {
// this page is outrageously long from all the javascript! So let's trim out the top fat
$trailer_trunc = strpos($contents, 'Newest Trailers');
$soundtrack_pos = strpos($contents, 'Broken Movie icons');
if ($trailer_trunc && $soundtrack_pos) {
$trailer_contents = substr($contents, $trailer_trunc, (strlen($contents) - $soundtrack_pos));
}
$trailer_pos = strpos($trailer_contents, $movie_name);
if ($trailer_pos) {
$trailer_contents = substr($trailer_contents, ($trailer_pos - 100), 200);
$url = crawl_forward_string ($trailer_contents, '<a href', '">', '">');
if ($url <> '') {
$url = 'http://www.apple.com'.trim(substr($url, 9, strlen($url)));
$trailer_id = is_duplicate_trailer ($t10, $movie_id);
if ($trailer_id == 0) {
$qry = "INSERT INTO trailers (movie_id, url) VALUES ($movie_id, '$url')";
} else {
$qry = "UPDATE trailers SET movie_id=$movie_id, url='$url' WHERE movie_id=$movie_id";
}
$sqlResult = mysql_query($qry, $t10) or print "_root.trailer_display=SQL failure for trailer $movie_name.";
}
}
// update the movie status to 30 - NO MATTER WHAT, so the bot doesn't hang if we don't find a trailer
$qryMovieStat = "UPDATE movies SET status_id=30 WHERE id=$movie_id";
$sqlStatResult = mysql_query($qryMovieStat, $t10) or print "_root.trailer_display=SQL failure - trailer $movie_name.";
if ($sqlStatResult == true) {
print "_root.trailer_display=Successfully updated trailer for $movie_name.";
}
} // end if
} else {
print "_root.trailer_display=No more movies ready for trailers.";
}
mysql_free_result($rstMovieName);
?>