2018-08-10 20:10:19 -04:00
|
|
|
<?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
|
2018-08-10 20:10:19 -04:00
|
|
|
*
|
|
|
|
* PHP version 7
|
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
|
|
|
* @copyright 2015 - 2018 Timothy J. Warren
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @version 4.0
|
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\API\Anilist;
|
|
|
|
|
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\Mapping\{AnimeWatchingStatus, MangaReadingStatus};
|
|
|
|
use Aviat\AnimeClient\Types\FormItem;
|
|
|
|
|
2018-08-10 20:10:19 -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
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
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));
|
2018-09-20 10:41:28 -04:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
if (empty($mediaId))
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException('Media id missing');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type === 'anime')
|
|
|
|
{
|
2018-08-15 08:51:37 -04:00
|
|
|
$createData = [
|
2018-09-20 10:41:28 -04:00
|
|
|
'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
|
|
|
}
|
|
|
|
elseif ($type === 'manga')
|
|
|
|
{
|
2018-08-15 08:51:37 -04:00
|
|
|
$createData = [
|
2018-09-20 10:41:28 -04:00
|
|
|
'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
|
|
|
|
* @return Request
|
|
|
|
*/
|
|
|
|
public function createFullListItem(array $data, string $type = 'anime'): Request
|
|
|
|
{
|
|
|
|
$createData = $data['data'];
|
|
|
|
$mediaId = $this->getMediaIdFromMalId($data['mal_id']);
|
|
|
|
|
|
|
|
$createData['id'] = $mediaId;
|
|
|
|
|
|
|
|
return $this->listItem->createFull($createData);
|
|
|
|
}
|
|
|
|
|
2018-08-15 08:51:37 -04:00
|
|
|
/**
|
|
|
|
* Get the data for a specific list item, generally for editing
|
|
|
|
*
|
2018-09-20 10:41:28 -04:00
|
|
|
* @param string $malId - The unique identifier of that list item
|
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);
|
2018-09-20 16:08:46 -04:00
|
|
|
|
|
|
|
$data = $this->listItem->get($id)['data'];
|
|
|
|
|
|
|
|
return ($data !== null)
|
|
|
|
? $data['MediaList']
|
|
|
|
: [];
|
2018-09-20 10:41:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increase the watch count for the current list item
|
|
|
|
*
|
|
|
|
* @param FormItem $data
|
|
|
|
* @return Request
|
|
|
|
*/
|
2018-09-26 22:31:04 -04:00
|
|
|
public function incrementListItem(FormItem $data, string $type): Request
|
2018-09-20 10:41:28 -04:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$id = $this->getListIdFromMalId($data['mal_id'], $type);
|
2018-09-20 10:41:28 -04:00
|
|
|
|
|
|
|
return $this->listItem->increment($id, $data['data']);
|
2018-08-15 08:51:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modify a list item
|
|
|
|
*
|
|
|
|
* @param FormItem $data
|
2018-09-26 22:31:04 -04:00
|
|
|
* @param int [$id]
|
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));
|
2018-09-20 10:41:28 -04:00
|
|
|
|
|
|
|
return $this->listItem->update($id, $data['data']);
|
2018-08-15 08:51:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a list item
|
|
|
|
*
|
2018-09-20 16:08:46 -04:00
|
|
|
* @param string $malId - The id of the list item to remove
|
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);
|
2018-09-20 16:08:46 -04:00
|
|
|
|
|
|
|
return $this->listItem->delete($item_id);
|
2018-08-15 08:51:37 -04:00
|
|
|
}
|
2018-09-20 10:41:28 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the id of the specific list entry from the malId
|
|
|
|
*
|
|
|
|
* @param string $malId
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-09-26 22:31:04 -04:00
|
|
|
public function getListIdFromMalId(string $malId, string $type): ?string
|
2018-09-20 10:41:28 -04:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$mediaId = $this->getMediaIdFromMalId($malId, $type);
|
|
|
|
return $this->getListIdFromMediaId($mediaId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Anilist media id from its MAL id
|
|
|
|
* this way is more accurate than getting the list item id
|
|
|
|
* directly from the MAL id
|
|
|
|
*/
|
|
|
|
private function getListIdFromMediaId(string $mediaId)
|
|
|
|
{
|
|
|
|
$config = $this->container->get('config');
|
|
|
|
$anilistUser = $config->get(['anilist', 'username']);
|
|
|
|
|
|
|
|
$info = $this->runQuery('ListItemIdByMediaId', [
|
|
|
|
'id' => $mediaId,
|
|
|
|
'userName' => $anilistUser,
|
|
|
|
]);
|
|
|
|
|
|
|
|
/* dump([
|
|
|
|
'media_id' => $mediaId,
|
|
|
|
'userName' => $anilistUser,
|
|
|
|
'response' => $info,
|
|
|
|
]);
|
|
|
|
die(); */
|
|
|
|
|
|
|
|
return (string)$info['data']['MediaList']['id'];
|
2018-09-20 10:41:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Anilist media id from the malId
|
|
|
|
*
|
|
|
|
* @param string $malId
|
|
|
|
* @param string $type
|
2018-09-20 16:08:46 -04:00
|
|
|
* @return string
|
2018-09-20 10:41:28 -04:00
|
|
|
*/
|
2018-09-20 16:08:46 -04:00
|
|
|
private function getMediaIdFromMalId(string $malId, string $type = 'ANIME'): ?string
|
2018-09-20 10:41:28 -04:00
|
|
|
{
|
2018-09-20 16:08:46 -04:00
|
|
|
$info = $this->runQuery('MediaIdByMalId', [
|
2018-09-20 10:41:28 -04:00
|
|
|
'id' => $malId,
|
2018-09-26 22:31:04 -04:00
|
|
|
'type' => mb_strtoupper($type),
|
|
|
|
]);
|
|
|
|
|
|
|
|
/* dump([
|
|
|
|
'mal_id' => $malId,
|
|
|
|
'response' => $info,
|
2018-09-20 10:41:28 -04:00
|
|
|
]);
|
2018-09-26 22:31:04 -04:00
|
|
|
die(); */
|
2018-09-20 16:08:46 -04:00
|
|
|
|
|
|
|
return (string)$info['data']['Media']['id'];
|
2018-09-20 10:41:28 -04:00
|
|
|
}
|
2018-08-10 20:10:19 -04:00
|
|
|
}
|