From 89080171c9b2a385028b9303bf1c703f2765a7bf Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 19 Apr 2016 13:02:50 -0400 Subject: [PATCH] Add batch image thumbnail creation, see #6, #14 --- composer.json | 5 +- console | 58 ++++++++++ public/images/anime/.gitkeep | 0 public/images/manga/.gitkeep | 0 src/Aviat/AnimeClient/Command/BaseCommand.php | 65 +++++++++++ src/Aviat/AnimeClient/Command/CacheImages.php | 109 ++++++++++++++++++ src/Aviat/AnimeClient/Model.php | 15 ++- src/Aviat/AnimeClient/Model/Anime.php | 22 ++++ 8 files changed, 269 insertions(+), 5 deletions(-) create mode 100755 console create mode 100644 public/images/anime/.gitkeep create mode 100644 public/images/manga/.gitkeep create mode 100644 src/Aviat/AnimeClient/Command/BaseCommand.php create mode 100644 src/Aviat/AnimeClient/Command/CacheImages.php diff --git a/composer.json b/composer.json index 1cfffa51..018dcfe7 100644 --- a/composer.json +++ b/composer.json @@ -17,9 +17,10 @@ "psr/log": "~1.0", "robmorgan/phinx": "0.4.*", "yosymfony/toml": "0.3.*", - "zendframework/zend-diactoros": "1.3.*" + "zendframework/zend-diactoros": "1.3.*", + "maximebf/consolekit": "^1.0" }, "require-dev": { "codeclimate/php-test-reporter": "dev-master" } -} \ No newline at end of file +} diff --git a/console b/console new file mode 100755 index 00000000..be735323 --- /dev/null +++ b/console @@ -0,0 +1,58 @@ +#!/usr/bin/env php + '\Aviat\AnimeClient\Command\CacheImages' +]); + +$console->run(); \ No newline at end of file diff --git a/public/images/anime/.gitkeep b/public/images/anime/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/public/images/manga/.gitkeep b/public/images/manga/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/src/Aviat/AnimeClient/Command/BaseCommand.php b/src/Aviat/AnimeClient/Command/BaseCommand.php new file mode 100644 index 00000000..f4300431 --- /dev/null +++ b/src/Aviat/AnimeClient/Command/BaseCommand.php @@ -0,0 +1,65 @@ +set('config', $config); + + // Create Cache Object + $container->set('cache', new CacheManager($container)); + + // Create session Object + $session = (new SessionFactory())->newInstance($_COOKIE); + $container->set('session', $session); + + // Models + $container->set('api-model', new Model\API($container)); + $container->set('anime-model', new Model\Anime($container)); + $container->set('manga-model', new Model\Manga($container)); + + $container->set('auth', new HummingbirdAuth($container)); + + return $container; + }; + + return $di($config_array); + } +} \ No newline at end of file diff --git a/src/Aviat/AnimeClient/Command/CacheImages.php b/src/Aviat/AnimeClient/Command/CacheImages.php new file mode 100644 index 00000000..70b4bbb3 --- /dev/null +++ b/src/Aviat/AnimeClient/Command/CacheImages.php @@ -0,0 +1,109 @@ +getConsole(), $message); + $box->write(); + echo "\n"; + } + + /* + * Convert manga images + * + * @throws \ConsoleKit\ConsoleException + */ + protected function getMangaImages() + { + $raw_list = $this->mangaModel->_get_list_from_api(); + $manga_list = array_column($raw_list, 'manga'); + + $total = count($raw_list); + $current = 0; + foreach($manga_list as $item) + { + $this->model->get_cached_image($item['poster_image'], $item['id'], 'manga'); + $current++; + + echo "Cached {$current} of {$total} manga images. \n"; + } + } + + /** + * Convert anime images + * + * @throws \ConsoleKit\ConsoleException + */ + protected function getAnimeImages() + { + $raw_list = $this->animeModel->get_raw_list(); + + $total = count($raw_list); + $current = 0; + foreach($raw_list as $item) + { + $this->model->get_cached_image($item['anime']['cover_image'], $item['anime']['slug'], 'anime'); + $current++; + + echo "Cached {$current} of {$total} anime images. \n"; + } + } + + /** + * Run the image conversion script + * + * @param array $args + * @param array $options + * @return void + * @throws \ConsoleKit\ConsoleException + */ + public function execute(array $args, array $options = array()) + { + $this->setContainer($this->setupContainer()); + $this->model = new Model($this->container); + $this->animeModel = $this->container->get('anime-model'); + $this->mangaModel = $this->container->get('manga-model'); + + $this->echoBox('Starting image conversion'); + + $this->echoBox('Converting manga images'); + $this->getMangaImages(); + + $this->echoBox('Converting anime images'); + $this->getAnimeImages(); + + $this->echoBox('Finished image conversion'); + } +} +// End of CacheImages.php \ No newline at end of file diff --git a/src/Aviat/AnimeClient/Model.php b/src/Aviat/AnimeClient/Model.php index 6d7150a1..ba4861fa 100644 --- a/src/Aviat/AnimeClient/Model.php +++ b/src/Aviat/AnimeClient/Model.php @@ -54,6 +54,7 @@ class Model { * @param string $series_slug - The part of the url with the series name, becomes the image name * @param string $type - Anime or Manga, controls cache path * @return string - the frontend path for the cached image + * @throws DomainException */ public function get_cached_image($api_path, $series_slug, $type = "anime") { @@ -62,7 +63,7 @@ class Model { $ext_parts = explode('.', $path); $ext = end($ext_parts); - // Workaround for some broken extensions + // Workaround for some broken file extensions if ($ext == "jjpg") { $ext = "jpg"; @@ -124,8 +125,16 @@ class Model { */ private function _resize($path, $width, $height) { - $img = new SimpleImage($path); - $img->resize($width, $height)->save(); + try + { + $img = new SimpleImage($path); + $img->resize($width, $height)->save(); + } + catch (Exception $e) + { + // Catch image errors, since they don't otherwise affect + // functionality + } } } // End of BaseModel.php \ No newline at end of file diff --git a/src/Aviat/AnimeClient/Model/Anime.php b/src/Aviat/AnimeClient/Model/Anime.php index caccac8e..8d13c208 100644 --- a/src/Aviat/AnimeClient/Model/Anime.php +++ b/src/Aviat/AnimeClient/Model/Anime.php @@ -234,6 +234,28 @@ class Anime extends API { return $output; } + /** + * Get the full list from the api + * + * @return array + */ + public function get_raw_list() + { + $config = [ + 'allow_redirects' => FALSE + ]; + + $username = $this->config->get('hummingbird_username'); + /*$auth = $this->container->get('auth'); + if ($auth->is_authenticated()) + { + $config['query']['auth_token'] = $auth->get_auth_token(); + }*/ + + $response = $this->get("users/{$username}/library", $config); + return Json::decode($response->getBody(), TRUE); + } + /** * Handle transforming of api data *