From f7cfcbbcedb93b98b9f36cac337513547ee1b6db Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 10 Oct 2018 16:04:58 -0400 Subject: [PATCH] Add console command to re-generate list thumbnails --- console | 1 + src/Command/BaseCommand.php | 36 ++++++++++ src/Command/UpdateThumbnails.php | 115 +++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 src/Command/UpdateThumbnails.php diff --git a/console b/console index a6be0347..19eb3956 100755 --- a/console +++ b/console @@ -17,6 +17,7 @@ try (new Console([ 'cache:clear' => Command\CacheClear::class, 'cache:refresh' => Command\CachePrime::class, + 'regenerate-thumbnails' => Command\UpdateThumbnails::class, 'lists:sync' => Command\SyncLists::class, 'mal_id:check' => Command\MALIDCheck::class, ]))->run(); diff --git a/src/Command/BaseCommand.php b/src/Command/BaseCommand.php index 842993ab..dcdf2ef9 100644 --- a/src/Command/BaseCommand.php +++ b/src/Command/BaseCommand.php @@ -19,12 +19,15 @@ namespace Aviat\AnimeClient\Command; use function Aviat\AnimeClient\loadToml; use function Aviat\AnimeClient\loadTomlFile; +use Aura\Router\RouterContainer; use Aura\Session\SessionFactory; +use Aviat\AnimeClient\UrlGenerator; use Aviat\AnimeClient\Util; use Aviat\AnimeClient\API\CacheTrait; use Aviat\AnimeClient\API\Anilist; use Aviat\AnimeClient\API\Kitsu; use Aviat\AnimeClient\API\Kitsu\KitsuRequestBuilder; +use Aviat\AnimeClient\Model; use Aviat\Banker\Pool; use Aviat\Ion\Config; use Aviat\Ion\Di\{Container, ContainerAware}; @@ -32,6 +35,7 @@ use ConsoleKit\{Command, ConsoleException}; use ConsoleKit\Widgets\Box; use Monolog\Handler\RotatingFileHandler; use Monolog\Logger; +use Zend\Diactoros\{Response, ServerRequestFactory}; /** * Base class for console command setup @@ -111,6 +115,25 @@ class BaseCommand extends Command { return new Pool($config, $logger); }); + // Create Aura Router Object + $container->set('aura-router', function() { + return new RouterContainer; + }); + + // Create Request/Response Objects + $container->set('request', function() { + return ServerRequestFactory::fromGlobals( + $_SERVER, + $_GET, + $_POST, + $_COOKIE, + $_FILES + ); + }); + $container->set('response', function() { + return new Response; + }); + // Create session Object $container->set('session', function() { return (new SessionFactory())->newInstance($_COOKIE); @@ -147,6 +170,19 @@ class BaseCommand extends Command { return $model; }); + $container->set('settings-model', function($container) { + $model = new Model\Settings($container->get('config')); + $model->setContainer($container); + return $model; + }); + + $container->set('auth', function($container) { + return new Kitsu\Auth($container); + }); + + $container->set('url-generator', function($container) { + return new UrlGenerator($container); + }); $container->set('util', function($container) { return new Util($container); diff --git a/src/Command/UpdateThumbnails.php b/src/Command/UpdateThumbnails.php new file mode 100644 index 00000000..33d1c61b --- /dev/null +++ b/src/Command/UpdateThumbnails.php @@ -0,0 +1,115 @@ + + * @copyright 2015 - 2018 Timothy J. Warren + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @version 4.1 + * @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient + */ + +namespace Aviat\AnimeClient\Command; + +use Aviat\AnimeClient\API\{ + FailedResponseException, + JsonAPI, + ParallelAPIRequest +}; +use Aviat\AnimeClient\API\Anilist\Transformer\{ + AnimeListTransformer as AALT, + MangaListTransformer as AMLT +}; +use Aviat\AnimeClient\API\Mapping\{AnimeWatchingStatus, MangaReadingStatus}; +use Aviat\AnimeClient\Controller\Index; +use Aviat\AnimeClient\Types\FormItem; +use Aviat\Ion\Json; +use DateTime; + +/** + * Clears out image cache directories, then re-creates the image cache + * for manga and anime + */ +final class UpdateThumbnails extends BaseCommand { + /** + * Model for making requests to Kitsu API + * @var \Aviat\AnimeClient\API\Kitsu\Model + */ + protected $kitsuModel; + + /** + * The default controller, which has the method to cache the images + */ + protected $controller; + + public function execute(array $args, array $options = []): void + { + $this->setContainer($this->setupContainer()); + $this->setCache($this->container->get('cache')); + + $this->controller = new Index($this->container); + $this->kitsuModel = $this->container->get('kitsu-model'); + + $this->clearThumbs(); + + $ids = $this->getImageList(); + + // print_r($ids); + // echo json_encode($ids, \JSON_PRETTY_PRINT); + + // Resave the images + foreach($ids as $type => $typeIds) + { + foreach ($typeIds as $id) + { + $this->controller->images($type, "{$id}.jpg", FALSE); + } + } + + $this->echoBox('Finished regenerating thumbnails'); + } + + public function clearThumbs() + { + $imgDir = realpath(__DIR__ . '/../../public/images'); + + $paths = [ + 'anime/*.jpg', + 'anime/*.webp', + 'manga/*.jpg', + 'manga/*.webp', + 'characters/*.jpg', + 'characters/*.webp', + ]; + + foreach($paths as $path) + { + $cmd = "rm -rf {$imgDir}/{$path}"; + exec($cmd); + } + } + + public function getImageList() + { + $mangaList = $this->kitsuModel->getFullRawMangaList(); + $includes = JsonAPI::organizeIncludes($mangaList['included']); + $mangaIds = array_keys($includes['manga']); + + $animeList = $this->kitsuModel->getFullRawAnimeList(); + $includes = JsonAPI::organizeIncludes($animeList['included']); + $animeIds = array_keys($includes['anime']); + + // print_r($mangaIds); + // die(); + + return [ + 'anime' => $animeIds, + 'manga' => $mangaIds, + ]; + } +} \ No newline at end of file