Browse Source

Clean up commands a little bit

pull/23/head
Timothy Warren 2 months ago
parent
commit
73488d8244
3 changed files with 1 additions and 257 deletions
  1. 0
    1
      console
  2. 0
    241
      src/Command/MALIDCheck.php
  3. 1
    15
      src/Command/SyncLists.php

+ 0
- 1
console View File

@@ -23,7 +23,6 @@ try
23 23
 		'refresh:thumbnails' => Command\UpdateThumbnails::class,
24 24
 		'regenerate-thumbnails' => Command\UpdateThumbnails::class,
25 25
 		'lists:sync' => Command\SyncLists::class,
26
-		'mal_id:check' => Command\MALIDCheck::class,
27 26
 	]))->run();
28 27
 }
29 28
 catch (\Exception $e)

+ 0
- 241
src/Command/MALIDCheck.php View File

@@ -1,241 +0,0 @@
1
-<?php declare(strict_types=1);
2
-/**
3
- * Hummingbird Anime List Client
4
- *
5
- * An API client for Kitsu to manage anime and manga watch lists
6
- *
7
- * PHP version 7.1
8
- *
9
- * @package     HummingbirdAnimeClient
10
- * @author      Timothy J. Warren <tim@timshomepage.net>
11
- * @copyright   2015 - 2018  Timothy J. Warren
12
- * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
13
- * @version     4.1
14
- * @link        https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
15
- */
16
-
17
-namespace Aviat\AnimeClient\Command;
18
-
19
-use const Aviat\AnimeClient\SRC_DIR;
20
-
21
-use function Amp\Promise\wait;
22
-
23
-use Aviat\AnimeClient\API\{
24
-	APIRequestBuilder,
25
-	JsonAPI,
26
-	ParallelAPIRequest
27
-};
28
-
29
-use Aviat\Ion\Json;
30
-
31
-
32
-final class MALIDCheck extends BaseCommand {
33
-
34
-	private $kitsuModel;
35
-
36
-	/**
37
-	 * Check MAL mapping validity
38
-	 *
39
-	 * @param array $args
40
-	 * @param array $options
41
-	 * @throws \Aviat\Ion\Di\Exception\ContainerException
42
-	 * @throws \Aviat\Ion\Di\Exception\NotFoundException
43
-	 * @throws \Throwable
44
-	 */
45
-	public function execute(array $args, array $options = []): void
46
-	{
47
-		$this->setContainer($this->setupContainer());
48
-		$this->setCache($this->container->get('cache'));
49
-		$this->kitsuModel = $this->container->get('kitsu-model');
50
-
51
-		$kitsuAnimeIdList = $this->formatKitsuList('anime');
52
-		$animeCount = count($kitsuAnimeIdList);
53
-		$this->echoBox("{$animeCount} mappings for Anime");
54
-		$animeMappings = $this->checkMALIds($kitsuAnimeIdList, 'anime');
55
-		$this->mappingStatus($animeMappings, $animeCount, 'anime');
56
-
57
-		$kitsuMangaIdList = $this->formatKitsuList('manga');
58
-		$mangaCount = count($kitsuMangaIdList);
59
-		$this->echoBox("{$mangaCount} mappings for Manga");
60
-		$mangaMappings = $this->checkMALIds($kitsuMangaIdList, 'manga');
61
-		$this->mappingStatus($mangaMappings, $mangaCount, 'manga');
62
-
63
-		$publicDir = realpath(SRC_DIR . '/../public') . '/';
64
-		file_put_contents($publicDir . 'mal_mappings.json', Json::encode([
65
-			'anime' => $animeMappings,
66
-			'manga' => $mangaMappings,
67
-		]));
68
-
69
-		$this->echoBox('Mapping file saved to "' . $publicDir . 'mal_mappings.json' . '"');
70
-	}
71
-
72
-	/**
73
-	 * Format a kitsu list for the sake of comparision
74
-	 *
75
-	 * @param string $type
76
-	 * @return array
77
-	 */
78
-	private function formatKitsuList(string $type = 'anime'): array
79
-	{
80
-		$options = [
81
-			'include' => 'media,media.mappings',
82
-		];
83
-		$data = $this->kitsuModel->{'getFullRaw' . ucfirst($type) . 'List'}($options);
84
-
85
-		if (empty($data))
86
-		{
87
-			return [];
88
-		}
89
-
90
-		$includes = JsonAPI::organizeIncludes($data['included']);
91
-
92
-		// Only bother with mappings from MAL that are of the specified media type
93
-		$includes['mappings'] = array_filter($includes['mappings'], function ($mapping) use ($type) {
94
-			return $mapping['externalSite'] === "myanimelist/{$type}";
95
-		});
96
-
97
-		$output = [];
98
-
99
-		foreach ($data['data'] as $listItem)
100
-		{
101
-			$id = $listItem['relationships']['media']['data']['id'];
102
-			$mediaItem = $includes[$type][$id];
103
-
104
-			// Set titles
105
-			$listItem['titles'] = $mediaItem['titles'];
106
-
107
-			$potentialMappings = $mediaItem['relationships']['mappings'];
108
-			$malId = NULL;
109
-
110
-			foreach ($potentialMappings as $mappingId)
111
-			{
112
-				if (array_key_exists($mappingId, $includes['mappings']))
113
-				{
114
-					$malId = $includes['mappings'][$mappingId]['externalId'];
115
-				}
116
-			}
117
-
118
-			// Skip to the next item if there isn't a MAL ID
119
-			if ($malId === NULL)
120
-			{
121
-				continue;
122
-			}
123
-
124
-			// Group by malIds to simplify lookup of media details
125
-			// for checking validity of the malId mappings
126
-			$output[$malId] = $listItem;
127
-		}
128
-
129
-		ksort($output);
130
-
131
-		return $output;
132
-	}
133
-
134
-	/**
135
-	 * Check for valid Kitsu -> MAL mapping
136
-	 *
137
-	 * @param array $kitsuList
138
-	 * @param string $type
139
-	 * @return array
140
-	 * @throws \Throwable
141
-	 */
142
-	private function checkMALIds(array $kitsuList, string $type): array
143
-	{
144
-		$goodMappings = [];
145
-		$badMappings = [];
146
-		$suspectMappings = [];
147
-
148
-		$responses = $this->makeMALRequests(array_keys($kitsuList), $type);
149
-
150
-		// If the page returns a 404, put it in the bad mappings list
151
-		// otherwise, do a search against the titles, to see if the mapping
152
-		// seems valid
153
-		foreach($responses as $id => $response)
154
-		{
155
-			$body = wait($response->getBody());
156
-			$titles = $kitsuList[$id]['titles'];
157
-
158
-			if ($response->getStatus() === 404)
159
-			{
160
-				$badMappings[$id] = $titles;
161
-			}
162
-			else
163
-			{
164
-				$titleMatches = FALSE;
165
-
166
-				// Attempt to determine if the id matches
167
-				// By searching for a matching title
168
-				foreach($titles as $title)
169
-				{
170
-					if (empty($title))
171
-					{
172
-						continue;
173
-					}
174
-
175
-					if (mb_stripos($body, $title) !== FALSE)
176
-					{
177
-						$titleMatches = TRUE;
178
-						$goodMappings[$id] = $title;
179
-
180
-						// Continue on outer loop
181
-						continue 2;
182
-					}
183
-				}
184
-
185
-				if ( ! $titleMatches)
186
-				{
187
-					$suspectMappings[$id] = $titles;
188
-				}
189
-				else
190
-				{
191
-					$goodMappings[$id] = $titles;
192
-				}
193
-			}
194
-		}
195
-
196
-		return [
197
-			'good' => $goodMappings,
198
-			'bad' => $badMappings,
199
-			'suspect' => $suspectMappings,
200
-		];
201
-	}
202
-
203
-	private function makeMALRequests(array $ids, string $type): array
204
-	{
205
-		$baseUrl = "https://myanimelist.net/{$type}/";
206
-
207
-		$requestChunks = array_chunk($ids, 50, TRUE);
208
-		$responses = [];
209
-
210
-		// Chunk parallel requests so that we don't hit rate
211
-		// limiting, and get spurious 404 HTML responses
212
-		foreach($requestChunks as $idChunk)
213
-		{
214
-			$requester = new ParallelAPIRequest();
215
-
216
-			foreach($idChunk as $id)
217
-			{
218
-				$request = APIRequestBuilder::simpleRequest($baseUrl . $id);
219
-				$requester->addRequest($request, (string)$id);
220
-			}
221
-
222
-			foreach($requester->getResponses() as $id => $response)
223
-			{
224
-				$responses[$id] = $response;
225
-			}
226
-		}
227
-
228
-		return $responses;
229
-	}
230
-
231
-	private function mappingStatus(array $mapping, int $count, string $type): void
232
-	{
233
-		$good = count($mapping['good']);
234
-		$bad = count($mapping['bad']);
235
-		$suspect = count($mapping['suspect']);
236
-
237
-		$uType = ucfirst($type);
238
-
239
-		$this->echoBox("{$uType} mappings: {$good}/{$count} Good, {$suspect}/{$count} Suspect, {$bad}/{$count} Broken");
240
-	}
241
-}

+ 1
- 15
src/Command/SyncLists.php View File

@@ -308,19 +308,9 @@ final class SyncLists extends BaseCommand {
308 308
 		$anilistUpdateItems = [];
309 309
 		$kitsuUpdateItems = [];
310 310
 
311
-		/* $malBlackList = ($type === 'anime')
312
-			? [
313
-				27821, // Fate/stay night: Unlimited Blade Works - Prologue
314
-				29317, // Saekano: How to Raise a Boring Girlfriend Prologue
315
-				30514, // Nisekoinogatari
316
-			] : [
317
-				114638, // Cells at Work: Black
318
-			]; */
319
-
320 311
 		$malIds = array_keys($anilistList);
321 312
 		$kitsuMalIds = array_map('intval', array_column($kitsuList, 'malId'));
322 313
 		$missingMalIds = array_diff($malIds, $kitsuMalIds);
323
-		// $missingMalIds = array_diff($missingMalIds, $malBlackList);
324 314
 
325 315
 		// Add items on Anilist, but not Kitsu to Kitsu
326 316
 		foreach($missingMalIds as $mid)
@@ -335,11 +325,6 @@ final class SyncLists extends BaseCommand {
335 325
 		{
336 326
 			$malId = $kitsuItem['malId'];
337 327
 
338
-			/* if (\in_array((int)$malId, $malBlackList, TRUE))
339
-			{
340
-				continue;
341
-			} */
342
-
343 328
 			if (array_key_exists($malId, $anilistList))
344 329
 			{
345 330
 				$anilistItem = $anilistList[$malId];
@@ -676,6 +661,7 @@ final class SyncLists extends BaseCommand {
676 661
 				}
677 662
 				catch (MissingIdException $e)
678 663
 				{
664
+					// Case where there's a MAL mapping from Kitsu, but no equivalent Anlist item
679 665
 					$id = $item['mal_id'];
680 666
 					$this->echoBox("Skipping Anilist ${type} with mal_id: {$id} due to missing mapping");
681 667
 				}

Loading…
Cancel
Save