Better handle empty lists on sync, resolves #29

This commit is contained in:
Timothy Warren 2017-09-15 15:04:57 -04:00
bovenliggende f9a667b20b
commit 4b248eb8cf
3 gewijzigde bestanden met toevoegingen van 77 en 8 verwijderingen

Bestand weergeven

@ -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',
]
];

Bestand weergeven

@ -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]
: [];
}
/**

Bestand weergeven

@ -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);