-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new media_format attribute to anime (#591)
- Loading branch information
Showing
14 changed files
with
238 additions
and
3 deletions.
There are no files selected for viewing
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
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Enums\Models\Wiki; | ||
|
||
use App\Concerns\Enums\LocalizesName; | ||
|
||
/** | ||
* Enum AnimeMediaFormat. | ||
*/ | ||
enum AnimeMediaFormat: int | ||
{ | ||
use LocalizesName; | ||
|
||
case UNKNOWN = 0; | ||
case TV = 1; | ||
case TV_SHORT = 2; | ||
case OVA = 3; | ||
case MOVIE = 4; | ||
case SPECIAL = 5; | ||
case ONA = 6; | ||
} |
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,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Api\Field\Wiki\Anime; | ||
|
||
use App\Contracts\Http\Api\Field\CreatableField; | ||
use App\Contracts\Http\Api\Field\UpdatableField; | ||
use App\Enums\Models\Wiki\AnimeMediaFormat; | ||
use App\Http\Api\Field\EnumField; | ||
use App\Http\Api\Schema\Schema; | ||
use App\Models\Wiki\Anime; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Validation\Rules\Enum; | ||
|
||
/** | ||
* Class AnimeMediaFormatField. | ||
*/ | ||
class AnimeMediaFormatField extends EnumField implements CreatableField, UpdatableField | ||
{ | ||
/** | ||
* Create a new field instance. | ||
* | ||
* @param Schema $schema | ||
*/ | ||
public function __construct(Schema $schema) | ||
{ | ||
parent::__construct($schema, Anime::ATTRIBUTE_MEDIA_FORMAT, AnimeMediaFormat::class); | ||
} | ||
|
||
/** | ||
* Set the creation validation rules for the field. | ||
* | ||
* @param Request $request | ||
* @return array | ||
*/ | ||
public function getCreationRules(Request $request): array | ||
{ | ||
return [ | ||
'required', | ||
new Enum(AnimeMediaFormat::class), | ||
]; | ||
} | ||
|
||
/** | ||
* Set the update validation rules for the field. | ||
* | ||
* @param Request $request | ||
* @return array | ||
*/ | ||
public function getUpdateRules(Request $request): array | ||
{ | ||
return [ | ||
'sometimes', | ||
'required', | ||
new Enum(AnimeMediaFormat::class), | ||
]; | ||
} | ||
} |
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
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
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
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
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
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
29 changes: 29 additions & 0 deletions
29
database/migrations/2023_10_11_221246_add_media_format_attribute_to_anime.php
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,29 @@ | ||
<?php | ||
|
||
use App\Models\Wiki\Anime; | ||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::table(Anime::TABLE, function (Blueprint $table) { | ||
$table->integer(Anime::ATTRIBUTE_MEDIA_FORMAT)->nullable(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table(Anime::TABLE, function (Blueprint $table) { | ||
$table->dropColumn(Anime::ATTRIBUTE_MEDIA_FORMAT); | ||
}); | ||
} | ||
}; |
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,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Database\Seeders\Wiki; | ||
|
||
use App\Enums\Models\Wiki\AnimeMediaFormat; | ||
use App\Enums\Models\Wiki\ResourceSite; | ||
use App\Models\Wiki\Anime; | ||
use App\Models\Wiki\ExternalResource; | ||
use Illuminate\Database\Seeder; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
class AnimeFormatSeeder extends Seeder | ||
{ | ||
/** | ||
* Run the database seeds. | ||
*/ | ||
public function run(): void | ||
{ | ||
$query = ' | ||
query ($id: Int) { | ||
Media (id: $id, type: ANIME) { | ||
format | ||
} | ||
} | ||
'; | ||
|
||
$chunkSize = 10; | ||
$animes = Anime::query()->where(Anime::ATTRIBUTE_MEDIA_FORMAT, 0)->get(); | ||
|
||
foreach ($animes->chunk($chunkSize) as $chunk) { | ||
foreach ($chunk as $anime) { | ||
$resource = $anime->resources()->firstWhere(ExternalResource::ATTRIBUTE_SITE, ResourceSite::ANILIST->value); | ||
|
||
if ($resource instanceof ExternalResource) { | ||
$variables = [ | ||
'id' => $resource->external_id | ||
]; | ||
|
||
$response = Http::post('https://graphql.anilist.co', [ | ||
'query' => $query, | ||
'variables' => $variables | ||
]); | ||
|
||
$format = Arr::get($response, 'data.Media.format'); | ||
|
||
if ($format !== null) { | ||
if (in_array($format, ['TV', 'TV_SHORT', 'MOVIE'], true)) { | ||
$formats = [ | ||
'TV' => AnimeMediaFormat::TV->value, | ||
'TV_SHORT' => AnimeMediaFormat::TV_SHORT->value, | ||
'MOVIE' => AnimeMediaFormat::MOVIE->value | ||
]; | ||
|
||
$anime->update([ | ||
Anime::ATTRIBUTE_MEDIA_FORMAT => $formats[$format] | ||
]); | ||
echo $format; | ||
echo ' -> '; | ||
echo $anime->name; | ||
echo "\n"; | ||
} else { | ||
echo 'no format include -> '; | ||
echo $anime->name; | ||
echo "\n"; | ||
} | ||
} else { | ||
echo 'format null'; | ||
echo "\n"; | ||
} | ||
} | ||
} | ||
sleep(5); | ||
} | ||
} | ||
} |
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
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
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