Better handle empty lists on sync, resolves #29

This commit is contained in:
Timothy Warren 2017-09-15 15:04:57 -04:00
parent f9a667b20b
commit 4b248eb8cf
3 changed files with 77 additions and 8 deletions

View File

@ -277,7 +277,7 @@ class Model {
'fields' => [ 'fields' => [
'media' => 'id,slug' 'media' => 'id,slug'
], ],
'include' => 'media' 'include' => 'item'
] ]
]; ];
@ -403,7 +403,7 @@ class Model {
* @return array * @return array
*/ */
public function getFullAnimeList(array $options = [ public function getFullAnimeList(array $options = [
'include' => 'anime.mappings' 'include' => 'media.mappings'
]): array ]): array
{ {
$status = $options['filter']['status'] ?? ''; $status = $options['filter']['status'] ?? '';
@ -560,6 +560,18 @@ class Model {
return $transformed; 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 * Get the manga list for the configured user
* *
@ -590,7 +602,15 @@ class Model {
if ( ! $cacheItem->isHit()) 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::organizeIncludes($data['included']);
$included = JsonAPI::inlineIncludedRelationships($included, 'manga'); $included = JsonAPI::inlineIncludedRelationships($included, 'manga');
@ -842,8 +862,8 @@ class Model {
$options = [ $options = [
'query' => [ 'query' => [
'include' => ($type === 'anime') 'include' => ($type === 'anime')
? 'genres,mappings,streamingLinks' ? 'categories,mappings,streamingLinks'
: 'genres,mappings', : 'categories,mappings',
] ]
]; ];

View File

@ -104,7 +104,7 @@ class Model {
* @param string $type "anime" or "manga" * @param string $type "anime" or "manga"
* @return array * @return array
*/ */
public function getList(string $type): array public function getList(string $type = "anime"): array
{ {
$config = $this->container->get('config'); $config = $this->container->get('config');
$userName = $config->get(['mal', 'username']); $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]
: [];
} }
/** /**

View File

@ -70,7 +70,18 @@ class SyncKitsuWithMal extends BaseCommand {
public function sync(string $type) public function sync(string $type)
{ {
$uType = ucfirst($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"}(); $kitsuCount = $this->kitsuModel->{"get{$uType}ListCount"}();
$this->echoBox("Number of MAL {$type} list items: {$malCount}"); $this->echoBox("Number of MAL {$type} list items: {$malCount}");
@ -141,6 +152,21 @@ class SyncKitsuWithMal extends BaseCommand {
$orig = $this->malModel->getList('anime'); $orig = $this->malModel->getList('anime');
$output = []; $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) foreach($orig as $item)
{ {
$output[$item['series_animedb_id']] = [ $output[$item['series_animedb_id']] = [
@ -165,6 +191,21 @@ class SyncKitsuWithMal extends BaseCommand {
$orig = $this->malModel->getList('manga'); $orig = $this->malModel->getList('manga');
$output = []; $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) foreach($orig as $item)
{ {
$output[$item['series_mangadb_id']] = [ $output[$item['series_mangadb_id']] = [
@ -189,6 +230,12 @@ class SyncKitsuWithMal extends BaseCommand {
public function formatKitsuList(string $type = 'anime'): array public function formatKitsuList(string $type = 'anime'): array
{ {
$data = $this->kitsuModel->{'getFull' . ucfirst($type) . 'List'}(); $data = $this->kitsuModel->{'getFull' . ucfirst($type) . 'List'}();
if (empty($data))
{
return [];
}
$includes = JsonAPI::organizeIncludes($data['included']); $includes = JsonAPI::organizeIncludes($data['included']);
$includes['mappings'] = $this->filterMappings($includes['mappings'], $type); $includes['mappings'] = $this->filterMappings($includes['mappings'], $type);