From 4b248eb8cfd10daa7996b3ce39135d3459bfdab6 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Fri, 15 Sep 2017 15:04:57 -0400 Subject: [PATCH] Better handle empty lists on sync, resolves #29 --- src/API/Kitsu/Model.php | 30 +++++++++++++++---- src/API/MAL/Model.php | 6 ++-- src/Command/SyncKitsuWithMal.php | 49 +++++++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/src/API/Kitsu/Model.php b/src/API/Kitsu/Model.php index 255c0451..182519a6 100644 --- a/src/API/Kitsu/Model.php +++ b/src/API/Kitsu/Model.php @@ -277,7 +277,7 @@ class Model { 'fields' => [ 'media' => 'id,slug' ], - 'include' => 'media' + 'include' => 'item' ] ]; @@ -403,7 +403,7 @@ class Model { * @return array */ public function getFullAnimeList(array $options = [ - 'include' => 'anime.mappings' + 'include' => 'media.mappings' ]): array { $status = $options['filter']['status'] ?? ''; @@ -560,6 +560,18 @@ class Model { return $transformed; } + /** + * Get information about a particular manga + * + * @param string $mangaId + * @return array + */ + public function getMangaById(string $mangaId): array + { + $baseData = $this->getRawMediaDataById('manga', $mangaId); + return $this->mangaTransformer->transform($baseData); + } + /** * Get the manga list for the configured user * @@ -590,7 +602,15 @@ class Model { if ( ! $cacheItem->isHit()) { - $data = $this->getRequest('library-entries', $options); + $data = $this->getRequest('library-entries', $options) ?? []; + + // Bail out on no data + if (empty($data) || ( ! array_key_exists('included', $data))) + { + $cacheItem->set([]); + $cacheItem->save(); + return $cacheItem->get(); + } $included = JsonAPI::organizeIncludes($data['included']); $included = JsonAPI::inlineIncludedRelationships($included, 'manga'); @@ -842,8 +862,8 @@ class Model { $options = [ 'query' => [ 'include' => ($type === 'anime') - ? 'genres,mappings,streamingLinks' - : 'genres,mappings', + ? 'categories,mappings,streamingLinks' + : 'categories,mappings', ] ]; diff --git a/src/API/MAL/Model.php b/src/API/MAL/Model.php index 5ce186bc..35c849ef 100644 --- a/src/API/MAL/Model.php +++ b/src/API/MAL/Model.php @@ -104,7 +104,7 @@ class Model { * @param string $type "anime" or "manga" * @return array */ - public function getList(string $type): array + public function getList(string $type = "anime"): array { $config = $this->container->get('config'); $userName = $config->get(['mal', 'username']); @@ -119,7 +119,9 @@ class Model { ] ]); - return $list['myanimelist'][$type] ?? []; + return (array_key_exists($type, $list['myanimelist'])) + ? $list['myanimelist'][$type] + : []; } /** diff --git a/src/Command/SyncKitsuWithMal.php b/src/Command/SyncKitsuWithMal.php index 3bcb0c4b..56bda066 100644 --- a/src/Command/SyncKitsuWithMal.php +++ b/src/Command/SyncKitsuWithMal.php @@ -70,7 +70,18 @@ class SyncKitsuWithMal extends BaseCommand { public function sync(string $type) { $uType = ucfirst($type); - $malCount = count($this->malModel->getList($type)); + + // Do a little check to make sure you don't have immediate issues + // if you have 0 or 1 items in a list on MAL. + $malList = $this->malModel->getList($type); + $malCount = 0; + if ( ! empty($malList)) + { + $malCount = (array_key_exists(0, $malList)) + ? count($malList) + : count([$malList]); + } + $kitsuCount = $this->kitsuModel->{"get{$uType}ListCount"}(); $this->echoBox("Number of MAL {$type} list items: {$malCount}"); @@ -141,6 +152,21 @@ class SyncKitsuWithMal extends BaseCommand { $orig = $this->malModel->getList('anime'); $output = []; + // Bail early on empty list + if (empty($orig)) + { + return []; + } + + // Due to xml parsing differences, + // 1 item has no wrapping array. + // In this case, just re-create the + // wrapper array + if ( ! array_key_exists(0, $orig)) + { + $orig = [$orig]; + } + foreach($orig as $item) { $output[$item['series_animedb_id']] = [ @@ -165,6 +191,21 @@ class SyncKitsuWithMal extends BaseCommand { $orig = $this->malModel->getList('manga'); $output = []; + // Bail early on empty list + if (empty($orig)) + { + return []; + } + + // Due to xml parsing differences, + // 1 item has no wrapping array. + // In this case, just re-create the + // wrapper array + if ( ! array_key_exists(0, $orig)) + { + $orig = [$orig]; + } + foreach($orig as $item) { $output[$item['series_mangadb_id']] = [ @@ -189,6 +230,12 @@ class SyncKitsuWithMal extends BaseCommand { public function formatKitsuList(string $type = 'anime'): array { $data = $this->kitsuModel->{'getFull' . ucfirst($type) . 'List'}(); + + if (empty($data)) + { + return []; + } + $includes = JsonAPI::organizeIncludes($data['included']); $includes['mappings'] = $this->filterMappings($includes['mappings'], $type);