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
|
|
|
*
|
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
|
|
|
|
*
|
|
|
|
* PHP version 7
|
|
|
|
*
|
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>
|
2018-01-15 14:43:15 -05:00
|
|
|
* @copyright 2015 - 2018 Timothy J. Warren
|
2017-01-27 12:35:28 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @version 4.0
|
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;
|
|
|
|
|
2017-03-29 12:32:36 -04:00
|
|
|
use Aviat\AnimeClient\API\{
|
2017-12-13 11:38:21 -05:00
|
|
|
FailedResponseException,
|
2017-03-29 12:32:36 -04:00
|
|
|
JsonAPI,
|
|
|
|
ParallelAPIRequest,
|
|
|
|
Mapping\AnimeWatchingStatus,
|
|
|
|
Mapping\MangaReadingStatus
|
|
|
|
};
|
|
|
|
use Aviat\AnimeClient\API\MAL\Transformer\{
|
2018-01-10 16:34:25 -05:00
|
|
|
AnimeListTransformer as ALT
|
2017-03-29 12:32:36 -04:00
|
|
|
};
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the API Cache
|
|
|
|
*/
|
2018-08-08 10:12:45 -04:00
|
|
|
final class SyncLists extends BaseCommand {
|
2017-02-08 15:48:20 -05:00
|
|
|
|
2017-02-17 10:55:17 -05:00
|
|
|
/**
|
|
|
|
* Model for making requests to Kitsu API
|
2017-02-22 14:46:35 -05:00
|
|
|
* @var \Aviat\AnimeClient\API\Kitsu\Model
|
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-17 10:55:17 -05:00
|
|
|
/**
|
|
|
|
* Model for making requests to MAL API
|
2017-02-22 14:46:35 -05:00
|
|
|
* @var \Aviat\AnimeClient\API\MAL\Model
|
2017-02-17 10:55:17 -05:00
|
|
|
*/
|
2017-02-08 15:48:20 -05:00
|
|
|
protected $malModel;
|
|
|
|
|
|
|
|
/**
|
2018-01-10 16:18:06 -05:00
|
|
|
* Run the Kitsu <=> MAL sync script
|
2017-02-08 15:48:20 -05:00
|
|
|
*
|
|
|
|
* @param array $args
|
|
|
|
* @param array $options
|
2018-01-31 15:44:48 -05:00
|
|
|
* @throws \Aviat\Ion\Di\ContainerException
|
|
|
|
* @throws \Aviat\Ion\Di\NotFoundException
|
2017-02-08 15:48:20 -05:00
|
|
|
* @return void
|
|
|
|
*/
|
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'));
|
|
|
|
$this->kitsuModel = $this->container->get('kitsu-model');
|
|
|
|
$this->malModel = $this->container->get('mal-model');
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2017-04-19 16:48:53 -04:00
|
|
|
$this->sync('anime');
|
|
|
|
$this->sync('manga');
|
2017-03-29 12:32:36 -04:00
|
|
|
}
|
|
|
|
|
2018-01-31 15:44:48 -05:00
|
|
|
/**
|
|
|
|
* Attempt to synchronize external apis
|
|
|
|
*
|
|
|
|
* @param string $type anime|manga
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
|
|
// Do a little check to make sure you don't have immediate issues
|
|
|
|
// if you have 0 or 1 items in a list on MAL.
|
|
|
|
$malList = $this->malModel->getList($type);
|
|
|
|
$malCount = 0;
|
|
|
|
if ( ! empty($malList))
|
|
|
|
{
|
2018-01-10 16:34:25 -05:00
|
|
|
$malCount = count(array_key_exists(0, $malList)
|
|
|
|
? $malList
|
|
|
|
: [$malList]
|
|
|
|
);
|
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 MAL {$type} list items: {$malCount}");
|
|
|
|
$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
|
|
|
|
2017-02-17 10:55:17 -05:00
|
|
|
if ( ! empty($data['addToMAL']))
|
2017-02-14 15:29:13 -05:00
|
|
|
{
|
2017-04-19 16:15:39 -04:00
|
|
|
$count = count($data['addToMAL']);
|
2017-04-19 16:48:53 -04:00
|
|
|
$this->echoBox("Adding {$count} missing {$type} list items to MAL");
|
2017-12-13 11:38:21 -05:00
|
|
|
$this->updateMALListItems($data['addToMAL'], 'create', $type);
|
2017-03-28 16:52:27 -04:00
|
|
|
}
|
2017-04-19 16:15:39 -04:00
|
|
|
|
|
|
|
if ( ! empty($data['updateMAL']))
|
|
|
|
{
|
|
|
|
$count = count($data['updateMAL']);
|
2017-04-19 16:48:53 -04:00
|
|
|
$this->echoBox("Updating {$count} outdated MAL {$type} list items");
|
2017-12-13 11:38:21 -05:00
|
|
|
$this->updateMALListItems($data['updateMAL'], 'update', $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Format a MAL list for comparison
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function formatMALList(string $type): array
|
2017-04-19 16:48:53 -04:00
|
|
|
{
|
2018-01-31 15:44:48 -05:00
|
|
|
$type = ucfirst($type);
|
|
|
|
$method = "formatMAL{$type}List";
|
|
|
|
return $this->$method();
|
2017-04-19 16:48:53 -04:00
|
|
|
}
|
|
|
|
|
2018-01-31 15:44:48 -05:00
|
|
|
/**
|
|
|
|
* Format a MAL anime list for comparison
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function formatMALAnimeList(): array
|
2017-02-14 15:29:13 -05:00
|
|
|
{
|
2017-06-19 13:49:28 -04:00
|
|
|
$orig = $this->malModel->getList('anime');
|
2017-02-14 15:29:13 -05:00
|
|
|
$output = [];
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2017-09-15 15:04:57 -04:00
|
|
|
// Bail early on empty list
|
|
|
|
if (empty($orig))
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Due to xml parsing differences,
|
|
|
|
// 1 item has no wrapping array.
|
|
|
|
// In this case, just re-create the
|
|
|
|
// wrapper array
|
|
|
|
if ( ! array_key_exists(0, $orig))
|
|
|
|
{
|
|
|
|
$orig = [$orig];
|
|
|
|
}
|
|
|
|
|
2017-02-14 15:29:13 -05:00
|
|
|
foreach($orig as $item)
|
2017-01-27 12:35:28 -05:00
|
|
|
{
|
2017-02-14 15:29:13 -05:00
|
|
|
$output[$item['series_animedb_id']] = [
|
|
|
|
'id' => $item['series_animedb_id'],
|
|
|
|
'data' => [
|
2017-03-07 20:49:31 -05:00
|
|
|
'status' => AnimeWatchingStatus::MAL_TO_KITSU[$item['my_status']],
|
2017-02-14 15:29:13 -05:00
|
|
|
'progress' => $item['my_watched_episodes'],
|
|
|
|
'reconsuming' => (bool) $item['my_rewatching'],
|
2017-03-28 16:52:27 -04:00
|
|
|
'rating' => $item['my_score'] / 2,
|
2017-02-14 15:29:13 -05:00
|
|
|
'updatedAt' => (new \DateTime())
|
|
|
|
->setTimestamp((int)$item['my_last_updated'])
|
|
|
|
->format(\DateTime::W3C),
|
|
|
|
]
|
|
|
|
];
|
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-01-27 12:35:28 -05:00
|
|
|
}
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2018-01-31 15:44:48 -05:00
|
|
|
/**
|
|
|
|
* Format a MAL manga list for comparison
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function formatMALMangaList(): array
|
2017-03-29 12:32:36 -04:00
|
|
|
{
|
2017-06-19 13:49:28 -04:00
|
|
|
$orig = $this->malModel->getList('manga');
|
2017-03-29 12:32:36 -04:00
|
|
|
$output = [];
|
|
|
|
|
2017-09-15 15:04:57 -04:00
|
|
|
// Bail early on empty list
|
|
|
|
if (empty($orig))
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Due to xml parsing differences,
|
|
|
|
// 1 item has no wrapping array.
|
|
|
|
// In this case, just re-create the
|
|
|
|
// wrapper array
|
|
|
|
if ( ! array_key_exists(0, $orig))
|
|
|
|
{
|
|
|
|
$orig = [$orig];
|
|
|
|
}
|
|
|
|
|
2017-03-29 12:32:36 -04:00
|
|
|
foreach($orig as $item)
|
|
|
|
{
|
|
|
|
$output[$item['series_mangadb_id']] = [
|
|
|
|
'id' => $item['series_mangadb_id'],
|
|
|
|
'data' => [
|
|
|
|
'my_status' => $item['my_status'],
|
|
|
|
'status' => MangaReadingStatus::MAL_TO_KITSU[$item['my_status']],
|
|
|
|
'progress' => $item['my_read_chapters'],
|
2017-03-31 13:37:53 -04:00
|
|
|
'volumes' => $item['my_read_volumes'],
|
2017-03-29 12:32:36 -04:00
|
|
|
'reconsuming' => (bool) $item['my_rereadingg'],
|
|
|
|
'rating' => $item['my_score'] / 2,
|
|
|
|
'updatedAt' => (new \DateTime())
|
|
|
|
->setTimestamp((int)$item['my_last_updated'])
|
|
|
|
->format(\DateTime::W3C),
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-04-19 16:48:53 -04:00
|
|
|
$data = $this->kitsuModel->{'getFull' . ucfirst($type) . 'List'}();
|
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)
|
|
|
|
{
|
|
|
|
if (array_key_exists($mappingId, $includes['mappings']))
|
|
|
|
{
|
|
|
|
$malId = $includes['mappings'][$mappingId]['externalId'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip to the next item if there isn't a MAL 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
|
|
|
|
|
|
|
// Get MAL list data
|
2017-04-19 16:48:53 -04:00
|
|
|
$malList = $this->formatMALList($type);
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2017-02-14 15:29:13 -05:00
|
|
|
$itemsToAddToMAL = [];
|
2017-03-28 16:52:27 -04:00
|
|
|
$itemsToAddToKitsu = [];
|
2017-03-31 13:37:53 -04:00
|
|
|
$malUpdateItems = [];
|
|
|
|
$kitsuUpdateItems = [];
|
2017-03-28 16:52:27 -04:00
|
|
|
|
|
|
|
$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'], [
|
2017-04-19 16:48:53 -04:00
|
|
|
'id' => $this->kitsuModel->getKitsuIdFromMALId($mid, $type),
|
|
|
|
'type' => $type
|
2017-03-28 16:52:27 -04:00
|
|
|
]);
|
|
|
|
}
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2017-02-14 15:29:13 -05:00
|
|
|
foreach($kitsuList as $kitsuItem)
|
|
|
|
{
|
2018-01-31 15:44:48 -05:00
|
|
|
if (\in_array($kitsuItem['malId'], $malIds, TRUE))
|
2017-02-14 15:29:13 -05:00
|
|
|
{
|
2017-04-19 16:48:53 -04:00
|
|
|
$item = $this->compareListItems($kitsuItem, $malList[$kitsuItem['malId']]);
|
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-01-31 15:44:48 -05:00
|
|
|
if (\in_array('mal', $item['updateType'], TRUE))
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
|
|
|
$malUpdateItems[] = $item['data'];
|
|
|
|
}
|
|
|
|
|
2017-02-14 15:29:13 -05:00
|
|
|
continue;
|
|
|
|
}
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2017-02-14 15:29:13 -05:00
|
|
|
// Looks like this item only exists on Kitsu
|
|
|
|
$itemsToAddToMAL[] = [
|
|
|
|
'mal_id' => $kitsuItem['malId'],
|
|
|
|
'data' => $kitsuItem['data']
|
|
|
|
];
|
2017-03-27 10:09:45 -04:00
|
|
|
|
2017-02-14 15:29:13 -05:00
|
|
|
}
|
2017-02-08 15:48:20 -05:00
|
|
|
|
2017-02-14 15:29:13 -05:00
|
|
|
return [
|
|
|
|
'addToMAL' => $itemsToAddToMAL,
|
2017-03-31 13:37:53 -04:00
|
|
|
'updateMAL' => $malUpdateItems,
|
|
|
|
'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
|
|
|
|
* @param array $malItem
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
protected function compareListItems(array $kitsuItem, array $malItem): ?array
|
2017-04-19 16:15:39 -04:00
|
|
|
{
|
|
|
|
$compareKeys = ['status', 'progress', 'rating', 'reconsuming'];
|
|
|
|
$diff = [];
|
2018-01-31 15:44:48 -05:00
|
|
|
$dateDiff = new DateTime($kitsuItem['data']['updatedAt']) <=> new DateTime($malItem['data']['updatedAt']);
|
2017-04-19 16:15:39 -04:00
|
|
|
|
|
|
|
foreach($compareKeys as $key)
|
|
|
|
{
|
|
|
|
$diff[$key] = $kitsuItem['data'][$key] <=> $malItem['data'][$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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' => []
|
|
|
|
];
|
|
|
|
|
|
|
|
$sameStatus = $diff['status'] === 0;
|
|
|
|
$sameProgress = $diff['progress'] === 0;
|
|
|
|
$sameRating = $diff['rating'] === 0;
|
|
|
|
|
|
|
|
|
|
|
|
// 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'];
|
|
|
|
$return['updateType'][] = 'mal';
|
|
|
|
}
|
|
|
|
else if($diff['progress'] === -1)
|
|
|
|
{
|
|
|
|
$update['data']['progress'] = $malItem['data']['progress'];
|
|
|
|
$return['updateType'][] = 'kitsu';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If status and progress are different, it's a bit more complicated...
|
|
|
|
// But, at least for now, assume newer record is correct
|
|
|
|
if ( ! ($sameStatus || $sameProgress))
|
|
|
|
{
|
|
|
|
if ($dateDiff === 1)
|
|
|
|
{
|
|
|
|
$update['data']['status'] = $kitsuItem['data']['status'];
|
|
|
|
|
|
|
|
if ((int)$kitsuItem['data']['progress'] !== 0)
|
|
|
|
{
|
|
|
|
$update['data']['progress'] = $kitsuItem['data']['progress'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$return['updateType'][] = 'mal';
|
|
|
|
}
|
|
|
|
else if($dateDiff === -1)
|
|
|
|
{
|
|
|
|
$update['data']['status'] = $malItem['data']['status'];
|
|
|
|
|
|
|
|
if ((int)$malItem['data']['progress'] !== 0)
|
|
|
|
{
|
|
|
|
$update['data']['progress'] = $kitsuItem['data']['progress'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$return['updateType'][] = 'kitsu';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If rating is different, use the rating from the item most recently updated
|
|
|
|
if ( ! $sameRating)
|
|
|
|
{
|
|
|
|
if ($dateDiff === 1)
|
|
|
|
{
|
|
|
|
$update['data']['rating'] = $kitsuItem['data']['rating'];
|
|
|
|
$return['updateType'][] = 'mal';
|
|
|
|
}
|
|
|
|
else if ($dateDiff === -1)
|
|
|
|
{
|
|
|
|
$update['data']['rating'] = $malItem['data']['rating'];
|
|
|
|
$return['updateType'][] = 'kitsu';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If status is different, use the status of the more recently updated item
|
|
|
|
if ( ! $sameStatus)
|
|
|
|
{
|
|
|
|
if ($dateDiff === 1)
|
|
|
|
{
|
|
|
|
$update['data']['status'] = $kitsuItem['data']['status'];
|
|
|
|
$return['updateType'][] = 'mal';
|
|
|
|
}
|
|
|
|
else if ($dateDiff === -1)
|
|
|
|
{
|
|
|
|
$update['data']['status'] = $malItem['data']['status'];
|
|
|
|
$return['updateType'][] = 'kitsu';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$return['meta'] = [
|
|
|
|
'kitsu' => $kitsuItem['data'],
|
|
|
|
'mal' => $malItem['data'],
|
|
|
|
'dateDiff' => $dateDiff,
|
|
|
|
'diff' => $diff,
|
|
|
|
];
|
|
|
|
$return['data'] = $update;
|
|
|
|
$return['updateType'] = array_unique($return['updateType']);
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
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
|
|
|
{
|
2017-12-13 11:38:21 -05:00
|
|
|
$requester->addRequest($this->kitsuModel->updateListItem($item));
|
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
|
|
|
{
|
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}");
|
2017-04-19 16:15:39 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-10 16:28:37 -05:00
|
|
|
dump($responseData);
|
2017-12-13 11:38:21 -05:00
|
|
|
$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
|
|
|
/**
|
|
|
|
* Create/Update list items on MAL
|
|
|
|
*
|
|
|
|
* @param array $itemsToUpdate
|
|
|
|
* @param string $action
|
|
|
|
* @param string $type
|
|
|
|
*/
|
|
|
|
protected function updateMALListItems(array$itemsToUpdate, string $action = 'update', string $type = 'anime'): void
|
2017-03-29 12:32:36 -04:00
|
|
|
{
|
2017-12-13 11:38:21 -05:00
|
|
|
$transformer = new ALT();
|
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')
|
2017-03-28 16:52:27 -04:00
|
|
|
{
|
2017-12-13 11:38:21 -05:00
|
|
|
$requester->addRequest($this->malModel->updateListItem($item, $type));
|
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
|
|
|
{
|
2017-12-13 11:38:21 -05:00
|
|
|
$data = $transformer->untransform($item);
|
|
|
|
$requester->addRequest($this->malModel->createFullListItem($data, $type));
|
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'];
|
|
|
|
$goodResponse = (
|
|
|
|
($action === 'update' && $response === 'Updated') ||
|
|
|
|
($action === 'create' && $response === 'Created')
|
|
|
|
);
|
|
|
|
if ($goodResponse)
|
2017-02-14 15:29:13 -05:00
|
|
|
{
|
2017-12-13 11:38:21 -05:00
|
|
|
$verb = ($action === 'update') ? 'updated' : 'created';
|
|
|
|
$this->echoBox("Successfully {$verb} MAL {$type} list item with id: {$id}");
|
2017-02-14 15:29:13 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-13 11:38:21 -05:00
|
|
|
$verb = ($action === 'update') ? 'update' : 'create';
|
|
|
|
$this->echoBox("Failed to {$verb} MAL {$type} list item with id: {$id}");
|
2017-02-14 15:29:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-10 16:18:06 -05:00
|
|
|
}
|