Fix anime collection

This commit is contained in:
Timothy Warren 2017-01-16 13:49:51 -05:00
parent a42a9bc785
commit 10368fabe4
7 changed files with 58 additions and 26 deletions

View File

@ -11,7 +11,7 @@
<section class="media-wrap"> <section class="media-wrap">
<?php foreach($items as $item): ?> <?php foreach($items as $item): ?>
<article class="media" id="a-<?= $item['hummingbird_id'] ?>"> <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"> <div class="name">
<a href="<?= $url->generate('anime.details', ['id' => $item['slug']]) ?>"> <a href="<?= $url->generate('anime.details', ['id' => $item['slug']]) ?>">
<?= $item['title'] ?> <?= $item['title'] ?>

View File

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

View File

@ -129,13 +129,19 @@ class KitsuModel {
/** /**
* Get information about a particular anime * Get information about a particular anime
* *
* @param string $animeId * @param string $slug
* @return array * @return array
*/ */
public function getAnime(string $animeId): array public function getAnime(string $slug): array
{ {
// @TODO catch non-existent anime // @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); return $this->animeTransformer->transform($baseData);
} }
@ -310,6 +316,22 @@ class KitsuModel {
->get('config') ->get('config')
->get(['kitsu_username']); ->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 private function getRawMediaData(string $type, string $slug): array
{ {

View File

@ -40,6 +40,7 @@ class AnimeTransformer extends AbstractTransformer {
$titles = Kitsu::filterTitles($item); $titles = Kitsu::filterTitles($item);
return [ return [
'slug' => $item['slug'],
'title' => $titles[0], 'title' => $titles[0],
'titles' => $titles, 'titles' => $titles,
'status' => Kitsu::getAiringStatus($item['startDate'], $item['endDate']), '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 * @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; namespace Aviat\AnimeClient\Model;
use Aviat\AnimeClient\API\Kitsu;
use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\Json; use Aviat\Ion\Json;
use PDO; use PDO;
@ -35,7 +36,7 @@ class AnimeCollection extends Collection {
parent::__construct($container); parent::__construct($container);
// Do an import if an import file exists // 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) 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'); $util = $this->container->get('util');
$this->db->set([ $this->db->set([
'hummingbird_id' => $data['id'], 'hummingbird_id' => $data['id'],
'slug' => $anime->slug, 'slug' => $anime->slug,
'title' => $anime->title, 'title' => $anime->title,
'alternate_title' => $anime->alternate_title, 'alternate_title' => implode('<br />', $anime->titles),
'show_type' => $anime->show_type, 'show_type' => $anime->show_type,
'age_rating' => $anime->age_rating, 'age_rating' => $anime->age_rating,
'cover_image' => basename( 'cover_image' => $anime->cover_image,
$util->get_cached_image($anime->cover_image, $anime->slug, 'anime')
),
'episode_count' => $anime->episode_count, 'episode_count' => $anime->episode_count,
'episode_length' => $anime->episode_length, 'episode_length' => $anime->episode_length,
'media_id' => $data['media_id'], 'media_id' => $data['media_id'],
@ -264,17 +262,17 @@ class AnimeCollection extends Collection {
extract($genre_info); extract($genre_info);
// Get api information // Get api information
$anime = $this->anime_model->get_anime($anime_id); $anime = $this->anime_model->getAnimeById($anime_id);
foreach ($anime['genres'] as $genre) foreach ($anime['genres'] as $genre)
{ {
// Add genres that don't currently exist // 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'); ->insert('genres');
$genres[] = $genre['name']; $genres[] = $genre;
} }
// Update link table // Update link table
@ -282,13 +280,13 @@ class AnimeCollection extends Collection {
$flipped_genres = array_flip($genres); $flipped_genres = array_flip($genres);
$insert_array = [ $insert_array = [
'hummingbird_id' => $anime['id'], 'hummingbird_id' => $anime_id,
'genre_id' => $flipped_genres[$genre['name']] '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'); $this->db->set($insert_array)->insert('genre_anime_set_link');
} }

View File

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