Fix anime collection

This commit is contained in:
Timothy Warren 2017-01-16 13:49:51 -05:00
parent 747dc66d67
commit 3dac007390
7 changed files with 58 additions and 26 deletions

View File

@ -11,7 +11,7 @@
<section class="media-wrap">
<?php foreach($items as $item): ?>
<article class="media" id="a-<?= $item['hummingbird_id'] ?>">
<img src="<?= $urlGenerator->asset_url('images', 'anime', basename($item['cover_image'])) ?>" alt="<?= $item['title'] ?> cover image" />
<img src="<?= $item['cover_image'] ?>" alt="<?= $item['title'] ?> cover image" />
<div class="name">
<a href="<?= $url->generate('anime.details', ['id' => $item['slug']]) ?>">
<?= $item['title'] ?>

View File

@ -13,7 +13,7 @@
</th>
<th>
<article class="media">
<?= $helper->img($urlGenerator->asset_url('images', 'anime', basename($item['cover_image']))); ?>
<?= $helper->img($item['cover_image']); ?>
</article>
</th>
</tr>

View File

@ -129,13 +129,19 @@ class KitsuModel {
/**
* Get information about a particular anime
*
* @param string $animeId
* @param string $slug
* @return array
*/
public function getAnime(string $animeId): array
public function getAnime(string $slug): array
{
// @TODO catch non-existent anime
$baseData = $this->getRawMediaData('anime', $animeId);
$baseData = $this->getRawMediaData('anime', $slug);
return $this->animeTransformer->transform($baseData);
}
public function getAnimeById(string $animeId): array
{
$baseData = $this->getRawMediaDataById('anime', $animeId);
return $this->animeTransformer->transform($baseData);
}
@ -311,6 +317,22 @@ class KitsuModel {
->get(['kitsu_username']);
}
private function getRawMediaDataById(string $type, string $id): array
{
$options = [
'query' => [
'include' => ($type === 'anime')
? 'genres,mappings,streamingLinks'
: 'genres,mappings',
]
];
$data = $this->getRequest("{$type}/{$id}", $options);
$baseData = $data['data']['attributes'];
$baseData['included'] = $data['included'];
return $baseData;
}
private function getRawMediaData(string $type, string $slug): array
{
$options = [

View File

@ -40,6 +40,7 @@ class AnimeTransformer extends AbstractTransformer {
$titles = Kitsu::filterTitles($item);
return [
'slug' => $item['slug'],
'title' => $titles[0],
'titles' => $titles,
'status' => Kitsu::getAiringStatus($item['startDate'], $item['endDate']),

View File

@ -71,14 +71,19 @@ class Anime extends API {
}
/**
* Get information about an anime from its id
* Get information about an anime from its slug
*
* @param string $anime_id
* @param string $slug
* @return array
*/
public function getAnime($anime_id)
public function getAnime($slug)
{
return $this->kitsuModel->getAnime($anime_id);
return $this->kitsuModel->getAnime($slug);
}
public function getAnimeById($anime_id)
{
return $this->kitsuModel->getAnimeById($anime_id);
}
/**

View File

@ -16,6 +16,7 @@
namespace Aviat\AnimeClient\Model;
use Aviat\AnimeClient\API\Kitsu;
use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\Json;
use PDO;
@ -35,7 +36,7 @@ class AnimeCollection extends Collection {
parent::__construct($container);
// Do an import if an import file exists
$this->json_import();
// $this->json_import();
}
/**
@ -131,20 +132,17 @@ class AnimeCollection extends Collection {
*/
public function add($data)
{
$anime = (object)$this->anime_model->get_anime($data['id']);
$anime = (object)$this->anime_model->getAnimeById($data['id']);
$util = $this->container->get('util');
$this->db->set([
'hummingbird_id' => $data['id'],
'slug' => $anime->slug,
'title' => $anime->title,
'alternate_title' => $anime->alternate_title,
'alternate_title' => implode('<br />', $anime->titles),
'show_type' => $anime->show_type,
'age_rating' => $anime->age_rating,
'cover_image' => basename(
$util->get_cached_image($anime->cover_image, $anime->slug, 'anime')
),
'cover_image' => $anime->cover_image,
'episode_count' => $anime->episode_count,
'episode_length' => $anime->episode_length,
'media_id' => $data['media_id'],
@ -264,17 +262,17 @@ class AnimeCollection extends Collection {
extract($genre_info);
// Get api information
$anime = $this->anime_model->get_anime($anime_id);
$anime = $this->anime_model->getAnimeById($anime_id);
foreach ($anime['genres'] as $genre)
{
// Add genres that don't currently exist
if ( ! in_array($genre['name'], $genres))
if ( ! in_array($genre, $genres))
{
$this->db->set('genre', $genre['name'])
$this->db->set('genre', $genre)
->insert('genres');
$genres[] = $genre['name'];
$genres[] = $genre;
}
// Update link table
@ -282,13 +280,13 @@ class AnimeCollection extends Collection {
$flipped_genres = array_flip($genres);
$insert_array = [
'hummingbird_id' => $anime['id'],
'genre_id' => $flipped_genres[$genre['name']]
'hummingbird_id' => $anime_id,
'genre_id' => $flipped_genres[$genre]
];
if (array_key_exists($anime['id'], $links))
if (array_key_exists($anime_id, $links))
{
if ( ! in_array($flipped_genres[$genre['name']], $links[$anime['id']]))
if ( ! in_array($flipped_genres[$genre], $links[$anime_id]))
{
$this->db->set($insert_array)->insert('genre_anime_set_link');
}

View File

@ -16,6 +16,8 @@
namespace Aviat\AnimeClient\Model;
use Aviat\Ion\Di\ContainerAware;
use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\Model\DB;
use PDO;
@ -26,6 +28,8 @@ use PDOException;
*/
class Collection extends DB {
use ContainerAware;
/**
* Anime API Model
* @var object $anime_model
@ -45,6 +49,8 @@ class Collection extends DB {
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
parent::__construct($container->get('config'));
try
@ -53,8 +59,8 @@ class Collection extends DB {
}
catch (PDOException $e)
{
$this->valid_database = FALSE;
return FALSE;
//$this->valid_database = FALSE;
//return FALSE;
}
$this->anime_model = $container->get('anime-model');