Fetch Manga List via GraphQL, see #33

This commit is contained in:
Timothy Warren 2020-10-16 13:28:35 -04:00
parent dddef5add6
commit 22de5776a7
4 changed files with 30 additions and 100 deletions

View File

@ -20,7 +20,6 @@ use Amp\Http\Client\Request;
use Aviat\AnimeClient\API\Kitsu\Transformer\AnimeListTransformer; use Aviat\AnimeClient\API\Kitsu\Transformer\AnimeListTransformer;
use Aviat\AnimeClient\Kitsu as K; use Aviat\AnimeClient\Kitsu as K;
use Aviat\AnimeClient\API\Enum\AnimeWatchingStatus\Kitsu as KitsuWatchingStatus; use Aviat\AnimeClient\API\Enum\AnimeWatchingStatus\Kitsu as KitsuWatchingStatus;
use Aviat\AnimeClient\API\JsonAPI;
use Aviat\AnimeClient\API\Kitsu\Transformer\AnimeHistoryTransformer; use Aviat\AnimeClient\API\Kitsu\Transformer\AnimeHistoryTransformer;
use Aviat\AnimeClient\API\Kitsu\Transformer\OldAnimeListTransformer; use Aviat\AnimeClient\API\Kitsu\Transformer\OldAnimeListTransformer;
use Aviat\AnimeClient\API\Kitsu\Transformer\AnimeTransformer; use Aviat\AnimeClient\API\Kitsu\Transformer\AnimeTransformer;
@ -151,52 +150,6 @@ trait AnimeTrait {
return $list; return $list;
} }
/**
* Get the anime list for the configured user
*
* @param string $status - The watching status to filter the list with
* @return array
* @throws InvalidArgumentException
*/
public function oldGetAnimeList(string $status): array
{
$key = "kitsu-anime-list-{$status}";
$list = $this->cache->get($key, NULL);
if ($list === NULL)
{
$data = $this->getRawAnimeList($status) ?? [];
// Bail out on no data
if (empty($data))
{
return [];
}
$included = JsonAPI::organizeIncludes($data['included']);
$included = JsonAPI::inlineIncludedRelationships($included, 'anime');
foreach($data['data'] as $i => &$item)
{
$item['included'] = $included;
}
unset($item);
$transformed = $this->oldListTransformer->transformCollection($data['data']);
$keyed = [];
foreach($transformed as $item)
{
$keyed[$item['id']] = $item;
}
$list = $keyed;
$this->cache->set($key, $list);
}
return $list;
}
/** /**
* Get the number of anime list items * Get the number of anime list items
* *

View File

@ -21,6 +21,7 @@ use Aviat\AnimeClient\Kitsu as K;
use Aviat\AnimeClient\API\Enum\MangaReadingStatus\Kitsu as KitsuReadingStatus; use Aviat\AnimeClient\API\Enum\MangaReadingStatus\Kitsu as KitsuReadingStatus;
use Aviat\AnimeClient\API\JsonAPI; use Aviat\AnimeClient\API\JsonAPI;
use Aviat\AnimeClient\API\Kitsu\Transformer\MangaHistoryTransformer; use Aviat\AnimeClient\API\Kitsu\Transformer\MangaHistoryTransformer;
use Aviat\AnimeClient\API\Kitsu\Transformer\MangaListTransformer;
use Aviat\AnimeClient\API\Kitsu\Transformer\OldMangaListTransformer; use Aviat\AnimeClient\API\Kitsu\Transformer\OldMangaListTransformer;
use Aviat\AnimeClient\API\Kitsu\Transformer\MangaTransformer; use Aviat\AnimeClient\API\Kitsu\Transformer\MangaTransformer;
use Aviat\AnimeClient\API\Mapping\MangaReadingStatus; use Aviat\AnimeClient\API\Mapping\MangaReadingStatus;
@ -109,54 +110,35 @@ trait MangaTrait {
* Get the manga list for the configured user * Get the manga list for the configured user
* *
* @param string $status - The reading status by which to filter the list * @param string $status - The reading status by which to filter the list
* @param int $limit - The number of list items to fetch per page
* @param int $offset - The page offset
* @return array * @return array
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function getMangaList(string $status, int $limit = 200, int $offset = 0): array public function getMangaList(string $status): array
{ {
$options = [
'query' => [
'filter' => [
'user_id' => $this->getUserId(),
'kind' => 'manga',
'status' => $status,
],
'include' => 'media,media.categories,media.mappings',
'page' => [
'offset' => $offset,
'limit' => $limit
],
'sort' => '-updated_at'
]
];
$key = "kitsu-manga-list-{$status}"; $key = "kitsu-manga-list-{$status}";
$list = $this->cache->get($key, NULL); $list = $this->cache->get($key, NULL);
if ($list === NULL) if ($list === NULL)
{ {
$data = $this->requestBuilder->getRequest('library-entries', $options) ?? []; $data = $this->getRawList(ListType::MANGA, $status) ?? [];
// Bail out on no data // Bail out on no data
if (empty($data) || ( ! array_key_exists('included', $data))) if (empty($data))
{ {
return []; return [];
} }
$included = JsonAPI::organizeIncludes($data['included']); $transformer = new MangaListTransformer();
$included = JsonAPI::inlineIncludedRelationships($included, 'manga'); $transformed = $transformer->transformCollection($data);
$keyed = [];
foreach($data['data'] as $i => &$item) foreach($transformed as $item)
{ {
$item['included'] = $included; $keyed[$item['id']] = $item;
} }
unset($item);
$list = $this->mangaListTransformer->transformCollection($data['data']);
$list = $keyed;
$this->cache->set($key, $list); $this->cache->set($key, $list);
} }

View File

@ -79,6 +79,7 @@ query (
} }
...on Manga { ...on Manga {
chapterCount chapterCount
volumeCount
subtype subtype
} }
} }

View File

@ -36,21 +36,14 @@ final class MangaListTransformer extends AbstractTransformer {
*/ */
public function transform($item): MangaListItem public function transform($item): MangaListItem
{ {
$included = $item['included']; $mangaId = $item['media']['id'];
$mangaId = $item['relationships']['media']['data']['id']; $manga = $item['media'];
$manga = $included['manga'][$mangaId];
$genres = []; $genres = [];
foreach ($manga['relationships']['categories'] as $genre) // Rating is 1-20, we want 1-10
{ $rating = (int) $item['rating'] !== 0
$genres[] = $genre['title']; ? $item['rating'] / 2
}
sort($genres);
$rating = (int) $item['attributes']['ratingTwenty'] !== 0
? $item['attributes']['ratingTwenty'] / 2
: '-'; : '-';
$totalChapters = ((int) $manga['chapterCount'] !== 0) $totalChapters = ((int) $manga['chapterCount'] !== 0)
@ -61,17 +54,18 @@ final class MangaListTransformer extends AbstractTransformer {
? $manga['volumeCount'] ? $manga['volumeCount']
: '-'; : '-';
$readChapters = ((int) $item['attributes']['progress'] !== 0) $readChapters = ((int) $item['progress'] !== 0)
? $item['attributes']['progress'] ? $item['progress']
: '-'; : '-';
$MALid = NULL; $MALid = NULL;
if (array_key_exists('mappings', $manga['relationships'])) $mappings = $manga['mappings']['nodes'] ?? [];
if ( ! empty($mappings))
{ {
foreach ($manga['relationships']['mappings'] as $mapping) foreach ($mappings as $mapping)
{ {
if ($mapping['externalSite'] === 'myanimelist/manga') if ($mapping['externalSite'] === 'MYANIMELIST_MANGA')
{ {
$MALid = $mapping['externalId']; $MALid = $mapping['externalId'];
break; break;
@ -79,8 +73,8 @@ final class MangaListTransformer extends AbstractTransformer {
} }
} }
$titles = Kitsu::filterTitles($manga); $titles = Kitsu::getFilteredTitles($manga['titles']);
$title = array_shift($titles); $title = $manga['titles']['canonical'];
return MangaListItem::from([ return MangaListItem::from([
'id' => $item['id'], 'id' => $item['id'],
@ -96,17 +90,17 @@ final class MangaListTransformer extends AbstractTransformer {
'manga' => MangaListItemDetail::from([ 'manga' => MangaListItemDetail::from([
'genres' => $genres, 'genres' => $genres,
'id' => $mangaId, 'id' => $mangaId,
'image' => $manga['posterImage']['small'], 'image' => $manga['posterImage']['views'][1]['url'],
'slug' => $manga['slug'], 'slug' => $manga['slug'],
'title' => $title, 'title' => $title,
'titles' => $titles, 'titles' => $titles,
'type' => (string)StringType::from($manga['subtype'])->upperCaseFirst(), 'type' => (string)StringType::from($manga['subtype'])->toLowerCase()->upperCaseFirst(),
'url' => 'https://kitsu.io/manga/' . $manga['slug'], 'url' => 'https://kitsu.io/manga/' . $manga['slug'],
]), ]),
'reading_status' => $item['attributes']['status'], 'reading_status' => strtolower($item['status']),
'notes' => $item['attributes']['notes'], 'notes' => $item['notes'],
'rereading' => (bool)$item['attributes']['reconsuming'], 'rereading' => (bool)$item['reconsuming'],
'reread' => $item['attributes']['reconsumeCount'], 'reread' => $item['reconsumeCount'],
'user_rating' => $rating, 'user_rating' => $rating,
]); ]);
} }