Version 5.1 - All the GraphQL #32

Closed
timw4mail wants to merge 1160 commits from develop into master
Showing only changes of commit 40a899066e - Show all commits

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');