2016-10-20 22:09:36 -04:00
|
|
|
<?php declare(strict_types=1);
|
2015-06-16 11:11:35 -04:00
|
|
|
/**
|
2016-12-20 12:58:37 -05:00
|
|
|
* Anime List Client
|
2015-11-16 11:40:01 -05:00
|
|
|
*
|
2016-12-20 12:58:37 -05:00
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
|
2015-11-16 11:40:01 -05:00
|
|
|
*
|
2016-10-20 22:09:36 -04:00
|
|
|
* PHP version 7
|
2016-08-30 10:01:18 -04:00
|
|
|
*
|
2016-12-21 12:46:20 -05:00
|
|
|
* @package AnimeListClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2016-08-30 10:01:18 -04:00
|
|
|
* @copyright 2015 - 2016 Timothy J. Warren
|
2016-12-21 12:46:20 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @version 4.0
|
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient
|
2015-06-16 11:11:35 -04:00
|
|
|
*/
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-09-14 19:54:34 -04:00
|
|
|
namespace Aviat\AnimeClient\Model;
|
2016-12-20 12:58:37 -05:00
|
|
|
use Aviat\AnimeClient\API\Kitsu\Enum\AnimeWatchingStatus;
|
2016-12-21 12:46:20 -05:00
|
|
|
use Aviat\Ion\Di\ContainerInterface;
|
2016-10-20 22:32:17 -04:00
|
|
|
use Aviat\Ion\Json;
|
2015-06-26 16:39:10 -04:00
|
|
|
|
2016-12-21 12:46:20 -05:00
|
|
|
|
2015-05-22 12:36:26 -04:00
|
|
|
/**
|
|
|
|
* Model for handling requests dealing with the anime list
|
|
|
|
*/
|
2015-09-14 10:54:50 -04:00
|
|
|
class Anime extends API {
|
2015-10-01 16:01:23 -04:00
|
|
|
|
|
|
|
// Display constants
|
|
|
|
const WATCHING = 'Watching';
|
|
|
|
const PLAN_TO_WATCH = 'Plan to Watch';
|
|
|
|
const DROPPED = 'Dropped';
|
|
|
|
const ON_HOLD = 'On Hold';
|
|
|
|
const COMPLETED = 'Completed';
|
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* The base url for api requests
|
|
|
|
* @var string $base_url
|
|
|
|
*/
|
2016-12-20 12:58:37 -05:00
|
|
|
protected $base_url = "https://kitsu.io/api/edge/";
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-10-05 16:54:25 -04:00
|
|
|
/**
|
|
|
|
* Map of API status constants to display constants
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $const_map = [
|
|
|
|
AnimeWatchingStatus::WATCHING => self::WATCHING,
|
|
|
|
AnimeWatchingStatus::PLAN_TO_WATCH => self::PLAN_TO_WATCH,
|
|
|
|
AnimeWatchingStatus::ON_HOLD => self::ON_HOLD,
|
|
|
|
AnimeWatchingStatus::DROPPED => self::DROPPED,
|
|
|
|
AnimeWatchingStatus::COMPLETED => self::COMPLETED,
|
|
|
|
];
|
|
|
|
|
2016-12-22 21:36:23 -05:00
|
|
|
/**
|
|
|
|
* Anime constructor.
|
|
|
|
* @param ContainerInterface $container
|
|
|
|
*/
|
2016-12-21 12:46:20 -05:00
|
|
|
public function __construct(ContainerInterface $container) {
|
|
|
|
parent::__construct($container);
|
|
|
|
|
2016-12-22 21:36:23 -05:00
|
|
|
$this->kitsuModel = $container->get('kitsu-model');
|
2016-12-21 12:46:20 -05:00
|
|
|
}
|
|
|
|
|
2015-06-17 08:50:01 -04:00
|
|
|
/**
|
2015-06-24 16:01:35 -04:00
|
|
|
* Update the selected anime
|
2015-06-17 08:50:01 -04:00
|
|
|
*
|
2015-06-24 16:01:35 -04:00
|
|
|
* @param array $data
|
2015-10-21 11:57:58 -04:00
|
|
|
* @return array|false
|
2015-06-17 08:50:01 -04:00
|
|
|
*/
|
2015-06-24 16:01:35 -04:00
|
|
|
public function update($data)
|
2015-06-17 08:50:01 -04:00
|
|
|
{
|
2015-10-21 11:57:58 -04:00
|
|
|
$auth = $this->container->get('auth');
|
2016-08-30 10:57:41 -04:00
|
|
|
if ( ! $auth->is_authenticated() OR ! array_key_exists('id', $data))
|
2015-10-21 11:57:58 -04:00
|
|
|
{
|
|
|
|
return FALSE;
|
2016-01-04 16:58:33 -05:00
|
|
|
}
|
2015-10-21 11:57:58 -04:00
|
|
|
|
2016-01-04 10:53:03 -05:00
|
|
|
$id = $data['id'];
|
2015-10-21 11:57:58 -04:00
|
|
|
$data['auth_token'] = $auth->get_auth_token();
|
2015-06-17 08:50:01 -04:00
|
|
|
|
2016-01-04 10:53:03 -05:00
|
|
|
$response = $this->client->post("libraries/{$id}", [
|
2015-11-13 11:33:27 -05:00
|
|
|
'form_params' => $data
|
2015-06-24 16:01:35 -04:00
|
|
|
]);
|
2015-06-17 08:50:01 -04:00
|
|
|
|
2016-01-06 11:08:56 -05:00
|
|
|
return [
|
2016-01-04 10:53:03 -05:00
|
|
|
'statusCode' => $response->getStatusCode(),
|
2016-01-07 13:45:43 -05:00
|
|
|
'body' => Json::decode($response->getBody(), TRUE)
|
2016-01-04 10:53:03 -05:00
|
|
|
];
|
2015-06-17 08:50:01 -04:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:28:32 -05:00
|
|
|
/**
|
|
|
|
* Remove an anime from a list
|
|
|
|
*
|
|
|
|
* @param array $data
|
2016-02-02 21:38:38 -05:00
|
|
|
* @return array|false
|
2016-02-02 21:28:32 -05:00
|
|
|
*/
|
|
|
|
public function delete($data)
|
|
|
|
{
|
|
|
|
$auth = $this->container->get('auth');
|
2016-08-30 10:57:41 -04:00
|
|
|
if ( ! $auth->is_authenticated() OR ! array_key_exists('id', $data))
|
2016-02-02 21:28:32 -05:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = $data['id'];
|
|
|
|
$data['auth_token'] = $auth->get_auth_token();
|
|
|
|
|
|
|
|
$response = $this->client->post("libraries/{$id}/remove", [
|
|
|
|
'form_params' => $data
|
|
|
|
]);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'statusCode' => $response->getStatusCode(),
|
|
|
|
'body' => Json::decode($response->getBody(), TRUE)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-05-22 12:36:26 -04:00
|
|
|
/**
|
|
|
|
* Get the full set of anime lists
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_all_lists()
|
|
|
|
{
|
|
|
|
$output = [
|
2015-10-01 16:01:23 -04:00
|
|
|
self::WATCHING => [],
|
|
|
|
self::PLAN_TO_WATCH => [],
|
|
|
|
self::ON_HOLD => [],
|
|
|
|
self::DROPPED => [],
|
|
|
|
self::COMPLETED => [],
|
2015-05-22 12:36:26 -04:00
|
|
|
];
|
|
|
|
|
2015-10-05 16:54:25 -04:00
|
|
|
$data = $this->_get_list_from_api();
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-10-06 10:24:48 -04:00
|
|
|
foreach ($data as $datum)
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2015-10-05 16:54:25 -04:00
|
|
|
$output[$this->const_map[$datum['watching_status']]][] = $datum;
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort anime by name
|
2015-10-06 10:24:48 -04:00
|
|
|
foreach ($output as &$status_list)
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2016-01-04 16:58:33 -05:00
|
|
|
$this->sort_by_name($status_list, 'anime');
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a category out of the full list
|
|
|
|
*
|
2015-06-09 18:18:53 -04:00
|
|
|
* @param string $status
|
2015-05-22 12:36:26 -04:00
|
|
|
* @return array
|
|
|
|
*/
|
2015-06-09 18:18:53 -04:00
|
|
|
public function get_list($status)
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2016-12-22 21:36:23 -05:00
|
|
|
$data = $this->kitsuModel->getAnimeList($status);
|
|
|
|
$this->sort_by_name($data, 'anime');
|
2015-05-22 12:36:26 -04:00
|
|
|
|
|
|
|
$output = [];
|
2015-10-05 16:54:25 -04:00
|
|
|
$output[$this->const_map[$status]] = $data;
|
2015-05-22 12:36:26 -04:00
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2015-06-24 16:01:35 -04:00
|
|
|
/**
|
|
|
|
* Get information about an anime from its id
|
|
|
|
*
|
|
|
|
* @param string $anime_id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_anime($anime_id)
|
|
|
|
{
|
2016-12-22 21:36:23 -05:00
|
|
|
return $this->kitsuModel->getAnime($anime_id);
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for anime by name
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return array
|
2016-08-30 10:57:41 -04:00
|
|
|
* @throws \RuntimeException
|
2015-06-24 16:01:35 -04:00
|
|
|
*/
|
|
|
|
public function search($name)
|
|
|
|
{
|
2016-01-11 14:39:53 -05:00
|
|
|
$logger = $this->container->getLogger('default');
|
2015-06-24 16:01:35 -04:00
|
|
|
|
|
|
|
$config = [
|
|
|
|
'query' => [
|
|
|
|
'query' => $name
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2015-10-15 09:25:30 -04:00
|
|
|
$response = $this->get('search/anime', $config);
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2016-08-30 10:57:41 -04:00
|
|
|
if ((int) $response->getStatusCode() !== 200)
|
2015-06-24 16:01:35 -04:00
|
|
|
{
|
2016-01-11 15:31:53 -05:00
|
|
|
$logger->warning("Non 200 response for search api call");
|
|
|
|
$logger->warning($response->getBody());
|
2016-01-11 14:39:53 -05:00
|
|
|
|
2016-08-30 10:57:41 -04:00
|
|
|
throw new \RuntimeException($response->getEffectiveUrl());
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
2016-01-07 13:45:43 -05:00
|
|
|
return Json::decode($response->getBody(), TRUE);
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
2016-04-19 13:02:50 -04:00
|
|
|
/**
|
|
|
|
* Get the full list from the api
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_raw_list()
|
|
|
|
{
|
|
|
|
$config = [
|
|
|
|
'allow_redirects' => FALSE
|
|
|
|
];
|
|
|
|
|
|
|
|
$username = $this->config->get('hummingbird_username');
|
|
|
|
|
|
|
|
$response = $this->get("users/{$username}/library", $config);
|
|
|
|
return Json::decode($response->getBody(), TRUE);
|
|
|
|
}
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
// End of AnimeModel.php
|