Skip to content

Commit

Permalink
👍 Generate demo data
Browse files Browse the repository at this point in the history
  • Loading branch information
juzaweb committed Oct 9, 2022
1 parent 2b4d4dc commit 2fdd2ca
Show file tree
Hide file tree
Showing 9 changed files with 417 additions and 171 deletions.
15 changes: 0 additions & 15 deletions src/Commands/GenerateDemoCommand.php

This file was deleted.

75 changes: 75 additions & 0 deletions src/Commands/GenerateDemoMovieCommand.php
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;
}
}
172 changes: 172 additions & 0 deletions src/Commands/GenerateDemoVideoCommand.php
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'
]
];
}
}
Loading

0 comments on commit 2fdd2ca

Please sign in to comment.