Skip to content

Commit

Permalink
Merge pull request #149
Browse files Browse the repository at this point in the history
[Add] Import Commands
  • Loading branch information
kiritokatklian authored Jun 11, 2021
2 parents 417160c + 46d0dd7 commit 864c71d
Show file tree
Hide file tree
Showing 512 changed files with 8,246 additions and 31,202 deletions.
107 changes: 107 additions & 0 deletions app/Console/Commands/ImportAnimeID.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace App\Console\Commands;

use App\Models\Anime;
use Illuminate\Console\Command;
use JsonMachine\JsonDecoder\ExtJsonDecoder;
use JsonMachine\JsonMachine;

class ImportAnimeID extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'import:anime-ids';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Imports anime ids for different services.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$file = storage_path('app/anime-offline-database.json');
$fileSize = filesize($file);
$animes = JsonMachine::fromFile($file, '/data', new ExtJsonDecoder);

$progressBar = $this->output->createProgressBar($fileSize);
$progressBar->start();

foreach ($animes as $data) {
$sources = $this->filterSources($data->sources);

if (array_key_exists('mal_id', $sources)) {
$anime = Anime::firstWhere([
['mal_id', $sources['mal_id']],
]);

if (!empty($anime)) {
$anime->update($sources);
}
}

$progress = $animes->getPosition();
$progressBar->setProgress($progress);
}

$progressBar->finish();
return 1;
}

/**
* Returns an array of the sources as key_id => id.
*
* @param array $sources
* @return array
*/
protected function filterSources(array $sources): array
{
if (empty($sources)) {
return $sources;
}
$regexSearch = [
'anidb_id' => '#https:\/\/anidb.net\/anime\/(\w+)$#i',
'anilist_id' => '#https:\/\/anilist.co\/anime\/(\w+)$#i',
'kitsu_id' => '#https:\/\/kitsu.io\/anime\/(\w+)$#i',
'mal_id' => '#https:\/\/myanimelist.net\/anime/(\w+)$#i',
'notify_id' => '#https:\/\/notify.moe\/anime/(\w+)$#i',
];
$matchedSources = [];

foreach ($regexSearch as $key => $regex) {
$matchArray = preg_grep($regex, $sources);

if (!empty($matchArray)) {
foreach ($matchArray as $item) {
$matchCount = preg_match($regex, $item, $match);

if ($matchCount) {
$matchedSources[$key] = $match[1];
}
}
}
}

return $matchedSources;
}
}
49 changes: 49 additions & 0 deletions app/Console/Commands/KDashboard/ImportAnime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Console\Commands\KDashboard;

use App\Jobs\ProcessImportAnime;
use App\Models\KDashboard\Anime as KAnime;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;

class ImportAnime extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'import:anime';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Imports the anime from the KDashboard database.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
KAnime::chunk(1000, function (Collection $kAnimes) {
ProcessImportAnime::dispatch($kAnimes);
});

return 1;
}
}
51 changes: 51 additions & 0 deletions app/Console/Commands/KDashboard/ImportAnimeCasts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Console\Commands\KDashboard;

use App\Jobs\ProcessImportAnimeCast;
use App\Models\KDashboard\AnimeCharacter as KAnimeCast;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;

class ImportAnimeCasts extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'import:anime-cast';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Imports the anime casts from the KDashboard database.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
KAnimeCast::where([
['id', '>', 250000],
])->chunk(1000, function (Collection $kAnimeCasts) {
ProcessImportAnimeCast::dispatch($kAnimeCasts);
});

return 1;
}
}
49 changes: 49 additions & 0 deletions app/Console/Commands/KDashboard/ImportAnimeGenres.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Console\Commands\KDashboard;

use App\Jobs\ProcessImportAnimeGenre;
use App\Models\KDashboard\MediaGenre as KMediaGenre;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;

class ImportAnimeGenres extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'import:anime-genres';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Imports the anime genres from the KDashboard database.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
KMediaGenre::where('type', 'anime')->chunk(1000, function (Collection $kMediaGenres) {
ProcessImportAnimeGenre::dispatch($kMediaGenres);
});

return 1;
}
}
52 changes: 52 additions & 0 deletions app/Console/Commands/KDashboard/ImportAnimeRelations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Console\Commands\KDashboard;

use App\Jobs\ProcessImportAnimeRelations;
use App\Models\KDashboard\MediaRelated as KMediaRelated;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;

class ImportAnimeRelations extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'import:anime-relations';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Imports the anime relations from the KDashboard database';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
KMediaRelated::where([
['media_type', 'anime'],
['related_type', 'anime'],
])->chunk(1000, function (Collection $kMediaRelated) {
ProcessImportAnimeRelations::dispatch($kMediaRelated);
});

return 1;
}
}
49 changes: 49 additions & 0 deletions app/Console/Commands/KDashboard/ImportAnimeSongs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Console\Commands\KDashboard;

use App\Jobs\ProcessImportAnimeSong;
use App\Models\KDashboard\Song as KSong;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;

class ImportAnimeSongs extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'import:anime-songs';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Imports the anime songs from the KDashboard database.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
KSong::chunk(1000, function (Collection $kAnimeSongs) {
ProcessImportAnimeSong::dispatch($kAnimeSongs);
});

return 1;
}
}
Loading

0 comments on commit 864c71d

Please sign in to comment.