some progress

This commit is contained in:
2024-12-30 01:51:28 +02:00
parent 71701e19bf
commit 7436b95f15
53 changed files with 5610 additions and 180 deletions
@@ -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();
}
}