2015-05-22 12:36:26 -04:00
|
|
|
<?php
|
2015-06-16 11:11:35 -04:00
|
|
|
/**
|
|
|
|
* Anime API Model
|
|
|
|
*/
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-09-14 19:54:34 -04:00
|
|
|
namespace Aviat\AnimeClient\Model;
|
2015-09-14 10:54:50 -04:00
|
|
|
|
2015-10-09 22:29:59 -04:00
|
|
|
use Aviat\AnimeClient\Hummingbird\Enum\AnimeWatchingStatus;
|
|
|
|
use Aviat\AnimeClient\Hummingbird\Transformer\AnimeListTransformer;
|
2015-06-26 16:39:10 -04: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
|
|
|
|
*/
|
2015-06-17 08:50:01 -04:00
|
|
|
protected $base_url = "https://hummingbird.me/api/v1/";
|
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,
|
|
|
|
];
|
|
|
|
|
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
|
|
|
|
* @return array
|
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-06 11:38:20 -04:00
|
|
|
// @TODO use Hummingbird Auth class
|
|
|
|
$data['auth_token'] = '';
|
2015-06-17 08:50:01 -04:00
|
|
|
|
2015-10-15 09:25:30 -04:00
|
|
|
$response = $this->client->post("libraries/{$data['id']}", [
|
2015-06-24 16:01:35 -04:00
|
|
|
'body' => $data
|
|
|
|
]);
|
2015-06-17 08:50:01 -04:00
|
|
|
|
2015-10-15 09:25:30 -04:00
|
|
|
return json_decode($result->getBody(), TRUE);
|
2015-06-17 08:50:01 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
$this->sort_by_name($status_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2015-10-01 16:01:23 -04:00
|
|
|
$data = $this->_get_list_From_api($status);
|
2015-05-22 12:36:26 -04:00
|
|
|
$this->sort_by_name($data);
|
|
|
|
|
|
|
|
$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)
|
|
|
|
{
|
|
|
|
$config = [
|
|
|
|
'query' => [
|
|
|
|
'id' => $anime_id
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->client->get("anime/{$anime_id}", $config);
|
|
|
|
|
|
|
|
return $response->json();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for anime by name
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function search($name)
|
|
|
|
{
|
2015-09-17 23:11:18 -04:00
|
|
|
$errorHandler = $this->container->get('error-handler');
|
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-09-17 23:11:18 -04:00
|
|
|
$errorHandler->addDataTable('anime_search_response', (array)$response);
|
2015-06-24 16:01:35 -04:00
|
|
|
|
|
|
|
if ($response->getStatusCode() != 200)
|
|
|
|
{
|
2015-07-02 14:04:04 -04:00
|
|
|
throw new RuntimeException($response->getEffectiveUrl());
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
2015-10-15 09:25:30 -04:00
|
|
|
return json_decode($response->getBody(), TRUE);
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:53 -04:00
|
|
|
/**
|
2015-10-01 16:01:23 -04:00
|
|
|
* Retrieve data from the api
|
2015-06-09 18:18:53 -04:00
|
|
|
*
|
2015-10-05 16:54:25 -04:00
|
|
|
* @codeCoverageIgnore
|
2015-10-01 16:01:23 -04:00
|
|
|
* @param string $status
|
2015-06-09 18:18:53 -04:00
|
|
|
* @return array
|
|
|
|
*/
|
2015-10-06 10:24:48 -04:00
|
|
|
protected function _get_list_from_api($status = "all")
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
|
|
|
$config = [
|
2015-06-29 10:26:50 -04:00
|
|
|
'allow_redirects' => FALSE
|
2015-05-22 12:36:26 -04:00
|
|
|
];
|
|
|
|
|
2015-06-09 18:18:53 -04:00
|
|
|
if ($status != "all")
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2015-06-09 18:18:53 -04:00
|
|
|
$config['query']['status'] = $status;
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
2015-10-06 11:38:20 -04:00
|
|
|
$username = $this->config->get('hummingbird_username');
|
2015-10-15 09:25:30 -04:00
|
|
|
$response = $this->get("users/{$username}/library", $config);
|
2015-10-01 16:01:23 -04:00
|
|
|
$output = $this->_check_cache($status, $response);
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-10-06 10:24:48 -04:00
|
|
|
foreach ($output as &$row)
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2015-10-01 16:01:23 -04:00
|
|
|
$row['anime']['image'] = $this->get_cached_image($row['anime']['image'], $row['anime']['slug'], 'anime');
|
2015-06-09 18:18:53 -04:00
|
|
|
}
|
2015-06-11 12:54:54 -04:00
|
|
|
|
2015-10-01 16:01:23 -04:00
|
|
|
return $output;
|
|
|
|
}
|
2015-06-09 11:54:42 -04:00
|
|
|
|
2015-10-01 16:01:23 -04:00
|
|
|
/**
|
|
|
|
* Handle caching of transformed api data
|
|
|
|
*
|
2015-10-05 16:54:25 -04:00
|
|
|
* @codeCoverageIgnore
|
2015-10-01 16:01:23 -04:00
|
|
|
* @param string $status
|
|
|
|
* @param \GuzzleHttp\Message\Response
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-10-05 16:54:25 -04:00
|
|
|
protected function _check_cache($status, $response)
|
2015-10-01 16:01:23 -04:00
|
|
|
{
|
2015-10-06 11:38:20 -04:00
|
|
|
$cache_file = _dir($this->config->get('data_cache_path'), "anime-{$status}.json");
|
|
|
|
$transformed_cache_file = _dir($this->config->get('data_cache_path'), "anime-{$status}-transformed.json");
|
2015-10-01 16:01:23 -04:00
|
|
|
|
|
|
|
$cached = json_decode(file_get_contents($cache_file), TRUE);
|
2015-10-05 16:54:25 -04:00
|
|
|
$api_data = json_decode($response->getBody(), TRUE);
|
2015-10-01 16:01:23 -04:00
|
|
|
|
2015-10-05 16:54:25 -04:00
|
|
|
if ($api_data === $cached && file_exists($transformed_cache_file))
|
2015-06-09 11:54:42 -04:00
|
|
|
{
|
2015-10-06 10:24:48 -04:00
|
|
|
return json_decode(file_get_contents($transformed_cache_file), TRUE);
|
2015-10-01 16:01:23 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-05 16:54:25 -04:00
|
|
|
file_put_contents($cache_file, json_encode($api_data));
|
|
|
|
$transformer = new AnimeListTransformer();
|
|
|
|
$transformed = $transformer->transform_collection($api_data);
|
|
|
|
file_put_contents($transformed_cache_file, json_encode($transformed));
|
|
|
|
return $transformed;
|
2015-06-09 11:54:42 -04:00
|
|
|
}
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:53 -04:00
|
|
|
/**
|
|
|
|
* Sort the list by title
|
|
|
|
*
|
2015-06-16 11:11:35 -04:00
|
|
|
* @param array $array
|
2015-06-09 18:18:53 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2015-10-05 16:54:25 -04:00
|
|
|
protected function sort_by_name(&$array)
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
|
|
|
$sort = array();
|
|
|
|
|
2015-10-06 10:24:48 -04:00
|
|
|
foreach ($array as $key => $item)
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
|
|
|
$sort[$key] = $item['anime']['title'];
|
|
|
|
}
|
|
|
|
|
|
|
|
array_multisort($sort, SORT_ASC, $array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// End of AnimeModel.php
|