Skip to content

Instantly share code, notes, and snippets.

@syg
Created March 10, 2014 03:49
Show Gist options
  • Save syg/9459174 to your computer and use it in GitHub Desktop.
Save syg/9459174 to your computer and use it in GitHub Desktop.
<pre>
<?php
// How many movies to ask RT at once.
define("PAGE_LIMIT", 50);
// The minimum critic score to be considered fresh.
define("FRESH_THRESHOLD", -1);
// The maximum number of days in theaters to be considered young.
define("YOUNG_THRESHOLD", 90);
function keyUrl($url) {
$sep = strstr($url, "?") ? "&" : "?";
return $url . $sep . "apikey=rbrrxgd6ktdk2e36ybgrwyn7";
}
$today = new DateTime();
function make_in_theaters_ch() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
return $ch;
}
function get_in_theaters_json($pageno, $ch = null) {
$url = keyUrl("http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json?page_limit=" . PAGE_LIMIT . "&page=" . $pageno);
// If we have a curl handle, reuse it.
if ($ch) {
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
} else {
$data = file_get_contents($url);
}
return json_decode($data);
}
function days_in_theater($movie) {
global $today;
return $today->diff(new DateTime($movie->release_dates->theater))->days;
return new DateTime($movie->release_dates->theater);
}
function fresh_and_young($movie) {
if ($movie->ratings->critics_score < FRESH_THRESHOLD)
return false;
return days_in_theater($movie) <= YOUNG_THRESHOLD;
}
function classify_age($movie) {
$days = days_in_theater($movie);
if ($days < 30)
return "just-in";
if ($days < 60)
return "on-its-way";
return "almost-gone";
}
function cmpdates($movie_lhs, $movie_rhs) {
$date_lhs = new DateTime($movie_lhs->release_dates->theater);
$date_rhs = new DateTime($movie_rhs->release_dates->theater);
if ($date_lhs == $date_rhs)
return 0;
return ($date_lhs > $date_rhs) ? -1 : 1;
}
// If PHP has curl support, use it.
if (function_exists("curl_init")) {
$ch = make_in_theaters_ch();
if (!$ch)
exit(-1);
}
// Grab the first page.
$pageno = 1;
$data = get_in_theaters_json($pageno, $ch);
if (!$data)
exit(-1);
// Check how many movies there are in total. We only grab PAGE_LIMIT movies at
// a time from RT.
$total_movies = $data->total;
$movies = $data->movies;
// Subtract the first page's movies from the number of total movies.
$total_movies -= PAGE_LIMIT;
$pageno++;
// While there are still movies left to get, fire off more requests.
while ($total_movies > 0) {
$data = get_in_theaters_json($pageno, $ch);
if (!$data)
exit(-1);
// Append the newly gotten list of movies to the existing $movies array.
$movies = array_merge($movies, $data->movies);
// Subtract this page's movies from the number of total movies.
$total_movies -= PAGE_LIMIT;
$pageno++;
}
// Close the curl handle if we used curl.
if ($ch)
curl_close($ch);
// Filter the array of movies to only contain those that are fresh and young,
// according to the thresholds defined at the top of the file.
$filtered = array_filter($movies, "fresh_and_young");
// Sort the filtered list in descending order of recency.
usort($filtered, "cmpdates");
// Print out the sorted, filtered list.
foreach ($filtered as $movie) {
echo $movie->title;
echo "\n Days in theater: ";
echo days_in_theater($movie);
echo "\n Classification: ";
echo classify_age($movie);
echo "\n\n";
}
?>
</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment