Commit 4b248eb8 by Timothy J Warren

Better handle empty lists on sync, resolves #29

parent f9a667b2
......@@ -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'] ?? '';
......@@ -561,6 +561,18 @@ class Model {
}
/**
* 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
*
* @param string $status - The reading status by which to filter the list
......@@ -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',
]
];
......
......@@ -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]
: [];
}
/**
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment