some progress
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\MediaProcessing;
|
||||
|
||||
use App\Models\ItemToFetch;
|
||||
|
||||
class FactoryService
|
||||
{
|
||||
private MovieProcessingService $movieProcessingService;
|
||||
private ShowProcessingService $showProcessingService;
|
||||
|
||||
public function __construct(
|
||||
MovieProcessingService $movieProcessingService,
|
||||
ShowProcessingService $showProcessingService
|
||||
) {
|
||||
$this->movieProcessingService = $movieProcessingService;
|
||||
$this->showProcessingService = $showProcessingService;
|
||||
}
|
||||
|
||||
public function process($item, string $job)
|
||||
{
|
||||
switch ($item->type) {
|
||||
case ItemToFetch::TYPE_MOVIE:
|
||||
return $this->movieProcessingService->process($item, $job);
|
||||
case ItemToFetch::TYPE_TV_SERIES:
|
||||
return $this->showProcessingService->process($item, $job);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\MediaProcessing;
|
||||
|
||||
use App\Models\Genre;
|
||||
use App\Models\ItemToFetchJobs;
|
||||
use App\Models\Movie;
|
||||
use App\Models\MovieCollection;
|
||||
use App\Service\TMDBApi\MoviesApi;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class MovieProcessingService implements ProcessingInterface
|
||||
{
|
||||
private MoviesApi $moviesApi;
|
||||
|
||||
public function __construct(MoviesApi $moviesApi)
|
||||
{
|
||||
$this->moviesApi = $moviesApi;
|
||||
}
|
||||
|
||||
public function process($item, $job)
|
||||
{
|
||||
$methodName = "get".ucfirst($job);
|
||||
|
||||
if (!method_exists($this, $methodName)) {
|
||||
// @todo maybe raise an exception here
|
||||
return;
|
||||
}
|
||||
|
||||
$this->$methodName($item);
|
||||
}
|
||||
|
||||
private function getDetails($item)
|
||||
{
|
||||
$tmdbMovie = $this->moviesApi->getMovie($item->tmdb_id);
|
||||
$tmdbMovie['tmdb_id'] = $tmdbMovie['id'];
|
||||
|
||||
$movie = Movie::where('tmdb_id', $tmdbMovie['tmdb_id'])->first();
|
||||
|
||||
if($movie) {
|
||||
return;
|
||||
}
|
||||
|
||||
$tmdbMovie['slug'] = Str::slug($tmdbMovie['title']);
|
||||
|
||||
$movieId = Movie::create($tmdbMovie);
|
||||
|
||||
if (!empty($tmdbMovie['belongs_to_collection'])) {
|
||||
$this->processMovieCollection($movieId->id, $tmdbMovie['belongs_to_collection']);
|
||||
}
|
||||
|
||||
if (!empty($tmdbMovie['genres'])) {
|
||||
$this->processMovieGenres($movieId->id, $tmdbMovie['genres']);
|
||||
}
|
||||
|
||||
$this->removeElementFromRemainingJobs($tmdbMovie['id'], 'Details');
|
||||
}
|
||||
|
||||
private function getAlternativeTitles($item)
|
||||
{
|
||||
$alternativeTitles = $this->moviesApi->getAlternativeTitles($item->tmdb_id);
|
||||
|
||||
if (empty($alternativeTitles) || !isset($alternativeTitles['titles'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$alternativeTitleModel = new MovieAlternativeTitle();
|
||||
|
||||
$movieModel = new Movie();
|
||||
$movie = $movieModel->getForTmdbId($item->tmdb_id);
|
||||
|
||||
foreach ($alternativeTitles['titles'] as $alternativeTitle) {
|
||||
$alternativeTitleExists = $alternativeTitleModel->where('movie_id', $movie->id)
|
||||
->where('iso', $alternativeTitle['iso_3166_1'])
|
||||
->first();
|
||||
|
||||
if (!$alternativeTitleExists) {
|
||||
$alternativeTitleModel->insert([
|
||||
'iso' => $alternativeTitle['iso_3166_1'],
|
||||
'movie_id' => $movie->id,
|
||||
'title' => $alternativeTitle['title'],
|
||||
'type' => $alternativeTitle['type'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->removeElementFromRemainingJobs($item->tmdb_id, ItemJob::JOB_ALTERNVATIVE_TITLES);
|
||||
}
|
||||
|
||||
private function processMovieCollection(int $movieId, $belongsToCollection)
|
||||
{
|
||||
$movieCollection = MovieCollection::where('tmdb_id', $belongsToCollection['id'])->first();
|
||||
|
||||
if (!$movieCollection) {
|
||||
$movieCollection = MovieCollection::create([
|
||||
'name' => $belongsToCollection['name'],
|
||||
'slug' => Str::slug($belongsToCollection['name']),
|
||||
'poster_path' => $belongsToCollection['poster_path'],
|
||||
'backdrop_path' => $belongsToCollection['backdrop_path'],
|
||||
'tmdb_id' => $belongsToCollection['id'],
|
||||
]);
|
||||
}
|
||||
|
||||
$movie = Movie::find($movieId);
|
||||
|
||||
if ($movie->whereHas('collections', function ($query) use ($movieCollection) {
|
||||
$query->where('tmdb_id', '=', $movieCollection->tmdb_id);
|
||||
})->exists()) {
|
||||
echo "Movie is in the collection";
|
||||
return;
|
||||
} else {
|
||||
echo "Movie is not in the collection";
|
||||
$movie->collections()->attach($movieCollection);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function processMovieGenres(int $movieId, array $tmdbGenres)
|
||||
{
|
||||
foreach ($tmdbGenres as $tmdbGenre) {
|
||||
$genre = Genre::where('name', $tmdbGenre['name'])
|
||||
->where('tmdb_id', $tmdbGenre['id'])
|
||||
->first();
|
||||
|
||||
if (!$genre) {
|
||||
$genre = Genre::create([
|
||||
'name' => $tmdbGenre['name'],
|
||||
'slug' => Str::slug($tmdbGenre['name']),
|
||||
'tmdb_id' => $tmdbGenre['id'],
|
||||
'type' => Genre::TYPE_MOVIE,
|
||||
]);
|
||||
}
|
||||
|
||||
$movie = Movie::find($movieId);
|
||||
|
||||
if ($movie->whereHas('genres', function ($query) use ($genre) {
|
||||
$query->where('tmdb_id', '=', $genre->tmdb_id);
|
||||
})->exists()) {
|
||||
echo "Movie is in the genre";
|
||||
return;
|
||||
} else {
|
||||
echo "Movie is not in the genre";
|
||||
$movie->genres()->attach($genre);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function removeElementFromRemainingJobs(int $tmdbId, string $job)
|
||||
{
|
||||
$itemToFetchJobs = ItemToFetchJobs::where('tmdb_id', $tmdbId)->first();
|
||||
|
||||
$remainingJobs = $itemToFetchJobs->remaining_jobs;
|
||||
$remainingJobKey = array_search($job, $remainingJobs);
|
||||
|
||||
unset($remainingJobs[$remainingJobKey]);
|
||||
|
||||
if (empty($remainingJobs)) {
|
||||
$itemToFetchJobs->delete();
|
||||
}
|
||||
|
||||
$itemToFetchJobs->remaining_jobs = $remainingJobs;
|
||||
$itemToFetchJobs->save();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\MediaProcessing;
|
||||
|
||||
interface ProcessingInterface
|
||||
{
|
||||
public function process($item, $job);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\MediaProcessing;
|
||||
|
||||
class ShowProcessingService implements ProcessingInterface
|
||||
{
|
||||
|
||||
public function process($item, $job)
|
||||
{
|
||||
// TODO: Implement process() method.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\TMDBApi;
|
||||
|
||||
use CodeIgniter\HTTP\CURLRequest;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
abstract class AbstractApiService
|
||||
{
|
||||
const BASE_URL = 'https://api.themoviedb.org/3/';
|
||||
|
||||
/** @var CURLRequest */
|
||||
protected $client;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// new guzzle client
|
||||
// $this->client = service('curlrequest', options: [
|
||||
// 'timeout' => 10.0,
|
||||
// 'connect_timeout' => 150,
|
||||
// 'debug' => false,
|
||||
// 'verify' => false,
|
||||
// ]);
|
||||
}
|
||||
|
||||
public function get(string $path, array $parameters = [], array $headers = []): array
|
||||
{
|
||||
$path = self::BASE_URL.$path.'?api_key=49a4c5ff00d832668aa505feaf197955';
|
||||
|
||||
$headers['Authorization'] = 'Bearer 49a4c5ff00d832668aa505feaf197955';
|
||||
$headers['accept'] = 'application/json';
|
||||
|
||||
try {
|
||||
$r = Http::withHeaders($headers)->get($path);
|
||||
// $r = $this->client->request('GET', $path, [
|
||||
// 'headers' => $headers
|
||||
// ]);
|
||||
} catch (\Throwable $e) {
|
||||
var_dump($e->getMessage());
|
||||
die;
|
||||
}
|
||||
|
||||
|
||||
return $this->decodeResponse(
|
||||
$r
|
||||
);
|
||||
}
|
||||
|
||||
private function decodeResponse($response)
|
||||
{
|
||||
try {
|
||||
if ($response->body()) {
|
||||
return json_decode($response->body(), true, 512, JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
return [];
|
||||
} catch (JsonException $e) {
|
||||
throw new UnexpectedResponseException(
|
||||
sprintf(
|
||||
'Unable to decode response with body "%s", %s.',
|
||||
(string)$response->body(),
|
||||
json_last_error_msg()
|
||||
),
|
||||
$response->status(),
|
||||
$e
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,300 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\TMDBApi;
|
||||
|
||||
class MoviesApi extends AbstractApiService
|
||||
{
|
||||
/**
|
||||
* Get the basic movie information for a specific movie id.
|
||||
*
|
||||
* @param $movieId
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getMovie($movieId, array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/' . $movieId, $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the alternative titles for a specific movie id.
|
||||
*
|
||||
* @param $movieId
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAlternativeTitles($movieId, array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/' . $movieId . '/alternative_titles', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the cast and crew information for a specific movie id.
|
||||
*
|
||||
* @param $movieId
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCredits($movieId, array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/' . $movieId . '/credits', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the images (posters and backdrops) for a specific movie id.
|
||||
*
|
||||
* @param $movieId
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getImages($movieId, array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/' . $movieId . '/images', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the plot keywords for a specific movie id.
|
||||
*
|
||||
* @param $movieId
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getKeywords($movieId, array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/' . $movieId . '/keywords', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the release date by country for a specific movie id.
|
||||
*
|
||||
* @param $movieId
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getReleases($movieId, array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/' . $movieId . '/releases', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the trailers for a specific movie id.
|
||||
*
|
||||
* @param $movieId
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
* @deprecated TMDB changed the way of requesting trailers, see getVideos instead!
|
||||
*/
|
||||
public function getTrailers($movieId, array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/' . $movieId . '/trailers', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the translations for a specific movie id.
|
||||
*
|
||||
* @param $movieId
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getTranslations($movieId, array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/' . $movieId . '/translations', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the similar movies for a specific movie id.
|
||||
*
|
||||
* @param $movieId
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSimilar($movieId, array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/' . $movieId . '/similar', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the recommended movies for a specific movie id.
|
||||
*
|
||||
* @param $movieId
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getRecommendations($movieId, array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/' . $movieId . '/recommendations', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the reviews for a particular movie id.
|
||||
*
|
||||
* @param $movieId
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getReviews($movieId, array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/' . $movieId . '/reviews', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the lists that the movie belongs to.
|
||||
*
|
||||
* @param $movieId
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLists($movieId, array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/' . $movieId . '/lists', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the changes for a specific movie id.
|
||||
*
|
||||
* @param $movieId
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getChanges($movieId, array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/' . $movieId . '/changes', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latest movie id.
|
||||
*
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLatest(array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/latest', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of upcoming movies. This list refreshes every day.
|
||||
* The maximum number of items this list will include is 100.
|
||||
*
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getUpcoming(array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/upcoming', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of movies playing in theatres. This list refreshes every day.
|
||||
* The maximum number of items this list will include is 100.
|
||||
*
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getNowPlaying(array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/now_playing', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of popular movies on The Movie Database.
|
||||
* This list refreshes every day.
|
||||
*
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPopular(array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/popular', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of top rated movies. By default, this list will only include
|
||||
* movies that have 10 or more votes. This list refreshes every day.
|
||||
*
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getTopRated(array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/top_rated', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the videos (trailers, teasers, clips, etc...) for a specific movie id.
|
||||
*
|
||||
* @param $movieId
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getVideos($movieId, array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/' . $movieId . '/videos', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the watch providers (by region) for a specific movie id.
|
||||
*
|
||||
* @param $movieId
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getWatchProviders($movieId, array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/' . $movieId . '/watch/providers', $parameters, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the external ids that we have stored for a movie.
|
||||
*
|
||||
* @param $movieId
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
*/
|
||||
public function getExternalIds($movieId, array $parameters = [], array $headers = [])
|
||||
{
|
||||
sleep(1);
|
||||
return $this->get('movie/' . $movieId . '/external_ids', $parameters, $headers);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user