Browse Source

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

tags/v4.0.0-beta-1
Timothy Warren 2 years ago
parent
commit
493ac3ca03
9 changed files with 69 additions and 73 deletions
  1. +2
    -2
      RoboFile.php
  2. +3
    -1
      composer.json
  3. +8
    -20
      console
  4. +1
    -1
      src/API/Enum/AnimeWatchingStatus/Kitsu.php
  5. +4
    -8
      src/API/Kitsu/Model.php
  6. +4
    -0
      src/API/Mapping/AnimeWatchingStatus.php
  7. +4
    -0
      src/API/Mapping/MangaReadingStatus.php
  8. +40
    -41
      src/Command/SyncKitsuWithMal.php
  9. +3
    -0
      src/Controller/Character.php

+ 2
- 2
RoboFile.php View File

@@ -105,7 +105,7 @@ class RoboFile extends Tasks {
*/
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()
{
$cmd_parts = [
'phpdox',
'vendor/bin/phpdox',
];
$this->_run($cmd_parts, ' && ');
}

+ 3
- 1
composer.json View File

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

+ 8
- 20
console View File

@@ -1,19 +1,10 @@
#!/usr/bin/env php
<?php declare(strict_types=1);

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());
}
}
// Set up autoloader for third-party dependencies
require_once realpath(__DIR__ . '/vendor/autoload.php');

use Aviat\AnimeClient\Command;

$_SERVER['HTTP_HOST'] = 'localhost';

@@ -22,9 +13,6 @@ $APP_DIR = __DIR__ . '/app/';
$SRC_DIR = __DIR__ . '/src/';
$CONF_DIR = realpath("${APP_DIR}/config/");

// Set up autoloader for third-party dependencies
require_once realpath(__DIR__ . '/vendor/autoload.php');

// Unset 'constants'
unset($APP_DIR);
unset($SRC_DIR);
@@ -34,10 +22,10 @@ unset($CONF_DIR);
// Start console script
// ---------------------------------------------------------------------------------------------------------------------
$console = new \ConsoleKit\Console([
'cache-prime' => '\Aviat\AnimeClient\Command\CachePrime',
'cache-clear' => '\Aviat\AnimeClient\Command\CacheClear',
'clear-cache' => '\Aviat\AnimeClient\Command\CacheClear',
'sync-lists' => '\Aviat\AnimeClient\Command\SyncKitsuWithMal',
'cache-prime' => Command\CachePrime::class,
'cache-clear' => Command\CacheClear::class,
'clear-cache' => Command\CacheClear::class,
'sync-lists' => Command\SyncKitsuWithMal::class,
]);

$console->run();

+ 1
- 1
src/API/Enum/AnimeWatchingStatus/Kitsu.php View File

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

+ 4
- 8
src/API/Kitsu/Model.php View File

@@ -400,17 +400,13 @@ class Model {
if ( ! $cacheItem->isHit())
{
$output = [];
$statuses = [
Title::WATCHING => KitsuWatchingStatus::WATCHING,
Title::PLAN_TO_WATCH => KitsuWatchingStatus::PLAN_TO_WATCH,
Title::ON_HOLD, KitsuWatchingStatus::ON_HOLD,
Title::DROPPED => KitsuWatchingStatus::DROPPED,
Title::COMPLETED => KitsuWatchingStatus::COMPLETED
];

$statuses = KitsuWatchingStatus::getConstList();

foreach ($statuses as $key => $status)
{
$output[$key] = $this->getAnimeList($status) ?? [];
$mappedStatus = AnimeWatchingStatus::KITSU_TO_TITLE[$status];
$output[$mappedStatus] = $this->getAnimeList($status) ?? [];
}

$cacheItem->set($output);

+ 4
- 0
src/API/Mapping/AnimeWatchingStatus.php View File

@@ -24,6 +24,10 @@ use Aviat\AnimeClient\API\Enum\AnimeWatchingStatus\{
};
use Aviat\Ion\Enum;

/**
* Anime watching status mappings, among Kitsu, MAL, Page titles
* and url route segments
*/
class AnimeWatchingStatus extends Enum {
const KITSU_TO_MAL = [
Kitsu::WATCHING => MAL::WATCHING,

+ 4
- 0
src/API/Mapping/MangaReadingStatus.php View File

@@ -24,6 +24,10 @@ use Aviat\AnimeClient\API\Enum\MangaReadingStatus\{
};
use Aviat\Ion\Enum;

/**
* Manga reading status mappings, among Kitsu, MAL, Page titles
* and url route segments
*/
class MangaReadingStatus extends Enum {
const MAL_TO_KITSU = [
Kitsu::READING => MAL::READING,

+ 40
- 41
src/Command/SyncKitsuWithMal.php View File

@@ -54,38 +54,38 @@ class SyncKitsuWithMal extends BaseCommand {
$this->setCache($this->container->get('cache'));
$this->kitsuModel = $this->container->get('kitsu-model');
$this->malModel = $this->container->get('mal-model');
$malCount = count($this->getMALList());
$malCount = count($this->getMALAnimeList());
$kitsuCount = $this->getKitsuAnimeListPageCount();
$this->echoBox("Number of MAL list items: {$malCount}");
$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));
if ( ! empty($data['addToMAL']))
{
$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();
$size = 100;
$pages = ceil($count / $size);
$requests = [];
// Set up requests
for ($i = 0; $i < $pages; $i++)
{
$offset = $i * $size;
$requests[] = $this->kitsuModel->getPagedAnimeList($size, $offset);
}
$promiseArray = (new Client())->requestMulti($requests);

$responses = wait(all($promiseArray));
@@ -100,15 +100,15 @@ class SyncKitsuWithMal extends BaseCommand {
return $output;
}

public function getMALList()
public function getMALAnimeList()
{
return $this->malModel->getFullList();
}
public function filterMappings(array $includes): array
{
$output = [];
foreach($includes as $id => $mapping)
{
if ($mapping['externalSite'] === 'myanimelist/anime')
@@ -116,15 +116,15 @@ class SyncKitsuWithMal extends BaseCommand {
$output[$id] = $mapping;
}
}
return $output;
}
public function formatMALList()
public function formatMALAnimeList()
{
$orig = $this->getMALList();
$orig = $this->getMALAnimeList();
$output = [];
foreach($orig as $item)
{
$output[$item['series_animedb_id']] = [
@@ -144,24 +144,24 @@ class SyncKitsuWithMal extends BaseCommand {
]
];
}
return $output;
}
public function filterKitsuList()
public function filterKitsuAnimeList()
{
$data = $this->kitsuModel->getFullAnimeList();
$includes = JsonAPI::organizeIncludes($data['included']);
$includes['mappings'] = $this->filterMappings($includes['mappings']);
$output = [];
foreach($data['data'] as $listItem)
{
$animeId = $listItem['relationships']['anime']['data']['id'];
$potentialMappings = $includes['anime'][$animeId]['relationships']['mappings'];
$malId = NULL;
foreach ($potentialMappings as $mappingId)
{
if (array_key_exists($mappingId, $includes['mappings']))
@@ -169,20 +169,20 @@ class SyncKitsuWithMal extends BaseCommand {
$malId = $includes['mappings'][$mappingId]['externalId'];
}
}
// Skip to the next item if there isn't a MAL ID
if (is_null($malId))
{
continue;
}
$output[$listItem['id']] = [
'id' => $listItem['id'],
'malId' => $malId,
'data' => $listItem['attributes'],
];
}
return $output;
}

@@ -191,32 +191,32 @@ class SyncKitsuWithMal extends BaseCommand {
return $this->kitsuModel->getAnimeListCount();
}

public function diffLists()
public function diffAnimeLists()
{
// Get libraryEntries with media.mappings from Kitsu
// Organize mappings, and ignore entries without mappings
$kitsuList = $this->filterKitsuList();
$kitsuList = $this->filterKitsuAnimeList();

// Get MAL list data
$malList = $this->formatMALList();
$malList = $this->formatMALAnimeList();
$itemsToAddToMAL = [];
foreach($kitsuList as $kitsuItem)
{
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
continue;
}
// Looks like this item only exists on Kitsu
$itemsToAddToMAL[] = [
'mal_id' => $kitsuItem['malId'],
'data' => $kitsuItem['data']
];
}

// 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
// to date entry
// Based on the 'newer' entry, update the other api list item
return [
'addToMAL' => $itemsToAddToMAL,
];
}

public function createMALListItems($itemsToAdd)
public function createMALAnimeListItems($itemsToAdd)
{
$transformer = new ALT();
$requests = [];
foreach($itemsToAdd as $item)
{
$data = $transformer->untransform($item);
@@ -261,5 +261,4 @@ class SyncKitsuWithMal extends BaseCommand {
}
}
}

}

+ 3
- 0
src/Controller/Character.php View File

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

Loading…
Cancel
Save