Code cleanup and fix 'On Hold' title on all section of anime list

This commit is contained in:
Timothy Warren 2017-03-27 10:09:45 -04:00
parent be0baac962
commit b8b5beeae1
9 changed files with 69 additions and 73 deletions

View File

@ -105,7 +105,7 @@ class RoboFile extends Tasks {
*/ */
public function coverage() public function coverage()
{ {
$this->_run(['vendor/bin/phpunit -c build']); $this->_run(['phpdbg -qrr -- vendor/bin/phpunit -c build']);
} }
/** /**
@ -114,7 +114,7 @@ class RoboFile extends Tasks {
public function docs() public function docs()
{ {
$cmd_parts = [ $cmd_parts = [
'phpdox', 'vendor/bin/phpdox',
]; ];
$this->_run($cmd_parts, ' && '); $this->_run($cmd_parts, ' && ');
} }

View File

@ -33,7 +33,7 @@
"require-dev": { "require-dev": {
"pdepend/pdepend": "^2.2", "pdepend/pdepend": "^2.2",
"sebastian/phpcpd": "^3.0", "sebastian/phpcpd": "^3.0",
"theseer/phpdox": "0.9.0", "theseer/phpdox": "dev-master",
"phploc/phploc": "^3.0", "phploc/phploc": "^3.0",
"phpmd/phpmd": "^2.4", "phpmd/phpmd": "^2.4",
"phpunit/phpunit": "^6.0", "phpunit/phpunit": "^6.0",
@ -45,7 +45,9 @@
"phpstan/phpstan": "^0.6.4" "phpstan/phpstan": "^0.6.4"
}, },
"scripts": { "scripts": {
"build": "vendor/bin/robo build",
"build:css": "cd public && npm run build && cd ..", "build:css": "cd public && npm run build && cd ..",
"clean": "vendor/bin/robo clean",
"coverage": "phpdbg -qrr -- vendor/bin/phpunit -c build", "coverage": "phpdbg -qrr -- vendor/bin/phpunit -c build",
"docs": "vendor/bin/phpdox", "docs": "vendor/bin/phpdox",
"phpstan": "phpstan analyse src tests", "phpstan": "phpstan analyse src tests",

28
console
View File

@ -1,19 +1,10 @@
#!/usr/bin/env php #!/usr/bin/env php
<?php declare(strict_types=1); <?php declare(strict_types=1);
if ( ! function_exists('_dir')) // Set up autoloader for third-party dependencies
{ require_once realpath(__DIR__ . '/vendor/autoload.php');
/**
* Joins paths together. Variadic to take an use Aviat\AnimeClient\Command;
* arbitrary number of arguments
*
* @return string
*/
function _dir()
{
return implode(DIRECTORY_SEPARATOR, func_get_args());
}
}
$_SERVER['HTTP_HOST'] = 'localhost'; $_SERVER['HTTP_HOST'] = 'localhost';
@ -22,9 +13,6 @@ $APP_DIR = __DIR__ . '/app/';
$SRC_DIR = __DIR__ . '/src/'; $SRC_DIR = __DIR__ . '/src/';
$CONF_DIR = realpath("${APP_DIR}/config/"); $CONF_DIR = realpath("${APP_DIR}/config/");
// Set up autoloader for third-party dependencies
require_once realpath(__DIR__ . '/vendor/autoload.php');
// Unset 'constants' // Unset 'constants'
unset($APP_DIR); unset($APP_DIR);
unset($SRC_DIR); unset($SRC_DIR);
@ -34,10 +22,10 @@ unset($CONF_DIR);
// Start console script // Start console script
// --------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------------------
$console = new \ConsoleKit\Console([ $console = new \ConsoleKit\Console([
'cache-prime' => '\Aviat\AnimeClient\Command\CachePrime', 'cache-prime' => Command\CachePrime::class,
'cache-clear' => '\Aviat\AnimeClient\Command\CacheClear', 'cache-clear' => Command\CacheClear::class,
'clear-cache' => '\Aviat\AnimeClient\Command\CacheClear', 'clear-cache' => Command\CacheClear::class,
'sync-lists' => '\Aviat\AnimeClient\Command\SyncKitsuWithMal', 'sync-lists' => Command\SyncKitsuWithMal::class,
]); ]);
$console->run(); $console->run();

View File

@ -24,7 +24,7 @@ use Aviat\Ion\Enum;
class Kitsu extends Enum { class Kitsu extends Enum {
const WATCHING = 'current'; const WATCHING = 'current';
const PLAN_TO_WATCH = 'planned'; const PLAN_TO_WATCH = 'planned';
const COMPLETED = 'completed';
const ON_HOLD = 'on_hold'; const ON_HOLD = 'on_hold';
const DROPPED = 'dropped'; const DROPPED = 'dropped';
const COMPLETED = 'completed';
} }

View File

@ -400,17 +400,13 @@ class Model {
if ( ! $cacheItem->isHit()) if ( ! $cacheItem->isHit())
{ {
$output = []; $output = [];
$statuses = [
Title::WATCHING => KitsuWatchingStatus::WATCHING, $statuses = KitsuWatchingStatus::getConstList();
Title::PLAN_TO_WATCH => KitsuWatchingStatus::PLAN_TO_WATCH,
Title::ON_HOLD, KitsuWatchingStatus::ON_HOLD,
Title::DROPPED => KitsuWatchingStatus::DROPPED,
Title::COMPLETED => KitsuWatchingStatus::COMPLETED
];
foreach ($statuses as $key => $status) foreach ($statuses as $key => $status)
{ {
$output[$key] = $this->getAnimeList($status) ?? []; $mappedStatus = AnimeWatchingStatus::KITSU_TO_TITLE[$status];
$output[$mappedStatus] = $this->getAnimeList($status) ?? [];
} }
$cacheItem->set($output); $cacheItem->set($output);

View File

@ -24,6 +24,10 @@ use Aviat\AnimeClient\API\Enum\AnimeWatchingStatus\{
}; };
use Aviat\Ion\Enum; use Aviat\Ion\Enum;
/**
* Anime watching status mappings, among Kitsu, MAL, Page titles
* and url route segments
*/
class AnimeWatchingStatus extends Enum { class AnimeWatchingStatus extends Enum {
const KITSU_TO_MAL = [ const KITSU_TO_MAL = [
Kitsu::WATCHING => MAL::WATCHING, Kitsu::WATCHING => MAL::WATCHING,

View File

@ -24,6 +24,10 @@ use Aviat\AnimeClient\API\Enum\MangaReadingStatus\{
}; };
use Aviat\Ion\Enum; use Aviat\Ion\Enum;
/**
* Manga reading status mappings, among Kitsu, MAL, Page titles
* and url route segments
*/
class MangaReadingStatus extends Enum { class MangaReadingStatus extends Enum {
const MAL_TO_KITSU = [ const MAL_TO_KITSU = [
Kitsu::READING => MAL::READING, Kitsu::READING => MAL::READING,

View File

@ -54,38 +54,38 @@ class SyncKitsuWithMal extends BaseCommand {
$this->setCache($this->container->get('cache')); $this->setCache($this->container->get('cache'));
$this->kitsuModel = $this->container->get('kitsu-model'); $this->kitsuModel = $this->container->get('kitsu-model');
$this->malModel = $this->container->get('mal-model'); $this->malModel = $this->container->get('mal-model');
$malCount = count($this->getMALList()); $malCount = count($this->getMALAnimeList());
$kitsuCount = $this->getKitsuAnimeListPageCount(); $kitsuCount = $this->getKitsuAnimeListPageCount();
$this->echoBox("Number of MAL list items: {$malCount}"); $this->echoBox("Number of MAL list items: {$malCount}");
$this->echoBox("Number of Kitsu list items: {$kitsuCount}"); $this->echoBox("Number of Kitsu list items: {$kitsuCount}");
$data = $this->diffLists(); $data = $this->diffAnimeLists();
$this->echoBox("Number of items that need to be added to MAL: " . count($data)); $this->echoBox("Number of items that need to be added to MAL: " . count($data));
if ( ! empty($data['addToMAL'])) if ( ! empty($data['addToMAL']))
{ {
$this->echoBox("Adding missing list items to MAL"); $this->echoBox("Adding missing list items to MAL");
$this->createMALListItems($data['addToMAL']); $this->createMALAnimeListItems($data['addToMAL']);
} }
} }
public function getKitsuList() public function getKitsuAnimeList()
{ {
$count = $this->getKitsuAnimeListPageCount(); $count = $this->getKitsuAnimeListPageCount();
$size = 100; $size = 100;
$pages = ceil($count / $size); $pages = ceil($count / $size);
$requests = []; $requests = [];
// Set up requests // Set up requests
for ($i = 0; $i < $pages; $i++) for ($i = 0; $i < $pages; $i++)
{ {
$offset = $i * $size; $offset = $i * $size;
$requests[] = $this->kitsuModel->getPagedAnimeList($size, $offset); $requests[] = $this->kitsuModel->getPagedAnimeList($size, $offset);
} }
$promiseArray = (new Client())->requestMulti($requests); $promiseArray = (new Client())->requestMulti($requests);
$responses = wait(all($promiseArray)); $responses = wait(all($promiseArray));
@ -100,15 +100,15 @@ class SyncKitsuWithMal extends BaseCommand {
return $output; return $output;
} }
public function getMALList() public function getMALAnimeList()
{ {
return $this->malModel->getFullList(); return $this->malModel->getFullList();
} }
public function filterMappings(array $includes): array public function filterMappings(array $includes): array
{ {
$output = []; $output = [];
foreach($includes as $id => $mapping) foreach($includes as $id => $mapping)
{ {
if ($mapping['externalSite'] === 'myanimelist/anime') if ($mapping['externalSite'] === 'myanimelist/anime')
@ -116,15 +116,15 @@ class SyncKitsuWithMal extends BaseCommand {
$output[$id] = $mapping; $output[$id] = $mapping;
} }
} }
return $output; return $output;
} }
public function formatMALList() public function formatMALAnimeList()
{ {
$orig = $this->getMALList(); $orig = $this->getMALAnimeList();
$output = []; $output = [];
foreach($orig as $item) foreach($orig as $item)
{ {
$output[$item['series_animedb_id']] = [ $output[$item['series_animedb_id']] = [
@ -144,24 +144,24 @@ class SyncKitsuWithMal extends BaseCommand {
] ]
]; ];
} }
return $output; return $output;
} }
public function filterKitsuList() public function filterKitsuAnimeList()
{ {
$data = $this->kitsuModel->getFullAnimeList(); $data = $this->kitsuModel->getFullAnimeList();
$includes = JsonAPI::organizeIncludes($data['included']); $includes = JsonAPI::organizeIncludes($data['included']);
$includes['mappings'] = $this->filterMappings($includes['mappings']); $includes['mappings'] = $this->filterMappings($includes['mappings']);
$output = []; $output = [];
foreach($data['data'] as $listItem) foreach($data['data'] as $listItem)
{ {
$animeId = $listItem['relationships']['anime']['data']['id']; $animeId = $listItem['relationships']['anime']['data']['id'];
$potentialMappings = $includes['anime'][$animeId]['relationships']['mappings']; $potentialMappings = $includes['anime'][$animeId]['relationships']['mappings'];
$malId = NULL; $malId = NULL;
foreach ($potentialMappings as $mappingId) foreach ($potentialMappings as $mappingId)
{ {
if (array_key_exists($mappingId, $includes['mappings'])) if (array_key_exists($mappingId, $includes['mappings']))
@ -169,20 +169,20 @@ class SyncKitsuWithMal extends BaseCommand {
$malId = $includes['mappings'][$mappingId]['externalId']; $malId = $includes['mappings'][$mappingId]['externalId'];
} }
} }
// Skip to the next item if there isn't a MAL ID // Skip to the next item if there isn't a MAL ID
if (is_null($malId)) if (is_null($malId))
{ {
continue; continue;
} }
$output[$listItem['id']] = [ $output[$listItem['id']] = [
'id' => $listItem['id'], 'id' => $listItem['id'],
'malId' => $malId, 'malId' => $malId,
'data' => $listItem['attributes'], 'data' => $listItem['attributes'],
]; ];
} }
return $output; return $output;
} }
@ -191,32 +191,32 @@ class SyncKitsuWithMal extends BaseCommand {
return $this->kitsuModel->getAnimeListCount(); return $this->kitsuModel->getAnimeListCount();
} }
public function diffLists() public function diffAnimeLists()
{ {
// Get libraryEntries with media.mappings from Kitsu // Get libraryEntries with media.mappings from Kitsu
// Organize mappings, and ignore entries without mappings // Organize mappings, and ignore entries without mappings
$kitsuList = $this->filterKitsuList(); $kitsuList = $this->filterKitsuAnimeList();
// Get MAL list data // Get MAL list data
$malList = $this->formatMALList(); $malList = $this->formatMALAnimeList();
$itemsToAddToMAL = []; $itemsToAddToMAL = [];
foreach($kitsuList as $kitsuItem) foreach($kitsuList as $kitsuItem)
{ {
if (array_key_exists($kitsuItem['malId'], $malList)) if (array_key_exists($kitsuItem['malId'], $malList))
{ {
// Eventually, compare the list entries, and determine which // Eventually, compare the list entries, and determine which
// needs to be updated // needs to be updated
continue; continue;
} }
// Looks like this item only exists on Kitsu // Looks like this item only exists on Kitsu
$itemsToAddToMAL[] = [ $itemsToAddToMAL[] = [
'mal_id' => $kitsuItem['malId'], 'mal_id' => $kitsuItem['malId'],
'data' => $kitsuItem['data'] 'data' => $kitsuItem['data']
]; ];
} }
// Compare each list entry // Compare each list entry
@ -227,17 +227,17 @@ class SyncKitsuWithMal extends BaseCommand {
// Otherwise, use rewatch count, then episode progress as critera for selecting the more up // Otherwise, use rewatch count, then episode progress as critera for selecting the more up
// to date entry // to date entry
// Based on the 'newer' entry, update the other api list item // Based on the 'newer' entry, update the other api list item
return [ return [
'addToMAL' => $itemsToAddToMAL, 'addToMAL' => $itemsToAddToMAL,
]; ];
} }
public function createMALListItems($itemsToAdd) public function createMALAnimeListItems($itemsToAdd)
{ {
$transformer = new ALT(); $transformer = new ALT();
$requests = []; $requests = [];
foreach($itemsToAdd as $item) foreach($itemsToAdd as $item)
{ {
$data = $transformer->untransform($item); $data = $transformer->untransform($item);
@ -261,5 +261,4 @@ class SyncKitsuWithMal extends BaseCommand {
} }
} }
} }
} }

View File

@ -18,6 +18,9 @@ namespace Aviat\AnimeClient\Controller;
use Aviat\AnimeClient\Controller as BaseController; use Aviat\AnimeClient\Controller as BaseController;
/**
* Controller for character description pages
*/
class Character extends BaseController { class Character extends BaseController {
public function index(string $slug) public function index(string $slug)