From 3030b6b9083fb3b6301779fb76ecd6a260a6c5c3 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 3 Jan 2017 21:06:49 -0500 Subject: [PATCH] Start of changes for Manga list --- app/config/menus.php | 4 +- app/views/manga/cover.php | 16 +- app/views/manga/list.php | 12 +- src/API/Kitsu/Enum/MangaReadingStatus.php | 10 +- src/API/Kitsu/KitsuModel.php | 40 ++++- .../Transformer/MangaListTransformer.php | 15 +- .../Kitsu/Transformer/MangaTransformer.php | 39 +++++ src/Model/Manga.php | 142 ++---------------- 8 files changed, 121 insertions(+), 157 deletions(-) create mode 100644 src/API/Kitsu/Transformer/MangaTransformer.php diff --git a/app/config/menus.php b/app/config/menus.php index e9fb72b7..6e349eba 100644 --- a/app/config/menus.php +++ b/app/config/menus.php @@ -23,7 +23,7 @@ return [ 'on_hold' => '/on_hold', 'dropped' => '/dropped', 'completed' => '/completed', - 'all' => '/all' + //'all' => '/all' ] ], 'manga_list' => [ @@ -34,7 +34,7 @@ return [ 'on_hold' => '/on_hold', 'dropped' => '/dropped', 'completed' => '/completed', - 'all' => '/all' + //'all' => '/all' ] ] ]; \ No newline at end of file diff --git a/app/views/manga/cover.php b/app/views/manga/cover.php index 2bb8498e..69b7fb63 100644 --- a/app/views/manga/cover.php +++ b/app/views/manga/cover.php @@ -1,7 +1,7 @@
-is_authenticated()): ?> +is_authenticated()): ?> Add Item - +

There's nothing here!

@@ -11,12 +11,12 @@
- is_authenticated()): ?> + is_authenticated()): ?> - +
- is_authenticated()): ?> + is_authenticated()): ?> - +
Rating: / 10
@@ -55,6 +55,6 @@
-is_authenticated()): ?> +is_authenticated()): ?> - + diff --git a/app/views/manga/list.php b/app/views/manga/list.php index a0b35bd2..481d85e7 100644 --- a/app/views/manga/list.php +++ b/app/views/manga/list.php @@ -1,7 +1,7 @@
-is_authenticated()): ?> +is_authenticated()): ?> Add Item - +

There's nothing here!

@@ -10,9 +10,9 @@ - is_authenticated()): ?> + is_authenticated()): ?> - + @@ -23,11 +23,11 @@ - is_authenticated()): ?> + is_authenticated()): ?> - +
  Title Rating Chapters
">Edit diff --git a/src/API/Kitsu/Enum/MangaReadingStatus.php b/src/API/Kitsu/Enum/MangaReadingStatus.php index 288ffe74..9d7f43e6 100644 --- a/src/API/Kitsu/Enum/MangaReadingStatus.php +++ b/src/API/Kitsu/Enum/MangaReadingStatus.php @@ -22,10 +22,10 @@ use Aviat\Ion\Enum as BaseEnum; * Possible values for current reading status of manga */ class MangaReadingStatus extends BaseEnum { - const READING = 'Currently Reading'; - const PLAN_TO_READ = 'Plan to Read'; - const DROPPED = 'Dropped'; - const ON_HOLD = 'On Hold'; - const COMPLETED = 'Completed'; + const READING = 1; + const PLAN_TO_READ = 2; + const DROPPED = 3; + const ON_HOLD = 4; + const COMPLETED = 5; } // End of MangaReadingStatus.php diff --git a/src/API/Kitsu/KitsuModel.php b/src/API/Kitsu/KitsuModel.php index ac13e17a..86c769a5 100644 --- a/src/API/Kitsu/KitsuModel.php +++ b/src/API/Kitsu/KitsuModel.php @@ -17,7 +17,9 @@ namespace Aviat\AnimeClient\API\Kitsu; use Aviat\AnimeClient\AnimeClient; -use Aviat\AnimeClient\API\Kitsu\Transformer\{AnimeTransformer, AnimeListTransformer}; +use Aviat\AnimeClient\API\Kitsu\Transformer\{ + AnimeTransformer, AnimeListTransformer, MangaListTransformer +}; use Aviat\Ion\Json; use GuzzleHttp\Exception\ClientException; @@ -45,6 +47,11 @@ class KitsuModel { */ protected $animeTransformer; + /** + * @var MangaListTransformer + */ + protected $mangaListTransformer; + /** * KitsuModel constructor. */ @@ -55,6 +62,7 @@ class KitsuModel { $this->animeTransformer = new AnimeTransformer(); $this->animeListTransformer = new AnimeListTransformer(); + $this->mangaListTransformer = new MangaListTransformer(); } /** @@ -130,6 +138,36 @@ class KitsuModel { return $transformed; } + public function getMangaList($status): array + { + $options = [ + 'query' => [ + 'filter' => [ + 'user_id' => 2644, + 'media_type' => 'Manga', + 'status' => $status, + ], + 'include' => 'media', + 'page' => [ + 'offset' => 0, + 'limit' => 200 + ], + 'sort' => '-updated_at' + ] + ]; + + $data = $this->getRequest('library-entries', $options); + + foreach($data['data'] as $i => &$item) + { + $item['manga'] = $data['included'][$i]; + } + + $transformed = $this->mangaListTransformer->transformCollection($data['data']); + + return $transformed; + } + private function getGenres(string $type, string $id): array { $data = $this->getRequest("{$type}/{$id}/genres"); diff --git a/src/API/Kitsu/Transformer/MangaListTransformer.php b/src/API/Kitsu/Transformer/MangaListTransformer.php index 79d70182..2a43be63 100644 --- a/src/API/Kitsu/Transformer/MangaListTransformer.php +++ b/src/API/Kitsu/Transformer/MangaListTransformer.php @@ -16,6 +16,7 @@ namespace Aviat\AnimeClient\API\Kitsu\Transformer; +use Aviat\Ion\StringWrapper; use Aviat\Ion\Transformer\AbstractTransformer; /** @@ -23,7 +24,7 @@ use Aviat\Ion\Transformer\AbstractTransformer; */ class MangaListTransformer extends AbstractTransformer { - use \Aviat\Ion\StringWrapper; + use StringWrapper; /** * Remap zipped anime data to a more logical form @@ -33,18 +34,20 @@ class MangaListTransformer extends AbstractTransformer { */ public function transform($item) { +?>
0) - ? $manga['chapter_count'] - : '-'; + $total_chapters = ($manga['attributes']['chapterCount'] > 0) + ? $manga['attributes']['chapterCount'] + : '-'; - $total_volumes = ($manga['volume_count'] > 0) - ? $manga['volume_count'] + $total_volumes = ($manga['attributes']['volumeCount'] > 0) + ? $manga['attributes']['volumeCount'] : '-'; $map = [ diff --git a/src/API/Kitsu/Transformer/MangaTransformer.php b/src/API/Kitsu/Transformer/MangaTransformer.php new file mode 100644 index 00000000..9c7ac517 --- /dev/null +++ b/src/API/Kitsu/Transformer/MangaTransformer.php @@ -0,0 +1,39 @@ + + * @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\Transformer; + +use Aviat\Ion\Transformer\AbstractTransformer; + +/** + * Transformer for anime description page + */ +class MangaTransformer extends AbstractTransformer { + + /** + * Convert raw api response to a more + * logical and workable structure + * + * @param array $item API library item + * @return array + */ + public function transform($item) + { + return [ + + ]; + } +} \ No newline at end of file diff --git a/src/Model/Manga.php b/src/Model/Manga.php index 75ecb2d8..6b9f7f60 100644 --- a/src/Model/Manga.php +++ b/src/Model/Manga.php @@ -18,6 +18,7 @@ namespace Aviat\AnimeClient\Model; use Aviat\AnimeClient\API\Kitsu\Enum\MangaReadingStatus; use Aviat\AnimeClient\API\Kitsu\Transformer; +use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\Json; use GuzzleHttp\Cookie\SetCookie; use RuntimeException; @@ -45,11 +46,12 @@ class Manga extends API { MangaReadingStatus::COMPLETED => self::COMPLETED ]; - /** - * The base url for api requests - * @var string - */ - protected $base_url = "https://hummingbird.me/"; + public function __construct(ContainerInterface $container) + { + parent::__construct($container); + + $this->kitsuModel = $container->get('kitsu-model'); + } /** * Make an authenticated manga API call @@ -91,85 +93,6 @@ class Manga extends API { ]; } - /** - * Add a manga to the list - * - * @param array $data - * @return array - */ - public function add($data) - { - $object = [ - 'manga_library_entry' => [ - 'status' => $data['status'], - 'manga_id' => $data['id'] - ] - ]; - - return $this->_manga_api_call('post', 'manga_library_entries', $object); - } - - /** - * Update the selected manga - * - * @param array $data - * @return array - */ - public function update($data) - { - $id = $data['id']; - - return $this->_manga_api_call( - 'put', - "manga_library_entries/{$id}", - ['manga_library_entry' => $data] - ); - } - - /** - * Delete a manga entry - * - * @param array $data - * @return array - */ - public function delete($data) - { - $id = $data['id']; - - return $this->_manga_api_call('delete', "manga_library_entries/{$id}"); - } - - /** - * Search for manga by name - * - * @param string $name - * @return array - * @throws RuntimeException - */ - public function search($name) - { - $logger = $this->container->getLogger('default'); - - $config = [ - 'query' => [ - 'scope' => 'manga', - 'depth' => 'full', - 'query' => $name - ] - ]; - - $response = $this->get('search.json', $config); - - if ((int) $response->getStatusCode() !== 200) - { - $logger->warning('Non 200 response for search api call'); - $logger->warning($response->getBody()); - - throw new RuntimeException($response->getEffectiveUrl()); - } - - return Json::decode($response->getBody(), TRUE); - } /** * Get a category out of the full list @@ -179,51 +102,12 @@ class Manga extends API { */ public function get_list($status) { - $data = $this->cache->get($this, '_get_list_from_api'); - return ($status !== 'All') ? $data[$status] : $data; + $data = $this->kitsuModel->getMangaList($status); + return $this->map_by_status($data)[$status]; + /*$data = $this->cache->get($this, '_get_list_from_api'); + return ($status !== 'All') ? $data[$status] : $data;*/ } - /** - * Retrieve the list from the hummingbird api - * - * @param string $status - * @return array - */ - public function _get_list_from_api(string $status = "All"): array - { - $config = [ - 'query' => [ - 'user_id' => $this->config->get('hummingbird_username') - ], - 'allow_redirects' => FALSE - ]; - - $response = $this->get('manga_library_entries', $config); - $data = $this->transform($response); - $final = $this->map_by_status($data); - return ($status !== 'All') ? $final[$status] : $final; - } - - /** - * Transform the response to be more consistent - * - * @param \GuzzleHttp\Message\Response $response - * @codeCoverageIgnore - * @return array - */ - private function transform($response) - { - // Bail out early if there isn't any manga data - $api_data = Json::decode($response->getBody(), TRUE); - if ( ! array_key_exists('manga', $api_data)) - { - return []; - } - - $zipperedData = $this->zipperLists($api_data); - $transformer = new Transformer\MangaListTransformer(); - return $transformer->transformCollection($zipperedData); - } /** * Get the details of a manga @@ -257,11 +141,11 @@ class Manga extends API { foreach ($data as &$entry) { - $entry['manga']['image'] = $util->get_cached_image( + /*$entry['manga']['image'] = $util->get_cached_image( $entry['manga']['image'], $entry['manga']['slug'], 'manga' - ); + );*/ $key = $this->const_map[$entry['reading_status']]; $output[$key][] = $entry; }