Files

81 lines
1.9 KiB
PHP
Raw Permalink Normal View History

2024-12-30 01:51:28 +02:00
<?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
{
2025-09-06 14:41:10 +03:00
echo "Downloading file at ".$url."\n";
2024-12-30 01:51:28 +02:00
$newFileName = $path;
$file = fopen ($url, 'rb');
$newFile = null;
if ($file) {
2025-09-06 14:41:10 +03:00
echo "Starting download ".$url."\n";
2024-12-30 01:51:28 +02:00
$newFile = fopen ($newFileName, 'wb');
if ($newFile) {
2025-09-06 14:41:10 +03:00
echo "in progress ".$url."\n";
2024-12-30 01:51:28 +02:00
while(!feof($file)) {
fwrite($newFile, fread($file, 1024 * 8), 1024 * 8);
}
}
}
if ($file) {
fclose($file);
}
if ($newFile) {
fclose($newFile);
}
}
}