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
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
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>
|
2021-01-13 01:52:03 -05:00
|
|
|
* @copyright 2015 - 2021 Timothy J. Warren
|
2017-01-27 12:35:28 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.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;
|
|
|
|
|
2021-02-22 15:39:03 -05:00
|
|
|
use Aviat\Ion\JsonException;
|
2020-05-04 17:13:03 -04:00
|
|
|
use ConsoleKit\Widgets;
|
|
|
|
|
2019-12-09 13:13:31 -05:00
|
|
|
use Aviat\AnimeClient\API\{
|
|
|
|
Anilist\MissingIdException,
|
|
|
|
ParallelAPIRequest
|
|
|
|
};
|
2020-08-17 21:08:53 -04:00
|
|
|
use Aviat\AnimeClient\API;
|
2020-05-04 17:13:03 -04:00
|
|
|
use Aviat\AnimeClient\API\Anilist;
|
2018-09-26 22:31:04 -04:00
|
|
|
use Aviat\AnimeClient\API\Mapping\{AnimeWatchingStatus, MangaReadingStatus};
|
2020-12-10 15:59:37 -05:00
|
|
|
use Aviat\AnimeClient\Enum;
|
|
|
|
use Aviat\AnimeClient\Enum\{MediaType, SyncAction};
|
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;
|
2020-04-22 07:53:52 -04:00
|
|
|
use Throwable;
|
|
|
|
use function in_array;
|
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 {
|
2020-10-16 16:18:56 -04:00
|
|
|
protected const KITSU_GREATER = 1;
|
|
|
|
protected const ANILIST_GREATER = -1;
|
|
|
|
protected const SAME = 0;
|
2017-02-08 15:48:20 -05:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
/**
|
|
|
|
* Model for making requests to Anilist API
|
2020-05-04 17:13:03 -04:00
|
|
|
* @var Anilist\Model
|
2018-09-26 22:31:04 -04:00
|
|
|
*/
|
2020-05-04 17:13:03 -04:00
|
|
|
private Anilist\Model $anilistModel;
|
2018-09-26 22:31:04 -04:00
|
|
|
|
2017-02-17 10:55:17 -05:00
|
|
|
/**
|
|
|
|
* Model for making requests to Kitsu API
|
2020-08-26 15:22:14 -04:00
|
|
|
* @var API\Kitsu\Model
|
2020-05-04 17:13:03 -04:00
|
|
|
*/
|
2020-08-26 15:22:14 -04:00
|
|
|
private API\Kitsu\Model $kitsuModel;
|
2020-05-04 17:13:03 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Does the Kitsu API have valid authentication?
|
|
|
|
* @var bool
|
2017-02-17 10:55:17 -05:00
|
|
|
*/
|
2020-05-04 17:13:03 -04:00
|
|
|
private bool $isKitsuAuthenticated = FALSE;
|
2017-03-31 13:37:53 -04:00
|
|
|
|
2017-02-08 15:48:20 -05:00
|
|
|
/**
|
2020-05-04 17:13:03 -04:00
|
|
|
* Sync Kitsu <=> Anilist
|
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
|
2020-04-22 07:53:52 -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
|
2020-05-04 17:13:03 -04:00
|
|
|
{
|
|
|
|
$this->init();
|
|
|
|
|
2020-12-10 15:59:37 -05:00
|
|
|
foreach ([MediaType::ANIME, MediaType::MANGA] as $type)
|
2020-05-04 17:13:03 -04:00
|
|
|
{
|
2020-05-04 17:15:50 -04:00
|
|
|
// Main Sync flow
|
2020-05-04 17:13:03 -04:00
|
|
|
$this->fetchCount($type);
|
|
|
|
$rawData = $this->fetch($type);
|
|
|
|
$normalized = $this->transform($type, $rawData);
|
|
|
|
$compared = $this->compare($type, $normalized);
|
|
|
|
$this->update($type, $compared);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// Main sync flow methods
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up dependencies
|
|
|
|
*
|
|
|
|
* @throws ContainerException
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
protected function init(): 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');
|
2020-12-10 15:59:37 -05:00
|
|
|
$anilistEnabled = $config->get([Enum\API::ANILIST, 'enabled']);
|
2018-10-09 18:10:20 -04:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
// We can't sync kitsu against itself!
|
2018-10-09 18:10:20 -04:00
|
|
|
if ( ! $anilistEnabled)
|
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
$this->echoErrorBox('Anlist API is not enabled. Can not sync.');
|
2020-08-17 10:23:32 -04:00
|
|
|
exit();
|
2020-05-04 17:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Authentication is required to update Kitsu
|
|
|
|
$this->isKitsuAuthenticated = $this->container->get('auth')->isAuthenticated();
|
|
|
|
if ( ! $this->isKitsuAuthenticated)
|
|
|
|
{
|
|
|
|
$this->echoWarningBox('Kitsu is not authenticated. Kitsu list can not be updated.');
|
2018-10-09 18:10:20 -04:00
|
|
|
}
|
|
|
|
|
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-29 12:32:36 -04:00
|
|
|
}
|
|
|
|
|
2018-01-31 15:44:48 -05:00
|
|
|
/**
|
2020-05-04 17:13:03 -04:00
|
|
|
* Get and display the count of items for each API
|
2018-01-31 15:44:48 -05:00
|
|
|
*
|
2018-09-27 16:45:12 -04:00
|
|
|
* @param string $type
|
2018-01-31 15:44:48 -05:00
|
|
|
*/
|
2020-05-04 17:13:03 -04:00
|
|
|
protected function fetchCount(string $type): void
|
2017-03-29 12:32:36 -04:00
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
$this->echo('Fetching List Counts');
|
|
|
|
$progress = new Widgets\ProgressBar($this->getConsole(), 2, 50, FALSE);
|
2017-09-15 15:04:57 -04:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
$displayLines = [];
|
|
|
|
|
|
|
|
$kitsuCount = $this->fetchKitsuCount($type);
|
|
|
|
$displayLines[] = "Number of Kitsu {$type} list items: {$kitsuCount}";
|
|
|
|
$progress->incr();
|
2017-12-13 11:38:21 -05:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
$anilistCount = $this->fetchAnilistCount($type);
|
|
|
|
$displayLines[] = "Number of Anilist {$type} list items: {$anilistCount}";
|
|
|
|
$progress->incr();
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
$this->clearLine();
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
$this->echoBox($displayLines);
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:15:50 -04:00
|
|
|
/**
|
|
|
|
* Get the list data
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @return array
|
|
|
|
*/
|
2020-05-04 17:13:03 -04:00
|
|
|
protected function fetch(string $type): array
|
|
|
|
{
|
|
|
|
$this->echo('Fetching List Data');
|
|
|
|
$progress = new Widgets\ProgressBar($this->getConsole(), 2, 50, FALSE);
|
2017-03-29 12:32:36 -04:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
$anilist = $this->fetchAnilist($type);
|
|
|
|
$progress->incr();
|
|
|
|
|
|
|
|
$kitsu = $this->fetchKitsu($type);
|
|
|
|
$progress->incr();
|
|
|
|
|
|
|
|
$this->clearLine();
|
|
|
|
|
|
|
|
return [
|
2020-12-10 15:59:37 -05:00
|
|
|
Enum\API::ANILIST => $anilist,
|
|
|
|
Enum\API::KITSU => $kitsu,
|
2020-05-04 17:13:03 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:15:50 -04:00
|
|
|
/**
|
|
|
|
* Normalize the list data for comparison
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param array $data
|
|
|
|
* @return array
|
|
|
|
*/
|
2020-05-04 17:13:03 -04:00
|
|
|
protected function transform(string $type, array $data): array
|
|
|
|
{
|
|
|
|
$this->echo('Normalizing List Data');
|
|
|
|
$progress = new Widgets\ProgressBar($this->getConsole(), 2, 50, FALSE);
|
|
|
|
|
2020-12-10 15:59:37 -05:00
|
|
|
$kitsu = $this->transformKitsu($type, $data[Enum\API::KITSU]);
|
2020-05-04 17:13:03 -04:00
|
|
|
$progress->incr();
|
|
|
|
|
2020-12-10 15:59:37 -05:00
|
|
|
$anilist = $this->transformAnilist($type, $data[Enum\API::ANILIST]);
|
2020-05-04 17:13:03 -04:00
|
|
|
$progress->incr();
|
|
|
|
|
|
|
|
$this->clearLine();
|
|
|
|
|
|
|
|
return [
|
2020-12-10 15:59:37 -05:00
|
|
|
Enum\API::ANILIST => $anilist,
|
|
|
|
Enum\API::KITSU => $kitsu,
|
2020-05-04 17:13:03 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:15:50 -04:00
|
|
|
/**
|
|
|
|
* Compare the lists data
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param array $data
|
|
|
|
* @return array|array[]
|
|
|
|
*/
|
2020-05-04 17:13:03 -04:00
|
|
|
protected function compare(string $type, array $data): array
|
|
|
|
{
|
|
|
|
$this->echo('Comparing List Items');
|
|
|
|
|
2020-12-10 15:59:37 -05:00
|
|
|
return $this->compareLists($type, $data[Enum\API::ANILIST], $data[Enum\API::KITSU]);
|
2020-05-04 17:13:03 -04:00
|
|
|
}
|
|
|
|
|
2020-05-04 17:15:50 -04:00
|
|
|
/**
|
|
|
|
* Updated outdated list items
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param array $data
|
|
|
|
* @throws Throwable
|
|
|
|
*/
|
2021-02-12 11:14:45 -05:00
|
|
|
protected function update(string $type, array $data): void
|
2020-05-04 17:13:03 -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");
|
2020-05-04 17:13:03 -04:00
|
|
|
$this->updateAnilistListItems($data['addToAnilist'], SyncAction::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");
|
2020-05-04 17:13:03 -04:00
|
|
|
$this->updateAnilistListItems($data['updateAnilist'], SyncAction::UPDATE, $type);
|
2018-09-26 22:31:04 -04:00
|
|
|
}
|
2017-12-13 11:38:21 -05:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
if ($this->isKitsuAuthenticated)
|
2017-12-13 11:38:21 -05:00
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
if ( ! empty($data['addToKitsu']))
|
|
|
|
{
|
|
|
|
$count = count($data['addToKitsu']);
|
|
|
|
$this->echoBox("Adding {$count} missing {$type} list items to Kitsu");
|
|
|
|
$this->updateKitsuListItems($data['addToKitsu'], SyncAction::CREATE, $type);
|
|
|
|
}
|
2017-04-19 16:15:39 -04:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
if ( ! empty($data['updateKitsu']))
|
|
|
|
{
|
|
|
|
$count = count($data['updateKitsu']);
|
|
|
|
$this->echoBox("Updating {$count} outdated Kitsu {$type} list items");
|
|
|
|
$this->updateKitsuListItems($data['updateKitsu'], SyncAction::UPDATE, $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
$this->echoErrorBox('Kitsu is not authenticated, so lists can not be updated');
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
2017-02-08 15:48:20 -05:00
|
|
|
}
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// Fetch helpers
|
|
|
|
// ------------------------------------------------------------------------
|
2021-02-12 11:14:45 -05:00
|
|
|
private function fetchAnilistCount(string $type): int
|
2017-01-27 12:35:28 -05:00
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
$list = $this->fetchAnilist($type);
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
if ( ! isset($list['data']['MediaListCollection']['lists']))
|
2017-01-27 12:35:28 -05:00
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
return 0;
|
2017-01-27 12:35:28 -05:00
|
|
|
}
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
$count = 0;
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
foreach ($list['data']['MediaListCollection']['lists'] as $subList)
|
|
|
|
{
|
|
|
|
$count += array_reduce($subList, fn ($carry, $item) => $carry + count(array_values($item)), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $count;
|
2018-09-26 22:31:04 -04:00
|
|
|
}
|
2017-04-19 16:48:53 -04:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
private function fetchAnilist(string $type): array
|
2017-02-14 15:29:13 -05:00
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
static $list = [
|
2020-12-10 15:59:37 -05:00
|
|
|
MediaType::ANIME => NULL,
|
|
|
|
MediaType::MANGA => NULL,
|
2020-05-04 17:13:03 -04:00
|
|
|
];
|
2017-09-15 15:04:57 -04:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
// This uses a static so I don't have to fetch this list twice for a count
|
|
|
|
if ($list[$type] === NULL)
|
2017-01-27 12:35:28 -05:00
|
|
|
{
|
2021-02-22 15:39:03 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$list[$type] = $this->anilistModel->getSyncList(strtoupper($type));
|
|
|
|
}
|
|
|
|
catch (JsonException)
|
|
|
|
{
|
|
|
|
$this->echoErrorBox('Anlist API exception. Can not sync.');
|
|
|
|
die();
|
|
|
|
}
|
2017-01-27 12:35:28 -05:00
|
|
|
}
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
return $list[$type];
|
2018-09-26 22:31:04 -04:00
|
|
|
}
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
private function fetchKitsuCount(string $type): int
|
2017-03-29 12:32:36 -04:00
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
$uType = ucfirst($type);
|
2017-09-15 15:04:57 -04:00
|
|
|
|
2020-08-17 10:23:32 -04:00
|
|
|
return $this->kitsuModel->{"get{$uType}ListCount"}() ?? 0;
|
2018-09-26 22:31:04 -04:00
|
|
|
}
|
2017-03-29 12:32:36 -04:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
private function fetchKitsu(string $type): array
|
2017-02-14 15:29:13 -05:00
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
return $this->kitsuModel->getSyncList($type);
|
|
|
|
}
|
2017-09-15 15:04:57 -04:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// Transform Helpers
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
|
|
|
private function transformKitsu(string $type, array $data): array
|
|
|
|
{
|
2017-09-15 15:04:57 -04:00
|
|
|
if (empty($data))
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-03-29 12:32:36 -04:00
|
|
|
$output = [];
|
|
|
|
|
2020-10-16 16:18:56 -04:00
|
|
|
foreach($data as $listItem)
|
2017-03-29 12:32:36 -04:00
|
|
|
{
|
2020-10-16 16:18:56 -04:00
|
|
|
// If there's no mapping, we can't sync, so continue
|
|
|
|
if ( ! is_array($listItem['media']['mappings']['nodes']))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-29 12:32:36 -04:00
|
|
|
$malId = NULL;
|
|
|
|
|
2020-10-16 16:18:56 -04:00
|
|
|
foreach ($listItem['media']['mappings']['nodes'] as $mapping)
|
2017-03-29 12:32:36 -04:00
|
|
|
{
|
2020-10-16 16:18:56 -04:00
|
|
|
$uType = strtoupper($type);
|
|
|
|
if ($mapping['externalSite'] === "MYANIMELIST_{$uType}")
|
2018-11-27 15:37:16 -05:00
|
|
|
{
|
2020-10-16 16:18:56 -04:00
|
|
|
$malId = $mapping['externalId'];
|
|
|
|
break;
|
2017-03-29 12:32:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-16 16:18:56 -04:00
|
|
|
$output[$listItem['media']['id']] = [
|
|
|
|
'id' => $listItem['media']['id'],
|
|
|
|
'slug' => $listItem['media']['slug'],
|
2017-03-29 12:32:36 -04:00
|
|
|
'malId' => $malId,
|
2020-10-16 16:18:56 -04:00
|
|
|
'data' => [
|
|
|
|
'notes' => $listItem['notes'],
|
|
|
|
'private' => $listItem['private'],
|
|
|
|
'progress' => $listItem['progress'],
|
|
|
|
// Comparision is done on 1-10 scale,
|
|
|
|
// Kitsu returns 1-20 scale.
|
2021-04-21 20:09:03 -04:00
|
|
|
'rating' => (int) $listItem['rating'] / 2,
|
2020-10-16 16:18:56 -04:00
|
|
|
'reconsumeCount' => $listItem['reconsumeCount'],
|
|
|
|
'reconsuming' => $listItem['reconsuming'],
|
|
|
|
'status' => strtolower($listItem['status']),
|
|
|
|
'updatedAt' => $listItem['progressedAt'],
|
|
|
|
]
|
2017-03-29 12:32:36 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
private function transformAnilist(string $type, array $data): array
|
2017-02-08 15:48:20 -05:00
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
$uType = ucfirst($type);
|
|
|
|
$className = "\\Aviat\\AnimeClient\\API\\Anilist\\Transformer\\{$uType}ListTransformer";
|
|
|
|
$transformer = new $className;
|
|
|
|
|
|
|
|
$firstTransformed = [];
|
|
|
|
|
|
|
|
foreach ($data['data']['MediaListCollection']['lists'] as $list)
|
|
|
|
{
|
|
|
|
$firstTransformed[] = $transformer->untransformCollection($list['entries']);
|
|
|
|
}
|
2017-02-08 15:48:20 -05:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
$transformed = array_merge_recursive(...$firstTransformed);
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
// Key the array by mal_id
|
|
|
|
$output = [];
|
|
|
|
foreach ($transformed as $item)
|
|
|
|
{
|
|
|
|
$output[$item['mal_id']] = $item->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// Compare Helpers
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
|
|
|
private function compareLists(string $type, array $anilistList, array $kitsuList): array
|
|
|
|
{
|
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'));
|
2021-04-21 19:33:51 -04:00
|
|
|
$missingMalIds = array_filter($malIds, fn ($id) => ! in_array($id, $kitsuMalIds));
|
2018-09-26 22:31:04 -04:00
|
|
|
|
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
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
if ( ! array_key_exists($mid, $anilistList))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $anilistList[$mid]['data'];
|
|
|
|
$data['id'] = $this->kitsuModel->getKitsuIdFromMALId((string)$mid, $type);
|
|
|
|
$data['type'] = $type;
|
|
|
|
|
|
|
|
$itemsToAddToKitsu[] = $data;
|
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;
|
|
|
|
}
|
|
|
|
|
2020-12-10 15:59:37 -05:00
|
|
|
if (in_array(Enum\API::KITSU, $item['updateType'], TRUE))
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
|
|
|
$kitsuUpdateItems[] = $item['data'];
|
|
|
|
}
|
|
|
|
|
2020-12-10 15:59:37 -05:00
|
|
|
if (in_array(Enum\API::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
|
|
|
|
2020-12-10 15:59:37 -05:00
|
|
|
$statusMap = ($type === MediaType::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'],
|
2020-10-16 16:18:56 -04:00
|
|
|
'score' => $kItem['rating'] * 10, // 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
|
|
|
|
*/
|
2020-05-04 17:13:03 -04:00
|
|
|
private 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 = [];
|
2020-10-16 16:18:56 -04:00
|
|
|
$dateDiff = ($kitsuItem['data']['updatedAt'] !== NULL)
|
|
|
|
? new DateTime($kitsuItem['data']['updatedAt']) <=> new DateTime((string)$anilistItem['data']['updatedAt'])
|
|
|
|
: 0;
|
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;
|
2020-12-10 15:59:37 -05:00
|
|
|
$return['updateType'][] = Enum\API::KITSU;
|
2018-09-26 22:31:04 -04:00
|
|
|
}
|
2017-04-19 16:15:39 -04:00
|
|
|
|
|
|
|
// If status is the same, and progress count is different, use greater progress
|
|
|
|
if ($sameStatus && ( ! $sameProgress))
|
|
|
|
{
|
2020-10-16 16:18:56 -04:00
|
|
|
if ($diff['progress'] === self::KITSU_GREATER)
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
|
|
|
$update['data']['progress'] = $kitsuItem['data']['progress'];
|
2020-12-10 15:59:37 -05:00
|
|
|
$return['updateType'][] = Enum\API::ANILIST;
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
2020-10-16 16:18:56 -04:00
|
|
|
else if($diff['progress'] === self::ANILIST_GREATER)
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$update['data']['progress'] = $anilistItem['data']['progress'];
|
2020-12-10 15:59:37 -05:00
|
|
|
$return['updateType'][] = Enum\API::KITSU;
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
{
|
2020-10-16 16:18:56 -04:00
|
|
|
if ($dateDiff === self::KITSU_GREATER)
|
2018-09-27 16:45:12 -04:00
|
|
|
{
|
|
|
|
$update['data']['status'] = $kitsuItem['data']['status'];
|
2020-12-10 15:59:37 -05:00
|
|
|
$return['updateType'][] = Enum\API::ANILIST;
|
2020-08-17 21:08:53 -04:00
|
|
|
}
|
2020-10-16 16:18:56 -04:00
|
|
|
else if ($dateDiff === self::ANILIST_GREATER)
|
2018-09-27 16:45:12 -04:00
|
|
|
{
|
|
|
|
$update['data']['status'] = $anilistItem['data']['status'];
|
2020-12-10 15:59:37 -05:00
|
|
|
$return['updateType'][] = Enum\API::KITSU;
|
2018-09-27 16:45:12 -04:00
|
|
|
}
|
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
|
|
|
{
|
2020-10-16 16:18:56 -04:00
|
|
|
if ($dateDiff === self::KITSU_GREATER)
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
|
|
|
$update['data']['status'] = $kitsuItem['data']['status'];
|
|
|
|
|
|
|
|
if ((int)$kitsuItem['data']['progress'] !== 0)
|
|
|
|
{
|
|
|
|
$update['data']['progress'] = $kitsuItem['data']['progress'];
|
|
|
|
}
|
|
|
|
|
2020-12-10 15:59:37 -05:00
|
|
|
$return['updateType'][] = Enum\API::ANILIST;
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
2020-10-16 16:18:56 -04:00
|
|
|
else if($dateDiff === self::ANILIST_GREATER)
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
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'];
|
|
|
|
}
|
|
|
|
|
2020-12-10 15:59:37 -05:00
|
|
|
$return['updateType'][] = Enum\API::KITSU;
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
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 (
|
2020-10-16 16:18:56 -04:00
|
|
|
$dateDiff === self::KITSU_GREATER &&
|
2018-12-12 15:31:59 -05:00
|
|
|
$kitsuItem['data']['rating'] !== 0 &&
|
|
|
|
$kitsuItem['data']['ratingTwenty'] !== 0
|
|
|
|
)
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
2021-02-22 15:39:03 -05:00
|
|
|
$update['data']['ratingTwenty'] = $kitsuItem['data']['rating'];
|
2020-12-10 15:59:37 -05:00
|
|
|
$return['updateType'][] = Enum\API::ANILIST;
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
2020-10-16 16:18:56 -04:00
|
|
|
else if($dateDiff === self::ANILIST_GREATER && $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;
|
2020-12-10 15:59:37 -05:00
|
|
|
$return['updateType'][] = Enum\API::KITSU;
|
2018-09-26 22:31:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If notes are set, use kitsu, otherwise, set kitsu from anilist
|
|
|
|
if ( ! $sameNotes)
|
|
|
|
{
|
2020-10-16 16:18:56 -04:00
|
|
|
if ( ! empty($kitsuItem['data']['notes']))
|
2018-09-26 22:31:04 -04:00
|
|
|
{
|
|
|
|
$update['data']['notes'] = $kitsuItem['data']['notes'];
|
2020-12-10 15:59:37 -05:00
|
|
|
$return['updateType'][] = Enum\API::ANILIST;
|
2018-09-26 22:31:04 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$update['data']['notes'] = $anilistItem['data']['notes'];
|
2020-12-10 15:59:37 -05:00
|
|
|
$return['updateType'][] = Enum\API::KITSU;
|
2018-09-26 22:31:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assume the larger reconsumeCount is correct
|
|
|
|
if ( ! $sameRewatchCount)
|
|
|
|
{
|
2020-10-16 16:18:56 -04:00
|
|
|
if ($diff['reconsumeCount'] === self::KITSU_GREATER)
|
2018-09-26 22:31:04 -04:00
|
|
|
{
|
|
|
|
$update['data']['reconsumeCount'] = $kitsuItem['data']['reconsumeCount'];
|
2020-12-10 15:59:37 -05:00
|
|
|
$return['updateType'][] = Enum\API::ANILIST;
|
2018-09-26 22:31:04 -04:00
|
|
|
}
|
2020-10-16 16:18:56 -04:00
|
|
|
else if ($diff['reconsumeCount'] === self::ANILIST_GREATER)
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$update['data']['reconsumeCount'] = $anilistItem['data']['reconsumeCount'];
|
2020-12-10 15:59:37 -05:00
|
|
|
$return['updateType'][] = Enum\API::KITSU;
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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'] = [
|
2020-12-10 15:59:37 -05:00
|
|
|
Enum\API::KITSU => $kitsuItem['data'],
|
|
|
|
Enum\API::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
|
|
|
|
2020-08-24 13:10:43 -04:00
|
|
|
// Fill in missing data values for update
|
2018-09-26 22:31:04 -04:00
|
|
|
// so I don't have to create a really complex graphql query
|
|
|
|
// to handle each combination of fields
|
2020-12-10 15:59:37 -05:00
|
|
|
if ($return['updateType'][0] === Enum\API::ANILIST)
|
2018-09-26 22:31:04 -04:00
|
|
|
{
|
2020-10-16 16:18:56 -04:00
|
|
|
// Anilist GraphQL expects a rating from 1-100
|
2018-09-26 22:31:04 -04:00
|
|
|
$prevData = [
|
|
|
|
'notes' => $kitsuItem['data']['notes'],
|
|
|
|
'private' => $kitsuItem['data']['private'],
|
|
|
|
'progress' => $kitsuItem['data']['progress'],
|
2020-10-16 16:18:56 -04:00
|
|
|
// Transformed Kitsu data returns a rating from 1-10
|
|
|
|
// Anilist expects a rating from 1-100
|
|
|
|
'rating' => $kitsuItem['data']['rating'] * 10,
|
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']);
|
|
|
|
}
|
2020-12-10 15:59:37 -05:00
|
|
|
else if ($return['updateType'][0] === Enum\API::KITSU)
|
2020-08-24 13:10:43 -04:00
|
|
|
{
|
|
|
|
$prevData = [
|
|
|
|
'notes' => $anilistItem['data']['notes'],
|
|
|
|
'private' => $anilistItem['data']['private'],
|
|
|
|
'progress' => $anilistItem['data']['progress'] ?? 0,
|
2020-10-16 16:18:56 -04:00
|
|
|
// Anilist returns a rating between 1-100
|
|
|
|
// Kitsu expects a rating from 1-20
|
2020-08-24 13:10:43 -04:00
|
|
|
'rating' => (((int)$anilistItem['data']['rating']) > 0)
|
2021-04-21 20:09:03 -04:00
|
|
|
? (int) $anilistItem['data']['rating'] / 5
|
2020-08-24 13:10:43 -04:00
|
|
|
: 0,
|
|
|
|
'reconsumeCount' => $anilistItem['data']['reconsumeCount'],
|
|
|
|
'reconsuming' => $anilistItem['data']['reconsuming'],
|
|
|
|
'status' => $anilistItem['data']['status'],
|
|
|
|
];
|
|
|
|
|
|
|
|
$return['data']['data'] = array_merge($prevData, $return['data']['data']);
|
|
|
|
}
|
2018-09-26 22:31:04 -04:00
|
|
|
|
2017-04-19 16:15:39 -04:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:13:03 -04:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// Update Helpers
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
2018-01-31 15:44:48 -05:00
|
|
|
/**
|
|
|
|
* Create/Update list items on Kitsu
|
|
|
|
*
|
|
|
|
* @param array $itemsToUpdate
|
|
|
|
* @param string $action
|
|
|
|
* @param string $type
|
2020-04-22 07:53:52 -04:00
|
|
|
* @throws Throwable
|
2018-01-31 15:44:48 -05:00
|
|
|
*/
|
2020-12-10 15:59:37 -05:00
|
|
|
private function updateKitsuListItems(array $itemsToUpdate, string $action = SyncAction::UPDATE, string $type = MediaType::ANIME): void
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
|
|
|
$requester = new ParallelAPIRequest();
|
|
|
|
foreach($itemsToUpdate as $item)
|
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
if ($action === SyncAction::UPDATE)
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$requester->addRequest(
|
2020-04-22 07:53:52 -04:00
|
|
|
$this->kitsuModel->updateListItem(FormItem::from($item))
|
2018-09-26 22:31:04 -04:00
|
|
|
);
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
2020-05-04 17:13:03 -04:00
|
|
|
else if ($action === SyncAction::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)
|
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
$this->echoWarning("Skipped creating Kitsu {$type} due to missing id ¯\_(ツ)_/¯");
|
2020-01-15 15:22:38 -05:00
|
|
|
continue;
|
|
|
|
}
|
2021-02-16 14:43:51 -05:00
|
|
|
$requester->addRequest($maybeRequest);
|
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'];
|
2021-04-21 19:33:51 -04:00
|
|
|
$mal_id = $itemsToUpdate[$key]['mal_id'] ?? NULL;
|
2018-01-10 16:34:25 -05:00
|
|
|
if ( ! array_key_exists('errors', $responseData))
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
$verb = ($action === SyncAction::UPDATE) ? 'updated' : 'created';
|
|
|
|
$this->echoSuccess("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')
|
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
$this->echoWarning("Skipped Kitsu {$type} {$id} due to episode count mismatch with other API");
|
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
|
|
|
|
2020-08-17 10:23:32 -04:00
|
|
|
dump([
|
|
|
|
'problem' => 'Failed to update kitsu list item',
|
|
|
|
'syncDate' => $itemsToUpdate[$key],
|
|
|
|
'responseData' => $responseData,
|
|
|
|
]);
|
2020-05-04 17:13:03 -04:00
|
|
|
$verb = ($action === SyncAction::UPDATE) ? SyncAction::UPDATE : SyncAction::CREATE;
|
2020-08-24 13:10:43 -04:00
|
|
|
$this->echoError("Failed to {$verb} Kitsu {$type} list item with id: {$id}, and mal_id: {$mal_id}");
|
2019-07-11 16:38:21 -04:00
|
|
|
|
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
|
2020-04-22 07:53:52 -04:00
|
|
|
* @throws Throwable
|
2018-01-31 15:44:48 -05:00
|
|
|
*/
|
2020-12-10 15:59:37 -05:00
|
|
|
private function updateAnilistListItems(array $itemsToUpdate, string $action = SyncAction::UPDATE, string $type = MediaType::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
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
if ($action === SyncAction::UPDATE)
|
2018-09-27 16:45:12 -04:00
|
|
|
{
|
2021-02-16 14:43:51 -05:00
|
|
|
$maybeRequest = $this->anilistModel->updateListItem(FormItem::from($item), $type);
|
|
|
|
if ($maybeRequest !== NULL)
|
|
|
|
{
|
|
|
|
$requester->addRequest($maybeRequest);
|
|
|
|
}
|
2017-03-28 16:52:27 -04:00
|
|
|
}
|
2020-05-04 17:13:03 -04:00
|
|
|
else if ($action === SyncAction::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'];
|
2020-05-04 17:13:03 -04:00
|
|
|
$this->echoWarning("Skipping Anilist ${type} with MAL id: {$id} due to missing mapping");
|
2019-07-11 16:38:21 -04:00
|
|
|
}
|
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
|
|
|
{
|
2020-05-04 17:13:03 -04:00
|
|
|
$verb = ($action === SyncAction::UPDATE) ? 'updated' : 'created';
|
|
|
|
$this->echoSuccess("Successfully {$verb} Anilist {$type} list item with id: {$id}");
|
2017-02-14 15:29:13 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-17 10:23:32 -04:00
|
|
|
dump([
|
|
|
|
'problem' => 'Failed to update anilist list item',
|
|
|
|
'syncDate' => $itemsToUpdate[$key],
|
|
|
|
'responseData' => $responseData,
|
|
|
|
]);
|
2020-05-04 17:13:03 -04:00
|
|
|
$verb = ($action === SyncAction::UPDATE) ? SyncAction::UPDATE : SyncAction::CREATE;
|
|
|
|
$this->echoError("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
|
|
|
}
|