diff --git a/lib/copy_this/misc/testing/rottentom.php b/lib/copy_this/misc/testing/rottentom.php index 18e897ab0..ac93f1395 100644 --- a/lib/copy_this/misc/testing/rottentom.php +++ b/lib/copy_this/misc/testing/rottentom.php @@ -8,13 +8,13 @@ if (isset($site->rottentomatokey)) { $rt = new RottenTomato($site->rottentomatokey); - //print_r(json_decode($rt->getBoxOffice())); - //print_r(json_decode($rt->getInTheaters())); - //print_r(json_decode($rt->getOpening())); - //print_r(json_decode($rt->getUpcoming())); - //print_r(json_decode($rt->getDVDReleases())); - //print_r(json_decode($rt->getMovie("770805418"))); - //print_r(json_decode($rt->getReviews("770805418"))); - //print_r(json_decode($rt->getCast("770805418"))); - print_r(json_decode($rt->searchMovie("moviename"))); + //print_r($rt->getMoviesBoxOffice()); + //print_r($rt->getMoviesInTheaters()); + //print_r($rt->getOpeningMovies()); + //print_r($rt->getUpcomingMovies()); + //print_r($rt->getNewDvdReleasess()); + //print_r($rt->getMovieInfo("770805418")); + //print_r($rt->getMovieReviews("770805418")); + //print_r($rt->getMovieCast("770805418")); + print_r($rt->movieSearch("moviename")); } \ No newline at end of file diff --git a/lib/copy_this/newznab/controllers/Film.php b/lib/copy_this/newznab/controllers/Film.php index 756bf8e78..4eace77a8 100644 --- a/lib/copy_this/newznab/controllers/Film.php +++ b/lib/copy_this/newznab/controllers/Film.php @@ -1362,23 +1362,23 @@ class Film switch ($operation) { case 'boxoffice': - $data = $rt->getBoxOffice(); + $data = $rt->getMoviesBoxOffice(); $update = \Film::SRC_BOXOFFICE; break; case 'theaters': - $data = $rt->getInTheaters(); + $data = $rt->getMoviesInTheaters(); $update = \Film::SRC_INTHEATRE; break; case 'opening': - $data = $rt->getOpening(); + $data = $rt->getOpeningMovies(); $update = \Film::SRC_OPENING; break; case 'upcoming': - $data = $rt->getUpcoming(); + $data = $rt->getUpcomingMovies(); $update = \Film::SRC_UPCOMING; break; case 'dvd': - $data = $rt->getDVDReleases(); + $data = $rt->getNewDvdReleases(); $update = \Film::SRC_DVD; break; default: @@ -1387,8 +1387,7 @@ class Film } if ($data !== false && $data !== '') { - $test = @json_decode($data); - if (isset($test)) { + if (isset($data)) { $count = 2; $check = true; } diff --git a/lib/copy_this/newznab/controllers/Movie.php b/lib/copy_this/newznab/controllers/Movie.php index 90d4029ea..7d13da0d9 100644 --- a/lib/copy_this/newznab/controllers/Movie.php +++ b/lib/copy_this/newznab/controllers/Movie.php @@ -799,23 +799,23 @@ class Movie { $rt = new RottenTomato($site->rottentomatokey); - $ret = $rt->getBoxOffice(); + $ret = $rt->getMoviesBoxOffice(); if ($ret != "") $this->updateInsUpcoming('rottentomato', Movie::SRC_BOXOFFICE, $ret); - $ret = $rt->getInTheaters(); + $ret = $rt->getMoviesInTheaters(); if ($ret != "") $this->updateInsUpcoming('rottentomato', Movie::SRC_INTHEATRE, $ret); - $ret = $rt->getOpening(); + $ret = $rt->getOpeningMovies(); if ($ret != "") $this->updateInsUpcoming('rottentomato', Movie::SRC_OPENING, $ret); - $ret = $rt->getUpcoming(); + $ret = $rt->getUpcomingMovies(); if ($ret != "") $this->updateInsUpcoming('rottentomato', Movie::SRC_UPCOMING, $ret); - $ret = $rt->getDVDReleases(); + $ret = $rt->getNewDvdReleases(); if ($ret != "") $this->updateInsUpcoming('rottentomato', Movie::SRC_DVD, $ret); } diff --git a/lib/copy_this/newznab/controllers/RottenTomato.php b/lib/copy_this/newznab/controllers/RottenTomato.php index 1ee16f710..aa20c74d8 100644 --- a/lib/copy_this/newznab/controllers/RottenTomato.php +++ b/lib/copy_this/newznab/controllers/RottenTomato.php @@ -1,234 +1,355 @@ _apikey = $apiKey; + protected $timeoutSeconds; + /** + * Country + * @var string + */ + protected $country; + /** + * Initialize + * @param string $apiKey + * @param int $timeoutSeconds + * @param string $country + */ + public function __construct($apiKey, $timeoutSeconds = 15, $country = 'us') { + $this->apiKey = $apiKey; + $this->timeoutSeconds = $timeoutSeconds; + $this->country = $country; + } + /** + * Movie search + * @throws Exception + * @param string $query search term + * @param int $pageLimit results per page + * @param int $page current page + * @return array results + */ + public function movieSearch($query, $pageLimit = 20, $page = 0) { + $params = array(); + $params['q'] = $query; + if ($pageLimit) + $params['page_limit'] .= $pageLimit; + if ($page) + $params['page'] = $page; + $result = $this->getResource(self::ROTTEN_TOMATOES_API_MOVIE_SEARCH, $params); + if(!isset($result['movies']) && !empty($result['movies'])) + throw new Exception("No results"); + return $result; + } + /** + * Get movie reviews + * @throws Exception + * @param int $id rotten tomatoes id + * @param string $reviewType review type + * @param int $pageLimit results per page + * @param int $page current page + * @return array results + */ + public function getMovieReviews($id, $reviewType = self::REVIEW_TYPE_ALL, $pageLimit = 20, $page = 0) { + $url = sprintf(self::ROTTEN_TOMATOES_API_MOVIE_REVIEWS, $id); + $params = array(); + $params['review_type'] = $reviewType; + if ($pageLimit) + $params['page_limit'] .= $pageLimit; + if ($page) + $params['page'] = $page; + $result = $this->getResource($url, $params); + if(!isset($result['reviews']) && !empty($result['reviews'])) + throw new Exception("No results"); + return $result; + } + /** + * Get movie cast + * @throws Exception + * @param int $id rotten tomatoes id + * @return + */ + public function getMovieCast($id) { + $url = sprintf(self::ROTTEN_TOMATOES_API_MOVIE_CAST, $id); + $result = $this->getResource($url); + if(!isset($result['cast']) || empty($result['cast'])) + throw new Exception("No results"); + return $result['cast']; + } + /** + * Get movie info + * @throws Exception + * @param int $id rotten tomatoes id + * @return array + */ + public function getMovieInfo($id) { + $url = sprintf(self::ROTTEN_TOMATOES_API_MOVIE_INFO, $id); + $result = $this->getResource($url); + if(!isset($result['id'])) + throw new Exception("No results"); + return $result; + } + /** + * Get new DVD releases + * @throws Exception + * @param int $pageLimit results per page + * @param int $page current page + * @return array results + */ + public function getNewDvdReleases($pageLimit = 20, $page = 0) { + $params = array(); + if ($pageLimit) + $params['page_limit'] .= $pageLimit; + if ($page) + $params['page'] = $page; + $result = $this->getResource(self::ROTTEN_TOMATOES_API_NEW_RELEASE_DVDS, $params); + if(!isset($result['movies']) && !empty($result['movies'])) + throw new Exception("No results"); + return $result; + } + /** + * Get movies that are coming soon + * @throws Exception + * @param int $pageLimit results per page + * @param int $page current page + * @return array results + */ + public function getUpcomingMovies($pageLimit = 20, $page = 0) { + $params = array(); + if ($pageLimit) + $params['page_limit'] .= $pageLimit; + if ($page) + $params['page'] = $page; + $result = $this->getResource(self::ROTTEN_TOMATOES_API_UPCOMING_MOVIES, $params); + if(!isset($result['movies']) && !empty($result['movies'])) + throw new Exception("No results"); + return $result; + } + /** + * Get movies currently in theaters + * @throws Exception + * @param int $pageLimit results per page + * @param int $page current page + * @return array results + */ + public function getMoviesInTheaters($pageLimit = 5, $page = 0) { + $params = array(); + if ($pageLimit) + $params['page_limit'] .= $pageLimit; + if ($page) + $params['page'] = $page; + $result = $this->getResource(self::ROTTEN_TOMATOES_API_IN_THEATERS, $params); + if(!isset($result['movies']) && !empty($result['movies'])) + throw new Exception("No results"); + return $result; + } + /** + * Get movies opening + * @throws Exception + * @param int $pageLimit number of results + * @return array results + */ + public function getOpeningMovies($pageLimit = 20) { + $params = array(); + if ($pageLimit) + $params['page_limit'] .= $pageLimit; + $result = $this->getResource(self::ROTTEN_TOMATOES_API_OPENING_MOVIES, $params); + if(!isset($result['movies']) && !empty($result['movies'])) + throw new Exception("No results"); + return $result; + } + /** + * Get top box office results + * @throws Exception + * @param int $pageLimit number of results + * @return array results + */ + public function getMoviesBoxOffice($pageLimit = 20) { + $params = array(); + if ($pageLimit) + $params['page_limit'] .= $pageLimit; + $result = $this->getResource(self::ROTTEN_TOMATOES_API_BOX_OFFICE, $params); + if(!isset($result['movies']) && !empty($result['movies'])) + throw new Exception("No results"); + return $result; + } + /** + * Get top DVD rentals + * @throws Exception + * @param int $pageLimit number of results + * @return array results + */ + public function getDvdTopRentals($pageLimit = 20) { + $params = array(); + if ($pageLimit) + $params['page_limit'] .= $pageLimit; + $result = $this->getResource(self::ROTTEN_TOMATOES_API_DVD_TOP_RENTALS, $params); + if(!isset($result['movies']) && !empty($result['movies'])) + throw new Exception("No results"); + return $result; + } + /** + * Get Movie Urls + * @throws Exception + * @return array results + */ + public function getMovieListsDirectory() { + $result = $this->getResource(self::ROTTEN_TOMATOES_API_DVD_LISTS_DIRECTORY); + if(!isset($result['links']) && !empty($result['links'])) + throw new Exception("No results"); + return $result; + } + /** + * Get list Urls + * @throws Exception + * @return array results + */ + public function getListsDirectory() { + $result = $this->getResource(self::ROTTEN_TOMATOES_API_LISTS_DIRECTORY); + if(!isset($result['links']) && !empty($result['links'])) + throw new Exception("No results"); + return $result; + } + /** + * Get DVD API Urls + * @throws Exception + * @return array results + */ + public function getDvdListsDirectory() { + $result = $this->getResource(self::ROTTEN_TOMATOES_API_DVD_LISTS_DIRECTORY); + if(!isset($result['links']) && !empty($result['links'])) + throw new Exception("No results"); + return $result; + } + /** + * Get resource + * @param string $url + * @param array $params parameters (key/value pairs) for query string + * @return array decoded json response + */ + protected function getResource($url, $params = array()) { + $params['apikey'] = $this->apiKey; + $params['country'] = $this->country; + $queryString = http_build_query($params); + $rawResponse = trim($this->httpRequest("{$url}?{$queryString}")); + if (!$rawResponse) + throw new Exception("No response"); + $decodedResponse = json_decode($rawResponse, true); + if (!$decodedResponse) + throw new Exception('Error parsing JSON response'); + if (isset($decodedResponse['error'])) + throw new Exception('API Error: ' + $decodedResponse['error']); + return $decodedResponse; + } + /** + * Perform HTTP Request + * @param string $url url for request + * @return string|boolean result + */ + protected function httpRequest($url) { + //Test for allow_url_fopen to be enabled. + //Many hosts disable this for security + if(function_exists('curl_init')){ + return $this->httpRequestCurl($url); + }elseif(ini_get('allow_url_fopen')){ + return $this->httpRequestFopen($url); + }else{ + return false; + } } /** - * Displays Top Box Office Earning Movies, Sorted by Most Recent Weekend Gross Ticket Sales. - * - * @param int $limit Limits the number of box office movies returned. - * @param string $country Provides localized data for the selected country (ISO 3166-1 alpha-2) if available. - * Otherwise, returns US data. - * - * @return string + * Perform HTTP Request with fopen + * @param string $url url for request + * @return string|boolean result */ - public function getBoxOffice($limit = 10, $country = 'us') - { - return $this->_makeCall( - 'lists/movies/box_office.json', - array( - 'limit' => $limit, - 'country' => $country - ) - ); + protected function httpRequestFopen($url){ + $http = array(); + $http['method'] = 'GET'; + $http['timeout'] = $this->timeoutSeconds; + $response = @fopen($url, 'r', false, stream_context_create(array('http' => $http))); + $result = false; + if ($response) + $result = $response; + return $result; } /** - * Retrieves movies currently in theaters. - * - * @param int $limit The amount of movies in theaters to show per page. - * @param int $page The selected page of in theaters movies. - * @param string $country Provides localized data for the selected country (ISO 3166-1 alpha-2) if available. - * Otherwise, returns US data. - * - * @return string + * Perform HTTP Request with curl + * @param string $url url for request + * @return string|boolean result */ - public function getInTheaters($limit = 16, $page = 1, $country = 'us') - { - return $this->_makeCall( - 'lists/movies/in_theaters.json', - array( - 'page_limit' => $limit, - 'page' => $page, - 'country' => $country - ) - ); - } + protected function httpRequestCurl($url){ + // create curl resource + $ch = curl_init(); - /** - * Retrieves current opening movies. - * - * @param int $limit Limits the number of opening movies returned. - * @param string $country Provides localized data for the selected country (ISO 3166-1 alpha-2) if available. - * Otherwise, returns US data. - * - * @return string - */ - public function getOpening($limit = 16, $country = 'us') - { - return $this->_makeCall( - 'lists/movies/opening.json', - array( - 'limit' => $limit, - 'country' => $country - ) - ); - } + // set url + curl_setopt($ch, CURLOPT_URL, $url); - /** - * Retrieves upcoming movies. Results are paginated if they go past the specified page limit. - * - * @param int $limit The amount of upcoming movies to show per page. - * @param int $page The selected page of upcoming movies. - * @param string $country Provides localized data for the selected country (ISO 3166-1 alpha-2) if available. - * Otherwise, returns US data. - * - * @return string - */ - public function getUpcoming($limit = 16, $page = 1, $country = 'us') - { - return $this->_makeCall( - 'lists/movies/upcoming.json', - array( - 'page_limit' => $limit, - 'page' => $page, - 'country' => $country - ) - ); - } + //return the transfer as a string + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + //set timeout + curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeoutSeconds); + // $output contains the output string + $response = curl_exec($ch); - /** - * Shows the DVD lists RT has available. - * - * @return string - */ - public function getDVDReleases() - { - return $this->_makeCall('lists/dvds/new_releases.json'); - } - - /** - * The movies search endpoint for plain text queries. Allows you to search for movies! - * - * @param string $title The plain text search query to search for a movie. Remember to URI encode this! - * @param int $limit The amount of movie search results to show per page. - * @param int $page The selected page of movie search results. - * - * @return string - */ - public function searchMovie($title, $limit = 50, $page = 1) - { - return $this->_makeCall( - 'movies.json', - array( - 'q' => $title, - 'page_limit' => $limit, - 'page' => $page - ) - ); - } - - /** - * Detailed information on a specific movie specified by Id. - * You can use the movies search endpoint or peruse the lists of movies/dvds to get the urls to movies. - * - * @param int $ID The RT id. - * - * @return string - */ - public function getMovie($ID) - { - return $this->_makeCall('movies/' . $ID . '.json'); - } - - /** - * Retrieves the reviews for a movie. - * Results are paginated if they go past the specified page limit. - * - * @param int $ID The RT id. - * @param string $type Three different review types are possible: - * "all", "top_critic" and "dvd". - * "top_critic" shows all the Rotten Tomatoes designated top critics. - * "dvd" pulls the reviews given on the DVD of the movie. - * "all" as the name implies retrieves all reviews. - * @param int $limit The number of reviews to show per page. - * @param int $page The selected page of reviews. - * @param string $country Provides localized data for the selected country (ISO 3166-1 alpha-2) if available. - * Otherwise, returns US data. - * - * @return string - */ - public function getReviews($ID, $type = 'top_critic', $limit = 20, $page = 1, $country = 'us') - { - return $this->_makeCall( - 'movies/' . $ID . '/reviews.json', - array( - 'review_type' => $type, - 'page_limit' => $limit, - 'page' => $page, - 'country' => $country - ) - ); - } - - /** - * Pulls the complete movie cast for a movie. - * - * @param int $ID The RT id. - * - * @return string - */ - public function getCast($ID) - { - return $this->_makeCall('movies/' . $ID . '/cast.json'); - } - - /** - * Make a request to RT. - * - * @param string $function The type of request. - * @param array $params Extra HTTP parameters. - * - * @return string JSON data from RT. - */ - private function _makeCall($function, $params = []) - { - return trim( - Utility::getUrl([ - 'url' => \RottenTomato::API_URL . - $function . - '?limit=' . - mt_rand(15, 20) . - '&apikey=' . - $this->_apikey . - (!empty($params) ? ('&' . http_build_query($params)) : '') - ] - ) - ); - } - - /** - * Get the RT api key. - * - * @return string - */ - public function getApiKey() - { - return $this->_apikey; + // close curl resource to free up system resources + curl_close($ch); + $result = false; + if ($response) + $result = $response; + return $result; } } \ No newline at end of file