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,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),
]);
}
}