Version 5.1 - All the GraphQL #32

Closed
timw4mail wants to merge 1160 commits from develop into master
6 changed files with 94 additions and 86 deletions
Showing only changes of commit e8a9982f9a - Show all commits

View File

@ -14,6 +14,7 @@
<th>Type</th> <th>Type</th>
<th>Progress</th> <th>Progress</th>
<th>Rated</th> <th>Rated</th>
<th>Notes</th>
<th>Genres</th> <th>Genres</th>
</tr> </tr>
</thead> </thead>
@ -26,15 +27,16 @@
</a> </a>
</td> </td>
<td class="align_left"><?= $item['anime']['alternate_title'] ?></td> <td class="align_left"><?= $item['anime']['alternate_title'] ?></td>
<td class="align_left"><?= $item['anime']['status'] ?></td> <td class="align_left"><?= $item['airing']['status'] ?></td>
<td><?= (int)($item['rating']['value'] * 2) ?> / 10 </td> <td><?= $item['user_rating'] ?> / 10 </td>
<td><?= $item['anime']['show_type'] ?></td> <td><?= $item['anime']['type'] ?></td>
<td>Episodes: <?= $item['episodes_watched'] ?> / <?= $item['anime']['episode_count'] ?></td> <td>Episodes: <?= $item['episodes']['watched'] ?> / <?= $item['episodes']['total'] ?></td>
<td><?= $item['anime']['age_rating'] ?></td> <td><?= $item['anime']['age_rating'] ?></td>
<td><?= $item['notes'] ?></td>
<td class="flex flex-justify-space-around align-left"> <td class="flex flex-justify-space-around align-left">
<?php sort($item['anime']['genres']) ?> <?php sort($item['anime']['genres']) ?>
<?php foreach($item['anime']['genres'] as $genre): ?> <?php foreach($item['anime']['genres'] as $genre): ?>
<span><?= $genre['name'] ?></span> <span><?= $genre ?></span>
<?php endforeach ?> <?php endforeach ?>
</td> </td>
</tr> </tr>

View File

@ -1,7 +1,7 @@
<main> <main>
<?php if (is_logged_in()): ?> <?php /* if (is_logged_in()): ?>
[<a href="<?= $urlGenerator->full_url('collection/add', 'anime') ?>">Add Item</a>] [<a href="<?= $urlGenerator->full_url('collection/add', 'anime') ?>">Add Item</a>]
<?php endif ?> <?php endif */ ?>
<?php if (empty($sections)): ?> <?php if (empty($sections)): ?>
<h3>There's nothing here!</h3> <h3>There's nothing here!</h3>
<?php else: ?> <?php else: ?>
@ -17,9 +17,9 @@
<th>Show Type</th> <th>Show Type</th>
<th>Age Rating</th> <th>Age Rating</th>
<th>Notes</th> <th>Notes</th>
<?php if (is_logged_in()): ?> <?php /*if (is_logged_in()): ?>
<th>&nbsp;</th> <th>&nbsp;</th>
<?php endif ?> <?php endif*/ ?>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -42,9 +42,9 @@
<td><?= $item['show_type'] ?></td> <td><?= $item['show_type'] ?></td>
<td><?= $item['age_rating'] ?></td> <td><?= $item['age_rating'] ?></td>
<td class="align_left"><?= $item['notes'] ?></td> <td class="align_left"><?= $item['notes'] ?></td>
<?php if (is_logged_in()): ?> <?php /* if (is_logged_in()): ?>
<td>[<a href="<?= $urlGenerator->full_url("collection/edit/{$item['hummingbird_id']}", "anime") ?>">Edit</a>]</td> <td>[<a href="<?= $urlGenerator->full_url("collection/edit/{$item['hummingbird_id']}", "anime") ?>">Edit</a>]</td>
<?php endif ?> <?php endif */ ?>
</tr> </tr>
<?php endforeach ?> <?php endforeach ?>
</tbody> </tbody>

View File

@ -1,7 +1,7 @@
<main> <main>
<?= $message ?> <?= $message ?>
<aside> <aside>
<form method="post" action="<?= $config->full_url('/login', $url_type) ?>"> <form method="post" action="<?= $urlGenerator->full_url('/login', $url_type) ?>">
<dl> <dl>
<?php /*<dt><label for="username">Username: </label></dt> <?php /*<dt><label for="username">Username: </label></dt>
<dd><input type="text" id="username" name="username" required="required" /></dd>*/ ?> <dd><input type="text" id="username" name="username" required="required" /></dd>*/ ?>

View File

@ -14,27 +14,27 @@
<button class="plus_one_volume">+1 Volume</button> <button class="plus_one_volume">+1 Volume</button>
</div> </div>
<?php endif*/ ?> <?php endif*/ ?>
<img src="<?= $escape->attr($item['manga']['poster_image']) ?>" /> <img src="<?= $escape->attr($item['manga']['image']) ?>" />
<div class="name"> <div class="name">
<a href="https://hummingbird.me/manga/<?= $item['manga']['id'] ?>"> <a href="<?= $item['manga']['url'] ?>">
<?= $escape->html($item['manga']['romaji_title']) ?> <?= $escape->html($item['manga']['title']) ?>
<?= (isset($item['manga']['english_title'])) ? "<br />({$item['manga']['english_title']})" : ""; ?> <?= (isset($item['manga']['alternate_title'])) ? "<br />({$item['manga']['alternate_title']})" : ""; ?>
</a> </a>
</div> </div>
<div class="table"> <div class="table">
<div class="row"> <div class="row">
<div class="user_rating">Rating: <?= ($item['rating'] > 0) ? (int)($item['rating'] * 2) : '-' ?> / 10</div> <div class="user_rating">Rating: <?= $item['user_rating'] ?> / 10</div>
</div> </div>
<div class="row"> <div class="row">
<div class="chapter_completion"> <div class="chapter_completion">
Chapters: <span class="chapters_read"><?= $item['chapters_read'] ?></span> / Chapters: <span class="chapters_read"><?= $item['chapters']['read'] ?></span> /
<span class="chapter_count"><?= ($item['manga']['chapter_count'] > 0) ? $item['manga']['chapter_count'] : "-" ?></span> <span class="chapter_count"><?= $item['chapters']['total'] ?></span>
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="volume_completion"> <div class="volume_completion">
Volumes: <span class="volumes_read"><?= $item['volumes_read'] ?></span> / Volumes: <span class="volumes_read"><?= $item['volumes']['read'] ?></span> /
<span class="volume_count"><?= ($item['manga']['volume_count'] > 0) ? $item['manga']['volume_count'] : "-" ?></span> <span class="volume_count"><?= $item['volumes']['total'] ?></span>
</div> </div>
</div> </div>
</div> </div>

View File

@ -19,14 +19,14 @@
<tr id="manga-<?= $item['manga']['id'] ?>"> <tr id="manga-<?= $item['manga']['id'] ?>">
<td class="align_left"> <td class="align_left">
<a href="https://hummingbird.me/manga/<?= $item['manga']['id'] ?>"> <a href="https://hummingbird.me/manga/<?= $item['manga']['id'] ?>">
<?= $item['manga']['romaji_title'] ?> <?= $item['manga']['title'] ?>
</a> </a>
<?= (array_key_exists('english_title', $item['manga'])) ? " &middot; " . $item['manga']['english_title'] : "" ?> <?= ( ! is_null($item['manga']['alternate_title'])) ? " &middot; " . $item['manga']['alternate_title'] : "" ?>
</td> </td>
<td><?= ($item['rating'] > 0) ? (int)($item['rating'] * 2) : '-' ?> / 10</td> <td><?= $item['user_rating'] ?> / 10</td>
<td><?= $item['chapters_read'] ?> / <?= ($item['manga']['chapter_count'] > 0) ? $item['manga']['chapter_count'] : "-" ?></td> <td><?= $item['chapters']['read'] ?> / <?= $item['chapters']['total'] ?></td>
<td><?= $item['volumes_read'] ?> / <?= ($item['manga']['volume_count'] > 0) ? $item['manga']['volume_count'] : "-" ?></td> <td><?= $item['volumes']['read'] ?> / <?= $item['volumes']['total'] ?></td>
<td><?= $item['manga']['manga_type'] ?></td> <td><?= $item['manga']['type'] ?></td>
</tr> </tr>
<?php endforeach ?> <?php endforeach ?>
</tbody> </tbody>

View File

@ -6,11 +6,21 @@
namespace Aviat\AnimeClient\Model; namespace Aviat\AnimeClient\Model;
use Aviat\AnimeClient\Model\API; use Aviat\AnimeClient\Model\API;
use Aviat\AnimeClient\Enum\Hummingbird\AnimeWatchingStatus;
use Aviat\AnimeClient\Transformer\Hummingbird\AnimeListTransformer;
/** /**
* Model for handling requests dealing with the anime list * Model for handling requests dealing with the anime list
*/ */
class Anime extends API { class Anime extends API {
// Display constants
const WATCHING = 'Watching';
const PLAN_TO_WATCH = 'Plan to Watch';
const DROPPED = 'Dropped';
const ON_HOLD = 'On Hold';
const COMPLETED = 'Completed';
/** /**
* The base url for api requests * The base url for api requests
* @var string $base_url * @var string $base_url
@ -42,11 +52,11 @@ class Anime extends API {
public function get_all_lists() public function get_all_lists()
{ {
$output = [ $output = [
'Watching' => [], self::WATCHING => [],
'Plan to Watch' => [], self::PLAN_TO_WATCH => [],
'On Hold' => [], self::ON_HOLD => [],
'Dropped' => [], self::DROPPED => [],
'Completed' => [], self::COMPLETED => [],
]; ];
$data = $this->_get_list(); $data = $this->_get_list();
@ -55,24 +65,24 @@ class Anime extends API {
{ {
switch($datum['status']) switch($datum['status'])
{ {
case "completed": case AnimeWatchingStatus::COMPLETED:
$output['Completed'][] = $datum; $output[self::COMPLETED][] = $datum;
break; break;
case "plan-to-watch": case AnimeWatchingStatus::PLAN_TO_WATCH:
$output['Plan to Watch'][] = $datum; $output[self::PLAN_TO_WATCH][] = $datum;
break; break;
case "dropped": case AnimeWatchingStatus::DROPPED:
$output['Dropped'][] = $datum; $output[self::DROPPED][] = $datum;
break; break;
case "on-hold": case AnimeWatchingStatus::ON_HOLD:
$output['On Hold'][] = $datum; $output[self::ON_HOLD][] = $datum;
break; break;
case "currently-watching": case AnimeWatchingStatus::WATCHING:
$output['Watching'][] = $datum; $output[self::WATCHING][] = $datum;
break; break;
} }
} }
@ -95,14 +105,14 @@ class Anime extends API {
public function get_list($status) public function get_list($status)
{ {
$map = [ $map = [
'currently-watching' => 'Watching', AnimeWatchingStatus::WATCHING => self::WATCHING,
'plan-to-watch' => 'Plan to Watch', AnimeWatchingStatus::PLAN_TO_WATCH => self::PLAN_TO_WATCH,
'on-hold' => 'On Hold', AnimeWatchingStatus::ON_HOLD => self::ON_HOLD,
'dropped' => 'Dropped', AnimeWatchingStatus::DROPPED => self::DROPPED,
'completed' => 'Completed', AnimeWatchingStatus::COMPLETED => self::COMPLETED,
]; ];
$data = $this->_get_list($status); $data = $this->_get_list_From_api($status);
$this->sort_by_name($data); $this->sort_by_name($data);
$output = []; $output = [];
@ -158,17 +168,13 @@ class Anime extends API {
} }
/** /**
* Actually retreive the data from the api * Retrieve data from the api
* *
* @param string $status - Status to filter by * @param string $status
* @return array * @return array
*/ */
private function _get_list($status="all") private function _get_list_from_api($status="all")
{ {
$errorHandler = $this->container->get('error-handler');
$cache_file = "{$this->config->data_cache_path}/anime-{$status}.json";
$config = [ $config = [
'allow_redirects' => FALSE 'allow_redirects' => FALSE
]; ];
@ -179,45 +185,45 @@ class Anime extends API {
} }
$response = $this->client->get("users/{$this->config->hummingbird_username}/library", $config); $response = $this->client->get("users/{$this->config->hummingbird_username}/library", $config);
$output = $this->_check_cache($status, $response);
$errorHandler->addDataTable('anime_list_response', (array)$response);
if ($response->getStatusCode() != 200)
{
if ( ! file_exists($cache_file))
{
throw new DomainException($response->getEffectiveUrl());
}
else
{
$output = json_decode(file_get_contents($cache_file), TRUE);
}
}
else
{
$output = $response->json();
$output_json = json_encode($output);
if (( ! file_exists($cache_file)) || file_get_contents($cache_file) !== $output_json)
{
// Attempt to create the cache folder if it doesn't exist
if ( ! is_dir($this->config->data_cache_path))
{
mkdir($this->config->data_cache_path);
}
// Cache the call in case of downtime
file_put_contents($cache_file, json_encode($output));
}
}
foreach($output as &$row) foreach($output as &$row)
{ {
$row['anime']['cover_image'] = $this->get_cached_image($row['anime']['cover_image'], $row['anime']['slug'], 'anime'); $row['anime']['image'] = $this->get_cached_image($row['anime']['image'], $row['anime']['slug'], 'anime');
} }
return $output; return $output;
} }
/**
* Handle caching of transformed api data
*
* @param string $status
* @param \GuzzleHttp\Message\Response
* @return array
*/
private function _check_cache($status, $response)
{
$cache_file = "{$this->config->data_cache_path}/anime-{$status}.json";
$transformed_cache_file = "{$this->config->data_cache_path}/anime-{$status}-transformed.json";
$cached = json_decode(file_get_contents($cache_file), TRUE);
$api = $response->json();
if ($api !== $cached)
{
file_put_contents($cache_file, json_encode($api));
$transformer = new AnimeListTransformer();
$transformed = $transformer->transform_collection($api);
file_put_contents($transformed_cache_file, json_encode($transformed));
return $transformed;
}
else
{
return json_decode(file_get_contents($transformed_cache_file),TRUE);
}
}
/** /**
* Sort the list by title * Sort the list by title
* *