2016-10-10 10:57:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2016-10-13 11:11:10 +02:00
|
|
|
use App\Item;
|
2016-10-10 10:57:39 +02:00
|
|
|
use DateTime;
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
|
|
|
class TMDB {
|
|
|
|
|
|
|
|
private $client;
|
|
|
|
private $apiKey;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the API Key for TMDB and create an instance of Guzzle.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->apiKey = config('app.TMDB_API_KEY');
|
|
|
|
$this->client = new Client(['base_uri' => 'http://api.themoviedb.org/']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search TMDB for an movie by 'title'.
|
|
|
|
*
|
|
|
|
* @param $title
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function search($title)
|
|
|
|
{
|
|
|
|
$response = $this->client->get('/3/search/movie', ['query' => ['api_key' => $this->apiKey, 'query' => $title]]);
|
|
|
|
|
2016-10-18 09:57:47 +02:00
|
|
|
return $this->createItems($response);
|
2016-10-10 10:57:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a new request to TMDB to get the 'alternative language' movie title.
|
|
|
|
*
|
|
|
|
* @param $tmdb_id
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function alternativeMovieTitle($tmdb_id)
|
|
|
|
{
|
|
|
|
$alternativeLanguage = config('app.ALTERNATIVE_LANGUAGE');
|
|
|
|
|
|
|
|
$response = $this->client->get('/3/movie/' . $tmdb_id . '/alternative_titles', ['query' => ['api_key' => $this->apiKey]]);
|
|
|
|
$titles = collect(json_decode($response->getBody())->titles);
|
|
|
|
|
|
|
|
$title = $titles->where('iso_3166_1', $alternativeLanguage);
|
|
|
|
|
|
|
|
if($title->isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// '3D' is often used in the title. We don't need them.
|
|
|
|
return trim(str_replace('3D', '', $title->first()->title));
|
|
|
|
}
|
2016-10-13 11:11:10 +02:00
|
|
|
|
2016-10-14 12:15:54 +02:00
|
|
|
public function trending()
|
|
|
|
{
|
2016-10-14 13:25:52 +02:00
|
|
|
return $this->searchTrendingOrUpcoming('popular');
|
|
|
|
}
|
2016-10-14 12:15:54 +02:00
|
|
|
|
2016-10-14 13:25:52 +02:00
|
|
|
public function upcoming()
|
|
|
|
{
|
|
|
|
return $this->searchTrendingOrUpcoming('upcoming');
|
2016-10-14 12:15:54 +02:00
|
|
|
}
|
|
|
|
|
2016-10-13 11:11:10 +02:00
|
|
|
/**
|
|
|
|
* Search TMDB for recommendations and similar movies.
|
|
|
|
*
|
|
|
|
* @param $tmdbID
|
|
|
|
* @return \Illuminate\Support\Collection
|
|
|
|
*/
|
|
|
|
public function suggestions($tmdbID)
|
|
|
|
{
|
|
|
|
$recommendations = $this->searchSuggestions($tmdbID, 'recommendations');
|
|
|
|
$similar = $this->searchSuggestions($tmdbID, 'similar');
|
|
|
|
|
|
|
|
$items = $recommendations->merge($similar);
|
|
|
|
|
|
|
|
$inDB = Item::all('tmdb_id')->toArray();
|
|
|
|
|
|
|
|
// Remove movies which already are in database.
|
|
|
|
return $items->filter(function($item) use ($inDB) {
|
|
|
|
return ! in_array($item['tmdb_id'], array_column($inDB, 'tmdb_id'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $tmdbID
|
|
|
|
* @param $type
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Support\Collection
|
|
|
|
*/
|
|
|
|
private function searchSuggestions($tmdbID, $type)
|
|
|
|
{
|
|
|
|
$response = $this->client->get('/3/movie/' . $tmdbID . '/' . $type, ['query' => ['api_key' => $this->apiKey]]);
|
2016-10-18 09:57:47 +02:00
|
|
|
|
2016-10-13 11:11:10 +02:00
|
|
|
return collect($this->createItems($response));
|
|
|
|
}
|
|
|
|
|
2016-10-14 13:25:52 +02:00
|
|
|
/**
|
2016-10-14 14:52:04 +02:00
|
|
|
* Search TMDB for current popular or upcoming movies.
|
2016-10-14 13:25:52 +02:00
|
|
|
*
|
|
|
|
* @param $type
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function searchTrendingOrUpcoming($type)
|
|
|
|
{
|
|
|
|
$response = $this->client->get('/3/movie/' . $type, ['query' => ['api_key' => $this->apiKey]]);
|
|
|
|
|
|
|
|
$items = collect($this->createItems($response));
|
|
|
|
$allID = $items->pluck('tmdb_id');
|
|
|
|
|
|
|
|
// Get all movies from trendig which already in database.
|
|
|
|
$inDB = Item::whereIn('tmdb_id', $allID)->get()->toArray();
|
|
|
|
|
|
|
|
// Remove all inDB movies from trending.
|
|
|
|
$filtered = $items->filter(function($item) use ($inDB) {
|
|
|
|
return ! in_array($item['tmdb_id'], array_column($inDB, 'tmdb_id'));
|
|
|
|
});
|
|
|
|
|
|
|
|
$merged = $filtered->merge($inDB);
|
|
|
|
|
2016-10-14 14:52:04 +02:00
|
|
|
// Reset array keys to display inDB items first.
|
2016-10-14 13:25:52 +02:00
|
|
|
return array_values($merged->reverse()->toArray());
|
|
|
|
}
|
|
|
|
|
2016-10-13 11:11:10 +02:00
|
|
|
/**
|
|
|
|
* @param $response
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function createItems($response)
|
|
|
|
{
|
|
|
|
$items = [];
|
|
|
|
$response = json_decode($response->getBody());
|
|
|
|
|
|
|
|
foreach($response->results as $result) {
|
|
|
|
$dtime = DateTime::createFromFormat('Y-m-d', ($result->release_date ?: '1970-12-1'));
|
|
|
|
$items[] = [
|
|
|
|
'tmdb_id' => $result->id,
|
|
|
|
'title' => $result->title,
|
|
|
|
'poster' => $result->poster_path,
|
|
|
|
'released' => $dtime->getTimestamp(),
|
2016-10-17 10:57:42 +02:00
|
|
|
'genre' => $this->parseGenre($result->genre_ids),
|
2016-10-13 11:11:10 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
}
|
2016-10-17 10:57:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create genre string from genre_ids.
|
|
|
|
*
|
|
|
|
* @param $ids
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function parseGenre($ids)
|
|
|
|
{
|
|
|
|
$genre = [];
|
|
|
|
|
|
|
|
foreach($ids as $id) {
|
|
|
|
$genre[] = $this->genreList()[$id];
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode($genre, ', ');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current genre list from TMDb.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function genreList()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
28 => 'Action',
|
2016-10-18 09:57:47 +02:00
|
|
|
12 => 'Adventure',
|
|
|
|
16 => 'Animation',
|
|
|
|
35 => 'Comedy',
|
2016-10-17 10:57:42 +02:00
|
|
|
80 => 'Crime',
|
2016-10-18 09:57:47 +02:00
|
|
|
99 => 'Documentary',
|
|
|
|
18 => 'Drama',
|
2016-10-17 10:57:42 +02:00
|
|
|
10751 => 'Family',
|
2016-10-18 09:57:47 +02:00
|
|
|
14 => 'Fantasy',
|
2016-10-17 10:57:42 +02:00
|
|
|
36 => 'History',
|
|
|
|
27 => 'Horror',
|
|
|
|
10402 => 'Music',
|
|
|
|
9648 => 'Mystery',
|
|
|
|
10749 => 'Romance',
|
2016-10-18 09:57:47 +02:00
|
|
|
878 => 'Sci-Fi',
|
|
|
|
10770 => 'TV Movie',
|
2016-10-17 10:57:42 +02:00
|
|
|
53 => 'Thriller',
|
|
|
|
10752 => 'War',
|
|
|
|
37 => 'Western',
|
|
|
|
];
|
|
|
|
}
|
2016-10-10 10:57:39 +02:00
|
|
|
}
|