Pull stuff from the Kitsu API

This commit is contained in:
Timothy Warren 2016-12-21 12:46:20 -05:00
parent a41acb28f6
commit 0c910bff1a
15 changed files with 417 additions and 125 deletions

View File

@ -0,0 +1,20 @@
################################################################################
# Cache Setup #
################################################################################
driver = "redis"
[connection]
# Host or socket to connect to
host = "127.0.0.1"
# Connection port
#port = 6379
# Connection password
#password = ""
# Database number
database = 2
[options]

View File

@ -1,20 +1,21 @@
<main>
<?php if ($auth->is_authenticated()): ?>
<?php /*if ($auth->is_authenticated()): ?>
<a class="bracketed" href="<?= $url->generate('anime.add.get') ?>">Add Item</a>
<?php endif ?>
<?php endif */ ?>
<?php if (empty($sections)): ?>
<h3>There's nothing here!</h3>
<?php else: ?>
<?php foreach ($sections as $name => $items): ?>
<?php /*<pre><?= print_r($items, TRUE) ?></pre> */ ?>
<section class="status">
<h2><?= $escape->html($name) ?></h2>
<section class="media-wrap">
<?php foreach($items as $item): ?>
<?php if ($item['private'] && ! $auth->is_authenticated()) continue; ?>
<?php if ($item['private']/* && ! $auth->is_authenticated()*/) continue; ?>
<article class="media" id="<?= $item['anime']['slug'] ?>">
<?php if ($auth->is_authenticated()): ?>
<?php /* if ($auth->is_authenticated()): ?>
<button title="Increment episode count" class="plus_one" hidden>+1 Episode</button>
<?php endif ?>
<?php endif */ ?>
<?= $helper->img($item['anime']['image']); ?>
<div class="name">
<a href="<?= $url->generate('anime.details', ['id' => $item['anime']['slug']]); ?>">
@ -23,13 +24,13 @@
</a>
</div>
<div class="table">
<?php if ($auth->is_authenticated()): ?>
<?php /* if ($auth->is_authenticated()): ?>
<div class="row">
<span class="edit">
<a class="bracketed" title="Edit information about this anime" href="<?= $urlGenerator->url("anime/edit/{$item['id']}/{$item['watching_status']}") ?>">Edit</a>
</span>
</div>
<?php endif ?>
<?php endif */ ?>
<?php if ($item['private'] || $item['rewatching']): ?>
<div class="row">
<?php foreach(['private', 'rewatching'] as $attr): ?>
@ -64,6 +65,6 @@
<?php endforeach ?>
<?php endif ?>
</main>
<?php if ($auth->is_authenticated()): ?>
<?php /* if ($auth->is_authenticated()): ?>
<script src="<?= $urlGenerator->asset_url('js.php/g/edit') ?>"></script>
<?php endif ?>
<?php endif */ ?>

View File

@ -17,7 +17,7 @@
[<a href="<?= $urlGenerator->default_url('manga') ?>">Manga List</a>]
<?php endif ?>
</span>
<?php if ($auth->is_authenticated()): ?>
<?php /* if ($auth->is_authenticated()): ?>
<span class="flex-no-wrap">&nbsp;</span>
<span class="flex-no-wrap small-font">
<button type="button" class="js-clear-cache user-btn">Clear API Cache</button>
@ -29,7 +29,7 @@
<a class="bracketed" href="<?= $url->generate('logout') ?>">Logout</a>
<?php else: ?>
[<a href="<?= $url->generate('login'); ?>"><?= $config->get('whose_list') ?>'s Login</a>]
<?php endif ?>
<?php endif */ ?>
</span>
</h1>
<nav>

View File

@ -18,6 +18,7 @@
"aura/html": "2.*",
"aura/router": "3.*",
"aura/session": "2.*",
"aviat/banker": "dev-master",
"aviat/ion": "dev-master",
"filp/whoops": "2.0.*",
"guzzlehttp/guzzle": "6.*",

View File

@ -58,7 +58,7 @@ $whoops->pushHandler($defaultHandler);
//$whoops->pushHandler($jsonHandler);
// Register as the error handler
$whoops->register();
// $whoops->register();
// -----------------------------------------------------------------------------
// Dependency Injection setup

86
src/API/GuzzleTrait.php Normal file
View File

@ -0,0 +1,86 @@
<?php declare(strict_types=1);
/**
* Anime List Client
*
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
*
* PHP version 7
*
* @package AnimeListClient
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2016 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 4.0
* @link https://github.com/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API;
use Psr\Http\Message\ResponseInterface;
/**
* Base trait for api interaction
*
* @method ResponseInterface get(string $uri, array $options);
* @method ResponseInterface delete(string $uri, array $options);
* @method ResponseInterface head(string $uri, array $options);
* @method ResponseInterface options(string $uri, array $options);
* @method ResponseInterface patch(string $uri, array $options);
* @method ResponseInterface post(string $uri, array $options);
* @method ResponseInterface put(string $uri, array $options);
*/
trait GuzzleTrait {
/**
* The Guzzle http client object
* @var object
*/
protected $client;
/**
* Cookie jar object for api requests
* @var object
*/
protected $cookieJar;
/**
* Set up the class properties
*
* @return void
*/
abstract protected function init();
/**
* Magic methods to call guzzle api client
*
* @param string $method
* @param array $args
* @return ResponseInterface|null
*/
public function __call($method, $args)
{
$valid_methods = [
'get',
'getAsync',
'delete',
'deleteAsync',
'head',
'headAsync',
'options',
'optionsAsync',
'patch',
'patchAsync',
'post',
'postAsync',
'put',
'putAsync'
];
if ( ! in_array($method, $valid_methods))
{
return NULL;
}
array_unshift($args, strtoupper($method));
return call_user_func_array([$this->client, 'request'], $args);
}
}

View File

@ -0,0 +1,43 @@
<?php declare(strict_types=1);
/**
* Anime List Client
*
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
*
* PHP version 7
*
* @package AnimeListClient
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2016 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 4.0
* @link https://github.com/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API\Kitsu;
use Aviat\AnimeClient\API\AbstractListItem;
class KitsuAnimeListItem extends AbstractListItem {
use KitsuTrait;
public function create(array $data): bool
{
// TODO: Implement create() method.
}
public function delete(string $id): bool
{
// TODO: Implement delete() method.
}
public function get(string $id): array
{
// TODO: Implement get() method.
}
public function update(string $id, array $data): bool
{
// TODO: Implement update() method.
}
}

View File

@ -0,0 +1,115 @@
<?php declare(strict_types=1);
/**
* Anime List Client
*
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
*
* PHP version 7
*
* @package AnimeListClient
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2016 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 4.0
* @link https://github.com/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API\Kitsu;
use Aviat\AnimeClient\API\Kitsu\Transformer\AnimeListTransformer;
use Aviat\Ion\Json;
/**
* Kitsu API Model
*/
class KitsuModel {
use KitsuTrait;
const CLIENT_ID = 'dd031b32d2f56c990b1425efe6c42ad847e7fe3ab46bf1299f05ecd856bdb7dd';
const CLIENT_SECRET = '54d7307928f63414defd96399fc31ba847961ceaecef3a5fd93144e960c0e151';
/**
* Class to map anime list items
* to a common format used by
* templates
*
* @var AnimeListTransformer
*/
protected $animeListTransformer;
public function __construct()
{
// Set up Guzzle trait
$this->init();
$this->animeListTransformer = new AnimeListTransformer();
}
/**
* Get the access token from the Kitsu API
*
* @param string $username
* @param string $password
* @return bool|string
*/
public function authenticate(string $username, string $password)
{
$response = $this->post('https://kitsu.io/api/oauth/token', [
'body' => http_build_query([
'grant_type' => 'password',
'username' => $username,
'password' => $password,
'client_id' => self::CLIENT_ID,
'client_secret' => self::CLIENT_SECRET
])
]);
$info = JSON::decode($response->getBody());
if (array_key_exists('access_token', $info)) {
// @TODO save token
return true;
}
return false;
}
public function getAnimeMedia($entryId): array {
$response = $this->get("library-entries/{$entryId}/media", [
'headers' => [
'client_id' => self::CLIENT_ID,
'client_secret' => self::CLIENT_SECRET
]
]);
return JSON::decode($response->getBody(), TRUE);
}
public function getAnimeList(): array {
$response = $this->get('library-entries', [
'headers' => [
'client_id' => self::CLIENT_ID,
'client_secret' => self::CLIENT_SECRET
],
'query' => [
'filter' => [
'user_id' => 2644,
'media_type' => 'Anime'
],
'include' => 'media'
]
]);
$data = JSON::decode($response->getBody(), TRUE);
foreach($data['data'] as &$item)
{
$item['anime'] = $this->getAnimeMedia($item['id'])['data']['attributes'];
}
$transformed = $this->animeListTransformer->transformCollection($data['data']);
return $transformed;
}
}

View File

@ -0,0 +1,56 @@
<?php declare(strict_types=1);
/**
* Anime List Client
*
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
*
* PHP version 7
*
* @package AnimeListClient
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2016 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 4.0
* @link https://github.com/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API\Kitsu;
use Aviat\AnimeClient\API\GuzzleTrait;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
trait KitsuTrait {
use GuzzleTrait;
/**
* The base url for api requests
* @var string $base_url
*/
protected $baseUrl = "https://kitsu.io/api/edge/";
/**
* Set up the class properties
*
* @return void
*/
protected function init()
{
$this->cookieJar = new CookieJar();
$this->client = new Client([
'base_uri' => $this->baseUrl,
'cookies' => TRUE,
'http_errors' => TRUE,
'defaults' => [
'cookies' => $this->cookieJar,
'headers' => [
'User-Agent' => "Tim's Anime Client/4.0",
'Accept-Encoding' => 'application/vnd.api+json',
'Content-Type' => 'application/vnd.api+json'
],
'timeout' => 25,
'connect_timeout' => 25
]
]);
}
}

View File

@ -1,68 +0,0 @@
<?php declare(strict_types=1);
/**
* Anime List Client
*
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
*
* PHP version 7
*
* @package AnimeListClient
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2016 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 4.0
* @link https://github.com/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API\Kitsu;
use Aviat\AnimeClient\Model\API;
/**
* Kitsu API Model
*/
class Model extends API {
const CLIENT_ID = 'dd031b32d2f56c990b1425efe6c42ad847e7fe3ab46bf1299f05ecd856bdb7dd';
const CLIENT_SECRET = '54d7307928f63414defd96399fc31ba847961ceaecef3a5fd93144e960c0e151';
/**
* Base url for Kitsu API
*/
protected $baseUrl = 'https://kitsu.io/api/edge/';
/**
* Default settings for Guzzle
* @var array
*/
protected $connectionDefaults = [];
/**
* Get the access token from the Kitsu API
*
* @param string $username
* @param string $password
* @return bool|string
*/
public function authenticate(string $username, string $password)
{
$response = $this->post('https://kitsu.io/api/oauth/token', [
'body' => http_build_query([
'grant_type' => 'password',
'username' => $username,
'password' => $password,
'client_id' => self::CLIENT_ID,
'client_secret' => self::CLIENT_SECRET
])
]);
$info = $response->getBody();
if (array_key_exists('access_token', $info)) {
// @TODO save token
return true;
}
return false;
}
}

View File

@ -32,65 +32,61 @@ class AnimeListTransformer extends AbstractTransformer {
*/
public function transform($item)
{
print_r($item);
die();
/* ?><pre><?= print_r($item, TRUE) ?></pre><?php
die(); */
// $anime = [];
$genres = [];
$anime =& $item['anime'];
$genres = $this->linearizeGenres($item['anime']['genres']);
// $genres = $this->linearizeGenres($item['anime']['genres']); */
$rating = NULL;
if ($item['rating']['type'] === 'advanced')
{
$rating = is_numeric($item['rating']['value'])
? (int) 2 * $item['rating']['value']
: '-';
}
$rating = (int) 2 * $item['attributes']['rating'];
$total_episodes = is_numeric($anime['episode_count'])
? $anime['episode_count']
$total_episodes = is_numeric($anime['episodeCount'])
? $anime['episodeCount']
: '-';
$alternate_title = NULL;
if (array_key_exists('alternate_title', $anime))
if (array_key_exists('en_jp', $anime['titles']))
{
// If the alternate title is very similar, or
// a subset of the main title, don't list the
// alternate title
$not_subset = stripos($anime['title'], $anime['alternate_title']) === FALSE;
$diff = levenshtein($anime['title'], $anime['alternate_title'] ?? '');
$not_subset = stripos($anime['canonicalTitle'], $anime['titles']['en_jp']) === FALSE;
$diff = levenshtein($anime['canonicalTitle'], $anime['titles']['en_jp'] ?? '');
if ($not_subset && $diff >= 5)
{
$alternate_title = $anime['alternate_title'];
$alternate_title = $anime['titles']['en_jp'];
}
}
return [
'id' => $item['id'],
'episodes' => [
'watched' => $item['episodes_watched'],
'watched' => $item['attributes']['progress'],
'total' => $total_episodes,
'length' => $anime['episode_length'],
'length' => $anime['episodeLength'],
],
'airing' => [
'status' => $anime['status'],
'started' => $anime['started_airing'],
'ended' => $anime['finished_airing']
'status' => $anime['status'] ?? '',
'started' => $anime['startDate'],
'ended' => $anime['endDate']
],
'anime' => [
'age_rating' => $anime['age_rating'],
'title' => $anime['title'],
'age_rating' => $anime['ageRating'],
'title' => $anime['canonicalTitle'],
'alternate_title' => $alternate_title,
'slug' => $anime['slug'],
'url' => $anime['url'],
'type' => $anime['show_type'],
'image' => $anime['cover_image'],
'genres' => $genres,
'url' => $anime['url'] ?? '',
'type' => $anime['showType'],
'image' => $anime['posterImage']['small'],
'genres' => [],//$genres,
],
'watching_status' => $item['status'],
'notes' => $item['notes'],
'rewatching' => (bool) $item['rewatching'],
'rewatched' => $item['rewatched_times'],
'watching_status' => $item['attributes']['status'],
'notes' => $item['attributes']['notes'],
'rewatching' => (bool) $item['attributes']['reconsuming'],
'rewatched' => (int) $item['attributes']['reconsumeCount'],
'user_rating' => $rating,
'private' => (bool) $item['private'],
'private' => (bool) $item['attributes']['private'] ?? false,
];
}

29
src/API/ListInterface.php Normal file
View File

@ -0,0 +1,29 @@
<?php declare(strict_types=1);
/**
* Anime List Client
*
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
*
* PHP version 7
*
* @package AnimeListClient
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2016 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 4.0
* @link https://github.com/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API;
/**
* Common interface for anime and manga list item CRUD
*/
interface ListInterface {
/**
* Get the raw list of items
*
* @return array
*/
public function get(): array;
}

View File

@ -40,8 +40,8 @@ interface ListItemInterface {
/**
* Update a list item
*
* @param string $id - The id of the list tiem to update
* @param array $data - The data with which to update the list itme
* @param string $id - The id of the list item to update
* @param array $data - The data with which to update the list item
* @return bool
*/
public function update(string $id, array $data): bool;

View File

@ -17,16 +17,17 @@
namespace Aviat\AnimeClient\Controller;
use Aviat\AnimeClient\Controller as BaseController;
use Aviat\AnimeClient\Kitsu\Enum\AnimeWatchingStatus;
use Aviat\AnimeClient\Kitsu\Transformer\AnimeListTransformer;
use Aviat\AnimeClient\API\Kitsu\Enum\AnimeWatchingStatus;
use Aviat\AnimeClient\API\Kitsu\Transformer\AnimeListTransformer;
use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\StringWrapper;
/**
* Controller for Anime-related pages
*/
class Anime extends BaseController {
use \Aviat\Ion\StringWrapper;
use StringWrapper;
/**
* The anime list model

View File

@ -16,10 +16,13 @@
namespace Aviat\AnimeClient\Model;
use Aviat\AnimeClient\API\Kitsu\KitsuModel;
use Aviat\AnimeClient\API\Kitsu\Enum\AnimeWatchingStatus;
use Aviat\AnimeClient\API\Kitsu\Transformer\AnimeListTransformer;
use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\Json;
/**
* Model for handling requests dealing with the anime list
*/
@ -50,6 +53,12 @@ class Anime extends API {
AnimeWatchingStatus::COMPLETED => self::COMPLETED,
];
public function __construct(ContainerInterface $container) {
parent::__construct($container);
$this->kitsuModel = new KitsuModel();
}
/**
* Update the selected anime
*
@ -143,8 +152,11 @@ class Anime extends API {
*/
public function get_list($status)
{
$data = $this->_get_list_from_api($status);
$this->sort_by_name($data, 'anime');
$data = $this->kitsuModel->getAnimeList();
//return JSON::decode((string)$stream, TRUE);
/*$data = $this->_get_list_from_api($status);
$this->sort_by_name($data, 'anime');*/
$output = [];
$output[$this->const_map[$status]] = $data;