some progress
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands\ItemProcessing;
|
||||
|
||||
use App\Models\ItemToFetch;
|
||||
use App\Service\MediaProcessing\FactoryService;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class FetchItemDataCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'processing:fetch-item-data {type=movie} {batch=50} {job=Details}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Command description';
|
||||
private FactoryService $factoryService;
|
||||
|
||||
public function __construct(FactoryService $factoryService)
|
||||
{
|
||||
$this->factoryService = $factoryService;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$type = $this->argument('type');
|
||||
$batch = $this->argument('batch');
|
||||
$job = $this->argument('job');
|
||||
|
||||
$items = $this->getBatch($type, $batch, $job);
|
||||
|
||||
$this->processItems($items, $job);
|
||||
}
|
||||
|
||||
private function getBatch(string $type, int $batch, string $job)
|
||||
{
|
||||
return ItemToFetch::has('jobs')
|
||||
->with([
|
||||
'jobs' => fn ($jobs) => array_search($job, $jobs->first()->remaining_jobs, true),
|
||||
])
|
||||
->where('type', $type)
|
||||
->take($batch)
|
||||
->get();
|
||||
}
|
||||
|
||||
private function processItems(\Illuminate\Database\Eloquent\Collection $items, $job)
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
$this->processItem($item, $job);
|
||||
}
|
||||
}
|
||||
|
||||
private function processItem($item, $job)
|
||||
{
|
||||
$this->factoryService->process($item, $job);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands\TmdbFetch;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class FetchExportCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'tmdb:fetch-export {type=movies}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Fetch TMDB Export file.';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$type = $this->argument('type');
|
||||
|
||||
$url = $this->prepareExportUrlBy($type);
|
||||
|
||||
$date = $this->getDate();
|
||||
|
||||
$path = storage_path('/'.$type."_".$date.".json.gz");
|
||||
|
||||
if (!file_exists($path)) {
|
||||
$this->downloadFile($url, $path);
|
||||
}
|
||||
}
|
||||
|
||||
private function getDate(): string
|
||||
{
|
||||
return date('m_d_Y',strtotime("-1 days"));
|
||||
}
|
||||
|
||||
private function prepareExportUrlBy(string $type): string
|
||||
{
|
||||
$date = $this->getDate();
|
||||
|
||||
if ($type === 'tv_series'){
|
||||
return 'https://files.tmdb.org/p/exports/tv_series_ids_'.$date.'.json.gz';
|
||||
}
|
||||
|
||||
return 'https://files.tmdb.org/p/exports/movie_ids_'.$date.'.json.gz';
|
||||
}
|
||||
|
||||
private function downloadFile($url, $path): void
|
||||
{
|
||||
$newFileName = $path;
|
||||
$file = fopen ($url, 'rb');
|
||||
$newFile = null;
|
||||
if ($file) {
|
||||
$newFile = fopen ($newFileName, 'wb');
|
||||
if ($newFile) {
|
||||
while(!feof($file)) {
|
||||
fwrite($newFile, fread($file, 1024 * 8), 1024 * 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($file) {
|
||||
fclose($file);
|
||||
}
|
||||
if ($newFile) {
|
||||
fclose($newFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands\TmdbFetch;
|
||||
|
||||
use App\Models\ItemToFetch;
|
||||
use App\Models\ItemToFetchJobs;
|
||||
use App\Models\Movie;
|
||||
use App\Models\Show;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class FetchExportItemsCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'tmdb:fetch-export-items {type=movie}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Fetch TMDB items from export file. Types: movie/show';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$type = $this->argument('type');
|
||||
$file = $this->getFileFor($type);
|
||||
|
||||
$gzOpenedFile = gzopen($file, "r");
|
||||
|
||||
$this->processItems($gzOpenedFile, $type);
|
||||
}
|
||||
|
||||
private function getFileFor(string $type)
|
||||
{
|
||||
$path = storage_path('/'.$type."*");
|
||||
$file = glob($path);
|
||||
|
||||
return reset($file);
|
||||
}
|
||||
|
||||
private function processItems($gzOpenedFile, string $type)
|
||||
{
|
||||
while ($line = fgets($gzOpenedFile)) {
|
||||
$decodedLine = json_decode($line, true);
|
||||
if (null === $decodedLine) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->processItem($decodedLine, $type);
|
||||
}
|
||||
}
|
||||
|
||||
private function processItem($decodedLine, string $type)
|
||||
{
|
||||
$itemToFetch = ItemToFetch::where('tmdb_id', $decodedLine['id'])->where('type', $type)->first();
|
||||
|
||||
if ($itemToFetch) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($type === ItemToFetch::TYPE_MOVIE) {
|
||||
$movie = Movie::where('tmdb_id', $decodedLine['id'])->first();
|
||||
if ($movie) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
$show = Show::where('tmdb_id', $decodedLine['id'])->first();
|
||||
if ($show) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ItemToFetch::create([
|
||||
'tmdb_id' => $decodedLine['id'],
|
||||
'type' => $type,
|
||||
]);
|
||||
|
||||
$jobs = ItemToFetchJobs::JOBS;
|
||||
$jobs[] = "Details";
|
||||
|
||||
ItemToFetchJobs::create([
|
||||
'tmdb_id' => $decodedLine['id'],
|
||||
'type' => $type,
|
||||
'remaining_jobs' => json_encode($jobs),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Foundation\Auth\ConfirmsPasswords;
|
||||
|
||||
class ConfirmPasswordController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Confirm Password Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller is responsible for handling password confirmations and
|
||||
| uses a simple trait to include the behavior. You're free to explore
|
||||
| this trait and override any functions that require customization.
|
||||
|
|
||||
*/
|
||||
|
||||
use ConfirmsPasswords;
|
||||
|
||||
/**
|
||||
* Where to redirect users when the intended url fails.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirectTo = '/home';
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
|
||||
|
||||
class ForgotPasswordController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Password Reset Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller is responsible for handling password reset emails and
|
||||
| includes a trait which assists in sending these notifications from
|
||||
| your application to your users. Feel free to explore this trait.
|
||||
|
|
||||
*/
|
||||
|
||||
use SendsPasswordResetEmails;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
||||
|
||||
class LoginController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Login Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller handles authenticating users for the application and
|
||||
| redirecting them to your home screen. The controller uses a trait
|
||||
| to conveniently provide its functionality to your applications.
|
||||
|
|
||||
*/
|
||||
|
||||
use AuthenticatesUsers;
|
||||
|
||||
/**
|
||||
* Where to redirect users after login.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirectTo = '/home';
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('guest')->except('logout');
|
||||
$this->middleware('auth')->only('logout');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Auth\RegistersUsers;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class RegisterController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Register Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller handles the registration of new users as well as their
|
||||
| validation and creation. By default this controller uses a trait to
|
||||
| provide this functionality without requiring any additional code.
|
||||
|
|
||||
*/
|
||||
|
||||
use RegistersUsers;
|
||||
|
||||
/**
|
||||
* Where to redirect users after registration.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirectTo = '/home';
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('guest');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a validator for an incoming registration request.
|
||||
*
|
||||
* @param array $data
|
||||
* @return \Illuminate\Contracts\Validation\Validator
|
||||
*/
|
||||
protected function validator(array $data)
|
||||
{
|
||||
return Validator::make($data, [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
|
||||
'password' => ['required', 'string', 'min:8', 'confirmed'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new user instance after a valid registration.
|
||||
*
|
||||
* @param array $data
|
||||
* @return \App\Models\User
|
||||
*/
|
||||
protected function create(array $data)
|
||||
{
|
||||
return User::create([
|
||||
'name' => $data['name'],
|
||||
'email' => $data['email'],
|
||||
'password' => Hash::make($data['password']),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Foundation\Auth\ResetsPasswords;
|
||||
|
||||
class ResetPasswordController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Password Reset Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller is responsible for handling password reset requests
|
||||
| and uses a simple trait to include this behavior. You're free to
|
||||
| explore this trait and override any methods you wish to tweak.
|
||||
|
|
||||
*/
|
||||
|
||||
use ResetsPasswords;
|
||||
|
||||
/**
|
||||
* Where to redirect users after resetting their password.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirectTo = '/home';
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Foundation\Auth\VerifiesEmails;
|
||||
|
||||
class VerificationController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Email Verification Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller is responsible for handling email verification for any
|
||||
| user that recently registered with the application. Emails may also
|
||||
| be re-sent if the user didn't receive the original email message.
|
||||
|
|
||||
*/
|
||||
|
||||
use VerifiesEmails;
|
||||
|
||||
/**
|
||||
* Where to redirect users after verification.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirectTo = '/home';
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
$this->middleware('signed')->only('verify');
|
||||
$this->middleware('throttle:6,1')->only('verify', 'resend');
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,11 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
abstract class Controller
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
|
||||
class Controller extends BaseController
|
||||
{
|
||||
//
|
||||
use AuthorizesRequests, ValidatesRequests;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the application dashboard.
|
||||
*
|
||||
* @return \Illuminate\Contracts\Support\Renderable
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return view('home');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Genre extends Model
|
||||
{
|
||||
public const TYPE_MOVIE = 'movie';
|
||||
public const TYPE_SHOW = 'show';
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'tmdb_id',
|
||||
'type',
|
||||
];
|
||||
|
||||
public function movies() {
|
||||
return $this->belongsToMany(Movie::class, 'movie_genres', 'genre_id', 'movie_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ItemToFetch extends Model
|
||||
{
|
||||
public const TYPE_MOVIE = 'movie';
|
||||
public const TYPE_TV_SERIES = 'tv_series';
|
||||
|
||||
protected $table = 'items_to_fetch';
|
||||
|
||||
protected $fillable = [
|
||||
'tmdb_id',
|
||||
'type',
|
||||
];
|
||||
|
||||
public function jobs()
|
||||
{
|
||||
return $this->hasOne(ItemToFetchJobs::class, 'tmdb_id', 'tmdb_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ItemToFetchJobs extends Model
|
||||
{
|
||||
public const JOB_ALTERNVATIVE_TITLES = 'AlternativeTitles';
|
||||
public const JOB_CREDITS = 'Credits';
|
||||
public const JOB_IMAGES = 'Images';
|
||||
public const JOB_KEYWORDS = 'Keywords';
|
||||
public const JOB_RELEASES = 'Releases';
|
||||
public const JOB_TRANSLATIONS = 'Translations';
|
||||
public const JOB_SIMILAR = 'Similar';
|
||||
public const JOB_RECOMMNDATION = 'Recommendations';
|
||||
public const JOB_REVIEWS = 'Reviews';
|
||||
public const JOB_LISTS = 'Lists';
|
||||
public const JOB_CHANGES = 'Changes';
|
||||
public const JOB_VIDEOS = 'Videos';
|
||||
public const JOB_WATCH_PROVIDERS = 'WatchProviders';
|
||||
public const JOB_EXTERNAL_IDS = 'ExternalIds';
|
||||
public const JOB_DETAILS = 'Details';
|
||||
|
||||
public const JOBS = [
|
||||
self::JOB_ALTERNVATIVE_TITLES,
|
||||
self::JOB_CREDITS,
|
||||
self::JOB_IMAGES,
|
||||
self::JOB_KEYWORDS,
|
||||
self::JOB_RELEASES,
|
||||
self::JOB_TRANSLATIONS,
|
||||
self::JOB_SIMILAR,
|
||||
self::JOB_RECOMMNDATION,
|
||||
self::JOB_REVIEWS,
|
||||
self::JOB_LISTS,
|
||||
self::JOB_CHANGES,
|
||||
self::JOB_VIDEOS,
|
||||
self::JOB_WATCH_PROVIDERS,
|
||||
self::JOB_EXTERNAL_IDS,
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'tmdb_id',
|
||||
'item_id',
|
||||
'type',
|
||||
'remaining_jobs',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'remaining_jobs' => 'array',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Movie extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'tmdb_id',
|
||||
'adult',
|
||||
'runtime',
|
||||
'backdrop_path',
|
||||
'budget',
|
||||
'revenue',
|
||||
'homepage',
|
||||
'imdb_id',
|
||||
'status',
|
||||
'original_language',
|
||||
'original_title',
|
||||
'title',
|
||||
'slug',
|
||||
'overview',
|
||||
'tagline',
|
||||
'poster_path',
|
||||
'release_date',
|
||||
'vote_average',
|
||||
'vote_count',
|
||||
];
|
||||
|
||||
public function collections()
|
||||
{
|
||||
return $this->belongsToMany(MovieCollection::class, 'movies_in_collection', 'movie_id', 'collection_id');
|
||||
}
|
||||
|
||||
public function genres()
|
||||
{
|
||||
return $this->belongsToMany(Genre::class, 'movie_genres', 'movie_id', 'genre_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class MovieCollection extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'poster_path',
|
||||
'backdrop_path',
|
||||
'tmdb_id',
|
||||
];
|
||||
|
||||
public function movies() {
|
||||
return $this->belongsToMany(Movie::class, 'movies_in_collection', 'collection_id', 'movie_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Show extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'adult',
|
||||
'backdrop_path',
|
||||
'episode_run_time',
|
||||
'first_air_date',
|
||||
'homepage',
|
||||
'tmdb_id',
|
||||
'in_production',
|
||||
'languages',
|
||||
'last_air_date',
|
||||
'name',
|
||||
'slug',
|
||||
'number_of_episodes',
|
||||
'number_of_seasons',
|
||||
'origin_country',
|
||||
'original_language',
|
||||
'original_name',
|
||||
'overview',
|
||||
'popularity',
|
||||
'poster_path',
|
||||
'status',
|
||||
'tagline',
|
||||
'type',
|
||||
'vote_average',
|
||||
'vote_count',
|
||||
];
|
||||
}
|
||||
@@ -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