All anime api calls are now using paginated requests, see #23

This commit is contained in:
Timothy Warren 2017-03-22 11:13:50 -04:00
parent 1a45e57b7c
commit c9768855a5

View File

@ -22,6 +22,7 @@ use Amp\Artax\{Client, Request};
use Aviat\AnimeClient\API\{CacheTrait, JsonAPI, Kitsu as K}; use Aviat\AnimeClient\API\{CacheTrait, JsonAPI, Kitsu as K};
use Aviat\AnimeClient\API\Enum\{ use Aviat\AnimeClient\API\Enum\{
AnimeWatchingStatus\Title, AnimeWatchingStatus\Title,
AnimeWatchingStatus\Kitsu as KitsuWatchingStatus,
MangaReadingStatus\Kitsu as KitsuReadingStatus MangaReadingStatus\Kitsu as KitsuReadingStatus
}; };
use Aviat\AnimeClient\API\Mapping\{AnimeWatchingStatus, MangaReadingStatus}; use Aviat\AnimeClient\API\Mapping\{AnimeWatchingStatus, MangaReadingStatus};
@ -324,8 +325,9 @@ class Model {
'include' => 'anime.mappings' 'include' => 'anime.mappings'
]): array ]): array
{ {
$count = $this->getAnimeListCount(); $status = $options['filter']['status'] ?? '';
$size = 75; $count = $this->getAnimeListCount($status);
$size = 100;
$pages = ceil($count / $size); $pages = ceil($count / $size);
$requests = []; $requests = [];
@ -355,29 +357,22 @@ class Model {
* Get the raw (unorganized) anime list for the configured user * Get the raw (unorganized) anime list for the configured user
* *
* @param string $status - The watching status to filter the list with * @param string $status - The watching status to filter the list with
* @param int $limit - The number of list entries to fetch for a page
* @param int $offset - The page offset
* @return array * @return array
*/ */
public function getRawAnimeList(string $status, int $limit = 600, int $offset = 0): array public function getRawAnimeList(string $status): array
{ {
$options = [ $options = [
'query' => [
'filter' => [ 'filter' => [
'user_id' => $this->getUserIdByUsername($this->getUsername()), 'user_id' => $this->getUserIdByUsername($this->getUsername()),
'media_type' => 'Anime', 'media_type' => 'Anime',
'status' => $status, 'status' => $status,
], ],
'include' => 'media,media.genres,media.mappings,anime.streamingLinks', 'include' => 'media,media.genres,media.mappings,anime.streamingLinks',
'page' => [
'offset' => $offset,
'limit' => $limit
],
'sort' => '-updated_at' 'sort' => '-updated_at'
]
]; ];
return $this->getRequest('library-entries', $options); return $this->getFullAnimeList($options);
} }
/** /**
@ -392,31 +387,12 @@ class Model {
if ( ! $cacheItem->isHit()) if ( ! $cacheItem->isHit())
{ {
$output = [ $output = [
Title::WATCHING => [], Title::WATCHING => $this->getAnimeList(KitsuWatchingStatus::WATCHING),
Title::PLAN_TO_WATCH => [], Title::PLAN_TO_WATCH => $this->getAnimeList(KitsuWatchingStatus::PLAN_TO_WATCH),
Title::ON_HOLD => [], Title::ON_HOLD => $this->getAnimeList(KitsuWatchingStatus::ON_HOLD),
Title::DROPPED => [], Title::DROPPED => $this->getAnimeList(KitsuWatchingStatus::DROPPED),
Title::COMPLETED => [] Title::COMPLETED => $this->getAnimeList(KitsuWatchingStatus::COMPLETED)
]; ];
$statusMap = AnimeWatchingStatus::KITSU_TO_TITLE;
$data = $this->getFullAnimeList([
'include' => 'media,media.genres,media.mappings,anime.streamingLinks'
]);
$included = JsonAPI::organizeIncludes($data['included']);
$included = JsonAPI::inlineIncludedRelationships($included, 'anime');
foreach($data['data'] as $i => &$item)
{
$item['included'] = $included;
}
$transformed = $this->animeListTransformer->transformCollection($data['data']);
foreach($transformed as $item)
{
$key = $statusMap[$item['watching_status']];
$output[$key][] = $item;
}
$cacheItem->set($output); $cacheItem->set($output);
$cacheItem->save(); $cacheItem->save();
@ -429,17 +405,15 @@ class Model {
* Get the anime list for the configured user * Get the anime list for the configured user
* *
* @param string $status - The watching status to filter the list with * @param string $status - The watching status to filter the list with
* @param int $limit - The number of list entries to fetch for a page
* @param int $offset - The page offset
* @return array * @return array
*/ */
public function getAnimeList(string $status, int $limit = 600, int $offset = 0): array public function getAnimeList(string $status): array
{ {
$cacheItem = $this->cache->getItem("kitsu-anime-list-{$status}"); $cacheItem = $this->cache->getItem("kitsu-anime-list-{$status}");
if ( ! $cacheItem->isHit()) if ( ! $cacheItem->isHit())
{ {
$data = $this->getRawAnimeList($status, $limit, $offset); $data = $this->getRawAnimeList($status);
$included = JsonAPI::organizeIncludes($data['included']); $included = JsonAPI::organizeIncludes($data['included']);
$included = JsonAPI::inlineIncludedRelationships($included, 'anime'); $included = JsonAPI::inlineIncludedRelationships($included, 'anime');