HummingBirdAnimeClient/app/models/AnimeModel.php

221 lines
4.0 KiB
PHP
Raw Normal View History

2015-05-22 12:36:26 -04:00
<?php
/**
* Model for handling requests dealing with the anime list
*/
class AnimeModel extends BaseApiModel {
/**
* The base url for api requests
* @var string $base_url
*/
2015-05-22 12:36:26 -04:00
protected $base_url = "https://hummingbird.me/api/v1";
/**
* Constructor
*/
2015-05-22 12:36:26 -04:00
public function __construct()
{
parent::__construct();
}
/**
* Get the full set of anime lists
*
* @return array
*/
public function get_all_lists()
{
$output = [
'Watching' => [],
'Plan to Watch' => [],
'On Hold' => [],
2015-05-27 09:03:42 -04:00
'Dropped' => [],
'Completed' => [],
2015-05-22 12:36:26 -04:00
];
$data = $this->_get_list();
foreach($data as $datum)
{
switch($datum['status'])
{
case "completed":
$output['Completed'][] = $datum;
break;
case "plan-to-watch":
$output['Plan to Watch'][] = $datum;
break;
case "dropped":
$output['Dropped'][] = $datum;
break;
case "on-hold":
$output['On Hold'][] = $datum;
break;
case "currently-watching":
$output['Watching'][] = $datum;
break;
}
}
// Sort anime by name
foreach($output as &$status_list)
{
$this->sort_by_name($status_list);
}
return $output;
}
/**
* Get a category out of the full list
*
* @param string $status
2015-05-22 12:36:26 -04:00
* @return array
*/
public function get_list($status)
2015-05-22 12:36:26 -04:00
{
$map = [
'currently-watching' => 'Watching',
'plan-to-watch' => 'Plan to Watch',
'on-hold' => 'On Hold',
2015-05-27 09:03:42 -04:00
'dropped' => 'Dropped',
'completed' => 'Completed',
2015-05-22 12:36:26 -04:00
];
$data = $this->_get_list($status);
2015-05-22 12:36:26 -04:00
$this->sort_by_name($data);
$output = [];
$output[$map[$status]] = $data;
2015-05-22 12:36:26 -04:00
return $output;
}
/**
* Actually retreive the data from the api
*
* @param string $status - Status to filter by
* @return array
*/
private function _get_list($status="all")
2015-05-22 12:36:26 -04:00
{
global $defaultHandler;
$cache_file = "{$this->config->data_cache_path}/anime-{$status}.json";
2015-05-22 12:36:26 -04:00
$config = [
'query' => [
'username' => $this->config->hummingbird_username,
2015-05-22 12:36:26 -04:00
],
'allow_redirects' => false
];
if ($status != "all")
2015-05-22 12:36:26 -04:00
{
$config['query']['status'] = $status;
2015-05-22 12:36:26 -04:00
}
$response = $this->client->get($this->_url('/users/' . $this->config->hummingbird_username . '/library'), $config);
2015-05-22 12:36:26 -04:00
$defaultHandler->addDataTable('anime_list_response', (array)$response);
2015-05-22 12:36:26 -04:00
if ($response->getStatusCode() != 200)
{
if ( ! file_exists($cache_file))
{
throw new Exception($response->getEffectiveUrl());
}
else
{
$output = json_decode(file_get_contents($cache_file), TRUE);
}
}
else
{
$output = $response->json();
$output_json = json_encode($output);
2015-06-11 12:54:54 -04:00
if (( ! file_exists($cache_file)) || file_get_contents($cache_file) !== $output_json)
{
// Cache the call in case of downtime
file_put_contents($cache_file, json_encode($output));
}
2015-05-22 12:36:26 -04:00
}
2015-06-09 11:54:42 -04:00
foreach($output as &$row)
{
$row['anime']['cover_image'] = $this->get_cached_image($row['anime']['cover_image'], $row['anime']['slug'], 'anime');
}
return $output;
2015-05-22 12:36:26 -04:00
}
2015-06-11 12:54:54 -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($this->_url("/anime/{$anime_id}"), $config);
return $response->json();
}
/**
* Search for anime by name
*
* @param string $name
* @return array
*/
public function search($name)
{
global $defaultHandler;
$config = [
'query' => [
'query' => $name
]
];
$response = $this->client->get($this->_url('/search/anime'), $config);
$defaultHandler->addDataTable('anime_search_response', (array)$response);
if ($response->getStatusCode() != 200)
{
throw new Exception($response->getEffectiveUrl());
}
return $response->json();
}
/**
* Sort the list by title
*
* @param array &$array
* @return void
*/
2015-05-22 12:36:26 -04:00
private function sort_by_name(&$array)
{
$sort = array();
foreach($array as $key => $item)
{
$sort[$key] = $item['anime']['title'];
}
array_multisort($sort, SORT_ASC, $array);
}
}
// End of AnimeModel.php