API client for Kitsu.io, with optional Anime collection, and optional Anilist syncing.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Anime.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php declare(strict_types=1);
  2. /**
  3. * Hummingbird Anime List Client
  4. *
  5. * An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
  6. *
  7. * PHP version 7
  8. *
  9. * @package HummingbirdAnimeClient
  10. * @author Timothy J. Warren <tim@timshomepage.net>
  11. * @copyright 2015 - 2018 Timothy J. Warren
  12. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  13. * @version 4.0
  14. * @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
  15. */
  16. namespace Aviat\AnimeClient\Model;
  17. use Aviat\AnimeClient\API\ParallelAPIRequest;
  18. use Aviat\AnimeClient\API\Mapping\AnimeWatchingStatus;
  19. use Aviat\Ion\Di\ContainerInterface;
  20. use Aviat\Ion\Json;
  21. /**
  22. * Model for handling requests dealing with the anime list
  23. */
  24. class Anime extends API {
  25. /**
  26. * Model for making requests to Kitsu API
  27. *
  28. * @var \Aviat\AnimeClient\API\Kitsu\Model
  29. */
  30. protected $kitsuModel;
  31. /**
  32. * Model for making requests to MAL API
  33. *
  34. * @var \Aviat\AnimeClient\API\MAL\Model
  35. */
  36. protected $malModel;
  37. /**
  38. * Anime constructor.
  39. *
  40. * @param ContainerInterface $container
  41. */
  42. public function __construct(ContainerInterface $container)
  43. {
  44. $this->kitsuModel = $container->get('kitsu-model');
  45. $this->malModel = $container->get('mal-model');
  46. $config = $container->get('config');
  47. $this->useMALAPI = $config->get(['use_mal_api']) === TRUE;
  48. }
  49. /**
  50. * Get a category out of the full list
  51. *
  52. * @param string $status
  53. * @return array
  54. */
  55. public function getList($status): array
  56. {
  57. $data = $this->kitsuModel->getAnimeList($status);
  58. $this->sortByName($data, 'anime');
  59. $key = AnimeWatchingStatus::KITSU_TO_TITLE[$status];
  60. $output = [];
  61. $output[$key] = $data;
  62. return $output;
  63. }
  64. /**
  65. * Get data for the 'all' anime page
  66. *
  67. * @return array
  68. */
  69. public function getAllLists(): array
  70. {
  71. $data = $this->kitsuModel->getFullOrganizedAnimeList();
  72. foreach($data as $section => &$list)
  73. {
  74. $this->sortByName($list, 'anime');
  75. }
  76. return $data;
  77. }
  78. /**
  79. * Get information about an anime from its slug
  80. *
  81. * @param string $slug
  82. * @return array
  83. */
  84. public function getAnime(string $slug): array
  85. {
  86. return $this->kitsuModel->getAnime($slug);
  87. }
  88. /**
  89. * Get anime by its kitsu id
  90. *
  91. * @param string $animeId
  92. * @return array
  93. */
  94. public function getAnimeById(string $animeId): array
  95. {
  96. return $this->kitsuModel->getAnimeById($animeId);
  97. }
  98. /**
  99. * Search for anime by name
  100. *
  101. * @param string $name
  102. * @return array
  103. */
  104. public function search(string $name): array
  105. {
  106. return $this->kitsuModel->search('anime', $name);
  107. }
  108. /**
  109. * Get information about a specific list item
  110. * for editing/updating that item
  111. *
  112. * @param string $itemId
  113. * @return array
  114. */
  115. public function getLibraryItem(string $itemId): array
  116. {
  117. return $this->kitsuModel->getListItem($itemId);
  118. }
  119. /**
  120. * Add an anime to your list
  121. *
  122. * @param array $data
  123. * @return bool
  124. */
  125. public function createLibraryItem(array $data): bool
  126. {
  127. $requester = new ParallelAPIRequest();
  128. if ($this->useMALAPI)
  129. {
  130. $malData = $data;
  131. $malId = $this->kitsuModel->getMalIdForAnime($malData['id']);
  132. if ( ! is_null($malId))
  133. {
  134. $malData['id'] = $malId;
  135. $requester->addRequest($this->malModel->createListItem($malData), 'mal');
  136. }
  137. }
  138. $requester->addRequest($this->kitsuModel->createListItem($data), 'kitsu');
  139. $results = $requester->makeRequests();
  140. return count($results) > 0;
  141. }
  142. /**
  143. * Update a list entry
  144. *
  145. * @param array $data
  146. * @return array
  147. */
  148. public function updateLibraryItem(array $data): array
  149. {
  150. $requester = new ParallelAPIRequest();
  151. if ($this->useMALAPI)
  152. {
  153. $requester->addRequest($this->malModel->updateListItem($data), 'mal');
  154. }
  155. $requester->addRequest($this->kitsuModel->updateListItem($data), 'kitsu');
  156. $results = $requester->makeRequests();
  157. $body = Json::decode($results['kitsu']);
  158. $statusCode = (array_key_exists('error', $body)) ? 400: 200;
  159. return [
  160. 'body' => Json::decode($results['kitsu']),
  161. 'statusCode' => $statusCode
  162. ];
  163. }
  164. /**
  165. * Delete a list entry
  166. *
  167. * @param string $id
  168. * @param string|null $malId
  169. * @return bool
  170. */
  171. public function deleteLibraryItem(string $id, string $malId = NULL): bool
  172. {
  173. $requester = new ParallelAPIRequest();
  174. if ($this->useMALAPI && ! is_null($malId))
  175. {
  176. $requester->addRequest($this->malModel->deleteListItem($malId), 'MAL');
  177. }
  178. $requester->addRequest($this->kitsuModel->deleteListItem($id), 'kitsu');
  179. $results = $requester->makeRequests();
  180. return count($results) > 0;
  181. }
  182. }