Browse Source

Better handle empty lists on sync, resolves #29

Timothy J. Warren 1 year ago
parent
commit
4b248eb8cf
3 changed files with 77 additions and 8 deletions
  1. 25
    5
      src/API/Kitsu/Model.php
  2. 4
    2
      src/API/MAL/Model.php
  3. 48
    1
      src/Command/SyncKitsuWithMal.php

+ 25
- 5
src/API/Kitsu/Model.php View File

@@ -277,7 +277,7 @@ class Model {
277 277
 				'fields' => [
278 278
 					'media' => 'id,slug'
279 279
 				],
280
-				'include' => 'media'
280
+				'include' => 'item'
281 281
 			]
282 282
 		];
283 283
 
@@ -403,7 +403,7 @@ class Model {
403 403
 	 * @return array
404 404
 	 */
405 405
 	public function getFullAnimeList(array $options = [
406
-		'include' => 'anime.mappings'
406
+		'include' => 'media.mappings'
407 407
 	]): array
408 408
 	{
409 409
 		$status = $options['filter']['status'] ?? '';
@@ -560,6 +560,18 @@ class Model {
560 560
 		return $transformed;
561 561
 	}
562 562
 
563
+	/**
564
+	 * Get information about a particular manga
565
+	 *
566
+	 * @param string $mangaId
567
+	 * @return array
568
+	 */
569
+	public function getMangaById(string $mangaId): array
570
+	{
571
+		$baseData = $this->getRawMediaDataById('manga', $mangaId);
572
+		return $this->mangaTransformer->transform($baseData);
573
+	}
574
+
563 575
 	/**
564 576
 	 * Get the manga list for the configured user
565 577
 	 *
@@ -590,7 +602,15 @@ class Model {
590 602
 
591 603
 		if ( ! $cacheItem->isHit())
592 604
 		{
593
-			$data = $this->getRequest('library-entries', $options);
605
+			$data = $this->getRequest('library-entries', $options) ?? [];
606
+
607
+			// Bail out on no data
608
+			if (empty($data) || ( ! array_key_exists('included', $data)))
609
+			{
610
+				$cacheItem->set([]);
611
+				$cacheItem->save();
612
+				return $cacheItem->get();
613
+			}
594 614
 
595 615
 			$included = JsonAPI::organizeIncludes($data['included']);
596 616
 			$included = JsonAPI::inlineIncludedRelationships($included, 'manga');
@@ -842,8 +862,8 @@ class Model {
842 862
 		$options = [
843 863
 			'query' => [
844 864
 				'include' => ($type === 'anime')
845
-					? 'genres,mappings,streamingLinks'
846
-					: 'genres,mappings',
865
+					? 'categories,mappings,streamingLinks'
866
+					: 'categories,mappings',
847 867
 			]
848 868
 		];
849 869
 

+ 4
- 2
src/API/MAL/Model.php View File

@@ -104,7 +104,7 @@ class Model {
104 104
 	 * @param string $type "anime" or "manga"
105 105
 	 * @return array
106 106
 	 */
107
-	public function getList(string $type): array
107
+	public function getList(string $type = "anime"): array
108 108
 	{
109 109
 		$config = $this->container->get('config');
110 110
 		$userName = $config->get(['mal', 'username']);
@@ -119,7 +119,9 @@ class Model {
119 119
 			]
120 120
 		]);
121 121
 
122
-		return $list['myanimelist'][$type] ?? [];
122
+		return (array_key_exists($type, $list['myanimelist']))
123
+			? $list['myanimelist'][$type]
124
+			: [];
123 125
 	}
124 126
 
125 127
 	/**

+ 48
- 1
src/Command/SyncKitsuWithMal.php View File

@@ -70,7 +70,18 @@ class SyncKitsuWithMal extends BaseCommand {
70 70
 	public function sync(string $type)
71 71
 	{
72 72
 		$uType = ucfirst($type);
73
-		$malCount = count($this->malModel->getList($type));
73
+
74
+		// Do a little check to make sure you don't have immediate issues
75
+		// if you have 0 or 1 items in a list on MAL.
76
+		$malList = $this->malModel->getList($type);
77
+		$malCount = 0;
78
+		if ( ! empty($malList))
79
+		{
80
+			$malCount = (array_key_exists(0, $malList))
81
+				? count($malList)
82
+				: count([$malList]);
83
+		}
84
+
74 85
 		$kitsuCount = $this->kitsuModel->{"get{$uType}ListCount"}();
75 86
 
76 87
 		$this->echoBox("Number of MAL {$type} list items: {$malCount}");
@@ -141,6 +152,21 @@ class SyncKitsuWithMal extends BaseCommand {
141 152
 		$orig = $this->malModel->getList('anime');
142 153
 		$output = [];
143 154
 
155
+		// Bail early on empty list
156
+		if (empty($orig))
157
+		{
158
+			return [];
159
+		}
160
+
161
+		// Due to xml parsing differences,
162
+		// 1 item has no wrapping array.
163
+		// In this case, just re-create the
164
+		// wrapper array
165
+		if ( ! array_key_exists(0, $orig))
166
+		{
167
+			$orig = [$orig];
168
+		}
169
+
144 170
 		foreach($orig as $item)
145 171
 		{
146 172
 			$output[$item['series_animedb_id']] = [
@@ -165,6 +191,21 @@ class SyncKitsuWithMal extends BaseCommand {
165 191
 		$orig = $this->malModel->getList('manga');
166 192
 		$output = [];
167 193
 
194
+		// Bail early on empty list
195
+		if (empty($orig))
196
+		{
197
+			return [];
198
+		}
199
+
200
+		// Due to xml parsing differences,
201
+		// 1 item has no wrapping array.
202
+		// In this case, just re-create the
203
+		// wrapper array
204
+		if ( ! array_key_exists(0, $orig))
205
+		{
206
+			$orig = [$orig];
207
+		}
208
+
168 209
 		foreach($orig as $item)
169 210
 		{
170 211
 			$output[$item['series_mangadb_id']] = [
@@ -189,6 +230,12 @@ class SyncKitsuWithMal extends BaseCommand {
189 230
 	public function formatKitsuList(string $type = 'anime'): array
190 231
 	{
191 232
 		$data = $this->kitsuModel->{'getFull' . ucfirst($type) . 'List'}();
233
+
234
+		if (empty($data))
235
+		{
236
+			return [];
237
+		}
238
+
192 239
 		$includes = JsonAPI::organizeIncludes($data['included']);
193 240
 		$includes['mappings'] = $this->filterMappings($includes['mappings'], $type);
194 241