2016-12-21 12:46:20 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Anime List Client
|
|
|
|
*
|
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
|
|
|
|
*
|
|
|
|
* PHP version 7
|
|
|
|
*
|
|
|
|
* @package AnimeListClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
|
|
|
* @copyright 2015 - 2016 Timothy J. Warren
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @version 4.0
|
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\API\Kitsu;
|
|
|
|
|
2016-12-22 21:36:23 -05:00
|
|
|
use Aviat\AnimeClient\AnimeClient;
|
|
|
|
use Aviat\AnimeClient\API\Kitsu\Transformer\{AnimeTransformer, AnimeListTransformer};
|
2016-12-21 12:46:20 -05:00
|
|
|
use Aviat\Ion\Json;
|
2016-12-22 21:36:23 -05:00
|
|
|
use GuzzleHttp\Exception\ClientException;
|
2016-12-21 12:46:20 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Kitsu API Model
|
|
|
|
*/
|
|
|
|
class KitsuModel {
|
|
|
|
|
|
|
|
use KitsuTrait;
|
|
|
|
|
|
|
|
const CLIENT_ID = 'dd031b32d2f56c990b1425efe6c42ad847e7fe3ab46bf1299f05ecd856bdb7dd';
|
|
|
|
const CLIENT_SECRET = '54d7307928f63414defd96399fc31ba847961ceaecef3a5fd93144e960c0e151';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class to map anime list items
|
|
|
|
* to a common format used by
|
|
|
|
* templates
|
|
|
|
*
|
|
|
|
* @var AnimeListTransformer
|
|
|
|
*/
|
|
|
|
protected $animeListTransformer;
|
|
|
|
|
2016-12-22 21:36:23 -05:00
|
|
|
/**
|
|
|
|
* @var AnimeTransformer
|
|
|
|
*/
|
|
|
|
protected $animeTransformer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* KitsuModel constructor.
|
|
|
|
*/
|
2016-12-21 12:46:20 -05:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
// Set up Guzzle trait
|
|
|
|
$this->init();
|
|
|
|
|
2016-12-22 21:36:23 -05:00
|
|
|
$this->animeTransformer = new AnimeTransformer();
|
2016-12-21 12:46:20 -05:00
|
|
|
$this->animeListTransformer = new AnimeListTransformer();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the access token from the Kitsu API
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
* @param string $password
|
|
|
|
* @return bool|string
|
|
|
|
*/
|
|
|
|
public function authenticate(string $username, string $password)
|
|
|
|
{
|
2016-12-22 21:36:23 -05:00
|
|
|
$data = $this->postRequest(AnimeClient::KITSU_AUTH_URL, [
|
2016-12-21 12:46:20 -05:00
|
|
|
'body' => http_build_query([
|
|
|
|
'grant_type' => 'password',
|
|
|
|
'username' => $username,
|
|
|
|
'password' => $password,
|
|
|
|
'client_id' => self::CLIENT_ID,
|
|
|
|
'client_secret' => self::CLIENT_SECRET
|
|
|
|
])
|
|
|
|
]);
|
|
|
|
|
2016-12-22 21:36:23 -05:00
|
|
|
if (array_key_exists('access_token', $data)) {
|
2016-12-21 12:46:20 -05:00
|
|
|
// @TODO save token
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-22 21:36:23 -05:00
|
|
|
public function getAnime(string $animeId): array
|
|
|
|
{
|
|
|
|
$baseData = $this->getRawAnimeData($animeId);
|
|
|
|
return $this->animeTransformer->transform($baseData);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRawAnimeData($animeId): array
|
|
|
|
{
|
|
|
|
return $this->getRawMediaData('anime', $animeId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAnimeMedia($entryId): array
|
|
|
|
{
|
|
|
|
return $this->getRequest("library-entries/{$entryId}/media");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAnimeList($status): array
|
|
|
|
{
|
|
|
|
$options = [
|
2016-12-21 12:46:20 -05:00
|
|
|
'query' => [
|
|
|
|
'filter' => [
|
|
|
|
'user_id' => 2644,
|
2016-12-22 21:36:23 -05:00
|
|
|
'media_type' => 'Anime',
|
|
|
|
'status' => $status,
|
|
|
|
],
|
2017-01-03 20:29:43 -05:00
|
|
|
'include' => 'media',
|
2016-12-22 21:36:23 -05:00
|
|
|
'page' => [
|
|
|
|
'offset' => 0,
|
|
|
|
'limit' => 200
|
2016-12-21 12:46:20 -05:00
|
|
|
],
|
2016-12-22 21:36:23 -05:00
|
|
|
'sort' => '-updated_at'
|
2016-12-21 12:46:20 -05:00
|
|
|
]
|
2016-12-22 21:36:23 -05:00
|
|
|
];
|
2016-12-21 12:46:20 -05:00
|
|
|
|
2016-12-22 21:36:23 -05:00
|
|
|
$data = $this->getRequest('library-entries', $options);
|
2016-12-21 12:46:20 -05:00
|
|
|
|
2017-01-03 20:29:43 -05:00
|
|
|
foreach($data['data'] as $i => &$item)
|
2016-12-21 12:46:20 -05:00
|
|
|
{
|
2017-01-03 20:29:43 -05:00
|
|
|
$item['anime'] = $data['included'][$i];
|
2016-12-21 12:46:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$transformed = $this->animeListTransformer->transformCollection($data['data']);
|
|
|
|
|
|
|
|
return $transformed;
|
|
|
|
}
|
2016-12-22 21:36:23 -05:00
|
|
|
|
|
|
|
private function getGenres(string $type, string $id): array
|
|
|
|
{
|
|
|
|
$data = $this->getRequest("{$type}/{$id}/genres");
|
|
|
|
$rawGenres = array_pluck($data['data'], 'attributes');
|
|
|
|
$genres = array_pluck($rawGenres, 'name');
|
|
|
|
|
|
|
|
return $genres;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getRawMediaData(string $type, string $id): array
|
|
|
|
{
|
|
|
|
$data = $this->getRequest("{$type}/{$id}");
|
|
|
|
$baseData = $data['data']['attributes'];
|
|
|
|
$baseData['genres'] = $this->getGenres($type, $id);
|
|
|
|
|
|
|
|
return $baseData;
|
|
|
|
}
|
2016-12-21 12:46:20 -05:00
|
|
|
}
|