-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 71c8c3a
Showing
8 changed files
with
264 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/.idea | ||
/vendor | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# omdb-imdb-api-client | ||
|
||
A package to retrieve movies and TV information from IMDB using the API at omdbapi.com | ||
|
||
Retrieve full movie details, if you know the name or ID of the movie: | ||
|
||
```php | ||
$movie = Imdb::retrieve('the matrix'); | ||
|
||
// If you need to be more specific: | ||
// This will also return Rotten Tomatoes ratings & a longer plot | ||
$movie = Imdb::retrieve('the matrix', Imdb::TYPE_MOVIE, 1999, true, true); | ||
``` | ||
|
||
Search for a movie: | ||
|
||
```php | ||
$movies = Imdb::search('the matrix'); | ||
|
||
// If you need to be more specific: | ||
$movies = Imdb::search('the matrix', Imdb::TYPE_MOVIE, 1999); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "jleagle/omdb-imdb-api-client", | ||
"keywords": [ | ||
"imdb", | ||
"omdb", | ||
"api", | ||
"client", | ||
"helper" | ||
], | ||
"description": "A package to retrieve movies and TV information from IMDB using the API at omdbapi.com", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "James Eagle", | ||
"email": "jimeagle@gmail.com", | ||
"homepage": "http://jimeagle.com" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.4", | ||
"guzzlehttp/guzzle": "~5.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "~4.6" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Jleagle\\Imdb\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
namespace Jleagle\Imdb\Exceptions; | ||
|
||
class ImdbException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
<?php | ||
namespace Jleagle\Imdb; | ||
|
||
use GuzzleHttp\Client; | ||
use Jleagle\Imdb\Exceptions\ImdbException; | ||
use Jleagle\Imdb\Responses\Movie; | ||
use Jleagle\Imdb\Responses\Result; | ||
|
||
class Imdb | ||
{ | ||
const TYPE_MOVIE = 'movie'; | ||
const TYPE_SERIES = 'series'; | ||
const TYPE_EPISODE = 'episode'; | ||
|
||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* @param string $movie | ||
* @param string $type | ||
* @param int $year | ||
* @param bool $tomatoes | ||
* @param bool $longPlot | ||
* | ||
* @return Movie | ||
* | ||
* @throws ImdbException | ||
*/ | ||
public static function retrieve( | ||
$movie, $type = null, $year = null, $tomatoes = false, $longPlot = false | ||
) | ||
{ | ||
$params = [ | ||
'type' => $type, | ||
'y' => $year, | ||
'plot' => $longPlot ? 'full' : 'short', | ||
'tomatoes' => $tomatoes ? 'true' : 'false', | ||
]; | ||
|
||
if(self::isValidId($movie)) | ||
{ | ||
$params['i'] = $movie; | ||
} | ||
else | ||
{ | ||
$params['t'] = $movie; | ||
} | ||
|
||
$data = self::_get($params); | ||
|
||
return new Movie( | ||
[ | ||
'title' => $data['Title'], | ||
'year' => $data['Year'], | ||
'rated' => $data['Rated'], | ||
'released' => $data['Released'], | ||
'runtime' => $data['Runtime'], | ||
'genre' => $data['Genre'], | ||
'director' => $data['Director'], | ||
'writer' => $data['Writer'], | ||
'actors' => $data['Actors'], | ||
'plot' => $data['Plot'], | ||
'language' => $data['Language'], | ||
'country' => $data['Country'], | ||
'awards' => $data['Awards'], | ||
'poster' => $data['Poster'], | ||
'metascore' => $data['Metascore'], | ||
'imdbRating' => $data['imdbRating'], | ||
'imdbVotes' => $data['imdbVotes'], | ||
'imdbId' => $data['imdbID'], | ||
'type' => $data['Type'], | ||
'response' => $data['Response'], | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @param string $search | ||
* @param string $movieType | ||
* @param string $year | ||
* | ||
* @return Result[] | ||
* | ||
* @throws ImdbException | ||
*/ | ||
public static function search($search, $movieType = null, $year = null) | ||
{ | ||
$data = self::_get( | ||
[ | ||
's' => $search, | ||
'type' => $movieType, | ||
'y' => $year | ||
] | ||
); | ||
|
||
$return = []; | ||
foreach($data['Search'] as $result) | ||
{ | ||
$return[] = new Result( | ||
[ | ||
'title' => $result['Title'], | ||
'year' => $result['Year'], | ||
'imdbId' => $result['imdbID'], | ||
'movieType' => $result['Type'] | ||
] | ||
); | ||
} | ||
return $return; | ||
} | ||
|
||
/** | ||
* @param $string | ||
* | ||
* @return bool | ||
*/ | ||
private static function isValidId($string) | ||
{ | ||
return preg_match("/tt\\d{7}/", $string) > 0; | ||
} | ||
|
||
/** | ||
* @param array $params | ||
* | ||
* @return array | ||
* | ||
* @throws ImdbException | ||
*/ | ||
private static function _get($params) | ||
{ | ||
$params = array_filter($params); | ||
|
||
$params['r'] = 'json'; | ||
$params['v'] = '1'; | ||
|
||
$client = new Client(); | ||
$response = $client | ||
->get('http://www.omdbapi.com/', ['query' => $params]) | ||
->json(); | ||
|
||
if(isset($response['Response']) && $response['Response'] == 'False') | ||
{ | ||
throw new ImdbException($response['Error']); | ||
} | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
namespace Jleagle\Imdb\Responses; | ||
|
||
class AbstractResponse | ||
{ | ||
public function __construct(array $data) | ||
{ | ||
foreach($data as $field => $value) | ||
{ | ||
$this->$field = $value; | ||
} | ||
} | ||
|
||
public function toArray() | ||
{ | ||
return get_object_vars($this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
namespace Jleagle\Imdb\Responses; | ||
|
||
class Movie extends AbstractResponse | ||
{ | ||
public $title; | ||
public $year; | ||
public $rated; | ||
public $released; | ||
public $runtime; | ||
public $genre; | ||
public $director; | ||
public $writer; | ||
public $actors; | ||
public $plot; | ||
public $language; | ||
public $country; | ||
public $awards; | ||
public $poster; | ||
public $metascore; | ||
public $imdbRating; | ||
public $imdbVotes; | ||
public $imdbId; | ||
public $type; | ||
public $response; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
namespace Jleagle\Imdb\Responses; | ||
|
||
class Result extends AbstractResponse | ||
{ | ||
public $title; | ||
public $year; | ||
public $imdbId; | ||
public $movieType; | ||
} |