HummingBirdAnimeClient/src/API/Anilist/Model.php

289 lines
6.7 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
/**
* Hummingbird Anime List Client
*
2018-08-22 13:48:27 -04:00
* An API client for Kitsu to manage anime and manga watch lists
*
2019-12-03 15:17:25 -05:00
* PHP version 7.2
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net>
2019-12-06 09:16:35 -05:00
* @copyright 2015 - 2019 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2019-12-06 09:16:35 -05:00
* @version 4.2
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API\Anilist;
use function Amp\Promise\wait;
2018-09-26 22:31:04 -04:00
use InvalidArgumentException;
2018-08-15 08:51:37 -04:00
use Amp\Artax\Request;
use Aviat\AnimeClient\API\Anilist;
2018-08-15 08:51:37 -04:00
use Aviat\AnimeClient\API\Mapping\{AnimeWatchingStatus, MangaReadingStatus};
use Aviat\AnimeClient\Types\FormItem;
use Aviat\Ion\Json;
2018-08-15 08:51:37 -04:00
/**
* Anilist API Model
*/
2018-08-15 08:51:37 -04:00
final class Model
{
use AnilistTrait;
/**
* @var ListItem
*/
private $listItem;
/**
* Constructor
*
* @param ListItem $listItem
*/
public function __construct(ListItem $listItem)
{
$this->listItem = $listItem;
}
// -------------------------------------------------------------------------
// ! Generic API calls
// -------------------------------------------------------------------------
/**
* Attempt to get an auth token
*
* @param string $code - The request token
* @param string $redirectUri - The oauth callback url
* @return array
*/
public function authenticate(string $code, string $redirectUri): array
{
$config = $this->getContainer()->get('config');
$request = $this->requestBuilder
->newRequest('POST', Anilist::TOKEN_URL)
->setJsonBody([
'grant_type' => 'authorization_code',
'client_id' => $config->get(['anilist', 'client_id']),
'client_secret' => $config->get(['anilist', 'client_secret']),
'redirect_uri' => $redirectUri,
'code' => $code,
])
->getFullRequest();
$response = $this->getResponseFromRequest($request);
return Json::decode(wait($response->getBody()));
}
/**
* Check auth status with simple API call
*
* @return array
*/
public function checkAuth(): array
{
return $this->runQuery('CheckLogin');
}
2018-09-26 22:31:04 -04:00
/**
* Get user list data for syncing with Kitsu
*
* @param string $type
* @return array
* @throws \Aviat\Ion\Di\Exception\ContainerException
* @throws \Aviat\Ion\Di\Exception\NotFoundException
*/
public function getSyncList(string $type = 'anime'): array
{
$config = $this->container->get('config');
$anilistUser = $config->get(['anilist', 'username']);
if ( ! (is_string($anilistUser) && $anilistUser !== ''))
2018-09-26 22:31:04 -04:00
{
throw new InvalidArgumentException('Anilist username is not defined in config');
}
return $this->runQuery('SyncUserList', [
'name' => $anilistUser,
'type' => $type,
]);
}
2018-08-15 08:51:37 -04:00
/**
* Create a list item
*
* @param array $data
* @param string $type
* @return Request
*/
public function createListItem(array $data, string $type = 'anime'): Request
{
$createData = [];
2018-09-26 22:31:04 -04:00
$mediaId = $this->getMediaIdFromMalId($data['mal_id'], mb_strtoupper($type));
/* if (empty($mediaId))
2018-09-26 22:31:04 -04:00
{
throw new InvalidArgumentException('Media id missing');
} */
2018-09-26 22:31:04 -04:00
2018-10-01 13:03:48 -04:00
if ($type === 'ANIME')
2018-09-26 22:31:04 -04:00
{
2018-08-15 08:51:37 -04:00
$createData = [
'id' => $mediaId,
'status' => AnimeWatchingStatus::KITSU_TO_ANILIST[$data['status']],
2018-08-15 08:51:37 -04:00
];
2018-09-26 22:31:04 -04:00
}
2018-10-01 13:03:48 -04:00
elseif ($type === 'MANGA')
2018-09-26 22:31:04 -04:00
{
2018-08-15 08:51:37 -04:00
$createData = [
'id' => $mediaId,
'status' => MangaReadingStatus::KITSU_TO_ANILIST[$data['status']],
2018-08-15 08:51:37 -04:00
];
}
return $this->listItem->create($createData, $type);
}
2018-09-26 22:31:04 -04:00
/**
* Create a list item with all the relevant data
*
* @param array $data
* @param string $type
2018-09-26 22:31:04 -04:00
* @return Request
*/
public function createFullListItem(array $data, string $type): Request
2018-09-26 22:31:04 -04:00
{
$createData = $data['data'];
$mediaId = $this->getMediaIdFromMalId($data['mal_id'], strtoupper($type));
2018-10-05 14:32:05 -04:00
if (empty($mediaId))
{
throw new MissingIdException('No id mapping found');
}
2018-09-26 22:31:04 -04:00
$createData['id'] = $mediaId;
2018-10-05 14:32:05 -04:00
2018-09-26 22:31:04 -04:00
return $this->listItem->createFull($createData);
}
2018-08-15 08:51:37 -04:00
/**
* Get the data for a specific list item, generally for editing
*
* @param string $malId - The unique identifier of that list item
2018-11-09 10:38:35 -05:00
* @param string $type - Them media type (anime/manga)
2018-08-15 08:51:37 -04:00
* @return mixed
*/
2018-09-26 22:31:04 -04:00
public function getListItem(string $malId, string $type): array
2018-08-15 08:51:37 -04:00
{
2018-09-26 22:31:04 -04:00
$id = $this->getListIdFromMalId($malId, $type);
$data = $this->listItem->get($id)['data'];
return ($data !== null)
? $data['MediaList']
: [];
}
/**
* Increase the watch count for the current list item
*
* @param FormItem $data
2018-11-09 10:38:35 -05:00
* @param string $type - Them media type (anime/manga)
* @return Request
*/
2018-09-26 22:31:04 -04:00
public function incrementListItem(FormItem $data, string $type): Request
{
2018-09-26 22:31:04 -04:00
$id = $this->getListIdFromMalId($data['mal_id'], $type);
return $this->listItem->increment($id, $data['data']);
2018-08-15 08:51:37 -04:00
}
/**
* Modify a list item
*
* @param FormItem $data
2018-11-09 10:38:35 -05:00
* @param string $type - Them media type (anime/manga)
2018-08-15 08:51:37 -04:00
* @return Request
*/
2018-09-26 22:31:04 -04:00
public function updateListItem(FormItem $data, string $type): Request
2018-08-15 08:51:37 -04:00
{
2018-09-26 22:31:04 -04:00
$id = $this->getListIdFromMalId($data['mal_id'], mb_strtoupper($type));
return $this->listItem->update($id, $data['data']);
2018-08-15 08:51:37 -04:00
}
/**
* Remove a list item
*
* @param string $malId - The id of the list item to remove
* @param string $type - Them media type (anime/manga)
2018-08-15 08:51:37 -04:00
* @return Request
*/
2018-09-26 22:31:04 -04:00
public function deleteListItem(string $malId, string $type): Request
2018-08-15 08:51:37 -04:00
{
2018-09-26 22:31:04 -04:00
$item_id = $this->getListIdFromMalId($malId, $type);
return $this->listItem->delete($item_id);
2018-08-15 08:51:37 -04:00
}
/**
* Get the id of the specific list entry from the malId
*
* @param string $malId
2018-11-09 10:38:35 -05:00
* @param string $type - The media type (anime/manga)
* @return string
*/
2018-09-26 22:31:04 -04:00
public function getListIdFromMalId(string $malId, string $type): ?string
{
2018-09-26 22:31:04 -04:00
$mediaId = $this->getMediaIdFromMalId($malId, $type);
return $this->getListIdFromMediaId($mediaId);
}
2018-10-05 14:32:05 -04:00
2018-09-26 22:31:04 -04:00
/**
2018-11-09 10:38:35 -05:00
* Get the Anilist list item id from the media id from its MAL id
2018-09-26 22:31:04 -04:00
* this way is more accurate than getting the list item id
* directly from the MAL id
*/
2018-10-05 14:32:05 -04:00
private function getListIdFromMediaId(string $mediaId): string
2018-09-26 22:31:04 -04:00
{
$config = $this->container->get('config');
$anilistUser = $config->get(['anilist', 'username']);
2018-10-05 14:32:05 -04:00
2018-09-26 22:31:04 -04:00
$info = $this->runQuery('ListItemIdByMediaId', [
'id' => $mediaId,
'userName' => $anilistUser,
]);
2018-10-05 14:32:05 -04:00
2018-09-26 22:31:04 -04:00
return (string)$info['data']['MediaList']['id'];
}
/**
* Get the Anilist media id from the malId
*
* @param string $malId
* @param string $type
* @return string
*/
private function getMediaIdFromMalId(string $malId, string $type = 'ANIME'): ?string
{
if ($malId === '')
{
return NULL;
}
$info = $this->runQuery('MediaIdByMalId', [
'id' => $malId,
2018-09-26 22:31:04 -04:00
'type' => mb_strtoupper($type),
]);
2018-10-05 14:32:05 -04:00
if (array_key_exists('errors', $info))
{
return NULL;
}
return (string)$info['data']['Media']['id'];
}
}