API client for Kitsu.io, with optional Anime collection, and optional Anilist syncing.
anime
manga
kitsu
anilist
single-user

Model.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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\API\MAL;
  17. use Amp\Artax\Request;
  18. use Aviat\AnimeClient\API\MAL\{
  19. ListItem,
  20. Transformer\AnimeListTransformer,
  21. Transformer\MangaListTransformer
  22. };
  23. use Aviat\AnimeClient\API\XML;
  24. use Aviat\AnimeClient\API\Mapping\{AnimeWatchingStatus, MangaReadingStatus};
  25. use Aviat\AnimeClient\Types\{Anime, AnimeFormItem};
  26. use Aviat\Ion\Di\ContainerAware;
  27. /**
  28. * MyAnimeList API Model
  29. */
  30. final class Model {
  31. use ContainerAware;
  32. use MALTrait;
  33. /**
  34. * @var AnimeListTransformer
  35. */
  36. protected $animeListTransformer;
  37. /**
  38. * @var MangaListTransformer
  39. */
  40. protected $mangaListTransformer;
  41. /**
  42. * @var ListItem
  43. */
  44. protected $listItem;
  45. /**
  46. * MAL Model constructor.
  47. *
  48. * @param ListItem $listItem
  49. */
  50. public function __construct(ListItem $listItem)
  51. {
  52. $this->animeListTransformer = new AnimeListTransformer();
  53. $this->mangaListTransformer = new MangaListTransformer();
  54. $this->listItem = $listItem;
  55. }
  56. /**
  57. * Create a list item on MAL
  58. *
  59. * @param array $data
  60. * @param string $type "anime" or "manga"
  61. * @return Request
  62. */
  63. public function createFullListItem(array $data, string $type = 'anime'): Request
  64. {
  65. return $this->listItem->create($data, $type);
  66. }
  67. /**
  68. * Create a list item on MAL from a Kitsu list item
  69. *
  70. * @param array $data
  71. * @param string $type "anime" or "manga"
  72. * @return Request
  73. */
  74. public function createListItem(array $data, string $type = 'anime'): Request
  75. {
  76. $createData = [];
  77. if ($type === 'anime')
  78. {
  79. $createData = [
  80. 'id' => $data['id'],
  81. 'data' => [
  82. 'status' => AnimeWatchingStatus::KITSU_TO_MAL[$data['status']]
  83. ]
  84. ];
  85. }
  86. elseif ($type === 'manga')
  87. {
  88. $createData = [
  89. 'id' => $data['id'],
  90. 'data' => [
  91. 'status' => MangaReadingStatus::KITSU_TO_MAL[$data['status']]
  92. ]
  93. ];
  94. }
  95. return $this->listItem->create($createData, $type);
  96. }
  97. /**
  98. * Get list info
  99. *
  100. * @param string $type "anime" or "manga"
  101. * @return array
  102. */
  103. public function getList(string $type = "anime"): array
  104. {
  105. $config = $this->container->get('config');
  106. $userName = $config->get(['mal', 'username']);
  107. $list = $this->getRequest('https://myanimelist.net/malappinfo.php', [
  108. 'headers' => [
  109. 'Accept' => 'text/xml'
  110. ],
  111. 'query' => [
  112. 'u' => $userName,
  113. 'status' => 'all',
  114. 'type' => $type
  115. ]
  116. ]);
  117. return array_key_exists($type, $list['myanimelist'])
  118. ? $list['myanimelist'][$type]
  119. : [];
  120. }
  121. /**
  122. * Retrieve a list item
  123. *
  124. * Does not apply to MAL
  125. *
  126. * @param string $listId
  127. * @return array
  128. */
  129. public function getListItem(string $listId): array
  130. {
  131. return [];
  132. }
  133. /**
  134. * Update a list item
  135. *
  136. * @param AnimeFormItem $data
  137. * @param string $type "anime" or "manga"
  138. * @return Request
  139. */
  140. public function updateListItem(AnimeFormItem $data, string $type = 'anime'): Request
  141. {
  142. $updateData = [];
  143. if ($type === 'anime')
  144. {
  145. $updateData = $this->animeListTransformer->untransform($data);
  146. }
  147. else if ($type === 'manga')
  148. {
  149. $updateData = $this->mangaListTransformer->untransform($data);
  150. }
  151. return $this->listItem->update($updateData['id'], $updateData['data'], $type);
  152. }
  153. /**
  154. * Delete a list item
  155. *
  156. * @param string $id
  157. * @param string $type "anime" or "manga"
  158. * @return Request
  159. */
  160. public function deleteListItem(string $id, string $type = 'anime'): Request
  161. {
  162. return $this->listItem->delete($id, $type);
  163. }
  164. }