2017-01-27 12:35:28 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
2017-02-15 16:13:32 -05:00
|
|
|
* Hummingbird Anime List Client
|
2017-01-27 12:35:28 -05:00
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2017-01-27 12:35:28 -05:00
|
|
|
*
|
2019-12-03 15:17:25 -05:00
|
|
|
* PHP version 7.2
|
2017-01-27 12:35:28 -05:00
|
|
|
*
|
2017-02-15 16:13:32 -05:00
|
|
|
* @package HummingbirdAnimeClient
|
2017-01-27 12:35:28 -05:00
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2020-01-08 15:39:49 -05:00
|
|
|
* @copyright 2015 - 2020 Timothy J. Warren
|
2017-01-27 12:35:28 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2019-12-06 09:16:35 -05:00
|
|
|
* @version 4.2
|
2017-03-07 20:53:58 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
2017-01-27 12:35:28 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\Command;
|
|
|
|
|
2019-12-09 13:13:31 -05:00
|
|
|
use Aviat\AnimeClient\API\{
|
|
|
|
Anilist\MissingIdException,
|
|
|
|
FailedResponseException,
|
|
|
|
JsonAPI,
|
|
|
|
ParallelAPIRequest
|
|
|
|
};
|
2018-09-26 22:31:04 -04:00
|
|
|
use Aviat\AnimeClient\API\Anilist\Transformer\{
|
|
|
|
AnimeListTransformer as AALT,
|
2018-09-27 16:45:12 -04:00
|
|
|
MangaListTransformer as AMLT
|
2017-03-29 12:32:36 -04:00
|
|
|
};
|
2020-01-15 15:22:38 -05:00
|
|
|
use Aviat\AnimeClient\API\Anilist\Model as AnilistModel;
|
|
|
|
use Aviat\AnimeClient\API\Kitsu\Model as KitsuModel;
|
2018-09-26 22:31:04 -04:00
|
|
|
use Aviat\AnimeClient\API\Mapping\{AnimeWatchingStatus, MangaReadingStatus};
|
2018-09-27 16:45:12 -04:00
|
|
|
use Aviat\AnimeClient\Types\FormItem;
|
2019-12-09 14:34:23 -05:00
|
|
|
use Aviat\Ion\Di\Exception\ContainerException;
|
|
|
|
use Aviat\Ion\Di\Exception\NotFoundException;
|
2017-02-14 15:29:13 -05:00
|
|
|
use Aviat\Ion\Json;
|
2018-01-31 15:44:48 -05:00
|
|
|
use DateTime;
|
2017-01-27 12:35:28 -05:00
|
|
|
|
|
|
|
/**
|
2018-09-26 22:31:04 -04:00
|
|
|
* Syncs list data between Anilist and Kitsu
|
2017-01-27 12:35:28 -05:00
|
|
|
*/
|
2018-08-08 10:12:45 -04:00
|
|
|
final class SyncLists extends BaseCommand {
|
2017-02-08 15:48:20 -05:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
/**
|
|
|
|
* Model for making requests to Anilist API
|
2020-01-15 15:22:38 -05:00
|
|
|
* @var AnilistModel
|
2018-09-26 22:31:04 -04:00
|
|
|
*/
|
|
|
|
protected $anilistModel;
|
|
|
|
|
2017-02-17 10:55:17 -05:00
|
|
|
/**
|
|
|
|
* Model for making requests to Kitsu API
|
2020-01-15 15:22:38 -05:00
|
|
|
* @var KitsuModel
|
2017-02-17 10:55:17 -05:00
|
|
|
*/
|
2017-01-27 12:35:28 -05:00
|
|
|
protected $kitsuModel;
|
2017-03-31 13:37:53 -04:00
|
|
|
|
2017-02-08 15:48:20 -05:00
|
|
|
/**
|
2018-09-26 22:31:04 -04:00
|
|
|
* Run the Kitsu <=> Anilist sync script
|
2017-02-08 15:48:20 -05:00
|
|
|
*
|
|
|
|
* @param array $args
|
|
|
|
* @param array $options
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws ContainerException
|
|
|
|
* @throws NotFoundException
|
2018-09-27 16:45:12 -04:00
|
|
|
* @throws \Throwable
|
2017-02-08 15:48:20 -05:00
|
|
|
*/
|
2018-01-31 15:44:48 -05:00
|
|
|
public function execute(array $args, array $options = []): void
|
2017-02-08 15:48:20 -05:00
|
|
|
{
|
|
|
|
$this->setContainer($this->setupContainer());
|
|
|
|
$this->setCache($this->container->get('cache'));
|
2018-10-09 18:10:20 -04:00
|
|
|
|
|
|
|
$config = $this->container->get('config');
|
|
|
|
$anilistEnabled = $config->get(['anilist', 'enabled']);
|
|
|
|
|
|
|
|
if ( ! $anilistEnabled)
|
|
|
|
{
|
|
|
|
$this->echoBox('Anlist API is not enabled. Can not sync.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
$this->anilistModel = $this->container->get('anilist-model');
|
2017-02-08 15:48:20 -05:00
|
|
|
$this->kitsuModel = $this->container->get('kitsu-model');
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2017-04-19 16:48:53 -04:00
|
|
|
$this->sync('anime');
|
|
|
|
$this->sync('manga');
|
2018-10-01 13:03:48 -04:00
|
|
|
|
|
|
|
$this->echoBox('Finished syncing lists');
|
2017-03-29 12:32:36 -04:00
|
|
|
}
|
|
|
|
|
2018-01-31 15:44:48 -05:00
|
|
|
/**
|
2018-09-27 16:45:12 -04:00
|
|
|
* Attempt to synchronize external APIs
|
2018-01-31 15:44:48 -05:00
|
|
|
*
|
2018-09-27 16:45:12 -04:00
|
|
|
* @param string $type
|
|
|
|
* @throws \Throwable
|
2018-01-31 15:44:48 -05:00
|
|
|
*/
|
|
|
|
protected function sync(string $type): void
|
2017-03-29 12:32:36 -04:00
|
|
|
{
|
2017-04-19 16:48:53 -04:00
|
|
|
$uType = ucfirst($type);
|
2017-09-15 15:04:57 -04:00
|
|
|
|
2018-01-16 14:58:07 -05:00
|
|
|
$kitsuCount = 0;
|
2017-12-13 11:38:21 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$kitsuCount = $this->kitsuModel->{"get{$uType}ListCount"}();
|
|
|
|
}
|
|
|
|
catch (FailedResponseException $e)
|
|
|
|
{
|
|
|
|
dump($e);
|
|
|
|
}
|
|
|
|
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2017-04-19 16:48:53 -04:00
|
|
|
$this->echoBox("Number of Kitsu {$type} list items: {$kitsuCount}");
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2017-04-19 16:48:53 -04:00
|
|
|
$data = $this->diffLists($type);
|
2017-03-29 12:32:36 -04:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
if ( ! empty($data['addToAnilist']))
|
2017-02-14 15:29:13 -05:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$count = count($data['addToAnilist']);
|
|
|
|
$this->echoBox("Adding {$count} missing {$type} list items to Anilist");
|
|
|
|
$this->updateAnilistListItems($data['addToAnilist'], 'create', $type);
|
2017-03-28 16:52:27 -04:00
|
|
|
}
|
2017-04-19 16:15:39 -04:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
if ( ! empty($data['updateAnilist']))
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$count = count($data['updateAnilist']);
|
|
|
|
$this->echoBox("Updating {$count} outdated Anilist {$type} list items");
|
|
|
|
$this->updateAnilistListItems($data['updateAnilist'], 'update', $type);
|
|
|
|
}
|
2017-12-13 11:38:21 -05:00
|
|
|
|
|
|
|
if ( ! empty($data['addToKitsu']))
|
|
|
|
{
|
|
|
|
$count = count($data['addToKitsu']);
|
|
|
|
$this->echoBox("Adding {$count} missing {$type} list items to Kitsu");
|
|
|
|
$this->updateKitsuListItems($data['addToKitsu'], 'create', $type);
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty($data['updateKitsu']))
|
|
|
|
{
|
|
|
|
$count = count($data['updateKitsu']);
|
2017-04-19 16:48:53 -04:00
|
|
|
$this->echoBox("Updating {$count} outdated Kitsu {$type} list items");
|
2017-12-13 11:38:21 -05:00
|
|
|
$this->updateKitsuListItems($data['updateKitsu'], 'update', $type);
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
2017-02-08 15:48:20 -05:00
|
|
|
}
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2018-01-31 15:44:48 -05:00
|
|
|
/**
|
|
|
|
* Filter Kitsu mappings for the specified type
|
|
|
|
*
|
|
|
|
* @param array $includes
|
|
|
|
* @param string $type
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function filterMappings(array $includes, string $type = 'anime'): array
|
2017-01-27 12:35:28 -05:00
|
|
|
{
|
2017-02-14 15:29:13 -05:00
|
|
|
$output = [];
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2017-02-14 15:29:13 -05:00
|
|
|
foreach($includes as $id => $mapping)
|
2017-01-27 12:35:28 -05:00
|
|
|
{
|
2017-03-29 12:32:36 -04:00
|
|
|
if ($mapping['externalSite'] === "myanimelist/{$type}")
|
2017-02-14 15:29:13 -05:00
|
|
|
{
|
|
|
|
$output[$id] = $mapping;
|
|
|
|
}
|
2017-01-27 12:35:28 -05:00
|
|
|
}
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2017-02-14 15:29:13 -05:00
|
|
|
return $output;
|
|
|
|
}
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2018-01-31 15:44:48 -05:00
|
|
|
/**
|
2018-09-26 22:31:04 -04:00
|
|
|
* Format an Anilist list for comparison
|
2018-01-31 15:44:48 -05:00
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-09-26 22:31:04 -04:00
|
|
|
protected function formatAnilistList(string $type): array
|
2017-04-19 16:48:53 -04:00
|
|
|
{
|
2018-01-31 15:44:48 -05:00
|
|
|
$type = ucfirst($type);
|
2018-09-26 22:31:04 -04:00
|
|
|
$method = "formatAnilist{$type}List";
|
2018-01-31 15:44:48 -05:00
|
|
|
return $this->$method();
|
2018-09-26 22:31:04 -04:00
|
|
|
}
|
2017-04-19 16:48:53 -04:00
|
|
|
|
2018-01-31 15:44:48 -05:00
|
|
|
/**
|
2018-09-26 22:31:04 -04:00
|
|
|
* Format an Anilist anime list for comparison
|
2018-01-31 15:44:48 -05:00
|
|
|
*
|
|
|
|
* @return array
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws ContainerException
|
|
|
|
* @throws NotFoundException
|
2018-01-31 15:44:48 -05:00
|
|
|
*/
|
2018-09-26 22:31:04 -04:00
|
|
|
protected function formatAnilistAnimeList(): array
|
2017-02-14 15:29:13 -05:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$anilistList = $this->anilistModel->getSyncList('ANIME');
|
|
|
|
$anilistTransformer = new AALT();
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
$transformedAnilist = [];
|
2017-09-15 15:04:57 -04:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
foreach ($anilistList['data']['MediaListCollection']['lists'] as $list)
|
2017-09-15 15:04:57 -04:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$newTransformed = $anilistTransformer->untransformCollection($list['entries']);
|
|
|
|
$transformedAnilist = array_merge($transformedAnilist, $newTransformed);
|
2017-09-15 15:04:57 -04:00
|
|
|
}
|
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
// Key the array by the mal_id for easier reference in the next comparision step
|
|
|
|
$output = [];
|
|
|
|
foreach ($transformedAnilist as $item)
|
2017-01-27 12:35:28 -05:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$output[$item['mal_id']] = $item->toArray();
|
2017-01-27 12:35:28 -05:00
|
|
|
}
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
$count = count($output);
|
|
|
|
$this->echoBox("Number of Anilist anime list items: {$count}");
|
|
|
|
|
2017-02-14 15:29:13 -05:00
|
|
|
return $output;
|
2018-09-26 22:31:04 -04:00
|
|
|
}
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2018-01-31 15:44:48 -05:00
|
|
|
/**
|
2018-09-26 22:31:04 -04:00
|
|
|
* Format an Anilist manga list for comparison
|
2018-01-31 15:44:48 -05:00
|
|
|
*
|
|
|
|
* @return array
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws ContainerException
|
|
|
|
* @throws NotFoundException
|
2018-01-31 15:44:48 -05:00
|
|
|
*/
|
2018-09-26 22:31:04 -04:00
|
|
|
protected function formatAnilistMangaList(): array
|
2017-03-29 12:32:36 -04:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$anilistList = $this->anilistModel->getSyncList('MANGA');
|
|
|
|
$anilistTransformer = new AMLT();
|
2017-03-29 12:32:36 -04:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
$transformedAnilist = [];
|
2017-09-15 15:04:57 -04:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
foreach ($anilistList['data']['MediaListCollection']['lists'] as $list)
|
2017-09-15 15:04:57 -04:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$newTransformed = $anilistTransformer->untransformCollection($list['entries']);
|
|
|
|
$transformedAnilist = array_merge($transformedAnilist, $newTransformed);
|
2017-09-15 15:04:57 -04:00
|
|
|
}
|
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
// Key the array by the mal_id for easier reference in the next comparision step
|
|
|
|
$output = [];
|
|
|
|
foreach ($transformedAnilist as $item)
|
2017-03-29 12:32:36 -04:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$output[$item['mal_id']] = $item->toArray();
|
2017-03-29 12:32:36 -04:00
|
|
|
}
|
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
$count = count($output);
|
|
|
|
$this->echoBox("Number of Anilist manga list items: {$count}");
|
|
|
|
|
2017-03-29 12:32:36 -04:00
|
|
|
return $output;
|
2018-09-26 22:31:04 -04:00
|
|
|
}
|
2017-03-29 12:32:36 -04:00
|
|
|
|
2018-01-31 15:44:48 -05:00
|
|
|
/**
|
|
|
|
* Format a kitsu list for the sake of comparision
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function formatKitsuList(string $type = 'anime'): array
|
2017-02-14 15:29:13 -05:00
|
|
|
{
|
2018-09-27 16:45:12 -04:00
|
|
|
$method = 'getFullRaw' . ucfirst($type) . 'List';
|
|
|
|
$data = $this->kitsuModel->$method();
|
2017-09-15 15:04:57 -04:00
|
|
|
|
|
|
|
if (empty($data))
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-03-29 12:32:36 -04:00
|
|
|
$includes = JsonAPI::organizeIncludes($data['included']);
|
2017-04-19 16:15:39 -04:00
|
|
|
$includes['mappings'] = $this->filterMappings($includes['mappings'], $type);
|
2017-03-29 12:32:36 -04:00
|
|
|
|
|
|
|
$output = [];
|
|
|
|
|
|
|
|
foreach($data['data'] as $listItem)
|
|
|
|
{
|
2017-04-19 16:15:39 -04:00
|
|
|
$id = $listItem['relationships'][$type]['data']['id'];
|
2017-12-04 16:06:27 -05:00
|
|
|
|
2017-04-19 16:15:39 -04:00
|
|
|
$potentialMappings = $includes[$type][$id]['relationships']['mappings'];
|
2017-03-29 12:32:36 -04:00
|
|
|
$malId = NULL;
|
|
|
|
|
|
|
|
foreach ($potentialMappings as $mappingId)
|
|
|
|
{
|
2018-11-27 15:37:16 -05:00
|
|
|
if (\is_array($mappingId))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-12 15:31:59 -05:00
|
|
|
|
2017-03-29 12:32:36 -04:00
|
|
|
if (array_key_exists($mappingId, $includes['mappings']))
|
|
|
|
{
|
|
|
|
$malId = $includes['mappings'][$mappingId]['externalId'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
// Skip to the next item if there isn't a Anilist ID
|
2018-01-31 15:44:48 -05:00
|
|
|
if ($malId === NULL)
|
2017-03-29 12:32:36 -04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$output[$listItem['id']] = [
|
|
|
|
'id' => $listItem['id'],
|
|
|
|
'malId' => $malId,
|
|
|
|
'data' => $listItem['attributes'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2018-01-31 15:44:48 -05:00
|
|
|
/**
|
|
|
|
* Go through lists of the specified type, and determine what kind of action each item needs
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function diffLists(string $type = 'anime'): array
|
2017-02-08 15:48:20 -05:00
|
|
|
{
|
|
|
|
// Get libraryEntries with media.mappings from Kitsu
|
|
|
|
// Organize mappings, and ignore entries without mappings
|
2017-04-19 16:48:53 -04:00
|
|
|
$kitsuList = $this->formatKitsuList($type);
|
2017-02-08 15:48:20 -05:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
// Get Anilist list data
|
|
|
|
$anilistList = $this->formatAnilistList($type);
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
$itemsToAddToAnilist = [];
|
2017-03-28 16:52:27 -04:00
|
|
|
$itemsToAddToKitsu = [];
|
2018-09-26 22:31:04 -04:00
|
|
|
$anilistUpdateItems = [];
|
2017-03-31 13:37:53 -04:00
|
|
|
$kitsuUpdateItems = [];
|
2017-03-28 16:52:27 -04:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
$malIds = array_keys($anilistList);
|
|
|
|
$kitsuMalIds = array_map('intval', array_column($kitsuList, 'malId'));
|
|
|
|
$missingMalIds = array_diff($malIds, $kitsuMalIds);
|
|
|
|
|
2019-07-11 15:24:34 -04:00
|
|
|
// Add items on Anilist, but not Kitsu to Kitsu
|
2018-09-26 22:31:04 -04:00
|
|
|
foreach($missingMalIds as $mid)
|
2018-09-27 16:45:12 -04:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$itemsToAddToKitsu[] = array_merge($anilistList[$mid]['data'], [
|
|
|
|
'id' => $this->kitsuModel->getKitsuIdFromMALId((string)$mid, $type),
|
2017-04-19 16:48:53 -04:00
|
|
|
'type' => $type
|
2017-03-28 16:52:27 -04:00
|
|
|
]);
|
2018-09-26 22:31:04 -04:00
|
|
|
}
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
foreach($kitsuList as $kitsuItem)
|
2017-02-14 15:29:13 -05:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$malId = $kitsuItem['malId'];
|
2018-09-27 16:45:12 -04:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
if (array_key_exists($malId, $anilistList))
|
|
|
|
{
|
|
|
|
$anilistItem = $anilistList[$malId];
|
|
|
|
|
|
|
|
$item = $this->compareListItems($kitsuItem, $anilistItem);
|
2017-04-19 16:15:39 -04:00
|
|
|
|
2018-01-31 15:44:48 -05:00
|
|
|
if ($item === NULL)
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-31 15:44:48 -05:00
|
|
|
if (\in_array('kitsu', $item['updateType'], TRUE))
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
|
|
|
$kitsuUpdateItems[] = $item['data'];
|
|
|
|
}
|
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
if (\in_array('anilist', $item['updateType'], TRUE))
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$anilistUpdateItems[] = $item['data'];
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
|
|
|
|
2017-02-14 15:29:13 -05:00
|
|
|
continue;
|
|
|
|
}
|
2018-09-27 16:45:12 -04:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
$statusMap = ($type === 'anime') ? AnimeWatchingStatus::class : MangaReadingStatus::class;
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2017-02-14 15:29:13 -05:00
|
|
|
// Looks like this item only exists on Kitsu
|
2018-09-26 22:31:04 -04:00
|
|
|
$kItem = $kitsuItem['data'];
|
|
|
|
$newItemStatus = ($kItem['reconsuming'] === true) ? 'REPEATING' : $statusMap::KITSU_TO_ANILIST[$kItem['status']];
|
|
|
|
$itemsToAddToAnilist[] = [
|
|
|
|
'mal_id' => $malId,
|
|
|
|
'data' => [
|
|
|
|
'notes' => $kItem['notes'],
|
|
|
|
'private' => $kItem['private'],
|
|
|
|
'progress' => $kItem['progress'],
|
|
|
|
'repeat' => $kItem['reconsumeCount'],
|
2018-10-01 13:03:48 -04:00
|
|
|
'score' => $kItem['ratingTwenty'] * 5, // 100 point score on Anilist
|
2018-09-26 22:31:04 -04:00
|
|
|
'status' => $newItemStatus,
|
2018-09-27 16:45:12 -04:00
|
|
|
],
|
2017-02-14 15:29:13 -05:00
|
|
|
];
|
2018-09-26 22:31:04 -04:00
|
|
|
}
|
2017-02-08 15:48:20 -05:00
|
|
|
|
2017-02-14 15:29:13 -05:00
|
|
|
return [
|
2018-09-26 22:31:04 -04:00
|
|
|
'addToAnilist' => $itemsToAddToAnilist,
|
|
|
|
'updateAnilist' => $anilistUpdateItems,
|
2017-03-31 13:37:53 -04:00
|
|
|
'addToKitsu' => $itemsToAddToKitsu,
|
|
|
|
'updateKitsu' => $kitsuUpdateItems
|
2017-02-14 15:29:13 -05:00
|
|
|
];
|
2017-01-27 12:35:28 -05:00
|
|
|
}
|
2017-02-08 15:48:20 -05:00
|
|
|
|
2018-01-31 15:44:48 -05:00
|
|
|
/**
|
|
|
|
* Compare two list items, and return the out of date one, if one exists
|
|
|
|
*
|
|
|
|
* @param array $kitsuItem
|
2018-09-26 22:31:04 -04:00
|
|
|
* @param array $anilistItem
|
2018-01-31 15:44:48 -05:00
|
|
|
* @return array|null
|
|
|
|
*/
|
2018-09-26 22:31:04 -04:00
|
|
|
protected function compareListItems(array $kitsuItem, array $anilistItem): ?array
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$compareKeys = [
|
|
|
|
'notes',
|
|
|
|
'progress',
|
|
|
|
'rating',
|
|
|
|
'reconsumeCount',
|
|
|
|
'reconsuming',
|
|
|
|
'status',
|
|
|
|
];
|
2017-04-19 16:15:39 -04:00
|
|
|
$diff = [];
|
2018-09-27 16:45:12 -04:00
|
|
|
$dateDiff = new DateTime($kitsuItem['data']['updatedAt']) <=> new DateTime((string)$anilistItem['data']['updatedAt']);
|
2018-09-26 22:31:04 -04:00
|
|
|
|
|
|
|
// Correct differences in notation
|
|
|
|
$kitsuItem['data']['rating'] = $kitsuItem['data']['ratingTwenty'] / 2;
|
2017-04-19 16:15:39 -04:00
|
|
|
|
|
|
|
foreach($compareKeys as $key)
|
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$diff[$key] = $kitsuItem['data'][$key] <=> $anilistItem['data'][$key];
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// No difference? Bail out early
|
|
|
|
$diffValues = array_values($diff);
|
|
|
|
$diffValues = array_unique($diffValues);
|
|
|
|
if (count($diffValues) === 1 && $diffValues[0] === 0)
|
|
|
|
{
|
2018-01-31 15:44:48 -05:00
|
|
|
return NULL;
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$update = [
|
|
|
|
'id' => $kitsuItem['id'],
|
|
|
|
'mal_id' => $kitsuItem['malId'],
|
|
|
|
'data' => []
|
|
|
|
];
|
|
|
|
$return = [
|
|
|
|
'updateType' => []
|
|
|
|
];
|
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
$sameNotes = $diff['notes'] === 0;
|
2017-04-19 16:15:39 -04:00
|
|
|
$sameStatus = $diff['status'] === 0;
|
|
|
|
$sameProgress = $diff['progress'] === 0;
|
2018-10-09 10:11:42 -04:00
|
|
|
$sameRating = $diff['rating'] === 0;
|
2018-09-26 22:31:04 -04:00
|
|
|
$sameRewatchCount = $diff['reconsumeCount'] === 0;
|
|
|
|
|
|
|
|
// If an item is completed, make sure the 'reconsuming' flag is false
|
|
|
|
if ($kitsuItem['data']['status'] === 'completed' && $kitsuItem['data']['reconsuming'] === TRUE)
|
|
|
|
{
|
|
|
|
$update['data']['reconsuming'] = FALSE;
|
|
|
|
$return['updateType'][] = 'kitsu';
|
|
|
|
}
|
2017-04-19 16:15:39 -04:00
|
|
|
|
|
|
|
// If status is the same, and progress count is different, use greater progress
|
|
|
|
if ($sameStatus && ( ! $sameProgress))
|
|
|
|
{
|
|
|
|
if ($diff['progress'] === 1)
|
|
|
|
{
|
|
|
|
$update['data']['progress'] = $kitsuItem['data']['progress'];
|
2018-09-26 22:31:04 -04:00
|
|
|
$return['updateType'][] = 'anilist';
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
|
|
|
else if($diff['progress'] === -1)
|
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$update['data']['progress'] = $anilistItem['data']['progress'];
|
2017-04-19 16:15:39 -04:00
|
|
|
$return['updateType'][] = 'kitsu';
|
|
|
|
}
|
|
|
|
}
|
2018-09-27 16:45:12 -04:00
|
|
|
|
|
|
|
// If status is different, use the status of the more recently updated item
|
2018-09-26 22:31:04 -04:00
|
|
|
if ( ! $sameStatus)
|
|
|
|
{
|
2018-09-27 16:45:12 -04:00
|
|
|
if ($dateDiff === 1)
|
|
|
|
{
|
|
|
|
$update['data']['status'] = $kitsuItem['data']['status'];
|
|
|
|
$return['updateType'][] = 'anilist';
|
|
|
|
} else if ($dateDiff === -1)
|
|
|
|
{
|
|
|
|
$update['data']['status'] = $anilistItem['data']['status'];
|
|
|
|
$return['updateType'][] = 'kitsu';
|
|
|
|
}
|
2018-09-26 22:31:04 -04:00
|
|
|
}
|
2017-04-19 16:15:39 -04:00
|
|
|
|
|
|
|
// If status and progress are different, it's a bit more complicated...
|
|
|
|
// But, at least for now, assume newer record is correct
|
2018-09-27 16:45:12 -04:00
|
|
|
if ( ! ($sameStatus || $sameProgress))
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
|
|
|
if ($dateDiff === 1)
|
|
|
|
{
|
|
|
|
$update['data']['status'] = $kitsuItem['data']['status'];
|
|
|
|
|
|
|
|
if ((int)$kitsuItem['data']['progress'] !== 0)
|
|
|
|
{
|
|
|
|
$update['data']['progress'] = $kitsuItem['data']['progress'];
|
|
|
|
}
|
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
$return['updateType'][] = 'anilist';
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
|
|
|
else if($dateDiff === -1)
|
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$update['data']['status'] = $anilistItem['data']['status'];
|
2017-04-19 16:15:39 -04:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
if ((int)$anilistItem['data']['progress'] !== 0)
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
|
|
|
$update['data']['progress'] = $kitsuItem['data']['progress'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$return['updateType'][] = 'kitsu';
|
|
|
|
}
|
2018-09-27 16:45:12 -04:00
|
|
|
}
|
2017-04-19 16:15:39 -04:00
|
|
|
|
2018-09-27 16:45:12 -04:00
|
|
|
// Use the first set rating, otherwise use the newer rating
|
2017-04-19 16:15:39 -04:00
|
|
|
if ( ! $sameRating)
|
|
|
|
{
|
2018-12-12 15:31:59 -05:00
|
|
|
if (
|
|
|
|
$dateDiff === 1 &&
|
|
|
|
$kitsuItem['data']['rating'] !== 0 &&
|
|
|
|
$kitsuItem['data']['ratingTwenty'] !== 0
|
|
|
|
)
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
2018-10-01 13:03:48 -04:00
|
|
|
$update['data']['ratingTwenty'] = $kitsuItem['data']['ratingTwenty'];
|
2018-09-26 22:31:04 -04:00
|
|
|
$return['updateType'][] = 'anilist';
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
2018-12-12 15:31:59 -05:00
|
|
|
else if($dateDiff === -1 && $anilistItem['data']['rating'] !== 0)
|
2018-09-26 22:31:04 -04:00
|
|
|
{
|
2018-10-01 13:03:48 -04:00
|
|
|
$update['data']['ratingTwenty'] = $anilistItem['data']['rating'] * 2;
|
2018-09-26 22:31:04 -04:00
|
|
|
$return['updateType'][] = 'kitsu';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If notes are set, use kitsu, otherwise, set kitsu from anilist
|
|
|
|
if ( ! $sameNotes)
|
|
|
|
{
|
|
|
|
if ($kitsuItem['data']['notes'] !== '')
|
|
|
|
{
|
|
|
|
$update['data']['notes'] = $kitsuItem['data']['notes'];
|
|
|
|
$return['updateType'][] = 'anilist';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$update['data']['notes'] = $anilistItem['data']['notes'];
|
|
|
|
$return['updateType'][] = 'kitsu';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assume the larger reconsumeCount is correct
|
|
|
|
if ( ! $sameRewatchCount)
|
|
|
|
{
|
|
|
|
if ($diff['reconsumeCount'] === 1)
|
|
|
|
{
|
|
|
|
$update['data']['reconsumeCount'] = $kitsuItem['data']['reconsumeCount'];
|
|
|
|
$return['updateType'][] = 'anilist';
|
|
|
|
}
|
|
|
|
else if ($diff['reconsumeCount'] === -1)
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$update['data']['reconsumeCount'] = $anilistItem['data']['reconsumeCount'];
|
2017-04-19 16:15:39 -04:00
|
|
|
$return['updateType'][] = 'kitsu';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 15:31:59 -05:00
|
|
|
// No changes? Let's bail!
|
|
|
|
if (empty($return['updateType']))
|
|
|
|
{
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2017-04-19 16:15:39 -04:00
|
|
|
$return['meta'] = [
|
|
|
|
'kitsu' => $kitsuItem['data'],
|
2018-09-26 22:31:04 -04:00
|
|
|
'anilist' => $anilistItem['data'],
|
2018-09-27 16:45:12 -04:00
|
|
|
'dateDiff' => $dateDiff,
|
2017-04-19 16:15:39 -04:00
|
|
|
'diff' => $diff,
|
|
|
|
];
|
|
|
|
$return['data'] = $update;
|
|
|
|
$return['updateType'] = array_unique($return['updateType']);
|
2018-09-27 16:45:12 -04:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
// Fill in missing data values for update on Anlist
|
|
|
|
// so I don't have to create a really complex graphql query
|
|
|
|
// to handle each combination of fields
|
|
|
|
if ($return['updateType'][0] === 'anilist')
|
|
|
|
{
|
|
|
|
$prevData = [
|
|
|
|
'notes' => $kitsuItem['data']['notes'],
|
|
|
|
'private' => $kitsuItem['data']['private'],
|
|
|
|
'progress' => $kitsuItem['data']['progress'],
|
2018-10-01 13:03:48 -04:00
|
|
|
'rating' => $kitsuItem['data']['ratingTwenty'] * 5,
|
2018-09-26 22:31:04 -04:00
|
|
|
'reconsumeCount' => $kitsuItem['data']['reconsumeCount'],
|
|
|
|
'reconsuming' => $kitsuItem['data']['reconsuming'],
|
|
|
|
'status' => $kitsuItem['data']['status'],
|
|
|
|
];
|
2018-09-27 16:45:12 -04:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
$return['data']['data'] = array_merge($prevData, $return['data']['data']);
|
|
|
|
}
|
|
|
|
|
2017-04-19 16:15:39 -04:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2018-01-31 15:44:48 -05:00
|
|
|
/**
|
|
|
|
* Create/Update list items on Kitsu
|
|
|
|
*
|
|
|
|
* @param array $itemsToUpdate
|
|
|
|
* @param string $action
|
|
|
|
* @param string $type
|
2018-09-27 16:45:12 -04:00
|
|
|
* @throws \Throwable
|
2018-01-31 15:44:48 -05:00
|
|
|
*/
|
|
|
|
protected function updateKitsuListItems(array $itemsToUpdate, string $action = 'update', string $type = 'anime'): void
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
|
|
|
$requester = new ParallelAPIRequest();
|
|
|
|
foreach($itemsToUpdate as $item)
|
|
|
|
{
|
2017-12-13 11:38:21 -05:00
|
|
|
if ($action === 'update')
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$requester->addRequest(
|
2018-09-27 16:45:12 -04:00
|
|
|
$this->kitsuModel->updateListItem(new FormItem($item))
|
2018-09-26 22:31:04 -04:00
|
|
|
);
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
2017-12-13 11:38:21 -05:00
|
|
|
else if ($action === 'create')
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
2020-01-15 15:22:38 -05:00
|
|
|
$maybeRequest = $this->kitsuModel->createListItem($item);
|
|
|
|
if ($maybeRequest === NULL)
|
|
|
|
{
|
|
|
|
$this->echoBox("Skipped creating Kitsu {$type} due to missing id ¯\_(ツ)_/¯");
|
|
|
|
continue;
|
|
|
|
}
|
2017-12-13 11:38:21 -05:00
|
|
|
$requester->addRequest($this->kitsuModel->createListItem($item));
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$responses = $requester->makeRequests();
|
|
|
|
|
|
|
|
foreach($responses as $key => $response)
|
|
|
|
{
|
2018-01-10 16:28:37 -05:00
|
|
|
$responseData = Json::decode($response);
|
2017-12-13 11:38:21 -05:00
|
|
|
|
2018-01-10 16:43:49 -05:00
|
|
|
$id = $itemsToUpdate[$key]['id'];
|
2018-01-10 16:34:25 -05:00
|
|
|
if ( ! array_key_exists('errors', $responseData))
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
2017-12-13 11:38:21 -05:00
|
|
|
$verb = ($action === 'update') ? 'updated' : 'created';
|
|
|
|
$this->echoBox("Successfully {$verb} Kitsu {$type} list item with id: {$id}");
|
2019-07-11 16:38:21 -04:00
|
|
|
continue;
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
2019-07-11 16:38:21 -04:00
|
|
|
|
|
|
|
// Show a different message when you have an episode count mismatch
|
|
|
|
if (isset($responseData['errors'][0]['title']))
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
2019-07-11 16:38:21 -04:00
|
|
|
$errorTitle = $responseData['errors'][0]['title'];
|
|
|
|
|
|
|
|
if ($errorTitle === 'cannot exceed length of media')
|
|
|
|
{
|
|
|
|
$this->echoBox("Skipped Kitsu {$type} {$id} due to episode count mismatch with other API");
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
2019-07-11 16:38:21 -04:00
|
|
|
|
|
|
|
dump($responseData);
|
|
|
|
$verb = ($action === 'update') ? 'update' : 'create';
|
|
|
|
$this->echoBox("Failed to {$verb} Kitsu {$type} list item with id: {$id}");
|
|
|
|
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 15:44:48 -05:00
|
|
|
/**
|
2018-09-26 22:31:04 -04:00
|
|
|
* Create/Update list items on Anilist
|
2018-01-31 15:44:48 -05:00
|
|
|
*
|
|
|
|
* @param array $itemsToUpdate
|
|
|
|
* @param string $action
|
|
|
|
* @param string $type
|
2018-09-27 16:45:12 -04:00
|
|
|
* @throws \Throwable
|
2018-01-31 15:44:48 -05:00
|
|
|
*/
|
2018-09-27 16:45:12 -04:00
|
|
|
protected function updateAnilistListItems(array $itemsToUpdate, string $action = 'update', string $type = 'anime'): void
|
2017-03-29 12:32:36 -04:00
|
|
|
{
|
|
|
|
$requester = new ParallelAPIRequest();
|
|
|
|
|
2017-12-13 11:38:21 -05:00
|
|
|
foreach($itemsToUpdate as $item)
|
2017-03-28 16:52:27 -04:00
|
|
|
{
|
2017-12-13 11:38:21 -05:00
|
|
|
if ($action === 'update')
|
2018-09-27 16:45:12 -04:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$requester->addRequest(
|
2018-09-27 16:45:12 -04:00
|
|
|
$this->anilistModel->updateListItem(new FormItem($item), $type)
|
2018-09-26 22:31:04 -04:00
|
|
|
);
|
2017-03-28 16:52:27 -04:00
|
|
|
}
|
2017-12-13 11:38:21 -05:00
|
|
|
else if ($action === 'create')
|
2017-03-28 16:52:27 -04:00
|
|
|
{
|
2019-07-11 16:38:21 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$requester->addRequest($this->anilistModel->createFullListItem($item, $type));
|
|
|
|
}
|
|
|
|
catch (MissingIdException $e)
|
|
|
|
{
|
2019-07-11 19:03:35 -04:00
|
|
|
// Case where there's a MAL mapping from Kitsu, but no equivalent Anlist item
|
2019-07-11 16:38:21 -04:00
|
|
|
$id = $item['mal_id'];
|
|
|
|
$this->echoBox("Skipping Anilist ${type} with mal_id: {$id} due to missing mapping");
|
|
|
|
}
|
2017-03-28 16:52:27 -04:00
|
|
|
}
|
|
|
|
}
|
2017-02-17 10:55:17 -05:00
|
|
|
|
2017-03-29 12:32:36 -04:00
|
|
|
$responses = $requester->makeRequests();
|
2017-02-17 10:55:17 -05:00
|
|
|
|
2017-02-14 15:29:13 -05:00
|
|
|
foreach($responses as $key => $response)
|
|
|
|
{
|
2017-12-13 11:38:21 -05:00
|
|
|
$id = $itemsToUpdate[$key]['mal_id'];
|
2018-09-26 22:31:04 -04:00
|
|
|
|
|
|
|
$responseData = Json::decode($response);
|
|
|
|
|
|
|
|
if ( ! array_key_exists('errors', $responseData))
|
2017-02-14 15:29:13 -05:00
|
|
|
{
|
2017-12-13 11:38:21 -05:00
|
|
|
$verb = ($action === 'update') ? 'updated' : 'created';
|
2018-09-26 22:31:04 -04:00
|
|
|
$this->echoBox("Successfully {$verb} Anilist {$type} list item with id: {$id}");
|
2017-02-14 15:29:13 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
dump($responseData);
|
2017-12-13 11:38:21 -05:00
|
|
|
$verb = ($action === 'update') ? 'update' : 'create';
|
2018-09-26 22:31:04 -04:00
|
|
|
$this->echoBox("Failed to {$verb} Anilist {$type} list item with id: {$id}");
|
2017-02-14 15:29:13 -05:00
|
|
|
}
|
|
|
|
}
|
2018-09-26 22:31:04 -04:00
|
|
|
}
|
2018-01-10 16:18:06 -05:00
|
|
|
}
|