LATEST POST - My first Arduino Circuit

Linking 2 APIs [RottenTomatoes & TheMovieDB]

As you know I am currently working on re-developing PixelGrafters. A friend of mine introduced me to a new API from Rotten Tomatoes, I was interested as what new information this could provide me with as I currently use TheMovieDB. It turned out the Information about the movies really excelled in lists of movies such as “Upcoming” , “In Theatres”, “Movies Opening This week”, “DVD Releases this week”. This Info could be incredibly useful for me.

Unfortunately The images provided by Rotten Tomatoes are kind of pathetic. So I want to keep getting the images from TheMovieDB but grab lists of these movies from Rotten Tomatoes. This is easier said than done. To accurately reference a movie on TheMovieDB a “IMDB-ID” is required which is not given out by Rotten Tomatoes which is a shame. I came up with a solution which will be on the most part completely accurate, I haven’t found it fail yet or pick up the wrong movie.

What I did was found a way to search TheMovieDB by the exact movie title pulled from Rotten Tomatoes and constrain the search by the year the movie was released also pulled from Rotten Tomatoes. I didn’t think it would be accurate enough at first but it certainly seems to do the trick. This now allows me to view the list of Movies I wanted with much better quality artwork. Im going to wack into the post the code below.

Bear in mind the PHP snippet below is just the code to display movies in a way I wanted which is combined from 2 of Rotten Tomatoes Lists from the API and then using PHP to filter out the movies not released this year allowing to display just the most popular movies being released in your local multiplex. Also I haven’t commented it yet as its still subject to change due to PixelGrafters being in development right now.

Please feel free to use this code and please credit me if you do. Thanks

  1. //Code By Tristan Bettany - TristanBettany.co.uk
  2. //Script Designed For PixelGrafters.co.uk
  3. //Please Credit Me if used - Thanks
  4. <?PHP
  5.     $date = date('d-m-Y');
  6.     $split = explode("-", $date);
  7.     $year = $split[2];
  8.  
  9.     $TMDB_APIKey = "Your TheMovieDB API Key";
  10.     $RT_APIKey = "Your Rotten Tomatoes API Key";
  11.  
  12.     $RT_NewReleases = 'http://api.rottentomatoes.com/api/public/v1.0/lists/movies/opening.json?apikey=' . $RT_APIKey . '&country=UK';
  13.     $RT_InTheatres = 'http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json?apikey=' . $RT_APIKey . '&country=UK';
  14.  
  15.     $session_A = curl_init($RT_NewReleases);
  16.     curl_setopt($session_A, CURLOPT_RETURNTRANSFER, true);
  17.     $return_A = curl_exec($session_A);
  18.     curl_close($session_A);
  19.     $NewReleases = json_decode($return_A);
  20.     if ($NewReleases === NULL) die('Error parsing json');
  21.     $movies_A = $NewReleases->movies;
  22.  
  23.     $session_B = curl_init($RT_InTheatres);
  24.     curl_setopt($session_B, CURLOPT_RETURNTRANSFER, true);
  25.     $return_B = curl_exec($session_B);
  26.     curl_close($session_B);
  27.     $InTheatres = json_decode($return_B);
  28.     if ($InTheatres === NULL) die('Error parsing json');
  29.     $movies_B = $InTheatres->movies;
  30.  
  31.     foreach ($movies_A as $NewRelease) {
  32.     $TMDB_Echoed = false;
  33.         if ($NewRelease->year == $year) {
  34.             if ($NewRelease->posters->detailed != "http://images.rottentomatoescdn.com/images/redesign/poster_default.gif") {
  35.  
  36.                 $title = urlencode($NewRelease->title);
  37.                 $TMDB_URL = 'http://api.themoviedb.org/2.1/Movie.browse/en/xml/'. $TMDB_APIKey .'?order_by=release&order=desc&per_page=10&page=1&query=' . $title .'&year=' . $NewRelease->year;
  38.                 if(!$xml = simplexml_load_file($TMDB_URL)) exit('Failed to open '.$TMDB_URL);
  39.                 $movieCount=0;
  40.                 foreach($xml->movies->movie as $movie) {
  41.                     if ($movieCount == 1){
  42.                         break;
  43.                     }
  44.                     $imdb_id = $movie->imdb_id;
  45.                     $imageCount=0;
  46.                     if ($imdb_id != "") {
  47.                         foreach($xml->movies->movie[$movieCount]->images->image as $image) {
  48.                             if ($image['type'] == 'poster') {
  49.                                 if ($image['size'] == 'original') {
  50.                                     $hiRes_Link[$movieCount] = $image['url'];
  51.                                 }
  52.                                 if ($image['size'] == 'cover') {
  53.                                     $medRes_Link[$movieCount] = $image['url'];
  54.                                 }
  55.                             }
  56.                             $imageCount++;
  57.                         }
  58.                         if (isset($medRes_Link[0])) {
  59.                             echo '<a target="_blank" href="' . $hiRes_Link[0] . '">';
  60.                             echo '<img width="185" height="278" src="' . $medRes_Link[0] . '" ></img></a>';
  61.                             $TMDB_Echoed = true;
  62.                             ob_flush();
  63.                             flush();
  64.                         }
  65.                     }
  66.                 $movieCount++;
  67.                 }
  68.  
  69.                 if(!$TMDB_Echoed){
  70.                     echo '<a href="' . $NewRelease->links->alternate . '">';
  71.                     echo '<img width="185" height="278" src="' . $NewRelease->posters->detailed . '"></img></a>';
  72.                     ob_flush();
  73.                     flush();
  74.                 }
  75.             }
  76.         }
  77.     }
  78.  
  79.     foreach ($movies_B as $InTheatre) {
  80.     $TMDB_Echoed = false;
  81.         if ($InTheatre->year == $year) {
  82.             if ($InTheatre->posters->detailed != "http://images.rottentomatoescdn.com/images/redesign/poster_default.gif") {
  83.  
  84.                 $title = urlencode($InTheatre->title);
  85.                 $TMDB_URL = 'http://api.themoviedb.org/2.1/Movie.browse/en/xml/'. $TMDB_APIKey .'?order_by=release&order=desc&per_page=10&page=1&query=' . $title .'&year=' . $InTheatre->year;
  86.                 if(!$xml = simplexml_load_file($TMDB_URL)) exit('Failed to open '.$TMDB_URL);
  87.                 $movieCount=0;
  88.                 foreach($xml->movies->movie as $movie) {
  89.                     if ($movieCount == 1){
  90.                         break;
  91.                     }
  92.                     $imdb_id = $movie->imdb_id;
  93.                     $imageCount=0;
  94.                     if ($imdb_id != "") {
  95.                         foreach($xml->movies->movie[$movieCount]->images->image as $image) {
  96.                             if ($image['type'] == 'poster') {
  97.                                 if ($image['size'] == 'original') {
  98.                                     $hiRes_Link[$movieCount] = $image['url'];
  99.                                 }
  100.                                 if ($image['size'] == 'cover') {
  101.                                     $medRes_Link[$movieCount] = $image['url'];
  102.                                 }
  103.                             }
  104.                             $imageCount++;
  105.                         }
  106.                         if (isset($medRes_Link[0])) {
  107.                             echo '<a target="_blank" href="' . $hiRes_Link[0] . '">';
  108.                             echo '<img width="185" height="278" src="' . $medRes_Link[0] . '" ></img></a>';
  109.                             $TMDB_Echoed = true;
  110.                             ob_flush();
  111.                             flush();
  112.                         }
  113.                     }
  114.                 $movieCount++;
  115.                 }
  116.  
  117.                 if(!$TMDB_Echoed){
  118.                     echo '<a href="' . $InTheatre->links->alternate . '">';
  119.                     echo '<img width="185" height="278" src="' . $InTheatre->posters->detailed . '"></img></a>';
  120.                     ob_flush();
  121.                     flush();
  122.                 }
  123.             }
  124.         }
  125.     }
  126.     echo '</ul>
  127.  
  128. ';
  129. ?>
//Code By Tristan Bettany - TristanBettany.co.uk
//Script Designed For PixelGrafters.co.uk
//Please Credit Me if used - Thanks
<?PHP
	$date = date('d-m-Y');
	$split = explode("-", $date);
	$year = $split[2];

	$TMDB_APIKey = "Your TheMovieDB API Key";
	$RT_APIKey = "Your Rotten Tomatoes API Key";

	$RT_NewReleases = 'http://api.rottentomatoes.com/api/public/v1.0/lists/movies/opening.json?apikey=' . $RT_APIKey . '&country=UK';
	$RT_InTheatres = 'http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json?apikey=' . $RT_APIKey . '&country=UK';

	$session_A = curl_init($RT_NewReleases);
	curl_setopt($session_A, CURLOPT_RETURNTRANSFER, true);
	$return_A = curl_exec($session_A);
	curl_close($session_A);
	$NewReleases = json_decode($return_A);
	if ($NewReleases === NULL) die('Error parsing json');
	$movies_A = $NewReleases->movies;

	$session_B = curl_init($RT_InTheatres);
	curl_setopt($session_B, CURLOPT_RETURNTRANSFER, true);
	$return_B = curl_exec($session_B);
	curl_close($session_B);
	$InTheatres = json_decode($return_B);
	if ($InTheatres === NULL) die('Error parsing json');
	$movies_B = $InTheatres->movies;

	foreach ($movies_A as $NewRelease) {
	$TMDB_Echoed = false;
		if ($NewRelease->year == $year) {
			if ($NewRelease->posters->detailed != "http://images.rottentomatoescdn.com/images/redesign/poster_default.gif") {

				$title = urlencode($NewRelease->title);
				$TMDB_URL = 'http://api.themoviedb.org/2.1/Movie.browse/en/xml/'. $TMDB_APIKey .'?order_by=release&order=desc&per_page=10&page=1&query=' . $title .'&year=' . $NewRelease->year;
				if(!$xml = simplexml_load_file($TMDB_URL)) exit('Failed to open '.$TMDB_URL);
				$movieCount=0;
				foreach($xml->movies->movie as $movie) {
					if ($movieCount == 1){
						break;
					}
					$imdb_id = $movie->imdb_id;
					$imageCount=0;
					if ($imdb_id != "") {
						foreach($xml->movies->movie[$movieCount]->images->image as $image) {
							if ($image['type'] == 'poster') {
								if ($image['size'] == 'original') {
									$hiRes_Link[$movieCount] = $image['url'];
								}
								if ($image['size'] == 'cover') {
									$medRes_Link[$movieCount] = $image['url'];
								}
							}
							$imageCount++;
						}
						if (isset($medRes_Link[0])) {
							echo '<a target="_blank" href="' . $hiRes_Link[0] . '">';
							echo '<img width="185" height="278" src="' . $medRes_Link[0] . '" ></img></a>';
							$TMDB_Echoed = true;
							ob_flush();
							flush();
						}
					}
				$movieCount++;
				}

				if(!$TMDB_Echoed){
					echo '<a href="' . $NewRelease->links->alternate . '">';
					echo '<img width="185" height="278" src="' . $NewRelease->posters->detailed . '"></img></a>';
					ob_flush();
					flush();
				}
			}
		}
	}

	foreach ($movies_B as $InTheatre) {
	$TMDB_Echoed = false;
		if ($InTheatre->year == $year) {
			if ($InTheatre->posters->detailed != "http://images.rottentomatoescdn.com/images/redesign/poster_default.gif") {

				$title = urlencode($InTheatre->title);
				$TMDB_URL = 'http://api.themoviedb.org/2.1/Movie.browse/en/xml/'. $TMDB_APIKey .'?order_by=release&order=desc&per_page=10&page=1&query=' . $title .'&year=' . $InTheatre->year;
				if(!$xml = simplexml_load_file($TMDB_URL)) exit('Failed to open '.$TMDB_URL);
				$movieCount=0;
				foreach($xml->movies->movie as $movie) {
					if ($movieCount == 1){
						break;
					}
					$imdb_id = $movie->imdb_id;
					$imageCount=0;
					if ($imdb_id != "") {
						foreach($xml->movies->movie[$movieCount]->images->image as $image) {
							if ($image['type'] == 'poster') {
								if ($image['size'] == 'original') {
									$hiRes_Link[$movieCount] = $image['url'];
								}
								if ($image['size'] == 'cover') {
									$medRes_Link[$movieCount] = $image['url'];
								}
							}
							$imageCount++;
						}
						if (isset($medRes_Link[0])) {
							echo '<a target="_blank" href="' . $hiRes_Link[0] . '">';
							echo '<img width="185" height="278" src="' . $medRes_Link[0] . '" ></img></a>';
							$TMDB_Echoed = true;
							ob_flush();
							flush();
						}
					}
				$movieCount++;
				}

				if(!$TMDB_Echoed){
					echo '<a href="' . $InTheatre->links->alternate . '">';
					echo '<img width="185" height="278" src="' . $InTheatre->posters->detailed . '"></img></a>';
					ob_flush();
					flush();
				}
			}
		}
	}
	echo '</ul>

';
?>

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">