Add batch image thumbnail creation, see #6, #14

This commit is contained in:
Timothy Warren 2016-04-19 13:02:50 -04:00
parent 733d3f3871
commit 89080171c9
8 changed files with 269 additions and 5 deletions

View File

@ -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"
}
}
}

58
console Executable file
View File

@ -0,0 +1,58 @@
#!/usr/bin/env php
<?php
use Aviat\AnimeClient\Model;
if ( ! function_exists('_dir'))
{
/**
* Joins paths together. Variadic to take an
* arbitrary number of arguments
*
* @return string
*/
function _dir()
{
return implode(DIRECTORY_SEPARATOR, func_get_args());
}
}
$_SERVER['HTTP_HOST'] = 'localhost';
// Define base directories
$APP_DIR = __DIR__ . '/app/';
$SRC_DIR = __DIR__ . '/src/';
$CONF_DIR = realpath("${APP_DIR}/config/");
/**
* Set up autoloaders
*
* @codeCoverageIgnore
* @return void
*/
spl_autoload_register(function($class) use ($SRC_DIR) {
$class_parts = explode('\\', $class);
$ns_path = $SRC_DIR . '/' . implode('/', $class_parts) . ".php";
if (file_exists($ns_path))
{
require_once($ns_path);
return;
}
});
// Set up autoloader for third-party dependencies
require_once realpath(__DIR__ . '/vendor/autoload.php');
// Unset 'constants'
unset($APP_DIR);
unset($SRC_DIR);
unset($CONF_DIR);
// ---------------------------------------------------------------------------------------------------------------------
// Start console script
// ---------------------------------------------------------------------------------------------------------------------
$console = new \ConsoleKit\Console([
'cache-images' => '\Aviat\AnimeClient\Command\CacheImages'
]);
$console->run();

View File

View File

View File

@ -0,0 +1,65 @@
<?php
/**
* Hummingbird Anime Client
*
* An API client for Hummingbird to manage anime and manga watch lists
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren
* @copyright Copyright (c) 2015 - 2016
* @link https://github.com/timw4mail/HummingBirdAnimeClient
* @license MIT
*/
namespace Aviat\AnimeClient\Command;
use Aura\Session\SessionFactory;
use Aviat\Ion\Di\Container;
use Aviat\Ion\Cache\CacheManager;
use Aviat\AnimeClient\Config;
use Aviat\AnimeClient\AnimeClient;
use Aviat\AnimeClient\Auth\HummingbirdAuth;
use Aviat\AnimeClient\Model;
/**
* Base class for console command setup
*/
class BaseCommand extends \ConsoleKit\Command {
use \Aviat\Ion\Di\ContainerAware;
protected function setupContainer()
{
$CONF_DIR = __DIR__ . '/../../../../app/config/';
require_once $CONF_DIR . '/base_config.php'; // $base_config
$config = AnimeClient::load_toml($CONF_DIR);
$config_array = array_merge($base_config, $config);
$di = function ($config_array) {
$container = new Container();
// Create Config Object
$config = new Config($config_array);
$container->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);
}
}

View File

@ -0,0 +1,109 @@
<?php
/**
* Hummingbird Anime Client
*
* An API client for Hummingbird to manage anime and manga watch lists
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren
* @copyright Copyright (c) 2015 - 2016
* @link https://github.com/timw4mail/HummingBirdAnimeClient
* @license MIT
*/
namespace Aviat\AnimeClient\Command;
use \ConsoleKit\Widgets\Box;
use Aviat\AnimeClient\Model;
/**
* Generates thumbnail image cache so that cover images load faster
*/
class CacheImages extends BaseCommand {
protected $mangaModel;
protected $animeModel;
protected $model;
/**
* Echo text in a box
*
* @param string $message
* @return void
*/
protected function echoBox($message)
{
echo "\n";
$box = new Box($this->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

View File

@ -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

View File

@ -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
*