99 lines
2.4 KiB
PHP
99 lines
2.4 KiB
PHP
<?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/tv_series';
|
|
|
|
/**
|
|
* 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 end($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;
|
|
}
|
|
|
|
$jobs = ItemToFetchJobs::MOVIE_JOBS;
|
|
$jobs[] = "Details";
|
|
} else {
|
|
$show = Show::where('tmdb_id', $decodedLine['id'])->first();
|
|
if ($show) {
|
|
return;
|
|
}
|
|
|
|
$jobs = ItemToFetchJobs::SHOW_JOBS;
|
|
$jobs[] = "Details";
|
|
}
|
|
|
|
ItemToFetch::create([
|
|
'tmdb_id' => $decodedLine['id'],
|
|
'type' => $type,
|
|
]);
|
|
|
|
ItemToFetchJobs::create([
|
|
'tmdb_id' => $decodedLine['id'],
|
|
'type' => $type,
|
|
'remaining_jobs' => json_encode($jobs),
|
|
'jobs' => implode(',', $jobs),
|
|
]);
|
|
}
|
|
}
|