From 7c913986d42d6cbcc8a7b13ffca677d97e8de76f Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 9 Jun 2015 18:18:53 -0400 Subject: [PATCH] More caching, and start of anime collection view --- .gitignore | 4 ++ anime_collection.sqlite | Bin 0 -> 8192 bytes app/base/BaseController.php | 25 ++------- app/base/BaseModel.php | 8 ++- app/config/config.php | 9 +++ app/config/database.php | 13 +++++ app/config/routes.php | 6 ++ app/controllers/AnimeController.php | 17 ++++-- app/controllers/MangaController.php | 5 -- app/models/AnimeCollectionModel.php | 28 ++++++++++ app/models/AnimeModel.php | 84 +++++++++++++++++++++++----- app/models/MangaModel.php | 46 ++++++++++++--- app/views/anime_collection.php | 0 app/views/anime_nav.php | 1 + composer.json | 3 +- index.php | 4 +- 16 files changed, 195 insertions(+), 58 deletions(-) create mode 100644 .gitignore create mode 100755 anime_collection.sqlite create mode 100644 app/config/config.php create mode 100644 app/config/database.php create mode 100644 app/models/AnimeCollectionModel.php create mode 100644 app/views/anime_collection.php 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 0000000000000000000000000000000000000000..d531f2afe150ee8dfab621ec566ddecd8b517ab0 GIT binary patch literal 8192 zcmeI0F>ljA6vyxUV!H_ns?@=%GUU3@MoonxW+a^U8b!3Bb=*QH%O%&=lH;U~EwzZH z;v>;d022#rAhi=9Re}K)*cdpw$(|IL5& zT+@wW5x=NK?zYd|z-u$7$0A|qixxF)N^G;bLWyvbxxKjE_JZAQFKjtpi&X2idc!7b zR(;j9w#Y-eRVF<@-X%@jsyAw75{JI9BQNrqXt{pG!oZD~Gwg4(mggpO^NDGVTEwEu z)S`_=+9YWone$quGU>+KzSrAh;fQNRfB*WR6R{q#=@YwD(KUk>G4g^IJLv8E{B3gF zIO?QrM`Y_%ggMd5=!Rk7quCT#8b^9ihd_z=MDQ2L5Oc$RRB!MBtF&R4Np#p9&>$O) z`uYYfl~m2RZ;T2oo!zOVCv6QhV}2fQrSbH0=&}8n1y9+fW@aeHOPzCp+h%1xved=t zJ8^V3ol@WvhB7pba3^^GCEesc{f9qDx1#**Ify$r6=9}&&U3lx@Djlf_yQl{J-mfi zyeT(HKoYo~1oAqdndO+TGj8N{f}94ZUoA46{?!~z<8%FI5*OD$B(8J)D>*1+`}$ATe+AAE*ZrAfD+x#f<47Q{KoN_=U(aGN zaQf*3iT(jj5Pahmz{xn~E_0FuZX$uS@L$?o%JIU#Q1#=5&^?^wmH#aFkvpXCN$P)q zQv{#k6u#d?PBLdnU@QqFg+JFiB;N;pwJVb_n;p`Bvi=YK)gMa~GAT(w61a*$vi?i` NUqwdxB!TfK@EeboJ|+MF literal 0 HcmV?d00001 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 @@
  • On Hold
  • Dropped
  • Completed
  • + ">Collection */ ?>
  • All
  • \ No newline at end of file diff --git a/composer.json b/composer.json index 5d069476..c126b95a 100644 --- a/composer.json +++ b/composer.json @@ -2,6 +2,7 @@ "require": { "guzzlehttp/guzzle": "5.3.*", "filp/whoops": "1.1.*", - "aura/router": "2.2.*" + "aura/router": "2.2.*", + "aviat4ion/query": "2.0.*" } } \ No newline at end of file diff --git a/index.php b/index.php index 07716458..0ce959eb 100644 --- a/index.php +++ b/index.php @@ -13,6 +13,8 @@ function is_selected($a, $b) return ($a === $b) ? 'selected' : ''; } +$config = require_once(__DIR__ . '/app/config/config.php'); + // ----------------------------------------------------------------------------- // Setup error handling // ----------------------------------------------------------------------------- @@ -34,7 +36,7 @@ $whoops->register(); // ----------------------------------------------------------------------------- $router = new Router(); -$defaultHandler->addDataTable('route', (array)$router->get_route()); +//$defaultHandler->addDataTable('route', (array)$router->get_route()); $router->dispatch(); // End of index.php \ No newline at end of file