Skip to content

Commit

Permalink
feat: new media_format attribute to anime (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrch authored Oct 29, 2023
1 parent 5302b2b commit a77f3c5
Show file tree
Hide file tree
Showing 14 changed files with 238 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app/Actions/Models/Wiki/Video/Audio/BackfillAudioAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,10 @@ protected function getAdjacentVideos(): Collection
$orderByNameQuery = $sortRelation->getRelationExistenceQuery($sortRelation->getQuery(), $builder, [Anime::ATTRIBUTE_NAME]);
$orderBySeasonQuery = $sortRelation->getRelationExistenceQuery($sortRelation->getQuery(), $builder, [Anime::ATTRIBUTE_SEASON]);
$orderByYearQuery = $sortRelation->getRelationExistenceQuery($sortRelation->getQuery(), $builder, [Anime::ATTRIBUTE_YEAR]);
$orderByMediaFormatQuery = $sortRelation->getRelationExistenceQuery($sortRelation->getQuery(), $builder, [Anime::ATTRIBUTE_MEDIA_FORMAT]);

return $builder->whereHas(AnimeTheme::RELATION_VIDEOS, fn (Builder $relationBuilder) => $relationBuilder->whereKey($this->getModel()))
->orderBy($orderByMediaFormatQuery->toBase())
->orderBy($orderByYearQuery->toBase())
->orderBy($orderBySeasonQuery->toBase())
->orderBy($orderByNameQuery->toBase())
Expand Down
23 changes: 23 additions & 0 deletions app/Enums/Models/Wiki/AnimeMediaFormat.php
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;
}
59 changes: 59 additions & 0 deletions app/Http/Api/Field/Wiki/Anime/AnimeMediaFormatField.php
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),
];
}
}
2 changes: 2 additions & 0 deletions app/Http/Api/Schema/Wiki/AnimeSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Contracts\Http\Api\Schema\SearchableSchema;
use App\Http\Api\Field\Base\IdField;
use App\Http\Api\Field\Field;
use App\Http\Api\Field\Wiki\Anime\AnimeMediaFormatField;
use App\Http\Api\Field\Wiki\Anime\AnimeNameField;
use App\Http\Api\Field\Wiki\Anime\AnimeSeasonField;
use App\Http\Api\Field\Wiki\Anime\AnimeSlugField;
Expand Down Expand Up @@ -95,6 +96,7 @@ public function fields(): array
new IdField($this, Anime::ATTRIBUTE_ID),
new AnimeNameField($this),
new AnimeSeasonField($this),
new AnimeMediaFormatField($this),
new AnimeSlugField($this),
new AnimeSynopsisField($this),
new AnimeYearField($this),
Expand Down
5 changes: 5 additions & 0 deletions app/Models/Wiki/Anime.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Models\Wiki;

use App\Enums\Models\Wiki\AnimeMediaFormat;
use App\Enums\Models\Wiki\AnimeSeason;
use App\Events\Wiki\Anime\AnimeCreated;
use App\Events\Wiki\Anime\AnimeDeleted;
Expand Down Expand Up @@ -44,6 +45,7 @@
* @property Collection<int, Studio> $studios
* @property string|null $synopsis
* @property int|null $year
* @property AnimeMediaFormat|null $media_format
*
* @method static AnimeFactory factory(...$parameters)
*/
Expand All @@ -60,6 +62,7 @@ class Anime extends BaseModel
final public const ATTRIBUTE_SLUG = 'slug';
final public const ATTRIBUTE_SYNOPSIS = 'synopsis';
final public const ATTRIBUTE_YEAR = 'year';
final public const ATTRIBUTE_MEDIA_FORMAT = 'media_format';

final public const RELATION_ARTISTS = 'animethemes.song.artists';
final public const RELATION_AUDIO = 'animethemes.animethemeentries.videos.audio';
Expand All @@ -85,6 +88,7 @@ class Anime extends BaseModel
Anime::ATTRIBUTE_SLUG,
Anime::ATTRIBUTE_SYNOPSIS,
Anime::ATTRIBUTE_YEAR,
Anime::ATTRIBUTE_MEDIA_FORMAT
];

/**
Expand Down Expand Up @@ -160,6 +164,7 @@ public function getRouteKeyName(): string
protected $casts = [
Anime::ATTRIBUTE_SEASON => AnimeSeason::class,
Anime::ATTRIBUTE_YEAR => 'int',
Anime::ATTRIBUTE_MEDIA_FORMAT => AnimeMediaFormat::class
];

/**
Expand Down
8 changes: 8 additions & 0 deletions app/Nova/Lenses/Anime/AnimeLens.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Nova\Lenses\Anime;

use App\Enums\Models\Wiki\AnimeMediaFormat;
use App\Enums\Models\Wiki\AnimeSeason;
use App\Models\BaseModel;
use App\Models\Wiki\Anime;
Expand Down Expand Up @@ -61,6 +62,13 @@ public function fields(NovaRequest $request): array
->showOnPreview()
->filterable(),

Select::make(__('nova.fields.anime.media_format.name'), Anime::ATTRIBUTE_MEDIA_FORMAT)
->options(AnimeMediaFormat::asSelectArray())
->displayUsing(fn (?int $enumValue) => AnimeMediaFormat::tryFrom($enumValue ?? 0)?->localize())
->sortable()
->showOnPreview()
->filterable(),

Textarea::make(__('nova.fields.anime.synopsis.name'), AnimeModel::ATTRIBUTE_SYNOPSIS)
->onlyOnPreview(),

Expand Down
11 changes: 11 additions & 0 deletions app/Nova/Resources/Wiki/Anime.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Nova\Resources\Wiki;

use App\Enums\Models\Wiki\AnimeMediaFormat;
use App\Enums\Models\Wiki\AnimeSeason;
use App\Enums\Models\Wiki\ImageFacet;
use App\Enums\Models\Wiki\ResourceSite;
Expand Down Expand Up @@ -204,6 +205,16 @@ public function fields(NovaRequest $request): array
->filterable()
->showWhenPeeking(),

Select::make(__('nova.fields.anime.media_format.name'), AnimeModel::ATTRIBUTE_MEDIA_FORMAT)
->options(AnimeMediaFormat::asSelectArray())
->displayUsing(fn (?int $enumValue) => AnimeMediaFormat::tryFrom($enumValue ?? 0)?->localize())
->sortable()
->rules(['required', new Enum(AnimeMediaFormat::class)])
->help(__('nova.fields.anime.media_format.help'))
->showOnPreview()
->filterable()
->showWhenPeeking(),

Textarea::make(__('nova.fields.anime.synopsis.name'), AnimeModel::ATTRIBUTE_SYNOPSIS)
->rules('max:65535')
->nullable()
Expand Down
4 changes: 2 additions & 2 deletions app/Nova/Resources/Wiki/Anime/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ function (Text $field, NovaRequest $novaRequest, FormData $formData) {
$type = ThemeType::tryFrom(intval($formData->offsetGet(AnimeTheme::ATTRIBUTE_TYPE)));
$slug = $slug->append($type->name);
}
if ($slug->isNotEmpty() && $formData->offsetExists(AnimeTheme::ATTRIBUTE_SEQUENCE)) {
$slug = $slug->append($formData->offsetGet(AnimeTheme::ATTRIBUTE_SEQUENCE));
if ($slug->isNotEmpty()) {
$slug = $slug->append($formData->offsetGet(AnimeTheme::ATTRIBUTE_SEQUENCE) ?? 1);
}
$field->value = $slug->__toString();
}
Expand Down
3 changes: 3 additions & 0 deletions database/factories/Wiki/AnimeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Database\Factories\Wiki;

use App\Enums\Models\Wiki\AnimeMediaFormat;
use App\Enums\Models\Wiki\AnimeSeason;
use App\Models\Wiki\Anime;
use App\Models\Wiki\Anime\AnimeSynonym;
Expand Down Expand Up @@ -44,13 +45,15 @@ class AnimeFactory extends Factory
public function definition(): array
{
$season = Arr::random(AnimeSeason::cases());
$media_format = Arr::random(AnimeMediaFormat::cases());

return [
Anime::ATTRIBUTE_NAME => fake()->words(3, true),
Anime::ATTRIBUTE_SEASON => $season->value,
Anime::ATTRIBUTE_SLUG => Str::slug(fake()->text(191), '_'),
Anime::ATTRIBUTE_SYNOPSIS => fake()->text(),
Anime::ATTRIBUTE_YEAR => fake()->numberBetween(1960, intval(date('Y')) + 1),
Anime::ATTRIBUTE_MEDIA_FORMAT => $media_format->value,
];
}

Expand Down
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);
});
}
};
78 changes: 78 additions & 0 deletions database/seeders/Wiki/AnimeFormatSeeder.php
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function up(): void
],
]);
$mapping->long('season');
$mapping->long('media_format');
$mapping->text('slug', [
'fields' => [
'keyword' => [
Expand Down
10 changes: 10 additions & 0 deletions lang/en/enums.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Enums\Models\Billing\BalanceFrequency;
use App\Enums\Models\Billing\Service;
use App\Enums\Models\List\PlaylistVisibility;
use App\Enums\Models\Wiki\AnimeMediaFormat;
use App\Enums\Models\Wiki\AnimeSeason;
use App\Enums\Models\Wiki\ImageFacet;
use App\Enums\Models\Wiki\ResourceSite;
Expand All @@ -13,6 +14,15 @@
use App\Enums\Models\Wiki\VideoSource;

return [
AnimeMediaFormat::class => [
AnimeMediaFormat::UNKNOWN->name => 'Unknown',
AnimeMediaFormat::TV->name => 'TV',
AnimeMediaFormat::TV_SHORT->name => 'TV Short',
AnimeMediaFormat::OVA->name => 'OVA',
AnimeMediaFormat::MOVIE->name => 'Movie',
AnimeMediaFormat::SPECIAL->name => 'Special',
AnimeMediaFormat::ONA->name => 'ONA'
],
AnimeSeason::class => [
AnimeSeason::WINTER->name => 'Winter',
AnimeSeason::SPRING->name => 'Spring',
Expand Down
6 changes: 5 additions & 1 deletion lang/en/nova.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@
'name' => 'Sequence',
],
'slug' => [
'help' => 'Used as the URL Slug / Model Route Key. By default, this should be the Type and Sequence lowercased and "_" replacing spaces. These should be unique within the scope of the anime. Ex: "OP", "ED1", "OP2-Dub".',
'help' => 'Used as the URL Slug / Model Route Key. By default, this should be the Type and Sequence lowercased and "-" replacing spaces. These should be unique within the scope of the anime. Ex: "OP1", "ED1", "OP2-Dub".',
'name' => 'Slug',
],
'type' => [
Expand Down Expand Up @@ -422,6 +422,10 @@
'help' => 'The Year in which the Anime premiered. By default, we will use the Premiered Field on the MAL page.',
'name' => 'Year',
],
'media_format' => [
'help' => 'The Format of the Anime. By default, we will use the Type Field on the MAL page.',
'name' => 'Media Format'
],
'resources' => [
'as' => [
'help' => 'Used to distinguish resources that map to the same anime. For example, Aware! Meisaku-kun has one MAL page and many aniDB pages.',
Expand Down

0 comments on commit a77f3c5

Please sign in to comment.