Kitsu <-> MAL manga list item comparison, resolves #18

This commit is contained in:
Timothy Warren 2017-04-19 16:48:53 -04:00
parent 510ae24dca
commit d8b985c993
2 changed files with 44 additions and 112 deletions

View File

@ -44,9 +44,7 @@ class MangaListTransformer extends AbstractTransformer {
{ {
$map = [ $map = [
'id' => $item['mal_id'], 'id' => $item['mal_id'],
'data' => [ 'data' => []
'chapter' => $item['data']['progress']
]
]; ];
$data =& $item['data']; $data =& $item['data'];
@ -55,6 +53,10 @@ class MangaListTransformer extends AbstractTransformer {
{ {
switch($key) switch($key)
{ {
case 'progress':
$map['data']['chapter'] = $value;
break;
case 'notes': case 'notes':
$map['data']['comments'] = $value; $map['data']['comments'] = $value;
break; break;

View File

@ -63,86 +63,48 @@ class SyncKitsuWithMal extends BaseCommand {
$this->kitsuModel = $this->container->get('kitsu-model'); $this->kitsuModel = $this->container->get('kitsu-model');
$this->malModel = $this->container->get('mal-model'); $this->malModel = $this->container->get('mal-model');
$this->syncAnime(); $this->sync('anime');
$this->syncManga(); $this->sync('manga');
} }
public function syncAnime() public function sync(string $type)
{ {
$malCount = count($this->malModel->getAnimeList()); $uType = ucfirst($type);
$kitsuCount = $this->kitsuModel->getAnimeListCount(); $malCount = count($this->malModel->{"get{$uType}List"}());
$kitsuCount = $this->kitsuModel->{"get{$uType}ListCount"}();
$this->echoBox("Number of MAL anime list items: {$malCount}"); $this->echoBox("Number of MAL {$type} list items: {$malCount}");
$this->echoBox("Number of Kitsu anime list items: {$kitsuCount}"); $this->echoBox("Number of Kitsu {$type} list items: {$kitsuCount}");
$data = $this->diffAnimeLists(); $data = $this->diffLists($type);
if ( ! empty($data['addToMAL'])) if ( ! empty($data['addToMAL']))
{ {
$count = count($data['addToMAL']); $count = count($data['addToMAL']);
$this->echoBox("Adding {$count} missing anime list items to MAL"); $this->echoBox("Adding {$count} missing {$type} list items to MAL");
$this->createMALListItems($data['addToMAL'], 'anime'); $this->createMALListItems($data['addToMAL'], $type);
} }
if ( ! empty($data['addToKitsu'])) if ( ! empty($data['addToKitsu']))
{ {
$count = count($data['addToKitsu']); $count = count($data['addToKitsu']);
$this->echoBox("Adding {$count} missing anime list items to Kitsu"); $this->echoBox("Adding {$count} missing {$type} list items to Kitsu");
$this->createKitsuListItems($data['addToKitsu'], 'anime'); $this->createKitsuListItems($data['addToKitsu'], $type);
} }
if ( ! empty($data['updateMAL'])) if ( ! empty($data['updateMAL']))
{ {
$count = count($data['updateMAL']); $count = count($data['updateMAL']);
$this->echoBox("Updating {$count} outdated MAL anime list items"); $this->echoBox("Updating {$count} outdated MAL {$type} list items");
$this->updateMALListItems($data['updateMAL'], 'anime'); $this->updateMALListItems($data['updateMAL'], $type);
} }
if ( ! empty($data['updateKitsu'])) if ( ! empty($data['updateKitsu']))
{ {
print_r($data['updateKitsu']); print_r($data['updateKitsu']);
$count = count($data['updateKitsu']); $count = count($data['updateKitsu']);
$this->echoBox("Updating {$count} outdated Kitsu anime list items"); $this->echoBox("Updating {$count} outdated Kitsu {$type} list items");
// $this->updateKitsuListItems($data['updateKitsu'], 'anime'); $this->updateKitsuListItems($data['updateKitsu'], $type);
}
}
public function syncManga()
{
$malCount = count($this->malModel->getMangaList());
$kitsuCount = $this->kitsuModel->getMangaListCount();
$this->echoBox("Number of MAL manga list items: {$malCount}");
$this->echoBox("Number of Kitsu manga list items: {$kitsuCount}");
$data = $this->diffMangaLists();
if ( ! empty($data['addToMAL']))
{
$count = count($data['addToMAL']);
$this->echoBox("Adding {$count} missing manga list items to MAL");
$this->createMALListItems($data['addToMAL'], 'manga');
}
if ( ! empty($data['addToKitsu']))
{
$count = count($data['addToKitsu']);
$this->echoBox("Adding {$count} missing manga list items to Kitsu");
$this->createKitsuListItems($data['addToKitsu'], 'manga');
}
if ( ! empty($data['updateMAL']))
{
$count = count($data['updateMAL']);
$this->echoBox("Updating {$count} outdated MAL manga list items");
$this->updateMALListItems($data['updateMAL'], 'manga');
}
if ( ! empty($data['updateKitsu']))
{
$count = count($data['updateKitsu']);
$this->echoBox("Updating {$count} outdated Kitsu manga list items");
$this->updateKitsuListItems($data['updateKitsu'], 'manga');
} }
} }
@ -161,6 +123,19 @@ class SyncKitsuWithMal extends BaseCommand {
return $output; return $output;
} }
public function formatMALList(string $type): array
{
if ($type === 'anime')
{
return $this->formatMALAnimeList();
}
if ($type === 'manga')
{
return $this->formatMALMangaList();
}
}
public function formatMALAnimeList() public function formatMALAnimeList()
{ {
$orig = $this->malModel->getAnimeList(); $orig = $this->malModel->getAnimeList();
@ -211,10 +186,9 @@ class SyncKitsuWithMal extends BaseCommand {
return $output; return $output;
} }
public function filterKitsuList(string $type = 'anime'): array public function formatKitsuList(string $type = 'anime'): array
{ {
$method = "getFull{$type}List"; $data = $this->kitsuModel->{'getFull' . ucfirst($type) . 'List'}();
$data = $this->kitsuModel->$method();
$includes = JsonAPI::organizeIncludes($data['included']); $includes = JsonAPI::organizeIncludes($data['included']);
$includes['mappings'] = $this->filterMappings($includes['mappings'], $type); $includes['mappings'] = $this->filterMappings($includes['mappings'], $type);
@ -250,56 +224,14 @@ class SyncKitsuWithMal extends BaseCommand {
return $output; return $output;
} }
public function diffMangaLists() public function diffLists(string $type = 'anime'): array
{
$kitsuList = $this->filterKitsuList('manga');
$malList = $this->formatMALMangaList();
$itemsToAddToMAL = [];
$itemsToAddToKitsu = [];
$malIds = array_column($malList, 'id');
$kitsuMalIds = array_column($kitsuList, 'malId');
$missingMalIds = array_diff($malIds, $kitsuMalIds);
foreach($missingMalIds as $mid)
{
$itemsToAddToKitsu[] = array_merge($malList[$mid]['data'], [
'id' => $this->kitsuModel->getKitsuIdFromMALId($mid, 'manga'),
'type' => 'manga'
]);
}
foreach($kitsuList as $kitsuItem)
{
if (in_array($kitsuItem['malId'], $malIds))
{
// Eventually, compare the list entries, and determine which
// needs to be updated
continue;
}
// Looks like this item only exists on Kitsu
$itemsToAddToMAL[] = [
'mal_id' => $kitsuItem['malId'],
'data' => $kitsuItem['data']
];
}
return [
'addToMAL' => $itemsToAddToMAL,
'addToKitsu' => $itemsToAddToKitsu
];
}
public function diffAnimeLists()
{ {
// Get libraryEntries with media.mappings from Kitsu // Get libraryEntries with media.mappings from Kitsu
// Organize mappings, and ignore entries without mappings // Organize mappings, and ignore entries without mappings
$kitsuList = $this->filterKitsuList('anime'); $kitsuList = $this->formatKitsuList($type);
// Get MAL list data // Get MAL list data
$malList = $this->formatMALAnimeList(); $malList = $this->formatMALList($type);
$itemsToAddToMAL = []; $itemsToAddToMAL = [];
$itemsToAddToKitsu = []; $itemsToAddToKitsu = [];
@ -314,8 +246,8 @@ class SyncKitsuWithMal extends BaseCommand {
{ {
// print_r($malList[$mid]); // print_r($malList[$mid]);
$itemsToAddToKitsu[] = array_merge($malList[$mid]['data'], [ $itemsToAddToKitsu[] = array_merge($malList[$mid]['data'], [
'id' => $this->kitsuModel->getKitsuIdFromMALId($mid), 'id' => $this->kitsuModel->getKitsuIdFromMALId($mid, $type),
'type' => 'anime' 'type' => $type
]); ]);
} }
@ -323,9 +255,7 @@ class SyncKitsuWithMal extends BaseCommand {
{ {
if (in_array($kitsuItem['malId'], $malIds)) if (in_array($kitsuItem['malId'], $malIds))
{ {
// Eventually, compare the list entries, and determine which $item = $this->compareListItems($kitsuItem, $malList[$kitsuItem['malId']]);
// needs to be updated
$item = $this->compareAnimeListItems($kitsuItem, $malList[$kitsuItem['malId']]);
if (is_null($item)) if (is_null($item))
{ {
@ -361,7 +291,7 @@ class SyncKitsuWithMal extends BaseCommand {
]; ];
} }
public function compareAnimeListItems(array $kitsuItem, array $malItem) public function compareListItems(array $kitsuItem, array $malItem)
{ {
$compareKeys = ['status', 'progress', 'rating', 'reconsuming']; $compareKeys = ['status', 'progress', 'rating', 'reconsuming'];
$diff = []; $diff = [];