From cb33329a21eb61d034e0811336aef3c141e977b7 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 16 Nov 2022 10:21:00 -0500 Subject: [PATCH] Use MAL and Anlist IDs to do simultaneous updates and syncing. Resolves #39 --- src/AnimeClient/API/Anilist/Model.php | 55 ++---------------------- src/AnimeClient/Command/SyncLists.php | 60 +++++++++++++-------------- src/AnimeClient/Kitsu.php | 5 +++ src/AnimeClient/Model/MediaTrait.php | 8 ++-- 4 files changed, 41 insertions(+), 87 deletions(-) diff --git a/src/AnimeClient/API/Anilist/Model.php b/src/AnimeClient/API/Anilist/Model.php index 82063023..d5833c84 100644 --- a/src/AnimeClient/API/Anilist/Model.php +++ b/src/AnimeClient/API/Anilist/Model.php @@ -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; } /** diff --git a/src/AnimeClient/Command/SyncLists.php b/src/AnimeClient/Command/SyncLists.php index fe40fc15..d73ba1b8 100644 --- a/src/AnimeClient/Command/SyncLists.php +++ b/src/AnimeClient/Command/SyncLists.php @@ -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,25 +222,18 @@ final class SyncLists extends BaseCommand $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"); - $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); - } + $count = is_countable($data['addToKitsu']) ? count($data['addToKitsu']) : 0; + $this->echoBox("Adding {$count} missing {$type} list items to Kitsu"); + $this->updateKitsuListItems($data['addToKitsu'], SyncAction::CREATE, $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); } } diff --git a/src/AnimeClient/Kitsu.php b/src/AnimeClient/Kitsu.php index 96a4f55d..e249c68e 100644 --- a/src/AnimeClient/Kitsu.php +++ b/src/AnimeClient/Kitsu.php @@ -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, diff --git a/src/AnimeClient/Model/MediaTrait.php b/src/AnimeClient/Model/MediaTrait.php index f525eab3..8e0d3177 100644 --- a/src/AnimeClient/Model/MediaTrait.php +++ b/src/AnimeClient/Model/MediaTrait.php @@ -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));