Skip to content

Commit

Permalink
feat: new included paths to song resources (#604)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrch authored Oct 31, 2023
1 parent b39c321 commit 3aa49ba
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 67 deletions.
10 changes: 5 additions & 5 deletions app/Enums/Models/Wiki/ResourceSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ public function formatArtistResourceLink(int $id, ?string $slug = null): ?string
public function formatSongResourceLink(int $id, ?string $slug = null): ?string
{
return match ($this) {
ResourceSite::SPOTIFY->value => "https://open.spotify.com/track/$slug",
ResourceSite::YOUTUBE_MUSIC->value => "https://music.youtube.com/watch?v=$slug",
ResourceSite::YOUTUBE->value => "https://youtube.com/watch?v=$slug",
ResourceSite::APPLE_MUSIC->value => "https://music.apple.com/jp/album/$id",
ResourceSite::AMAZON_MUSIC->value => "https://amazon.co.jp/music/player/albums/$slug",
ResourceSite::SPOTIFY => "https://open.spotify.com/track/$slug",
ResourceSite::YOUTUBE_MUSIC => "https://music.youtube.com/watch?v=$slug",
ResourceSite::YOUTUBE => "https://www.youtube.com/watch?v=$slug",
ResourceSite::APPLE_MUSIC => "https://music.apple.com/jp/album/$id",
ResourceSite::AMAZON_MUSIC => "https://amazon.co.jp/music/player/albums/$slug",
default => null
};
}
Expand Down
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 @@ -79,6 +79,8 @@ public function allowedIncludes(): array
new AllowedInclude(new VideoSchema(), 'animethemes.animethemeentries.videos.animethemeentries.animetheme.animethemeentries.videos'),
new AllowedInclude(new AudioSchema(), 'animethemes.animethemeentries.videos.animethemeentries.animetheme.animethemeentries.videos.audio'),
new AllowedInclude(new ArtistSchema(), 'animethemes.animethemeentries.videos.animethemeentries.animetheme.song.artists'),
new AllowedInclude(new ExternalResourceSchema(), 'animethemes.animethemeentries.videos.animethemeentries.animetheme.song.resources'),
new AllowedInclude(new ExternalResourceSchema(), 'animethemes.song.resources'),
new AllowedInclude(new ImageSchema(), 'animethemes.song.artists.images'),
];
}
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Api/Schema/Wiki/ArtistSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ public function allowedIncludes(): array
// Undocumented paths needed for client builds
new AllowedInclude(new ArtistSchema(), 'groups.songs.artists'),
new AllowedInclude(new ImageSchema(), 'groups.songs.animethemes.anime.images'),
new AllowedInclude(new ExternalResourceSchema(), 'groups.songs.resources'),
new AllowedInclude(new ArtistSchema(), 'songs.artists'),
new AllowedInclude(new SongSchema(), 'songs.animethemes.song'),
new AllowedInclude(new ArtistSchema(), 'songs.animethemes.song.artists'),
new AllowedInclude(new ImageSchema(), 'songs.animethemes.anime.images'),
new AllowedInclude(new EntrySchema(), 'songs.animethemes.animethemeentries'),
new AllowedInclude(new VideoSchema(), 'songs.animethemes.animethemeentries.videos'),
new AllowedInclude(new AudioSchema(), 'songs.animethemes.animethemeentries.videos.audio'),
new AllowedInclude(new ExternalResourceSchema(), 'songs.resources'),
];
}

Expand Down
3 changes: 3 additions & 0 deletions app/Http/Api/Schema/Wiki/SongSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
use App\Http\Api\Include\AllowedInclude;
use App\Http\Api\Schema\EloquentSchema;
use App\Http\Api\Schema\Pivot\Wiki\ArtistSongSchema;
use App\Http\Api\Schema\Pivot\Wiki\SongResourceSchema;
use App\Http\Api\Schema\Wiki\Anime\ThemeSchema;
use App\Http\Resources\Pivot\Wiki\Resource\ArtistSongResource;
use App\Http\Resources\Pivot\Wiki\Resource\SongResourceResource;
use App\Http\Resources\Wiki\Resource\SongResource;
use App\Models\Wiki\Song;

Expand All @@ -31,6 +33,7 @@ public function allowedPivots(): array
{
return [
new AllowedInclude(new ArtistSongSchema(), ArtistSongResource::$wrap),
new AllowedInclude(new SongResourceSchema(), SongResourceResource::$wrap)
];
}

Expand Down
83 changes: 83 additions & 0 deletions database/seeders/Wiki/Anime/AnimeFormatAnilistSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Database\Seeders\Wiki\Anime;

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 AnimeFormatAnilistSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$this->anilistSeeder();
}

protected function anilistSeeder(): void
{
$query = '
query ($id: Int) {
Media (id: $id, type: ANIME) {
format
}
}
';

$chunkSize = 10;
$animes = Anime::query()->where(Anime::ATTRIBUTE_MEDIA_FORMAT, null)->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 @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Database\Seeders\Wiki;
namespace Database\Seeders\Wiki\Anime;

use App\Enums\Models\Wiki\AnimeMediaFormat;
use App\Enums\Models\Wiki\ResourceSite;
Expand All @@ -13,14 +13,13 @@
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;

class AnimeFormatSeeder extends Seeder
class AnimeFormatMalSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
//$this->anilistSeeder();
$this->malSeeder();
}

Expand Down Expand Up @@ -73,63 +72,4 @@ protected function malSeeder(): void
sleep(5);
}
}

protected function anilistSeeder(): void
{
$query = '
query ($id: Int) {
Media (id: $id, type: ANIME) {
format
}
}
';

$chunkSize = 10;
$animes = Anime::query()->where(Anime::ATTRIBUTE_MEDIA_FORMAT, null)->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);
}
}
}

0 comments on commit 3aa49ba

Please sign in to comment.