Use MAL and Anlist IDs to do simultaneous updates and syncing. Resolves #39

This commit is contained in:
Timothy Warren 2022-11-16 10:21:00 -05:00
parent f673a84cf6
commit e1ff1c6e21
4 changed files with 41 additions and 87 deletions

View File

@ -102,8 +102,6 @@ final class Model
/**
* Create a list item
*
* @return Request
*/
public function createListItem(array $data, string $type = 'anime'): ?Request
{
@ -151,29 +149,6 @@ final class Model
return $this->listItem->createFull($createData);
}
/**
* Get the data for a specific list item, generally for editing
*
* @param string $malId - The unique identifier of that list item
* @param string $type - Them media type (anime/manga)
*
* @return mixed[]
*/
public function getListItem(string $malId, string $type): array
{
$id = $this->getListIdFromMalId($malId, $type);
if ($id === NULL)
{
return [];
}
$data = $this->listItem->get($id)['data'];
return ($data !== NULL)
? $data['MediaList']
: [];
}
/**
* Increase the watch count for the current list item
*
@ -206,23 +181,6 @@ final class Model
return $this->listItem->update($id, $data['data']);
}
/**
* Remove a list item
*
* @param string $malId - The id of the list item to remove
* @param string $type - Them media type (anime/manga)
*/
// public function deleteListItem(string $malId, string $type): ?Request
// {
// $id = $this->getListIdFromMalId($malId, $type);
// if ($id === NULL)
// {
// return NULL;
// }
//
// return $this->listItem->delete($id);
// }
/**
* Remove a list item
*
@ -285,10 +243,6 @@ final class Model
/**
* Find the id to update by
*
* @param array $data
* @param string $type
* @return string|null
*/
private function getMediaId (array $data, string $type = 'ANIME'): ?string
{
@ -297,12 +251,9 @@ final class Model
return $data['anilist_id'];
}
if (isset($data['mal_id']))
{
return $this->getMediaIdFromMalId($data['mal_id'], mb_strtoupper($type));
}
return NULL;
return (isset($data['mal_id']))
? $this->getMediaIdFromMalId($data['mal_id'], mb_strtoupper($type))
: NULL;
}
/**

View File

@ -49,11 +49,6 @@ final class SyncLists extends BaseCommand
*/
private API\Kitsu\Model $kitsuModel;
/**
* Does the Kitsu API have valid authentication?
*/
private bool $isKitsuAuthenticated = FALSE;
/**
* Sync Kitsu <=> Anilist
*
@ -63,7 +58,12 @@ final class SyncLists extends BaseCommand
*/
public function execute(array $args, array $options = []): void
{
$this->init();
$canRun = $this->init();
if ( ! $canRun)
{
return;
}
foreach ([MediaType::MANGA, MediaType::ANIME] as $type)
{
@ -86,7 +86,7 @@ final class SyncLists extends BaseCommand
* @throws ContainerException
* @throws NotFoundException
*/
protected function init(): void
protected function init(): bool
{
$this->setContainer($this->setupContainer());
$this->setCache($this->container->get('cache'));
@ -98,19 +98,21 @@ final class SyncLists extends BaseCommand
if ( ! $anilistEnabled)
{
$this->echoErrorBox('Anlist API is not enabled. Can not sync.');
exit();
return false;
}
// Authentication is required to update Kitsu
$this->isKitsuAuthenticated = $this->container->get('auth')->isAuthenticated();
if ( ! $this->isKitsuAuthenticated)
$isKitsuAuthenticated = $this->container->get('auth')->isAuthenticated();
if ( !$isKitsuAuthenticated)
{
$this->echoWarningBox('Kitsu is not authenticated. Kitsu list can not be updated.');
$this->echoErrorBox('Kitsu is not authenticated. Kitsu list can not be updated.');
return false;
}
$this->anilistModel = $this->container->get('anilist-model');
$this->kitsuModel = $this->container->get('kitsu-model');
return true;
}
/**
@ -118,7 +120,10 @@ final class SyncLists extends BaseCommand
*/
protected function fetchCount(string $type): void
{
$this->echo('Fetching List Counts');
// This pulls too much data from Anilist, so skipping this step should result
// in fewer instances of API throttling
/* $this->echo('Fetching List Counts');
$progress = new Widgets\ProgressBar($this->getConsole(), 2, 50, FALSE);
$displayLines = [];
@ -133,7 +138,7 @@ final class SyncLists extends BaseCommand
$this->clearLine();
$this->echoBox($displayLines);
$this->echoBox($displayLines); */
}
/**
@ -143,7 +148,7 @@ final class SyncLists extends BaseCommand
*/
protected function fetch(string $type): array
{
$this->echo('Fetching List Data');
$this->echo("Fetching $type List Data");
$progress = new Widgets\ProgressBar($this->getConsole(), 2, 50, FALSE);
$anilist = $this->fetchAnilist($type);
@ -217,8 +222,6 @@ final class SyncLists extends BaseCommand
$this->updateAnilistListItems($data['updateAnilist'], SyncAction::UPDATE, $type);
}
if ($this->isKitsuAuthenticated)
{
if ( ! empty($data['addToKitsu']))
{
$count = is_countable($data['addToKitsu']) ? count($data['addToKitsu']) : 0;
@ -233,11 +236,6 @@ final class SyncLists extends BaseCommand
$this->updateKitsuListItems($data['updateKitsu'], SyncAction::UPDATE, $type);
}
}
else
{
$this->echoErrorBox('Kitsu is not authenticated, so lists can not be updated');
}
}
// ------------------------------------------------------------------------
// Fetch helpers

View File

@ -382,6 +382,11 @@ final class Kitsu
'link' => TRUE,
'image' => 'streaming-logos/hulu.svg',
],
'netflix.com' => [
'name' => 'Netflix',
'link' => FALSE,
'image' => 'streaming-logos/netflix.svg',
],
'tubitv.com' => [
'name' => 'TubiTV',
'link' => TRUE,

View File

@ -95,7 +95,7 @@ trait MediaTrait
$requester->addRequest($kitsuRequest, 'kitsu');
if ($this->anilistEnabled && (isset($data['anilist_id']) || isset($data['mal_id'])))
if ($this->anilistEnabled)
{
// If can't map MAL id, this will be null
$maybeRequest = $this->anilistModel->createListItem($data, strtoupper($this->type));
@ -121,7 +121,7 @@ trait MediaTrait
$requester = new ParallelAPIRequest();
$requester->addRequest($this->kitsuModel->incrementListItem($data), 'kitsu');
if ($this->anilistEnabled && (isset($data['anilist_id']) || isset($data['mal_id'])))
if ($this->anilistEnabled)
{
// If can't map MAL id, this will be null
$maybeRequest = $this->anilistModel->incrementListItem($data, strtoupper($this->type));
@ -153,7 +153,7 @@ trait MediaTrait
$requester = new ParallelAPIRequest();
$requester->addRequest($this->kitsuModel->updateListItem($data), 'kitsu');
if ($this->anilistEnabled && (isset($data['anilist_id']) || isset($data['mal_id'])))
if ($this->anilistEnabled)
{
// If can't map MAL id, this will be null
$maybeRequest = $this->anilistModel->updateListItem($data, strtoupper($this->type));
@ -184,7 +184,7 @@ trait MediaTrait
$requester = new ParallelAPIRequest();
$requester->addRequest($this->kitsuModel->deleteItem($data), 'kitsu');
if ($this->anilistEnabled && (isset($data['anilist_id']) || isset($data['mal_id'])))
if ($this->anilistEnabled)
{
// If can't map MAL id, this will be null
$maybeRequest = $this->anilistModel->deleteItem($data, strtoupper($this->type));