diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..d8e76b2e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +vendor +app/cache/* +public/images/* +composer.lock \ No newline at end of file diff --git a/anime_collection.sqlite b/anime_collection.sqlite new file mode 100755 index 00000000..d531f2af Binary files /dev/null and b/anime_collection.sqlite differ diff --git a/app/base/BaseController.php b/app/base/BaseController.php index 0c9d2126..a3aa14e5 100644 --- a/app/base/BaseController.php +++ b/app/base/BaseController.php @@ -2,29 +2,12 @@ class BaseController { + protected $config; + public function __construct() { - - } - - public function __destruct() - { - - } - - public function login() - { - - } - - public function logout() - { - - } - - public function search() - { - + global $config; + $this->config = $config; } /** diff --git a/app/base/BaseModel.php b/app/base/BaseModel.php index 8e14f0b0..a0de0e86 100644 --- a/app/base/BaseModel.php +++ b/app/base/BaseModel.php @@ -6,10 +6,14 @@ use \GuzzleHttp\Cookie\CookieJar; class BaseModel { protected $client; + protected $config; protected $cookieJar; public function __construct() { + global $config; + + $this->config = $config; $this->cookieJar = new CookieJar(); $this->client = new Client([ 'base_url' => $this->base_url, @@ -53,7 +57,7 @@ class BaseModel { $ext = end($ext_parts); $cached_image = "{$series_slug}.{$ext}"; - $cached_path = __DIR__ . "/../../public/cache/{$type}/{$cached_image}"; + $cached_path = "{$this->config->img_cache_path}/{$type}/{$cached_image}"; // Cache the file if it doesn't already exist if ( ! file_exists($cached_path)) @@ -80,7 +84,7 @@ class BaseModel { } } - return "/public/cache/{$type}/{$cached_image}"; + return "/public/images/{$type}/{$cached_image}"; } } // End of BaseModel.php \ No newline at end of file diff --git a/app/config/config.php b/app/config/config.php new file mode 100644 index 00000000..1982bc8f --- /dev/null +++ b/app/config/config.php @@ -0,0 +1,9 @@ + 'timw4mail', + + // Cache paths + 'data_cache_path' => __DIR__ . '/../cache', + 'img_cache_path' => __DIR__ .'/../../public/images' +]; \ No newline at end of file diff --git a/app/config/database.php b/app/config/database.php new file mode 100644 index 00000000..629b5ffb --- /dev/null +++ b/app/config/database.php @@ -0,0 +1,13 @@ + [ + 'type' => 'sqlite', + 'host' => '', + 'user' => '', + 'pass' => '', + 'port' => '', + 'name' => 'default', + 'database' => '', + 'file' => __DIR__ . '/../../anime_collection.sqlite', + ] +]; \ No newline at end of file diff --git a/app/config/routes.php b/app/config/routes.php index 22189a0d..1718f5b0 100644 --- a/app/config/routes.php +++ b/app/config/routes.php @@ -43,6 +43,12 @@ return [ 'type' => 'completed' ] ], + 'collection' => [ + 'path' => '/collection', + 'controller' => 'AnimeController', + 'action' => 'collection', + 'params' => [] + ], 'anime_login' => [ 'path' => '/login', 'controller' => 'AnimeController', diff --git a/app/controllers/AnimeController.php b/app/controllers/AnimeController.php index e854f51d..a0c26f5f 100644 --- a/app/controllers/AnimeController.php +++ b/app/controllers/AnimeController.php @@ -3,16 +3,13 @@ class AnimeController extends BaseController { private $model; + private $collection_model; public function __construct() { parent::__construct(); $this->model = new AnimeModel(); - } - - public function __destruct() - { - parent::__destruct(); + $this->collection_model = new AnimeCollectionModel(); } public function index() @@ -38,6 +35,16 @@ class AnimeController extends BaseController { ]); } + public function collection() + { + $this->collection_model->get_collection_seed(); + + $this->outputHTML('anime_list', [ + 'title' => "Tim's Anime Collection", + 'sections' => [] + ]); + } + public function login() { $data = $this->model->authenticate(); diff --git a/app/controllers/MangaController.php b/app/controllers/MangaController.php index c36935af..1b28eb8b 100644 --- a/app/controllers/MangaController.php +++ b/app/controllers/MangaController.php @@ -10,11 +10,6 @@ class MangaController extends BaseController { $this->model = new MangaModel(); } - public function __destruct() - { - parent::__destruct(); - } - public function index() { $this->manga_list('Reading'); diff --git a/app/models/AnimeCollectionModel.php b/app/models/AnimeCollectionModel.php new file mode 100644 index 00000000..c446db1e --- /dev/null +++ b/app/models/AnimeCollectionModel.php @@ -0,0 +1,28 @@ +db_config = require_once(__DIR__ . '/../config/database.php'); + $this->db = Query($this->db_config['collection']); + + $this->anime_model = new AnimeModel(); + + //parent::__construct(); + } + + public function get_collection() + { + // TODO: show collection from database + } + +} +// End of AnimeCollectionModel.php \ No newline at end of file diff --git a/app/models/AnimeModel.php b/app/models/AnimeModel.php index 93109482..78958ae2 100644 --- a/app/models/AnimeModel.php +++ b/app/models/AnimeModel.php @@ -4,7 +4,6 @@ * Model for handling requests dealing with the anime list */ class AnimeModel extends BaseModel { - protected $client; protected $cookieJar; protected $base_url = "https://hummingbird.me/api/v1"; @@ -69,10 +68,10 @@ class AnimeModel extends BaseModel { /** * Get a category out of the full list * - * @param string $type + * @param string $status * @return array */ - public function get_list($type) + public function get_list($status) { $map = [ 'currently-watching' => 'Watching', @@ -82,41 +81,65 @@ class AnimeModel extends BaseModel { 'completed' => 'Completed', ]; - $data = $this->_get_list($type); + $data = $this->_get_list($status); $this->sort_by_name($data); $output = []; - $output[$map[$type]] = $data; + $output[$map[$status]] = $data; return $output; } - private function _get_list($type="all") + /** + * Actually retreive the data from the api + * + * @param string $status - Status to filter by + * @return array + */ + private function _get_list($status="all") { global $defaultHandler; + $cache_file = "{$this->config->data_cache_path}/anime-{$status}.json"; + $config = [ 'query' => [ - 'username' => 'timw4mail', + 'username' => $this->config->hummingbird_username, ], 'allow_redirects' => false ]; - if ($type != "all") + if ($status != "all") { - $config['query']['status'] = $type; + $config['query']['status'] = $status; } - $response = $this->client->get($this->_url('/users/timw4mail/library'), $config); + $response = $this->client->get($this->_url('/users/' . $this->config->hummingbird_username . '/library'), $config); - $defaultHandler->addDataTable('response', (array)$response); + $defaultHandler->addDataTable('anime_list_response', (array)$response); if ($response->getStatusCode() != 200) { - throw new Exception($response->getEffectiveUrl()); + 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); + + if (file_get_contents($cache_file) !== $output_json) + { + // Cache the call in case of downtime + file_put_contents($cache_file, json_encode($output)); + } } - - $output = $response->json(); foreach($output as &$row) { @@ -126,6 +149,39 @@ class AnimeModel extends BaseModel { return $output; } + /** + * 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 + */ private function sort_by_name(&$array) { $sort = array(); diff --git a/app/models/MangaModel.php b/app/models/MangaModel.php index 1ebdfb2f..e2f1b068 100644 --- a/app/models/MangaModel.php +++ b/app/models/MangaModel.php @@ -34,25 +34,33 @@ class MangaModel extends BaseModel { /** * Get a category out of the full list * - * @param string $type + * @param string $status * @return array */ - public function get_list($type) + public function get_list($status) { - $data = $this->_get_list($type); + $data = $this->_get_list($status); $this->sort_by_name($data); return $data; } - private function _get_list($type="all") + /** + * Massage the list of manga entries into something more usable + * + * @param string $status + * @return array + */ + private function _get_list($status="all") { global $defaultHandler; + $cache_file = __DIR__ . "/../cache/manga.json"; + $config = [ 'query' => [ - 'user_id' => 'timw4mail', + 'user_id' => $this->config->hummingbird_username ], 'allow_redirects' => false ]; @@ -63,11 +71,23 @@ class MangaModel extends BaseModel { if ($response->getStatusCode() != 200) { - throw new Exception($response->getEffectiveUrl()); + if ( ! file_exists($cache_file)) + { + throw new Exception($response->getEffectiveUrl()); + } + else + { + $raw_data = json_decode(file_get_contents($cache_file), TRUE); + } } + else + { + // Reorganize data to be more usable + $raw_data = $response->json(); - // Reorganize data to be more usable - $raw_data = $response->json(); + // Cache data in case of downtime + file_put_contents($cache_file, json_encode($raw_data)); + } $data = [ 'Reading' => [], @@ -78,11 +98,13 @@ class MangaModel extends BaseModel { ]; $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']]; @@ -115,9 +137,15 @@ class MangaModel extends BaseModel { } } - return (array_key_exists($type, $data)) ? $data[$type] : $data; + 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(); diff --git a/app/views/anime_collection.php b/app/views/anime_collection.php new file mode 100644 index 00000000..e69de29b diff --git a/app/views/anime_nav.php b/app/views/anime_nav.php index a76afda4..287d2040 100644 --- a/app/views/anime_nav.php +++ b/app/views/anime_nav.php @@ -5,6 +5,7 @@