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};
@ -253,7 +254,7 @@ class Model {
$baseData = $this->getRawMediaData('manga', $mangaId); $baseData = $this->getRawMediaData('manga', $mangaId);
return $this->mangaTransformer->transform($baseData); return $this->mangaTransformer->transform($baseData);
} }
/** /**
* Get the number of anime list items * Get the number of anime list items
* *
@ -274,16 +275,16 @@ class Model {
'sort' => '-updated_at' 'sort' => '-updated_at'
] ]
]; ];
if ( ! empty($status)) if ( ! empty($status))
{ {
$options['query']['filter']['status'] = $status; $options['query']['filter']['status'] = $status;
} }
$response = $this->getRequest('library-entries', $options); $response = $this->getRequest('library-entries', $options);
return $response['meta']['count']; return $response['meta']['count'];
} }
/** /**
@ -310,10 +311,10 @@ class Model {
'sort' => '-updated_at' 'sort' => '-updated_at'
]; ];
$options = array_merge($defaultOptions, $options); $options = array_merge($defaultOptions, $options);
return $this->setUpRequest('GET', 'library-entries', ['query' => $options]); return $this->setUpRequest('GET', 'library-entries', ['query' => $options]);
} }
/** /**
* Get the full anime list * Get the full anime list
* *
@ -324,19 +325,20 @@ 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 = [];
// Set up requests // Set up requests
for ($i = 0; $i < $pages; $i++) for ($i = 0; $i < $pages; $i++)
{ {
$offset = $i * $size; $offset = $i * $size;
$requests[] = $this->getPagedAnimeList($size, $offset, $options); $requests[] = $this->getPagedAnimeList($size, $offset, $options);
} }
$promiseArray = (new Client())->requestMulti($requests); $promiseArray = (new Client())->requestMulti($requests);
$responses = wait(all($promiseArray)); $responses = wait(all($promiseArray));
@ -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', 'sort' => '-updated_at'
'page' => [
'offset' => $offset,
'limit' => $limit
],
'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');
@ -470,7 +444,7 @@ class Model {
$mappedStatus = MangaReadingStatus::KITSU_TO_TITLE[$status]; $mappedStatus = MangaReadingStatus::KITSU_TO_TITLE[$status];
$output[$mappedStatus] = $this->getMangaList($status); $output[$mappedStatus] = $this->getMangaList($status);
} }
return $output; return $output;
} }