Anime detail page cleanup

This commit is contained in:
Timothy Warren 2020-07-29 15:49:16 -04:00
parent dcd138cba6
commit 375b8f9bcb
4 changed files with 99 additions and 115 deletions

View File

@ -1,4 +1,7 @@
<?php use function Aviat\AnimeClient\getLocalImg; ?>
<?php
use Aviat\AnimeClient\API\Kitsu;
use function Aviat\AnimeClient\getLocalImg;
?>
<main class="details fixed">
<section class="flex">
<aside class="info">
@ -11,20 +14,28 @@
<td class="align-right">Airing Status</td>
<td><?= $data['status'] ?></td>
</tr>
<?php /* <tr>
<td>Show Type</td>
<td><?= $data['show_type'] ?></td>
</tr> */ ?>
<tr>
<td>Episode Count</td>
<td><?= $data['episode_count'] ?? '-' ?></td>
</tr>
<?php if ( ! empty($data['episode_length'])): ?>
<?php if ($data['episode_count'] !== 1): ?>
<tr>
<td>Episode Length</td>
<td><?= $data['episode_length'] ?> minutes</td>
<td>Episode Count</td>
<td><?= $data['episode_count'] ?? '-' ?></td>
</tr>
<?php endif ?>
<?php if (( ! empty($data['episode_length'])) && $data['episode_count'] !== 1): ?>
<tr>
<td>Episode Length</td>
<td><?= Kitsu::friendlyTime($data['episode_length']) ?></td>
</tr>
<?php endif ?>
<?php if (isset($data['total_length'], $data['episode_count'])): ?>
<tr>
<td>Total Length</td>
<td><?= Kitsu::friendlyTime($data['total_length']) ?></td>
</tr>
<?php endif ?>
<?php if ( ! empty($data['age_rating'])): ?>
<tr>
<td>Age Rating</td>

View File

@ -31,6 +31,11 @@ final class Kitsu {
public const ANIME_HISTORY_LIST_CACHE_KEY = 'kitsu-anime-history-list';
public const MANGA_HISTORY_LIST_CACHE_KEY = 'kitsu-manga-history-list';
public const SECONDS_IN_MINUTE = 60;
public const MINUTES_IN_HOUR = 60;
public const MINUTES_IN_DAY = 1440;
public const MINUTES_IN_YEAR = 525_600;
/**
* Determine whether an anime is airing, finished airing, or has not yet aired
*
@ -270,6 +275,64 @@ final class Kitsu {
];
}
/**
* Convert a time in seconds to a more human-readable format
*
* @param int $seconds
* @return string
*/
public static function friendlyTime(int $seconds): string
{
// All the seconds left
$remSeconds = $seconds % self::SECONDS_IN_MINUTE;
$minutes = ($seconds - $remSeconds) / self::SECONDS_IN_MINUTE;
// Minutes short of a year
$years = (int)floor($minutes / self::MINUTES_IN_YEAR);
$minutes %= self::MINUTES_IN_YEAR;
// Minutes short of a day
$extraMinutes = $minutes % self::MINUTES_IN_DAY;
$days = ($minutes - $extraMinutes) / self::MINUTES_IN_DAY;
// Minutes short of an hour
$remMinutes = $extraMinutes % self::MINUTES_IN_HOUR;
$hours = ($extraMinutes - $remMinutes) / self::MINUTES_IN_HOUR;
$parts = [];
foreach ([
'year' => $years,
'day' => $days,
'hour' => $hours,
'minute' => $remMinutes,
'second' => $remSeconds
] as $label => $value)
{
if ($value === 0)
{
continue;
}
if ($value > 1)
{
$label .= 's';
}
$parts[] = "{$value} {$label}";
}
$last = array_pop($parts);
if (empty($parts))
{
return $last;
}
return (count($parts) > 1)
? implode(', ', $parts) . ", and {$last}"
: "{$parts[0]}, {$last}";
}
/**
* Determine if an alternate title is unique enough to list
*

View File

@ -16,10 +16,9 @@
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
use Aviat\AnimeClient\API\{JsonAPI, Kitsu};
use Aviat\AnimeClient\API\Kitsu;
use Aviat\AnimeClient\Types\AnimePage;
use Aviat\Ion\Transformer\AbstractTransformer;
use Aviat\Ion\Type\StringType;
/**
* Transformer for anime description page
@ -36,7 +35,6 @@ final class AnimeTransformer extends AbstractTransformer {
public function transform($item): AnimePage
{
$base = $item['data']['findAnimeBySlug'];
$characters = [];
$staff = [];
$genres = array_map(fn ($genre) => $genre['title']['en'], $base['categories']['nodes']);
@ -93,16 +91,16 @@ final class AnimeTransformer extends AbstractTransformer {
ksort($staff);
}
// @TODO: Streaming Links
$data = [
'age_rating' => $base['ageRating'],
'age_rating_guide' => $base['ageRatingGuide'],
'characters' => $characters,
'cover_image' => $base['posterImage']['views'][1]['url'],
'episode_count' => $base['episodeCount'],
'episode_length' => (int)($base['episodeLength'] / 60),
'episode_length' => $base['episodeLength'],
'genres' => $genres,
'id' => $base['id'],
// 'show_type' => (string)StringType::from($item['showType'])->upperCaseFirst(),
'slug' => $base['slug'],
'staff' => $staff,
'status' => Kitsu::getAiringStatus($base['startDate'], $base['endDate']),
@ -111,108 +109,11 @@ final class AnimeTransformer extends AbstractTransformer {
'title' => $title,
'titles' => [],
'titles_more' => $titles,
'total_length' => $base['totalLength'],
'trailer_id' => $base['youtubeTrailerVideoId'],
'url' => "https://kitsu.io/anime/{$base['slug']}",
];
// dump($data); die();
return AnimePage::from($data);
}
private function oldTransform($item): AnimePage {
$item['included'] = JsonAPI::organizeIncludes($item['included']);
$genres = $item['included']['categories'] ?? [];
$item['genres'] = array_column($genres, 'title') ?? [];
sort($item['genres']);
$title = $item['canonicalTitle'];
$titles = Kitsu::filterTitles($item);
$titles_more = Kitsu::getTitles($item);
$characters = [];
$staff = [];
if (array_key_exists('animeCharacters', $item['included']))
{
$animeCharacters = $item['included']['animeCharacters'];
foreach ($animeCharacters as $rel)
{
$charId = $rel['relationships']['character']['data']['id'];
$role = $rel['role'];
if (array_key_exists($charId, $item['included']['characters']))
{
$characters[$role][$charId] = $item['included']['characters'][$charId];
}
}
}
if (array_key_exists('mediaStaff', $item['included']))
{
foreach ($item['included']['mediaStaff'] as $id => $staffing)
{
$personId = $staffing['relationships']['person']['data']['id'];
$personDetails = $item['included']['people'][$personId];
$role = $staffing['role'];
if ( ! array_key_exists($role, $staff))
{
$staff[$role] = [];
}
$staff[$role][$personId] = [
'id' => $personId,
'name' => $personDetails['name'] ?? '??',
'image' => $personDetails['image'],
];
usort($staff[$role], function ($a, $b) {
return $a['name'] <=> $b['name'];
});
}
}
if ( ! empty($characters['main']))
{
uasort($characters['main'], static function ($a, $b) {
return $a['name'] <=> $b['name'];
});
}
if ( ! empty($characters['supporting']))
{
uasort($characters['supporting'], static function ($a, $b) {
return $a['name'] <=> $b['name'];
});
}
ksort($characters);
ksort($staff);
return AnimePage::from([
'age_rating' => $item['ageRating'],
'age_rating_guide' => $item['ageRatingGuide'],
'characters' => $characters,
'cover_image' => $item['posterImage']['small'],
'episode_count' => $item['episodeCount'],
'episode_length' => $item['episodeLength'],
'genres' => $item['genres'],
'id' => $item['id'],
'included' => $item['included'],
'show_type' => (string)StringType::from($item['showType'])->upperCaseFirst(),
'slug' => $item['slug'],
'staff' => $staff,
'status' => Kitsu::getAiringStatus($item['startDate'], $item['endDate']),
'streaming_links' => Kitsu::parseStreamingLinks($item['included']),
'synopsis' => $item['synopsis'],
'title' => $title,
'titles' => $titles,
'titles_more' => $titles_more,
'trailer_id' => $item['youtubeVideoId'],
'url' => "https://kitsu.io/anime/{$item['slug']}",
]);
}
}

View File

@ -108,6 +108,15 @@ class Anime extends AbstractType {
public ?string $trailer_id;
/**
* Length of the entire series in seconds
*
* @var int|null
*/
public ?int $total_length;
/**
* Kitsu detail page url
*
* @var string
*/
public ?string $url;