Better handling of alternate titles, Airing Status and genres for anime list views

This commit is contained in:
Timothy Warren 2017-01-05 22:24:45 -05:00
parent 9eda005399
commit 4c75701c0d
22 changed files with 379 additions and 213 deletions

View File

@ -18,8 +18,10 @@
<?= $helper->img($item['anime']['image']); ?>
<div class="name">
<a href="<?= $url->generate('anime.details', ['id' => $item['anime']['slug']]); ?>">
<?= $escape->html($item['anime']['title']) ?>
<?= ($item['anime']['alternate_title'] != "") ? "<br />({$item['anime']['alternate_title']})" : ""; ?>
<?= array_shift($item['anime']['titles']) ?>
<?php foreach ($item['anime']['titles'] as $title): ?>
<br /><small><?= $title ?></small>
<?php endforeach ?>
</a>
</div>
<div class="table">

View File

@ -5,10 +5,10 @@
<br />
<br />
<table>
<?php /*<tr>
<tr>
<td class="align_right">Airing Status</td>
<td><?= $data['status'] ?></td>
</tr>*/ ?>
</tr>
<tr>
<td>Show Type</td>
<td><?= $data['show_type'] ?></td>
@ -34,14 +34,10 @@
</table>
</div>
<div>
<h2><a rel="external" href="<?= $data['url'] ?>"><?= $data['title'] ?></a></h2>
<?php if ( ! empty($data['jp_title'])): ?>
<h3><?= $data['jp_title'] ?></h3>
<?php endif ?>
<?php if( ! empty($data['en_title'] && $data['en_title'] !== $data['title'])): ?>
<h3><?= $data['en_title'] ?></h3>
<?php endif ?>
<h2><a rel="external" href="<?= $data['url'] ?>"><?= array_shift($data['titles']) ?></a></h2>
<?php foreach ($data['titles'] as $title): ?>
<h3><?= $title ?></h3>
<?php endforeach ?>
<br />
<p><?= nl2br($data['synopsis']) ?></p>
</div>

View File

@ -6,10 +6,10 @@
<thead>
<tr>
<th>
<h3><?= $escape->html($item['anime']['title']) ?></h3>
<?php if($item['anime']['alternate_title'] != ""): ?>
<h4><?= $escape->html($item['anime']['alternate_title']) ?></h4>
<?php endif ?>
<h3><?= $escape->html(array_shift($item['anime']['titles'])) ?></h3>
<?php foreach($item['anime']['titles'] as $title): ?>
<h4><?= $escape->html($title) ?></h4>
<?php endforeach ?>
</th>
<th>
<article class="media">

View File

@ -26,7 +26,7 @@
</thead>
<tbody>
<?php foreach($items as $item): ?>
<?php if ($item['private']) && ! $auth->is_authenticated()) continue; ?>
<?php if ($item['private'] && ! $auth->is_authenticated()) continue; ?>
<tr id="a-<?= $item['id'] ?>">
<?php if ($auth->is_authenticated()): ?>
<td>
@ -35,36 +35,38 @@
<?php endif ?>
<td class="justify">
<a href="<?= $url->generate('anime.details', ['id' => $item['anime']['slug']]) ?>">
<?= $item['anime']['title'] ?>
<?= array_shift($item['anime']['titles']) ?>
</a>
<?= ( ! empty($item['anime']['alternate_title'])) ? " <br /> " . $item['anime']['alternate_title'] : "" ?>
<?php foreach($item['anime']['titles'] as $title): ?>
<br /><?= $title ?>
<?php endforeach ?>
</td>
<td class="align_left"><?= $item['airing']['status'] ?></td>
<td><?= $item['airing']['status'] ?></td>
<td><?= $item['user_rating'] ?> / 10 </td>
<td><?= $item['anime']['type'] ?></td>
<td class="align_left" id="<?= $item['anime']['slug'] ?>">
<td id="<?= $item['anime']['slug'] ?>">
Episodes: <br />
<span class="completed_number"><?= $item['episodes']['watched'] ?></span>&nbsp;/&nbsp;<span class="total_number"><?= $item['episodes']['total'] ?></span>
</td>
<td><?= $item['anime']['age_rating'] ?></td>
<td>
<ul>
<?php if ($item['rewatched'] > 0): ?>
Rewatched <?= $item['rewatched'] ?> time(s)<br />
<li>Rewatched <?= $item['rewatched'] ?> time(s)</li>
<?php endif ?>
<?php $attr_list = []; ?>
<?php foreach(['private','rewatching'] as $attr): ?>
<?php if($item[$attr]): ?>
<?php $attr_list[] = ucfirst($attr); ?>
<li><?= ucfirst($attr); ?></li>
<?php endif ?>
<?php endforeach ?>
<?= implode(', ', $attr_list); ?>
</ul>
</td>
<td>
<p><?= $escape->html($item['notes']) ?></p>
</td>
<td class="align_left">
<?php sort($item['anime']['genres']) ?>
<?= join(', ', $item['anime']['genres']) ?>
<?= implode(', ', $item['anime']['genres']) ?>
</td>
</tr>
<?php endforeach ?>

View File

@ -1,7 +1,7 @@
<main>
<?php /* if ($auth->is_authenticated()): ?>
<?php if ($auth->is_authenticated()): ?>
<a class="bracketed" href="<?= $urlGenerator->url('manga/add') ?>">Add Item</a>
<?php endif */ ?>
<?php endif ?>
<?php if (empty($sections)): ?>
<h3>There's nothing here!</h3>
<?php else: ?>
@ -11,26 +11,28 @@
<section class="media-wrap">
<?php foreach($items as $item): ?>
<article class="media" id="manga-<?= $item['id'] ?>">
<?php /*if ($auth->is_authenticated()): ?>
<?php if ($auth->is_authenticated()): ?>
<div class="edit_buttons" hidden>
<button class="plus_one_chapter">+1 Chapter</button>
</div>
<?php endif */ ?>
<?php endif ?>
<img src="<?= $escape->attr($item['manga']['image']) ?>" />
<div class="name">
<a href="<?= $url->generate('manga.details', ['id' => $item['manga']['slug']]) ?>">
<?= $escape->html($item['manga']['title']) ?>
<?= (isset($item['manga']['alternate_title'])) ? "<br />({$item['manga']['alternate_title']})" : ""; ?>
<?= $escape->html(array_shift($item['manga']['titles'])) ?>
<?php foreach($item['manga']['titles'] as $title): ?>
<br /><small><?= $title ?></small>
<?php endforeach ?>
</a>
</div>
<div class="table">
<?php /*if ($auth->is_authenticated()): ?>
<?php if ($auth->is_authenticated()): ?>
<div class="row">
<span class="edit">
<a class="bracketed" title="Edit information about this manga" href="<?= $urlGenerator->url("manga/edit/{$item['id']}/{$name}") ?>">Edit</a>
</span>
</div>
<?php endif */ ?>
<?php endif ?>
<div class="row">
<div class="user_rating">Rating: <?= $item['user_rating'] ?> / 10</div>
</div>
@ -53,6 +55,6 @@
<?php endforeach ?>
<?php endif ?>
</main>
<?php /*if ($auth->is_authenticated()): ?>
<?php if ($auth->is_authenticated()): ?>
<script src="<?= $urlGenerator->asset_url('js.php/g/edit') ?>"></script>
<?php endif*/ ?>
<?php endif ?>

View File

@ -1,18 +1,17 @@
<?php if ($auth->is_authenticated()): ?>
<main>
<h1>
Edit <?= $item['manga']['title'] ?>
<?= ($item['manga']['alternate_title'] != "") ? "({$item['manga']['alternate_title']})" : ""; ?>
Edit <?= $item['manga']['titles'][0] ?>
</h1>
<form action="<?= $action ?>" method="post">
<table class="form">
<thead>
<tr>
<th>
<h3><?= $escape->html($item['manga']['title']) ?></h3>
<?php if($item['manga']['alternate_title'] != ""): ?>
<h4><?= $escape->html($item['manga']['alternate_title']) ?></h4>
<?php endif ?>
<h3><?= $escape->html(array_shift($item['manga']['titles'])) ?></h3>
<?php foreach($item['manga']['titles'] as $title): ?>
<h4><?= $escape->html($title) ?></h4>
<?php endforeach ?>
</th>
<th>
<article class="media">
@ -45,12 +44,12 @@
<input type="number" min="0" name="chapters_read" id="chapters_read" value="<?= $item['chapters']['read'] ?>" /> / <?= $item['chapters']['total'] ?>
</td>
</tr>
<tr>
<? /*<tr>
<td><label for="volumes_read">Volumes Read</label></td>
<td>
<input type="number" min="0" name="volumes_read" id="volumes_read" value="<?= $item['volumes']['read'] ?>" /> / <?= $item['volumes']['total'] ?>
</td>
</tr>
</tr> */ ?>
<tr>
<td><label for="rereading_flag">Rereading?</label></td>
<td>

View File

@ -10,9 +10,9 @@
<table>
<thead>
<tr>
<?php /*if ($auth->is_authenticated()): ?>
<?php if ($auth->is_authenticated()): ?>
<th>&nbsp;</th>
<?php endif*/ ?>
<?php endif ?>
<th>Title</th>
<th>Rating</th>
<th>Completed Chapters</th>
@ -23,16 +23,18 @@
<tbody>
<?php foreach($items as $item): ?>
<tr id="manga-<?= $item['id'] ?>">
<?php /*if($auth->is_authenticated()): ?>
<?php if($auth->is_authenticated()): ?>
<td>
<a class="bracketed" href="<?= $urlGenerator->url("manga/edit/{$item['id']}/{$name}") ?>">Edit</a>
</td>
<?php endif*/ ?>
<?php endif ?>
<td class="align_left">
<a href="<?= $url->generate('manga.details', ['id' => $item['manga']['slug']]) ?>">
<?= $item['manga']['title'] ?>
<?= array_shift($item['manga']['titles']) ?>
</a>
<?= ( ! is_null($item['manga']['alternate_title'])) ? " &middot; " . $item['manga']['alternate_title'] : "" ?>
<?php foreach($item['manga']['titles'] as $title): ?>
<br /><?= $title ?>
<?php endforeach ?>
</td>
<td><?= $item['user_rating'] ?> / 10</td>
<td><?= $item['chapters']['read'] ?> / <?= $item['chapters']['total'] ?></td>

View File

@ -53,12 +53,11 @@ $whoops = new Run();
$defaultHandler = new PrettyPageHandler();
$whoops->pushHandler($defaultHandler);
// Set up json handler for ajax errors
//$jsonHandler = new JsonResponseHandler();
//$whoops->pushHandler($jsonHandler);
// Register as the error handler
$whoops->register();
if (array_key_exists('whoops', $_GET))
{
$whoops->register();
}
// -----------------------------------------------------------------------------
// Dependency Injection setup

View File

@ -1085,7 +1085,9 @@ a:hover, a:active {
display:block;
}
.media > .name > a {
.media > .name a,
.media > .name a small
{
background:none;
color:#fff;
text-shadow:1px 2px 1px rgba(0, 0, 0, .85);

View File

@ -354,7 +354,9 @@ a:hover, a:active {
display:block;
}
.media > .name > a {
.media > .name a,
.media > .name a small
{
background:none;
color:#fff;
text-shadow: var(--shadow);

View File

@ -16,13 +16,20 @@
namespace Aviat\AnimeClient\API;
use Aviat\AnimeClient\API\Kitsu\Enum\AnimeWatchingStatus;
use Aviat\AnimeClient\API\Kitsu\Enum\{AnimeAiringStatus, AnimeWatchingStatus};
use DateTimeImmutable;
/**
* Constants and mappings for the Kitsu API
*/
class Kitsu {
const AUTH_URL = 'https://kitsu.io/api/oauth/token';
/**
* Map of Kitsu status to label for select menus
*
* @return array
*/
public static function getStatusToSelectMap()
{
return [
@ -33,4 +40,147 @@ class Kitsu {
AnimeWatchingStatus::DROPPED => 'Dropped'
];
}
/**
* Determine whether an anime is airing, finished airing, or has not yet aired
*
* @param string $startDate
* @param string $endDate
* @return string
*/
public static function getAiringStatus(string $startDate = null, string $endDate = null): string
{
$startAirDate = new DateTimeImmutable($startDate ?? 'tomorrow');
$endAirDate = new DateTimeImmutable($endDate ?? 'tomorrow');
$now = new DateTimeImmutable();
$isDoneAiring = $now > $endAirDate;
$isCurrentlyAiring = ($now > $startAirDate) && ! $isDoneAiring;
switch (true)
{
case $isCurrentlyAiring:
return AnimeAiringStatus::AIRING;
case $isDoneAiring:
return AnimeAiringStatus::FINISHED_AIRING;
default:
return AnimeAiringStatus::NOT_YET_AIRED;
}
}
/**
* Filter out duplicate and very similar names from
*
* @param array $data The 'attributes' section of the api data response
* @return array List of alternate titles
*/
public static function filterTitles(array $data): array
{
// The 'canonical' title is always returned
$valid = [$data['canonicalTitle']];
if (array_key_exists('titles', $data))
{
foreach($data['titles'] as $alternateTitle)
{
if (self::titleIsUnique($alternateTitle, $valid))
{
$valid[] = $alternateTitle;
}
}
}
return $valid;
}
/**
* Reorganizes 'included' data to be keyed by
* type => [
* id => data/attributes,
* ]
*
* @param array $includes
* @return array
*/
public static function organizeIncludes(array $includes): array
{
$organized = [];
foreach ($includes as $item)
{
$type = $item['type'];
$id = $item['id'];
$organized[$type] = $organized[$type] ?? [];
$organized[$type][$id] = $item['attributes'];
if (array_key_exists('relationships', $item))
{
$organized[$type][$id]['relationships'] = self::organizeRelationships($item['relationships']);
}
}
return $organized;
}
/**
* Reorganize relationship mappings to make them simpler to use
*
* Remove verbose structure, and just map:
* type => [ idArray ]
*
* @param array $relationships
* @return array
*/
public static function organizeRelationships(array $relationships): array
{
$organized = [];
foreach($relationships as $key => $data)
{
if ( ! array_key_exists('data', $data))
{
continue;
}
$organized[$key] = $organized[$key] ?? [];
foreach ($data['data'] as $item)
{
$organized[$key][] = $item['id'];
}
}
return $organized;
}
/**
* Determine if an alternate title is unique enough to list
*
* @param string $title
* @param array $existingTitles
* @return bool
*/
private static function titleIsUnique(string $title = null, array $existingTitles): bool
{
if (empty($title))
{
return false;
}
foreach($existingTitles as $existing)
{
$isSubset = stripos($existing, $title) !== FALSE;
$diff = levenshtein($existing, $title);
$onlydifferentCase = (mb_strtolower($existing) === mb_strtolower($title));
if ($diff < 3 || $isSubset || $onlydifferentCase)
{
return false;
}
}
return true;
}
}

View File

@ -17,6 +17,7 @@
namespace Aviat\AnimeClient\API\Kitsu;
use Aviat\AnimeClient\AnimeClient;
use Aviat\AnimeClient\API\Kitsu as K;
use Aviat\AnimeClient\API\Kitsu\Transformer\{
AnimeTransformer, AnimeListTransformer, MangaTransformer, MangaListTransformer
};
@ -92,7 +93,7 @@ class KitsuModel {
*/
public function authenticate(string $username, string $password)
{
$data = $this->postRequest(AnimeClient::KITSU_AUTH_URL, [
$data = $this->postRequest(K::AUTH_URL, [
'form_params' => [
'grant_type' => 'password',
'username' => $username,
@ -164,7 +165,7 @@ class KitsuModel {
'media_type' => 'Anime',
'status' => $status,
],
'include' => 'media',
'include' => 'media,media.genres',
'page' => [
'offset' => 0,
'limit' => 200
@ -174,10 +175,21 @@ class KitsuModel {
];
$data = $this->getRequest('library-entries', $options);
$included = K::organizeIncludes($data['included']);
/*?><pre><?= print_r($included, TRUE) ?></pre><?php*/
foreach($data['data'] as $i => &$item)
{
$item['anime'] = $data['included'][$i];
$item['anime'] = $included['anime'][$item['relationships']['media']['data']['id']];
$animeGenres = $item['anime']['relationships']['genres'];
foreach($animeGenres as $id)
{
$item['genres'][] = $included['genres'][$id]['name'];
}
// $item['genres'] = array_pluck($genres, 'name');
}
$transformed = $this->animeListTransformer->transformCollection($data['data']);
@ -246,7 +258,9 @@ class KitsuModel {
'filter' => [
'slug' => $slug
],
'include' => 'genres,mappings,streamingLinks',
'include' => ($type === 'anime')
? 'genres,mappings,streamingLinks'
: 'genres,mappings',
]
];

View File

@ -18,7 +18,6 @@ namespace Aviat\AnimeClient\API\Kitsu;
use Aviat\AnimeClient\AnimeClient;
use Aviat\AnimeClient\API\GuzzleTrait;
use Aviat\Ion\Di\ContainerAware;
use Aviat\Ion\Json;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;

View File

@ -16,6 +16,7 @@
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
use Aviat\AnimeClient\API\Kitsu;
use Aviat\Ion\Transformer\AbstractTransformer;
/**
@ -34,49 +35,34 @@ class AnimeListTransformer extends AbstractTransformer {
{
/* ?><pre><?= print_r($item, TRUE) ?></pre><?php
// die(); */
$anime =& $item['anime'];
$genres = $item['anime']['genres'] ?? [];
$anime = $item['anime']['attributes'] ?? $item['anime'];
$genres = $item['genres'] ?? [];
$rating = (int) 2 * $item['attributes']['rating'];
$total_episodes = array_key_exists('episodeCount', $item['anime']['attributes'])
? (int) $anime['attributes']['episodeCount']
$total_episodes = array_key_exists('episodeCount', $anime)
? (int) $anime['episodeCount']
: '-';
$alternate_title = NULL;
if (array_key_exists('titles', $item['anime']) && array_key_exists('en_jp', $anime['titles']))
{
// If the alternate title is very similar, or
// a subset of the main title, don't list the
// alternate title
$not_subset = stripos($anime['canonicalTitle'], $anime['titles']['en_jp']) === FALSE;
$diff = levenshtein($anime['canonicalTitle'], $anime['titles']['en_jp'] ?? '');
if ($not_subset && $diff >= 5)
{
$alternate_title = $anime['titles']['en_jp'];
}
}
return [
'id' => $item['id'],
'episodes' => [
'watched' => $item['attributes']['progress'],
'total' => $total_episodes,
'length' => $anime['attributes']['episodeLength'],
'length' => $anime['episodeLength'],
],
'airing' => [
'status' => $anime['status'] ?? '',
'started' => $anime['attributes']['startDate'],
'ended' => $anime['attributes']['endDate']
'status' => Kitsu::getAiringStatus($anime['startDate'], $anime['endDate']),
'started' => $anime['startDate'],
'ended' => $anime['endDate']
],
'anime' => [
'age_rating' => $anime['attributes']['ageRating'],
'title' => $anime['attributes']['canonicalTitle'],
'alternate_title' => $alternate_title,
'slug' => $anime['attributes']['slug'],
'url' => $anime['attributes']['url'] ?? '',
'type' => $anime['attributes']['showType'],
'image' => $anime['attributes']['posterImage']['small'],
'age_rating' => $anime['ageRating'],
'titles' => Kitsu::filterTitles($anime),
'slug' => $anime['slug'],
'url' => $anime['url'] ?? '',
'type' => $this->string($anime['showType'])->upperCaseFirst()->__toString(),
'image' => $anime['posterImage']['small'],
'genres' => $genres,
],
'watching_status' => $item['attributes']['status'],

View File

@ -16,6 +16,7 @@
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
use Aviat\AnimeClient\API\Kitsu;
use Aviat\Ion\Transformer\AbstractTransformer;
/**
@ -32,15 +33,12 @@ class AnimeTransformer extends AbstractTransformer {
*/
public function transform($item)
{
?><pre><?= print_r($item, TRUE) ?></pre><?php
$item['genres'] = $item['genres'] ?? [];
sort($item['genres']);
return [
'title' => $item['canonicalTitle'],
'en_title' => $item['titles']['en_jp'],
'jp_title' => $item['titles']['ja_jp'],
'titles' => Kitsu::filterTitles($item),
'status' => Kitsu::getAiringStatus($item['startDate'], $item['endDate']),
'cover_image' => $item['posterImage']['small'],
'show_type' => $item['showType'],
'episode_count' => $item['episodeCount'],

View File

@ -16,6 +16,7 @@
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
use Aviat\AnimeClient\API\Kitsu;
use Aviat\Ion\StringWrapper;
use Aviat\Ion\Transformer\AbstractTransformer;
@ -34,6 +35,7 @@ class MangaListTransformer extends AbstractTransformer {
*/
public function transform($item)
{
/*?><pre><?= print_r($item, TRUE) ?></pre><?php*/
$manga =& $item['manga'];
$rating = (is_numeric($item['attributes']['rating']))
@ -59,10 +61,10 @@ class MangaListTransformer extends AbstractTransformer {
'total' => $total_volumes
],
'manga' => [
'title' => $manga['attributes']['canonicalTitle'],
'titles' => Kitsu::filterTitles($manga['attributes']),
'alternate_title' => NULL,
'slug' => $manga['id'],
'url' => 'https://kitsu.io/manga/' . $manga['id'],
'slug' => $manga['attributes']['slug'],
'url' => 'https://kitsu.io/manga/' . $manga['attributes']['slug'],
'type' => $manga['attributes']['mangaType'],
'image' => $manga['attributes']['posterImage']['small'],
'genres' => [], //$manga['genres'],

View File

@ -25,7 +25,6 @@ define('SRC_DIR', realpath(__DIR__));
*/
class AnimeClient {
const KITSU_AUTH_URL = 'https://kitsu.io/api/oauth/token';
const SESSION_SEGMENT = 'Aviat\AnimeClient\Auth';
const DEFAULT_CONTROLLER_NAMESPACE = 'Aviat\AnimeClient\Controller';
const DEFAULT_CONTROLLER = 'Aviat\AnimeClient\Controller\Anime';

View File

@ -293,7 +293,7 @@ class Anime extends BaseController {
$data = $this->model->getAnime($anime_id);
$this->outputHTML('anime/details', [
'title' => 'Anime &middot ' . $data['title'],
'title' => 'Anime &middot ' . $data['titles'][0],
'data' => $data,
]);
}

View File

@ -161,7 +161,7 @@ class Manga extends Controller {
public function edit($id, $status = "All")
{
$this->set_session_redirect();
$item = $this->model->get_library_item($id, $status);
$item = $this->model->getLibraryItem($id);
$title = $this->config->get('whose_list') . "'s Manga List &middot; Edit";
$this->outputHTML('manga/edit', [

View File

@ -69,7 +69,7 @@ class API extends Model {
foreach ($array as $key => $item)
{
$sort[$key] = $item[$sort_key]['title'];
$sort[$key] = $item[$sort_key]['titles'][0];
}
array_multisort($sort, SORT_ASC, $array);

View File

@ -83,6 +83,18 @@ class Manga extends API
return $this->kitsuModel->getManga($manga_id);
}
/**
* Get information about a specific list item
* for editing/updating that item
*
* @param string $itemId
* @return array
*/
public function getLibraryItem(string $itemId): array
{
return $this->kitsuModel->getListItem($itemId);
}
/**
* Map transformed anime data to be organized by reading status
*