From a892d875fd2062eb7c11165bcf883b5187d0caaf Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Tue, 28 Mar 2017 16:52:27 -0400 Subject: [PATCH] Update sync lists command to create Kitsu items that are missing compared to MAL --- src/API/Kitsu/ListItem.php | 8 +++++ src/API/Kitsu/Model.php | 27 +++++++++++++++ src/Command/SyncKitsuWithMal.php | 56 ++++++++++++++++++++++++++++++-- 3 files changed, 88 insertions(+), 3 deletions(-) diff --git a/src/API/Kitsu/ListItem.php b/src/API/Kitsu/ListItem.php index 003308a9..bd852daa 100644 --- a/src/API/Kitsu/ListItem.php +++ b/src/API/Kitsu/ListItem.php @@ -33,6 +33,8 @@ class ListItem extends AbstractListItem { private function getAuthHeader() { + $cache = $this->getContainer()->get('cache'); + $cacheItem = $cache->getItem('kitsu-auth-token'); $sessionSegment = $this->getContainer() ->get('session') ->getSegment(SESSION_SEGMENT); @@ -43,6 +45,12 @@ class ListItem extends AbstractListItem { return "bearer {$token}"; } + if ($cacheItem->isHit()) + { + $token = $cacheItem->get(); + return "bearer {$token}"; + } + return FALSE; } diff --git a/src/API/Kitsu/Model.php b/src/API/Kitsu/Model.php index 08dbcd84..f44c53c3 100644 --- a/src/API/Kitsu/Model.php +++ b/src/API/Kitsu/Model.php @@ -223,6 +223,33 @@ class Model { return $raw; } + /** + * Find a media item on Kitsu by its associated MAL id + * + * @param string $malId + * @param string $type "anime" or "manga" + * @return string + */ + public function getKitsuIdFromMALId(string $malId, string $type="anime"): string + { + $options = [ + 'query' => [ + 'filter' => [ + 'external_site' => "myanimelist/{$type}", + 'external_id' => $malId + ], + 'fields' => [ + 'media' => 'id,slug' + ], + 'include' => 'media' + ] + ]; + + $raw = $this->getRequest('mappings', $options); + + return $raw['included'][0]['id']; + } + // ------------------------------------------------------------------------- // ! Anime-specific methods // ------------------------------------------------------------------------- diff --git a/src/Command/SyncKitsuWithMal.php b/src/Command/SyncKitsuWithMal.php index 441396f7..b33a8a04 100644 --- a/src/Command/SyncKitsuWithMal.php +++ b/src/Command/SyncKitsuWithMal.php @@ -62,13 +62,21 @@ class SyncKitsuWithMal extends BaseCommand { $this->echoBox("Number of Kitsu list items: {$kitsuCount}"); $data = $this->diffAnimeLists(); - $this->echoBox("Number of items that need to be added to MAL: " . count($data)); + $this->echoBox("Number of items that need to be added to MAL: " . count($data['addToMAL'])); if ( ! empty($data['addToMAL'])) { $this->echoBox("Adding missing list items to MAL"); $this->createMALAnimeListItems($data['addToMAL']); } + + $this->echoBox('Number of items that need to be added to Kitsu: ' . count($data['addToKitsu'])); + + if ( ! empty($data['addToKitsu'])) + { + $this->echoBox("Adding missing list items to Kitsu"); + $this->createKitusAnimeListItems($data['addToKitsu']); + } } public function getKitsuAnimeList() @@ -137,7 +145,7 @@ class SyncKitsuWithMal extends BaseCommand { ? $item['times_rewatched'] : 0, // 'notes' => , - 'rating' => $item['my_score'], + 'rating' => $item['my_score'] / 2, 'updatedAt' => (new \DateTime()) ->setTimestamp((int)$item['my_last_updated']) ->format(\DateTime::W3C), @@ -201,10 +209,24 @@ class SyncKitsuWithMal extends BaseCommand { $malList = $this->formatMALAnimeList(); $itemsToAddToMAL = []; + $itemsToAddToKitsu = []; + + $malIds = array_column($malList, 'id'); + $kitsuMalIds = array_column($kitsuList, 'malId'); + $missingMalIds = array_diff($malIds, $kitsuMalIds); + + foreach($missingMalIds as $mid) + { + // print_r($malList[$mid]); + $itemsToAddToKitsu[] = array_merge($malList[$mid]['data'], [ + 'id' => $this->kitsuModel->getKitsuIdFromMALId($mid), + 'type' => 'anime' + ]); + } foreach($kitsuList as $kitsuItem) { - if (array_key_exists($kitsuItem['malId'], $malList)) + if (in_array($kitsuItem['malId'], $malIds)) { // Eventually, compare the list entries, and determine which // needs to be updated @@ -230,9 +252,37 @@ class SyncKitsuWithMal extends BaseCommand { return [ 'addToMAL' => $itemsToAddToMAL, + 'addToKitsu' => $itemsToAddToKitsu ]; } + public function createKitusAnimeListItems($itemsToAdd) + { + $requests = []; + foreach($itemsToAdd as $item) + { + $requests[] = $this->kitsuModel->createListItem($item); + } + + $promiseArray = (new Client())->requestMulti($requests); + + $responses = wait(all($promiseArray)); + + foreach($responses as $key => $response) + { + $id = $itemsToAdd[$key]['id']; + if ($response->getStatus() === 201) + { + $this->echoBox("Successfully create list item with id: {$id}"); + } + else + { + echo $response->getBody(); + $this->echoBox("Failed to create list item with id: {$id}"); + } + } + } + public function createMALAnimeListItems($itemsToAdd) { $transformer = new ALT();