1
0
mirror of https://github.com/devfake/flox.git synced 2024-11-15 22:52:32 +01:00
flox/backend/app/Services/TMDB.php

426 lines
11 KiB
PHP
Raw Normal View History

2016-10-10 10:57:39 +02:00
<?php
namespace App\Services;
2016-10-13 11:11:10 +02:00
use App\Item;
use Carbon\Carbon;
2016-10-10 10:57:39 +02:00
use GuzzleHttp\Client;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use GuzzleHttp\Exception\ClientException;
2016-10-10 10:57:39 +02:00
class TMDB {
private $client;
private $apiKey;
2016-11-28 08:48:12 +01:00
private $translation;
2016-10-10 10:57:39 +02:00
private $base = 'https://api.themoviedb.org';
2016-10-10 10:57:39 +02:00
/**
* Get the API Key for TMDb and create an instance of Guzzle.
*
* @param Client $client
2016-10-10 10:57:39 +02:00
*/
public function __construct(Client $client)
2016-10-10 10:57:39 +02:00
{
$this->apiKey = config('services.tmdb.key');
2016-11-28 08:48:12 +01:00
$this->translation = config('app.TRANSLATION');
$this->client = $client;
2016-10-10 10:57:39 +02:00
}
/**
* Search TMDb by 'title'.
2016-10-10 10:57:39 +02:00
*
* @param $title
* @param null $mediaType
* @return Collection
2016-10-10 10:57:39 +02:00
*/
public function search($title, $mediaType = null)
2016-10-10 10:57:39 +02:00
{
$tv = collect();
$movies = collect();
if( ! $mediaType || $mediaType == 'tv') {
$response = $this->fetchSearch($title, 'tv');
$tv = collect($this->createItems($response, 'tv'));
}
if( ! $mediaType || $mediaType == 'movies') {
$response = $this->fetchSearch($title, 'movie');
$movies = collect($this->createItems($response, 'movie'));
}
2016-10-10 10:57:39 +02:00
return $movies->merge($tv)->sortByDesc('popularity')->values()->all();
}
private function fetchSearch($title, $mediaType) {
return $this->requestTmdb($this->base . '/3/search/' . $mediaType, [
'query' => $title,
]);
2016-10-10 10:57:39 +02:00
}
2016-10-13 11:11:10 +02:00
/**
* Search TMDb for recommendations and similar movies.
2016-10-13 11:11:10 +02:00
*
2016-11-28 08:48:12 +01:00
* @param $mediaType
* @param $tmdbId
2016-10-13 11:11:10 +02:00
* @return \Illuminate\Support\Collection
*/
public function suggestions($mediaType, $tmdbId)
2016-10-13 11:11:10 +02:00
{
$recommendations = $this->searchSuggestions($mediaType, $tmdbId, 'recommendations');
$similar = $this->searchSuggestions($mediaType, $tmdbId, 'similar');
2016-10-13 11:11:10 +02:00
$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'));
});
}
/**
2016-11-28 08:48:12 +01:00
* @param $mediaType
* @param $tmdbId
2016-10-13 11:11:10 +02:00
* @param $type
*
* @return Collection
2016-10-13 11:11:10 +02:00
*/
private function searchSuggestions($mediaType, $tmdbId, $type)
2016-10-13 11:11:10 +02:00
{
$response = $this->requestTmdb($this->base . '/3/' . $mediaType . '/' . $tmdbId . '/' . $type);
2016-11-28 08:48:12 +01:00
return collect($this->createItems($response, $mediaType));
2016-10-13 11:11:10 +02:00
}
2016-10-14 13:25:52 +02:00
/**
2017-04-11 14:42:36 +02:00
* Search TMDb for upcoming movies in our region.
2016-10-14 13:25:52 +02:00
*
* @return array
*/
public function upcoming()
2016-10-14 13:25:52 +02:00
{
2017-02-21 18:15:43 +01:00
$cache = Cache::remember('upcoming', $this->untilEndOfDay(), function() {
$region = getRegion($this->translation);
2017-04-11 14:42:36 +02:00
$response = $this->requestTmdb($this->base . '/3/movie/upcoming', [
'region' => $region,
]);
2016-10-14 13:25:52 +02:00
2017-02-21 18:15:43 +01:00
return collect($this->createItems($response, 'movie'));
});
2017-02-21 18:15:43 +01:00
return $this->filterItems($cache);
}
/**
* Search TMDb for current playing movies in our region.
*
* @return array
*/
public function nowPlaying()
{
$cache = Cache::remember('current', $this->untilEndOfDay(), function() {
$region = getRegion($this->translation);
$response = $this->requestTmdb($this->base . '/3/movie/now_playing', [
'region' => $region,
]);
return collect($this->createItems($response, 'movie'));
});
return $this->filterItems($cache);
}
/**
* Search TMDb for current popular movies and tv shows.
*
* @return array
*/
public function trending()
{
2017-02-21 18:15:43 +01:00
$cache = Cache::remember('trending', $this->untilEndOfDay(), function() {
$responseMovies = $this->fetchPopular('movie');
$responseTv = $this->fetchPopular('tv');
$tv = collect($this->createItems($responseTv, 'tv'));
$movies = collect($this->createItems($responseMovies, 'movie'));
2017-02-21 18:15:43 +01:00
return $tv->merge($movies)->shuffle();
});
2017-02-21 18:15:43 +01:00
return $this->filterItems($cache);
}
/**
* Merge the response with items from our database.
*
* @param Collection $items
* @return array
*/
private function filterItems($items)
{
$allId = $items->pluck('tmdb_id');
2016-10-14 13:25:52 +02:00
// Get all movies / tv shows from trendig / upcoming which already in database.
$inDB = Item::whereIn('tmdb_id', $allId)->withCount('episodesWithSrc')->get()->toArray();
2016-10-14 13:25:52 +02:00
// Remove all inDB movies / tv shows from trending / upcoming.
2016-10-14 13:25:52 +02:00
$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());
}
private function fetchPopular($mediaType)
{
return $this->requestTmdb($this->base . '/3/' . $mediaType . '/popular');
}
2016-10-13 11:11:10 +02:00
/**
* @param $response
* @param $mediaType
2016-10-13 11:11:10 +02:00
* @return array
*/
private function createItems($response, $mediaType)
2016-10-13 11:11:10 +02:00
{
$items = [];
$response = json_decode($response->getBody());
foreach($response->results as $result) {
$items[] = $this->createItem($result, $mediaType);
2016-10-13 11:11:10 +02:00
}
return $items;
}
public function createItem($data, $mediaType)
{
$release = Carbon::createFromFormat('Y-m-d',
isset($data->release_date) ? ($data->release_date ?: '1970-12-1') : ($data->first_air_date ?: '1970-12-1')
);
$title = $data->name ?? $data->title;
return [
'tmdb_id' => $data->id,
'title' => $title,
'slug' => getSlug($title),
'original_title' => $data->original_name ?? $data->original_title,
'poster' => $data->poster_path,
'media_type' => $mediaType,
'released' => $release->getTimestamp(),
'genre' => $this->parseGenre($data->genre_ids),
'episodes' => [],
'overview' => $data->overview,
'backdrop' => $data->backdrop_path,
'tmdb_rating' => $data->vote_average,
'popularity' => $data->popularity,
];
}
private function requestTmdb($url, $query = [])
{
$query = array_merge([
'api_key' => $this->apiKey,
'language' => strtolower($this->translation)
], $query);
try {
$response = $this->client->get($url, [
'query' => $query
]);
if($this->hasLimitRemaining($response)) {
return $response;
}
} catch (ClientException $e) {
// wtf? throws exception because of "bad" statuscode?
$response = $e->getResponse();
if($this->hasLimitRemaining($response)) {
return $response;
}
}
sleep(1);
return $this->requestTmdb($url, $query);
}
2016-10-18 10:54:10 +02:00
/**
* Get full movie or tv details with trailers.
2016-10-18 10:54:10 +02:00
*
* @param $tmdbId
* @param $mediaType
2016-10-18 10:54:10 +02:00
* @return mixed
*/
public function details($tmdbId, $mediaType)
2016-10-18 10:54:10 +02:00
{
$response = $this->requestTmdb($this->base . '/3/' . $mediaType . '/' . $tmdbId, [
'append_to_response' => 'videos,external_ids',
]);
return json_decode($response->getBody());
}
public function videos($tmdbId, $mediaType, $translation = null)
{
$response = $this->requestTmdb($this->base . '/3/' . $mediaType . '/' . $tmdbId . '/videos', [
'language' => $translation ?? $this->translation,
]);
return json_decode($response->getBody());
2016-10-18 10:54:10 +02:00
}
2016-11-28 08:48:12 +01:00
/**
* Get current count of seasons.
*
* @param $id
* @param $mediaType
2016-11-28 08:48:12 +01:00
* @return integer | null
*/
private function tvSeasonsCount($id, $mediaType)
{
if($mediaType == 'tv') {
$response = $this->requestTmdb($this->base . '/3/tv/' . $id);
2016-11-28 08:48:12 +01:00
$seasons = collect(json_decode($response->getBody())->seasons);
return $seasons->filter(function ($season) {
// We don't need pilots
return $season->season_number > 0;
})->count();
}
return null;
}
/**
* Get all episodes of each season.
*
* @param $tmdbId
2016-11-28 08:48:12 +01:00
* @return array
*/
public function tvEpisodes($tmdbId)
2016-11-28 08:48:12 +01:00
{
$seasons = $this->tvSeasonsCount($tmdbId, 'tv');
2016-11-28 08:48:12 +01:00
$data = [];
for($i = 1; $i <= $seasons; $i++) {
$response = $this->requestTmdb($this->base . '/3/tv/' . $tmdbId . '/season/' . $i);
2016-11-28 08:48:12 +01:00
$data[$i] = json_decode($response->getBody());
}
return $data;
}
/**
* Make a new request to TMDb to get the alternative titles.
*
* @param $item
* @return array
*/
public function getAlternativeTitles($item)
{
$response = $this->fetchAlternativeTitles($item);
$body = json_decode($response->getBody());
if(property_exists($body, 'titles') || property_exists($body, 'results')) {
return $body->titles ?? $body->results;
}
return [];
}
public function fetchAlternativeTitles($item)
{
return $this->requestTmdb($this->base . '/3/' . $item['media_type'] . '/' . $item['tmdb_id'] . '/alternative_titles');
}
/**
* 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',
12 => 'Adventure',
16 => 'Animation',
35 => 'Comedy',
80 => 'Crime',
99 => 'Documentary',
18 => 'Drama',
10751 => 'Family',
14 => 'Fantasy',
36 => 'History',
27 => 'Horror',
10402 => 'Music',
9648 => 'Mystery',
10749 => 'Romance',
878 => 'Sci-Fi',
10770 => 'TV Movie',
53 => 'Thriller',
10752 => 'War',
37 => 'Western',
2016-11-24 08:52:14 +01:00
10759 => 'Action & Adventure',
10762 => 'Kids',
10763 => 'News',
10764 => 'Reality',
10765 => 'Sci-Fi & Fantasy',
10766 => 'Soap',
10767 => 'Talk',
10768 => 'War & Politics',
];
}
/**
* @param $response
* @return boolean
*/
public function hasLimitRemaining($response)
{
if($response->getStatusCode() == 429) {
return false;
}
return ((int) $response->getHeader('X-RateLimit-Remaining')[0]) > 1;
}
/**
* @return float|int
*/
private function untilEndOfDay()
{
return Carbon::now()->secondsUntilEndOfDay() / 60;
}
2017-01-24 16:11:47 +01:00
}