-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
417 additions
and
171 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace Juzaweb\Movie\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Facades\DB; | ||
use Juzaweb\Backend\Models\PostMeta; | ||
use Juzaweb\Movie\Helpers\TmdbApi; | ||
use Juzaweb\Movie\Helpers\TmdbImport; | ||
|
||
class GenerateDemoMovieCommand extends Command | ||
{ | ||
protected $signature = 'movie:generate-demo-movie'; | ||
|
||
public function handle(): int | ||
{ | ||
$api = new TmdbApi(); | ||
$api->setAPIKey(get_config('tmdb_api_key')); | ||
|
||
$movies = $api->getPopularMovies(); | ||
foreach ($movies as $movie) { | ||
$id = Arr::get($movie, 'id'); | ||
|
||
if (PostMeta::where('meta_key', '=', 'tmdb_id')->where('meta_value', '=', $id)->exists()) { | ||
continue; | ||
} | ||
|
||
DB::beginTransaction(); | ||
try { | ||
$model = TmdbImport::make()->import( | ||
$id, | ||
1 | ||
); | ||
|
||
$this->info("Import success {$model->title}."); | ||
|
||
DB::commit(); | ||
} catch (\Exception $e) { | ||
DB::rollBack(); | ||
$this->error($e->getMessage()); | ||
} | ||
|
||
sleep(1); | ||
} | ||
|
||
$tvshows = $api->getPopularTVShows(); | ||
foreach ($tvshows as $movie) { | ||
$id = Arr::get($movie, 'id'); | ||
|
||
if (PostMeta::where('meta_key', '=', 'tmdb_id')->where('meta_value', '=', $id)->exists()) { | ||
continue; | ||
} | ||
|
||
DB::beginTransaction(); | ||
try { | ||
$model = TmdbImport::make()->import( | ||
Arr::get($movie, 'id'), | ||
2 | ||
); | ||
|
||
$this->info("Import success {$model->title}."); | ||
|
||
DB::commit(); | ||
} catch (\Exception $e) { | ||
DB::rollBack(); | ||
$this->error($e->getMessage()); | ||
} | ||
|
||
sleep(1); | ||
} | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
<?php | ||
|
||
namespace Juzaweb\Movie\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Support\Facades\DB; | ||
use Juzaweb\Backend\Models\Post; | ||
|
||
class GenerateDemoVideoCommand extends Command | ||
{ | ||
protected $name = 'movie:generate-demo-video'; | ||
|
||
public function handle() | ||
{ | ||
$movies = Post::with( | ||
[ | ||
'resources' => function ($q) { | ||
$q->where('type', 'servers'); | ||
$q->orWhere('type', 'files'); | ||
} | ||
] | ||
) | ||
->whereType('movies') | ||
->where( | ||
function (Builder $q) { | ||
$q->whereDoesntHave( | ||
'resources', | ||
function ($q2) { | ||
$q2->where('type', 'servers'); | ||
} | ||
); | ||
$q->orWhereDoesntHave( | ||
'resources', | ||
function ($q2) { | ||
$q2->where('type', 'files'); | ||
} | ||
); | ||
} | ||
) | ||
->orderBy('id', 'DESC') | ||
->limit(1) | ||
->get(); | ||
|
||
foreach ($movies as $movie) { | ||
$this->info("Generate movie {$movie->title}"); | ||
|
||
DB::beginTransaction(); | ||
try { | ||
if ($movie->getMeta('tv_series')) { | ||
$this->updateForTvSeries($movie); | ||
} else { | ||
$this->updateForMovies($movie); | ||
} | ||
|
||
DB::commit(); | ||
} catch (\Exception $e) { | ||
DB::rollBack(); | ||
throw $e; | ||
} | ||
} | ||
} | ||
|
||
private function updateForMovies(Post $movie) | ||
{ | ||
if (!$server = $movie->resources->where('type', 'servers')->first()) { | ||
$server = $movie->resources()->create( | ||
[ | ||
'name' => 'S1', | ||
'type' => 'servers' | ||
] | ||
); | ||
} | ||
|
||
$files = $movie->resources->where('type', 'files') | ||
->map( | ||
function ($item) { | ||
return $item->getMeta('url'); | ||
} | ||
) | ||
->toArray(); | ||
|
||
$videos = $this->getVideos(); | ||
|
||
foreach ($videos as $video) { | ||
if (in_array($video['url'], $files)) { | ||
continue; | ||
} | ||
|
||
$file = $movie->resources()->create( | ||
[ | ||
'name' => $video['name'], | ||
'type' => 'files', | ||
'parent_id' => $server->id | ||
] | ||
); | ||
|
||
$file->setMeta('url', $video['url']); | ||
|
||
$file->setMeta('source', $video['source']); | ||
|
||
$this->info("-- Add file {$video['url']}"); | ||
} | ||
} | ||
|
||
private function updateForTvSeries(Post $movie) | ||
{ | ||
if (!$server = $movie->resources->where('type', 'servers')->first()) { | ||
$server = $movie->resources()->create( | ||
[ | ||
'name' => 'S1', | ||
'type' => 'servers' | ||
] | ||
); | ||
} | ||
|
||
$files = $movie->resources->where('type', 'files') | ||
->map( | ||
function ($item) { | ||
return $item->getMeta('url'); | ||
} | ||
) | ||
->toArray(); | ||
|
||
$displayOrder = $movie->resources->where('type', 'files')->max('display_order') + 1; | ||
$videos = $this->getVideos(); | ||
|
||
foreach ($videos as $video) { | ||
if (in_array($video['url'], $files)) { | ||
continue; | ||
} | ||
|
||
$file = $movie->resources()->create( | ||
[ | ||
'name' => $displayOrder, | ||
'type' => 'files', | ||
'display_order' => $displayOrder, | ||
'parent_id' => $server->id | ||
] | ||
); | ||
|
||
$file->setMeta('url', $video['url']); | ||
|
||
$file->setMeta('source', $video['source']); | ||
|
||
$displayOrder++; | ||
|
||
$this->info("-- Add file {$video['url']}"); | ||
} | ||
} | ||
|
||
private function getVideos(): array | ||
{ | ||
return [ | ||
[ | ||
'name' => 'mp4', | ||
'source' => 'mp4', | ||
'url' => 'https://cdn.juzaweb.com/storage/demo/demo.mp4', | ||
], | ||
[ | ||
'name' => 'hls', | ||
'source' => 'm3u8', | ||
'url' => 'https://cdn.juzaweb.com/storage/demo/demo.m3u8', | ||
], | ||
[ | ||
'name' => 'youtube', | ||
'source' => 'youtube', | ||
'url' => 'https://www.youtube.com/watch?v=kErrg42WLcg' | ||
] | ||
]; | ||
} | ||
} |
Oops, something went wrong.