Add random anime and random manga pages

This commit is contained in:
Timothy Warren 2020-12-02 12:42:47 -05:00
parent f003905c0b
commit 2b01871e5b
9 changed files with 258 additions and 7 deletions

View File

@ -51,6 +51,10 @@ $routes = [
'action' => 'add',
'verb' => 'post',
],
'anime.random' => [
'path' => '/anime/details/random',
'action' => 'random',
],
'anime.details' => [
'path' => '/anime/details/{id}',
'action' => 'details',
@ -84,6 +88,10 @@ $routes = [
'action' => 'delete',
'verb' => 'post',
],
'manga.random' => [
'path' => '/manga/details/random',
'action' => 'random',
],
'manga.details' => [
'path' => '/manga/details/{id}',
'action' => 'details',

View File

@ -256,6 +256,15 @@ final class Model {
return $this->animeTransformer->transform($baseData);
}
public function getRandomAnime(): Anime
{
$baseData = $this->requestBuilder->runQuery('RandomMedia', [
'type' => 'ANIME'
]);
return $this->animeTransformer->transform($baseData);
}
/**
* Get information about a particular anime
*
@ -392,6 +401,15 @@ final class Model {
return $this->mangaTransformer->transform($baseData);
}
public function getRandomManga(): MangaPage
{
$baseData = $this->requestBuilder->runQuery('RandomMedia', [
'type' => 'MANGA'
]);
return $this->mangaTransformer->transform($baseData);
}
/**
* Get information about a particular manga
*

View File

@ -0,0 +1,134 @@
query ($type: MediaTypeEnum!) {
randomMedia(mediaType: $type, ageRatings: [G,PG,R]) {
id
ageRating
ageRatingGuide
posterImage {
original {
height
name
url
width
}
views {
height
name
url
width
}
}
categories(first: 100) {
nodes {
title
}
}
characters(first: 100) {
nodes {
character {
id
names {
alternatives
canonical
localized
}
image {
original {
height
name
url
width
}
}
slug
}
role
}
pageInfo {
endCursor
hasNextPage
hasPreviousPage
startCursor
}
}
description
startDate
endDate
sfw
slug
mappings(first: 10) {
nodes {
externalId
externalSite
}
}
staff(first: 100) {
nodes {
person {
id
birthday
image {
original {
height
name
url
width
}
views {
height
name
url
width
}
}
names {
alternatives
canonical
localized
}
slug
}
role
}
pageInfo {
endCursor
hasNextPage
hasPreviousPage
startCursor
}
}
status
titles {
alternatives
canonical
canonicalLocale
localized
}
...on Anime {
episodeCount
episodeLength
totalLength
season
streamingLinks(first: 10) {
nodes {
dubs
subs
regions
streamer {
id
siteName
}
url
}
}
subtype
totalLength
youtubeTrailerVideoId
}
...on Manga {
chapterCount
volumeCount
subtype
}
}
}

View File

@ -34,9 +34,7 @@ final class AnimeTransformer extends AbstractTransformer {
*/
public function transform($item): AnimePage
{
$base = array_key_exists('findAnimeBySlug', $item['data'])
? $item['data']['findAnimeBySlug']
: $item['data']['findAnimeById'];
$base = $item['data']['findAnimeBySlug'] ?? $item['data']['findAnimeById'] ?? $item['data']['randomMedia'];
$characters = [];
$links = [];
$staff = [];

View File

@ -34,10 +34,7 @@ final class MangaTransformer extends AbstractTransformer {
*/
public function transform($item): MangaPage
{
$base = array_key_exists('findMangaBySlug', $item['data'])
? $item['data']['findMangaBySlug']
: $item['data']['findMangaById'];
$base = $item['data']['findMangaBySlug'] ?? $item['data']['findMangaById'] ?? $item['data']['randomMedia'];
$characters = [];
$links = [];
$staff = [];

View File

@ -348,5 +348,43 @@ final class Anime extends BaseController {
);
}
}
public function random()
{
try
{
$data = $this->model->getRandomAnime();
if ($data->isEmpty())
{
$this->notFound(
$this->config->get('whose_list') .
"'s Anime List · Anime · " .
'Anime not found',
'Anime Not Found'
);
return;
}
$this->outputHTML('anime/details', [
'title' => $this->formatTitle(
$this->config->get('whose_list') . "'s Anime List",
'Anime',
$data->title
),
'data' => $data,
]);
}
catch (TypeError $e)
{
$this->notFound(
$this->config->get('whose_list') .
"'s Anime List · Anime · " .
'Anime not found',
'Anime Not Found'
);
}
}
}
// End of AnimeController.php

View File

@ -338,5 +338,43 @@ final class Manga extends Controller {
'staff' => $staff,
]);
}
/**
* View details of a random manga
*
* @throws ContainerException
* @throws NotFoundException
* @throws InvalidArgumentException
* @throws Throwable
* @return void
*/
public function random(): void
{
$data = $this->model->getRandomManga();
$staff = [];
$characters = [];
if ($data->isEmpty())
{
$this->notFound(
$this->config->get('whose_list') .
"'s Manga List · Manga · " .
'Manga not found',
'Manga Not Found'
);
return;
}
$this->outputHTML('manga/details', [
'title' => $this->formatTitle(
$this->config->get('whose_list') . "'s Manga List",
'Manga',
$data['title']
),
'characters' => $characters,
'data' => $data,
'staff' => $staff,
]);
}
}
// End of MangaController.php

View File

@ -83,6 +83,16 @@ class Anime extends API {
return $this->kitsuModel->getAnime($slug);
}
/**
* Get information about a random anime
*
* @return AnimeType
*/
public function getRandomAnime(): AnimeType
{
return $this->kitsuModel->getRandomAnime();
}
/**
* Get anime by its kitsu id
*

View File

@ -68,6 +68,16 @@ class Manga extends API {
return $this->kitsuModel->getManga($manga_id);
}
/**
* Get the details of a random manga
*
* @return MangaPage
*/
public function getRandomManga(): MangaPage
{
return $this->kitsuModel->getRandomManga();
}
/**
* Get anime by its kitsu id
*