2015-06-16 11:11:35 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Manga API Model
|
|
|
|
*/
|
2015-09-14 19:54:34 -04:00
|
|
|
namespace Aviat\AnimeClient\Model;
|
2015-09-14 10:54:50 -04:00
|
|
|
|
2015-09-15 13:19:29 -04:00
|
|
|
use Aviat\AnimeClient\Model\API;
|
2015-06-16 11:11:35 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Model for handling requests dealing with the manga list
|
|
|
|
*/
|
2015-09-14 10:54:50 -04:00
|
|
|
class Manga extends API {
|
2015-06-16 11:11:35 -04:00
|
|
|
|
|
|
|
/**
|
2015-06-26 16:39:10 -04:00
|
|
|
* The base url for api requests
|
|
|
|
* @var string
|
2015-06-16 11:11:35 -04:00
|
|
|
*/
|
2015-06-17 08:50:01 -04:00
|
|
|
protected $base_url = "https://hummingbird.me/";
|
2015-06-16 11:11:35 -04:00
|
|
|
|
2015-06-24 16:01:35 -04:00
|
|
|
/**
|
|
|
|
* Update the selected manga
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function update($data)
|
|
|
|
{
|
|
|
|
$id = $data['id'];
|
|
|
|
unset($data['id']);
|
|
|
|
|
|
|
|
$result = $this->client->put("manga_library_entries/{$id}", [
|
|
|
|
'cookies' => ['token' => $_SESSION['hummingbird_anime_token']],
|
|
|
|
'json' => ['manga_library_entry' => $data]
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $result->json();
|
|
|
|
}
|
|
|
|
|
2015-06-16 11:11:35 -04:00
|
|
|
/**
|
|
|
|
* Get the full set of anime lists
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_all_lists()
|
|
|
|
{
|
|
|
|
$data = $this->_get_list();
|
|
|
|
|
|
|
|
foreach ($data as $key => &$val)
|
|
|
|
{
|
|
|
|
$this->sort_by_name($val);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a category out of the full list
|
|
|
|
*
|
|
|
|
* @param string $status
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_list($status)
|
|
|
|
{
|
|
|
|
$data = $this->_get_list($status);
|
|
|
|
|
|
|
|
$this->sort_by_name($data);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Massage the list of manga entries into something more usable
|
|
|
|
*
|
|
|
|
* @param string $status
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function _get_list($status="all")
|
|
|
|
{
|
2015-09-17 23:11:18 -04:00
|
|
|
$errorHandler = $this->container->get('error-handler');
|
2015-06-16 11:11:35 -04:00
|
|
|
|
|
|
|
$cache_file = _dir($this->config->data_cache_path, 'manga.json');
|
|
|
|
|
|
|
|
$config = [
|
|
|
|
'query' => [
|
|
|
|
'user_id' => $this->config->hummingbird_username
|
|
|
|
],
|
2015-06-29 10:26:50 -04:00
|
|
|
'allow_redirects' => FALSE
|
2015-06-16 11:11:35 -04:00
|
|
|
];
|
|
|
|
|
2015-06-17 08:50:01 -04:00
|
|
|
$response = $this->client->get('manga_library_entries', $config);
|
2015-06-16 11:11:35 -04:00
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
$errorHandler->addDataTable('response', (array)$response);
|
2015-06-16 11:11:35 -04:00
|
|
|
|
|
|
|
if ($response->getStatusCode() != 200)
|
|
|
|
{
|
|
|
|
if ( ! file_exists($cache_file))
|
|
|
|
{
|
2015-07-02 14:04:04 -04:00
|
|
|
throw new DomainException($response->getEffectiveUrl());
|
2015-06-16 11:11:35 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$raw_data = json_decode(file_get_contents($cache_file), TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Reorganize data to be more usable
|
|
|
|
$raw_data = $response->json();
|
|
|
|
|
2015-06-26 15:32:49 -04:00
|
|
|
// Attempt to create the cache dir if it doesn't exist
|
2015-06-26 16:39:10 -04:00
|
|
|
if ( ! is_dir($this->config->data_cache_path))
|
2015-06-26 15:32:49 -04:00
|
|
|
{
|
2015-06-26 16:39:10 -04:00
|
|
|
mkdir($this->config->data_cache_path);
|
2015-06-26 15:32:49 -04:00
|
|
|
}
|
|
|
|
|
2015-06-16 11:11:35 -04:00
|
|
|
// Cache data in case of downtime
|
|
|
|
file_put_contents($cache_file, json_encode($raw_data));
|
|
|
|
}
|
|
|
|
|
2015-06-25 17:00:29 -04:00
|
|
|
// Bail out early if there isn't any manga data
|
2015-07-02 14:04:04 -04:00
|
|
|
if ( ! array_key_exists('manga', $raw_data)) return [];
|
2015-06-25 17:00:29 -04:00
|
|
|
|
2015-06-16 11:11:35 -04:00
|
|
|
$data = [
|
|
|
|
'Reading' => [],
|
|
|
|
'Plan to Read' => [],
|
|
|
|
'On Hold' => [],
|
|
|
|
'Dropped' => [],
|
|
|
|
'Completed' => [],
|
|
|
|
];
|
|
|
|
$manga_data = [];
|
|
|
|
|
|
|
|
// Massage the two lists into one
|
|
|
|
foreach($raw_data['manga'] as $manga)
|
|
|
|
{
|
|
|
|
$manga_data[$manga['id']] = $manga;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter data by status
|
|
|
|
foreach($raw_data['manga_library_entries'] as &$entry)
|
|
|
|
{
|
|
|
|
$entry['manga'] = $manga_data[$entry['manga_id']];
|
|
|
|
|
|
|
|
// Cache poster images
|
|
|
|
$entry['manga']['poster_image'] = $this->get_cached_image($entry['manga']['poster_image'], $entry['manga_id'], 'manga');
|
|
|
|
|
|
|
|
switch($entry['status'])
|
|
|
|
{
|
|
|
|
case "Plan to Read":
|
|
|
|
$data['Plan to Read'][] = $entry;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Dropped":
|
|
|
|
$data['Dropped'][] = $entry;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "On Hold":
|
|
|
|
$data['On Hold'][] = $entry;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Currently Reading":
|
|
|
|
$data['Reading'][] = $entry;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Completed":
|
|
|
|
default:
|
|
|
|
$data['Completed'][] = $entry;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (array_key_exists($status, $data)) ? $data[$status] : $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort the manga entries by their title
|
|
|
|
*
|
|
|
|
* @param array $array
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function sort_by_name(&$array)
|
|
|
|
{
|
|
|
|
$sort = array();
|
|
|
|
|
|
|
|
foreach($array as $key => $item)
|
|
|
|
{
|
|
|
|
$sort[$key] = $item['manga']['romaji_title'];
|
|
|
|
}
|
|
|
|
|
|
|
|
array_multisort($sort, SORT_ASC, $array);
|
|
|
|
}
|
|
|
|
}
|
2015-09-17 23:11:18 -04:00
|
|
|
// End of MangaModel.php
|