From 766e3cd71c355a8b9eac05b59be769c1c237a0a4 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 29 Jul 2020 16:25:57 -0400 Subject: [PATCH] Fetch anime details by id for the collection --- src/AnimeClient/API/Kitsu.php | 19 ++- .../GraphQL/Queries/AnimeDetailsById.graphql | 117 ++++++++++++++++++ src/AnimeClient/API/Kitsu/KitsuAnimeTrait.php | 6 +- .../Kitsu/Transformer/AnimeTransformer.php | 12 +- 4 files changed, 147 insertions(+), 7 deletions(-) create mode 100644 src/AnimeClient/API/Kitsu/GraphQL/Queries/AnimeDetailsById.graphql diff --git a/src/AnimeClient/API/Kitsu.php b/src/AnimeClient/API/Kitsu.php index 07b9728f..1ef7ae97 100644 --- a/src/AnimeClient/API/Kitsu.php +++ b/src/AnimeClient/API/Kitsu.php @@ -141,7 +141,7 @@ final class Kitsu { * @param array $data * @return array */ - public static function getTitles(array $data): array + public static function oldGetTitles(array $data): array { $raw = array_unique([ $data['canonicalTitle'], @@ -152,6 +152,23 @@ final class Kitsu { return array_diff($raw,[$data['canonicalTitle']]); } + /** + * Get the list of titles + * + * @param array $titles + * @return array + */ + public static function getTitles(array $titles): array + { + $raw = array_unique([ + $titles['canonical'], + ...array_values($titles['localized']), + // ...array_values($data['abbreviatedTitles'] ?? []), + ]); + + return array_diff($raw,[$titles['canonical']]); + } + /** * Filter out duplicate and very similar names from * diff --git a/src/AnimeClient/API/Kitsu/GraphQL/Queries/AnimeDetailsById.graphql b/src/AnimeClient/API/Kitsu/GraphQL/Queries/AnimeDetailsById.graphql new file mode 100644 index 00000000..8469a4df --- /dev/null +++ b/src/AnimeClient/API/Kitsu/GraphQL/Queries/AnimeDetailsById.graphql @@ -0,0 +1,117 @@ +query ($id: ID!) { + findAnimeById(id: $id) { + id + ageRating + ageRatingGuide + posterImage { + original { + height + name + url + width + } + views { + height + name + url + width + } + } + categories { + nodes { + title + } + } + characters { + nodes { + character { + id + names { + canonical + alternatives + } + image { + original { + height + name + url + width + } + } + slug + } + role + } + pageInfo { + endCursor + hasNextPage + hasPreviousPage + startCursor + } + } + startDate + endDate + episodeCount + episodeLength + totalLength + posterImage { + original { + height + name + url + width + } + views { + height + name + url + width + } + } + season + sfw + slug + staff { + nodes { + person { + id + birthday + image { + original { + height + name + url + width + } + views { + height + name + url + width + } + } + names { + alternatives + canonical + localized + } + } + role + } + pageInfo { + endCursor + hasNextPage + hasPreviousPage + startCursor + } + } + status + synopsis + titles { + canonical + localized + } + totalLength + youtubeTrailerVideoId + } +} diff --git a/src/AnimeClient/API/Kitsu/KitsuAnimeTrait.php b/src/AnimeClient/API/Kitsu/KitsuAnimeTrait.php index 75901ec3..98196dc4 100644 --- a/src/AnimeClient/API/Kitsu/KitsuAnimeTrait.php +++ b/src/AnimeClient/API/Kitsu/KitsuAnimeTrait.php @@ -63,7 +63,6 @@ trait KitsuAnimeTrait { $baseData = $this->requestBuilder->runQuery('AnimeDetails', [ 'slug' => $slug ]); - // $baseData = $this->getRawMediaData('anime', $slug); if (empty($baseData)) { @@ -109,7 +108,10 @@ trait KitsuAnimeTrait { */ public function getAnimeById(string $animeId): Anime { - $baseData = $this->getRawMediaDataById('anime', $animeId); + $baseData = $this->requestBuilder->runQuery('AnimeDetailsById', [ + 'id' => $animeId, + ]); + // $baseData = $this->getRawMediaDataById('anime', $animeId); return $this->animeTransformer->transform($baseData); } diff --git a/src/AnimeClient/API/Kitsu/Transformer/AnimeTransformer.php b/src/AnimeClient/API/Kitsu/Transformer/AnimeTransformer.php index 074185bf..c95202f6 100644 --- a/src/AnimeClient/API/Kitsu/Transformer/AnimeTransformer.php +++ b/src/AnimeClient/API/Kitsu/Transformer/AnimeTransformer.php @@ -34,7 +34,9 @@ final class AnimeTransformer extends AbstractTransformer { */ public function transform($item): AnimePage { - $base = $item['data']['findAnimeBySlug']; + $base = array_key_exists('findAnimeBySlug', $item['data']) + ? $item['data']['findAnimeBySlug'] + : $item['data']['findAnimeById']; $characters = []; $staff = []; $genres = array_map(fn ($genre) => $genre['title']['en'], $base['categories']['nodes']); @@ -42,7 +44,8 @@ final class AnimeTransformer extends AbstractTransformer { sort($genres); $title = $base['titles']['canonical']; - $titles = Kitsu::filterLocalizedTitles($base['titles']); + $titles = Kitsu::getTitles($base['titles']); + $titles_more = Kitsu::filterLocalizedTitles($base['titles']); if (count($base['characters']['nodes']) > 0) { @@ -103,12 +106,13 @@ final class AnimeTransformer extends AbstractTransformer { 'id' => $base['id'], 'slug' => $base['slug'], 'staff' => $staff, + 'show_type' => 'TV', // TODO: get show type 'status' => Kitsu::getAiringStatus($base['startDate'], $base['endDate']), 'streaming_links' => [], // Kitsu::parseStreamingLinks($item['included']), 'synopsis' => $base['synopsis']['en'], 'title' => $title, - 'titles' => [], - 'titles_more' => $titles, + 'titles' => $titles, + 'titles_more' => $titles_more, 'total_length' => $base['totalLength'], 'trailer_id' => $base['youtubeTrailerVideoId'], 'url' => "https://kitsu.io/anime/{$base['slug']}",