Use MAL and Anlist IDs to do simultaneous updates and syncing. Resolves #39
timw4mail/HummingBirdAnimeClient/pipeline/head This commit looks good Details

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

View File

@ -102,8 +102,6 @@ final class Model
/** /**
* Create a list item * Create a list item
*
* @return Request
*/ */
public function createListItem(array $data, string $type = 'anime'): ?Request public function createListItem(array $data, string $type = 'anime'): ?Request
{ {
@ -151,29 +149,6 @@ final class Model
return $this->listItem->createFull($createData); 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 * Increase the watch count for the current list item
* *
@ -206,23 +181,6 @@ final class Model
return $this->listItem->update($id, $data['data']); 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 * Remove a list item
* *
@ -285,10 +243,6 @@ final class Model
/** /**
* Find the id to update by * Find the id to update by
*
* @param array $data
* @param string $type
* @return string|null
*/ */
private function getMediaId (array $data, string $type = 'ANIME'): ?string private function getMediaId (array $data, string $type = 'ANIME'): ?string
{ {
@ -297,12 +251,9 @@ final class Model
return $data['anilist_id']; return $data['anilist_id'];
} }
if (isset($data['mal_id'])) return (isset($data['mal_id']))
{ ? $this->getMediaIdFromMalId($data['mal_id'], mb_strtoupper($type))
return $this->getMediaIdFromMalId($data['mal_id'], mb_strtoupper($type)); : NULL;
}
return NULL;
} }
/** /**

View File

@ -49,11 +49,6 @@ final class SyncLists extends BaseCommand
*/ */
private API\Kitsu\Model $kitsuModel; private API\Kitsu\Model $kitsuModel;
/**
* Does the Kitsu API have valid authentication?
*/
private bool $isKitsuAuthenticated = FALSE;
/** /**
* Sync Kitsu <=> Anilist * Sync Kitsu <=> Anilist
* *
@ -63,7 +58,12 @@ final class SyncLists extends BaseCommand
*/ */
public function execute(array $args, array $options = []): void public function execute(array $args, array $options = []): void
{ {
$this->init(); $canRun = $this->init();
if ( ! $canRun)
{
return;
}
foreach ([MediaType::MANGA, MediaType::ANIME] as $type) foreach ([MediaType::MANGA, MediaType::ANIME] as $type)
{ {
@ -86,7 +86,7 @@ final class SyncLists extends BaseCommand
* @throws ContainerException * @throws ContainerException
* @throws NotFoundException * @throws NotFoundException
*/ */
protected function init(): void protected function init(): bool
{ {
$this->setContainer($this->setupContainer()); $this->setContainer($this->setupContainer());
$this->setCache($this->container->get('cache')); $this->setCache($this->container->get('cache'));
@ -98,19 +98,21 @@ final class SyncLists extends BaseCommand
if ( ! $anilistEnabled) if ( ! $anilistEnabled)
{ {
$this->echoErrorBox('Anlist API is not enabled. Can not sync.'); $this->echoErrorBox('Anlist API is not enabled. Can not sync.');
return false;
exit();
} }
// Authentication is required to update Kitsu // Authentication is required to update Kitsu
$this->isKitsuAuthenticated = $this->container->get('auth')->isAuthenticated(); $isKitsuAuthenticated = $this->container->get('auth')->isAuthenticated();
if ( ! $this->isKitsuAuthenticated) 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->anilistModel = $this->container->get('anilist-model');
$this->kitsuModel = $this->container->get('kitsu-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 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); $progress = new Widgets\ProgressBar($this->getConsole(), 2, 50, FALSE);
$displayLines = []; $displayLines = [];
@ -133,7 +138,7 @@ final class SyncLists extends BaseCommand
$this->clearLine(); $this->clearLine();
$this->echoBox($displayLines); $this->echoBox($displayLines); */
} }
/** /**
@ -143,7 +148,7 @@ final class SyncLists extends BaseCommand
*/ */
protected function fetch(string $type): array 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); $progress = new Widgets\ProgressBar($this->getConsole(), 2, 50, FALSE);
$anilist = $this->fetchAnilist($type); $anilist = $this->fetchAnilist($type);
@ -217,25 +222,18 @@ final class SyncLists extends BaseCommand
$this->updateAnilistListItems($data['updateAnilist'], SyncAction::UPDATE, $type); $this->updateAnilistListItems($data['updateAnilist'], SyncAction::UPDATE, $type);
} }
if ($this->isKitsuAuthenticated) if ( ! empty($data['addToKitsu']))
{ {
if ( ! empty($data['addToKitsu'])) $count = is_countable($data['addToKitsu']) ? count($data['addToKitsu']) : 0;
{ $this->echoBox("Adding {$count} missing {$type} list items to Kitsu");
$count = is_countable($data['addToKitsu']) ? count($data['addToKitsu']) : 0; $this->updateKitsuListItems($data['addToKitsu'], SyncAction::CREATE, $type);
$this->echoBox("Adding {$count} missing {$type} list items to Kitsu");
$this->updateKitsuListItems($data['addToKitsu'], SyncAction::CREATE, $type);
}
if ( ! empty($data['updateKitsu']))
{
$count = is_countable($data['updateKitsu']) ? count($data['updateKitsu']) : 0;
$this->echoBox("Updating {$count} outdated Kitsu {$type} list items");
$this->updateKitsuListItems($data['updateKitsu'], SyncAction::UPDATE, $type);
}
} }
else
if ( ! empty($data['updateKitsu']))
{ {
$this->echoErrorBox('Kitsu is not authenticated, so lists can not be updated'); $count = is_countable($data['updateKitsu']) ? count($data['updateKitsu']) : 0;
$this->echoBox("Updating {$count} outdated Kitsu {$type} list items");
$this->updateKitsuListItems($data['updateKitsu'], SyncAction::UPDATE, $type);
} }
} }

View File

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

View File

@ -95,7 +95,7 @@ trait MediaTrait
$requester->addRequest($kitsuRequest, 'kitsu'); $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 // If can't map MAL id, this will be null
$maybeRequest = $this->anilistModel->createListItem($data, strtoupper($this->type)); $maybeRequest = $this->anilistModel->createListItem($data, strtoupper($this->type));
@ -121,7 +121,7 @@ trait MediaTrait
$requester = new ParallelAPIRequest(); $requester = new ParallelAPIRequest();
$requester->addRequest($this->kitsuModel->incrementListItem($data), 'kitsu'); $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 // If can't map MAL id, this will be null
$maybeRequest = $this->anilistModel->incrementListItem($data, strtoupper($this->type)); $maybeRequest = $this->anilistModel->incrementListItem($data, strtoupper($this->type));
@ -153,7 +153,7 @@ trait MediaTrait
$requester = new ParallelAPIRequest(); $requester = new ParallelAPIRequest();
$requester->addRequest($this->kitsuModel->updateListItem($data), 'kitsu'); $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 // If can't map MAL id, this will be null
$maybeRequest = $this->anilistModel->updateListItem($data, strtoupper($this->type)); $maybeRequest = $this->anilistModel->updateListItem($data, strtoupper($this->type));
@ -184,7 +184,7 @@ trait MediaTrait
$requester = new ParallelAPIRequest(); $requester = new ParallelAPIRequest();
$requester->addRequest($this->kitsuModel->deleteItem($data), 'kitsu'); $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 // If can't map MAL id, this will be null
$maybeRequest = $this->anilistModel->deleteItem($data, strtoupper($this->type)); $maybeRequest = $this->anilistModel->deleteItem($data, strtoupper($this->type));